System.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // Code from FOGG project
  3. // http://bazaar.launchpad.net/~arkadini/fogg/trunk/files
  4. // Licensed under GPL
  5. //
  6. /* This code is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 only, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This code is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * version 2 for more details (a copy is included in the LICENSE file that
  14. * accompanied this code).
  15. */
  16. import org.xiph.system.Bytes;
  17. import flash.Vector;
  18. class System {
  19. /*
  20. * Note: modifies .position of both src and dst!
  21. */
  22. public static inline function
  23. bytescopy(src : Bytes, src_pos : Int,
  24. dst : Bytes, dst_pos : Int, length : Int) : Void {
  25. src.position = src_pos;
  26. if (src == dst && dst_pos > src_pos) {
  27. var tmp : Bytes = new Bytes();
  28. tmp.length = length;
  29. src.readBytes(tmp, 0, length);
  30. tmp.position = 0;
  31. tmp.readBytes(dst, dst_pos, length);
  32. } else {
  33. src.readBytes(dst, dst_pos, length);
  34. }
  35. }
  36. /*
  37. * That one, though a tiny bit faster, doesn't handle overlapping if:
  38. * - src and dst are the same object, and
  39. * - dst_pos > src_pos
  40. */
  41. public static inline function
  42. unsafe_bytescopy(src : Bytes, src_pos : Int,
  43. dst : Bytes, dst_pos : Int, length : Int) : Void {
  44. src.position = src_pos;
  45. src.readBytes(dst, dst_pos, length);
  46. }
  47. public static inline function
  48. fromString(str : String) : Bytes {
  49. var b = new Bytes();
  50. b.writeUTFBytes(str);
  51. b.position = 0;
  52. return b;
  53. }
  54. public static inline function
  55. fromBytes(b : Bytes, start : Int, length : Int) : String {
  56. b.position = start;
  57. return b.readUTFBytes(length);
  58. }
  59. public static function
  60. alloc(size : Int) : Bytes {
  61. var b = new Bytes();
  62. b.length = size;
  63. b[size - 1] = 0;
  64. b.position = 0;
  65. return b;
  66. }
  67. public static inline function
  68. resize(b : Bytes, new_size : Int) : Void {
  69. b.length = new_size;
  70. // do we need to do: "b[new_size - 1] = 0" (even if only when growing)?
  71. }
  72. public static inline function
  73. abs(n : Int) : Int {
  74. return n < 0 ? -n : n;
  75. }
  76. public static inline function
  77. max(a : Int, b : Int) : Int {
  78. return a > b ? a : b;
  79. }
  80. public static function
  81. floatToIntBits(value : Float) : Int {
  82. var b : Bytes = new Bytes();
  83. b.writeFloat(value);
  84. b.position = 0;
  85. var i : Int = b.readInt();
  86. return i;
  87. }
  88. public static function
  89. intBitsToFloat(value : Int) : Float {
  90. var b : Bytes = new Bytes();
  91. b.writeInt(value);
  92. b.position = 0;
  93. var f : Float = b.readFloat();
  94. return f;
  95. }
  96. }