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