~arkadini/fogg/trunk

5 by Arek Korbik
Some improvements in the fogg package. Get rid of the lotsatraces later.
1
import org.xiph.system.Bytes;
1 by Arek Korbik
Initial version of the more-or-less complete translation of cortado's
2
22 by Arek Korbik
Add vectory arraycopy.
3
import flash.Vector;
4
1 by Arek Korbik
Initial version of the more-or-less complete translation of cortado's
5
class System {
36 by Arek Korbik
Add dedicated ByteArray copy utils.
6
    /*
7
     * Note: modifies .position of both src and dst!
8
     */
9
    public static inline function
10
    bytescopy(src : Bytes, src_pos : Int,
11
              dst : Bytes, dst_pos : Int, length : Int) : Void {
12
        src.position = src_pos;
37 by Arek Korbik
Don't make additional copies unless really necessary.
13
        if (src == dst && dst_pos > src_pos) {
36 by Arek Korbik
Add dedicated ByteArray copy utils.
14
            var tmp : Bytes = new Bytes();
15
            tmp.length = length;
16
            src.readBytes(tmp, 0, length);
17
            tmp.position = 0;
18
            tmp.readBytes(dst, dst_pos, length);
19
        } else {
20
            src.readBytes(dst, dst_pos, length);
21
        }
22
    }
23
24
    /*
25
     * That one, though a tiny bit faster, doesn't handle overlapping if:
26
     * - src and dst are the same object, and
27
     * - dst_pos > src_pos
28
     */
29
    public static inline function
30
    unsafe_bytescopy(src : Bytes, src_pos : Int,
31
                     dst : Bytes, dst_pos : Int, length : Int) : Void {
32
        src.position = src_pos;
33
        src.readBytes(dst, dst_pos, length);
34
    }
35
39 by Arek Korbik
Don't use Vectors for types other than Int and Float - it's slow and/or
36
37
    public static inline function
38
    fromString(str : String) : Bytes {
25 by Arek Korbik
Add more quick helpers.
39
        var b = new Bytes();
40
        b.writeUTFBytes(str);
41
        b.position = 0;
42
        return b;
43
    }
44
39 by Arek Korbik
Don't use Vectors for types other than Int and Float - it's slow and/or
45
    public static inline function
46
    fromBytes(b : Bytes, start : Int, length : Int) : String {
30 by Arek Korbik
One more helper.
47
        b.position = start;
48
        return b.readUTFBytes(length);
49
    }
50
39 by Arek Korbik
Don't use Vectors for types other than Int and Float - it's slow and/or
51
    public static function
52
    alloc(size : Int) : Bytes {
5 by Arek Korbik
Some improvements in the fogg package. Get rid of the lotsatraces later.
53
        var b = new Bytes();
54
        b.length = size;
55
        b[size - 1] = 0;
19 by Arek Korbik
Reset position to 0 after initializing.
56
        b.position = 0;
5 by Arek Korbik
Some improvements in the fogg package. Get rid of the lotsatraces later.
57
        return b;
58
    }
25 by Arek Korbik
Add more quick helpers.
59
39 by Arek Korbik
Don't use Vectors for types other than Int and Float - it's slow and/or
60
    public static inline function
61
    resize(b : Bytes, new_size : Int) : Void {
62
        b.length = new_size;
63
        // do we need to do: "b[new_size - 1] = 0" (even if only when growing)?
64
    }
65
66
    public static inline function
67
    abs(n : Int) : Int {
25 by Arek Korbik
Add more quick helpers.
68
        return n < 0 ? -n : n;
69
    }
27 by Arek Korbik
More helper/emulation functions.
70
39 by Arek Korbik
Don't use Vectors for types other than Int and Float - it's slow and/or
71
    public static inline function
72
    max(a : Int, b : Int) : Int {
27 by Arek Korbik
More helper/emulation functions.
73
        return a > b ? a : b;
74
    }
75
39 by Arek Korbik
Don't use Vectors for types other than Int and Float - it's slow and/or
76
    public static function
77
    floatToIntBits(value : Float) : Int {
27 by Arek Korbik
More helper/emulation functions.
78
        var b : Bytes = new Bytes();
79
        b.writeFloat(value);
80
        b.position = 0;
81
        var i : Int = b.readInt();
82
        return i;
83
    }
84
39 by Arek Korbik
Don't use Vectors for types other than Int and Float - it's slow and/or
85
    public static function
86
    intBitsToFloat(value : Int) : Float {
27 by Arek Korbik
More helper/emulation functions.
87
        var b : Bytes = new Bytes();
88
        b.writeInt(value);
89
        b.position = 0;
90
        var f : Float = b.readFloat();
91
        return f;
92
    }
19 by Arek Korbik
Reset position to 0 after initializing.
93
}