~ubuntu-branches/ubuntu/oneiric/haxe/oneiric

« back to all changes in this revision

Viewing changes to haxe/std/haxe/io/Bytes.hx

  • Committer: Bazaar Package Importer
  • Author(s): Jens Peter Secher
  • Date: 2009-03-18 23:09:50 UTC
  • mfrom: (3.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090318230950-pgfuxg2ucolps74t
Tags: 1:2.2-2
* Use ocamlfind to locate and use the libraries xml-light and extlib
  which already exist in Debian as separate packages.
  (Closes: #519630)
* Fixed compile error with camlp4 3.11, thanks to Stéphane Glondu.
  (Closes: #519627)
* Use quilt instead of dpatch for patches, and describe how to use
  quilt in Debian.source (thanks to Russ Allbery).
* Added a Vcs-Hg control filed to indicate the location of the public
  repository.
* Bumped Standards-Version to 3.8.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2008, The haXe Project Contributors
 
3
 * All rights reserved.
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 *   - Redistributions of source code must retain the above copyright
 
8
 *     notice, this list of conditions and the following disclaimer.
 
9
 *   - Redistributions in binary form must reproduce the above copyright
 
10
 *     notice, this list of conditions and the following disclaimer in the
 
11
 *     documentation and/or other materials provided with the distribution.
 
12
 *
 
13
 * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
 
14
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
15
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
16
 * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
 
17
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
18
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
19
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
20
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
21
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
22
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 
23
 * DAMAGE.
 
24
 */
 
25
package haxe.io;
 
26
 
 
27
class Bytes {
 
28
 
 
29
        public var length(default,null) : Int;
 
30
        var b : BytesData;
 
31
 
 
32
        function new(length,b) {
 
33
                this.length = length;
 
34
                this.b = b;
 
35
        }
 
36
 
 
37
        public inline function get( pos : Int ) : Int {
 
38
                #if neko
 
39
                return untyped __dollar__sget(b,pos);
 
40
                #elseif flash9
 
41
                return b[pos];
 
42
                #elseif php
 
43
                return untyped __call__("ord", b[pos]);
 
44
                #else
 
45
                return b[pos];
 
46
                #end
 
47
        }
 
48
 
 
49
        public inline function set( pos : Int, v : Int ) : Void {
 
50
                #if neko
 
51
                untyped __dollar__sset(b,pos,v);
 
52
                #elseif flash9
 
53
                b[pos] = v;
 
54
                #elseif php
 
55
                b[pos] = untyped __call__("chr", v);
 
56
                #else
 
57
                b[pos] = v;
 
58
                #end
 
59
        }
 
60
 
 
61
        public function blit( pos : Int, src : Bytes, srcpos : Int, len : Int ) : Void {
 
62
                #if !neko
 
63
                if( pos < 0 || srcpos < 0 || len < 0 || pos + len > length || srcpos + len > src.length ) throw Error.OutsideBounds;
 
64
                #end
 
65
                #if neko
 
66
                try untyped __dollar__sblit(b,pos,src.b,srcpos,len) catch( e : Dynamic ) throw Error.OutsideBounds;
 
67
                #elseif php
 
68
                // TODO: test me
 
69
                b = untyped __php__("substr($this->b, 0, $pos) . substr($src->b, $srcpos, $len) . substr($this->b, $pos+$len)"); //__call__("substr", b, 0, pos)+__call__("substr", src.b, srcpos, len)+__call__("substr", b, pos+len);
 
70
                #elseif flash9
 
71
                b.position = pos;
 
72
                b.writeBytes(src.b,srcpos,len);
 
73
                #else
 
74
                var b1 = b;
 
75
                var b2 = src.b;
 
76
                if( b1 == b2 && pos > srcpos ) {
 
77
                        var i = len;
 
78
                        while( i > 0 ) {
 
79
                                i--;
 
80
                                b1[i + pos] = b2[i + srcpos];
 
81
                        }
 
82
                        return;
 
83
                }
 
84
                for( i in 0...len )
 
85
                        b1[i+pos] = b2[i+srcpos];
 
86
                #end
 
87
        }
 
88
 
 
89
        public function sub( pos : Int, len : Int ) : Bytes {
 
90
                #if !neko
 
91
                if( pos < 0 || len < 0 || pos + len > length ) throw Error.OutsideBounds;
 
92
                #end
 
93
                #if neko
 
94
                return try new Bytes(len,untyped __dollar__ssub(b,pos,len)) catch( e : Dynamic ) throw Error.OutsideBounds;
 
95
                #elseif flash9
 
96
                b.position = pos;
 
97
                var b2 = new flash.utils.ByteArray();
 
98
                b.readBytes(b2,0,len);
 
99
                return new Bytes(len,b2);
 
100
                #elseif php
 
101
                // TODO: test me
 
102
                return new Bytes(len, untyped __call__("substr", b, pos, len));
 
103
                #else
 
104
                return new Bytes(len,b.slice(pos,pos+len));
 
105
                #end
 
106
        }
 
107
 
 
108
        public function compare( other : Bytes ) : Int {
 
109
                #if neko
 
110
                return untyped __dollar__compare(b,other.b);
 
111
                #elseif flash9
 
112
                var len = (length < other.length) ? length : other.length;
 
113
                var b1 = b;
 
114
                var b2 = other.b;
 
115
                b1.position = 0;
 
116
                b2.position = 0;
 
117
                for( i in 0...len>>2 )
 
118
                        if( b1.readUnsignedInt() != b2.readUnsignedInt() ) {
 
119
                                b1.position -= 4;
 
120
                                b2.position -= 4;
 
121
                                return b1.readUnsignedInt() - b2.readUnsignedInt();
 
122
                        }
 
123
                for( i in 0...len & 3 )
 
124
                        if( b1.readUnsignedByte() != b2.readUnsignedByte() )
 
125
                                return b1[b1.position-1] - b2[b2.position-1];
 
126
                return length - other.length;
 
127
                #elseif php
 
128
                return untyped __php__("$this->b < $other->b ? -1 : ($this->b == $other->b ? 0 : 1)");
 
129
                #else
 
130
                var b1 = b;
 
131
                var b2 = other.b;
 
132
                var len = (length < other.length) ? length : other.length;
 
133
                for( i in 0...len )
 
134
                        if( b1[i] != b2[i] )
 
135
                                return b1[i] - b2[i];
 
136
                return length - other.length;
 
137
                #end
 
138
        }
 
139
 
 
140
        public function readString( pos : Int, len : Int ) : String {
 
141
                #if !neko
 
142
                if( pos < 0 || len < 0 || pos + len > length ) throw Error.OutsideBounds;
 
143
                #end
 
144
                #if neko
 
145
                return try new String(untyped __dollar__ssub(b,pos,len)) catch( e : Dynamic ) throw Error.OutsideBounds;
 
146
                #elseif flash9
 
147
                b.position = pos;
 
148
                return b.readUTFBytes(len);
 
149
                #elseif php
 
150
                // TODO: test me
 
151
                return untyped __call__("substr", b, pos, len);
 
152
//              return untyped __call__("call_user_func_array", "pack", __call__("array_merge", __call__("array", "C*"), __call__("array_slice", b.__a, pos, len)));
 
153
                #else
 
154
                var s = "";
 
155
                var b = b;
 
156
                var fcc = String.fromCharCode;
 
157
                var i = pos;
 
158
                var max = pos+len;
 
159
                // utf8-encode
 
160
                while( i < max ) {
 
161
                        var c = b[i++];
 
162
                        if( c < 0x80 ) {
 
163
                                if( c == 0 ) break;
 
164
                                s += fcc(c);
 
165
                        } else if( c < 0xE0 )
 
166
                                s += fcc( ((c & 0x3F) << 6) | (b[i++] & 0x7F) );
 
167
                        else if( c < 0xF0 ) {
 
168
                                var c2 = b[i++];
 
169
                                s += fcc( ((c & 0x1F) << 12) | ((c2 & 0x7F) << 6) | (b[i++] & 0x7F) );
 
170
                        } else {
 
171
                                var c2 = b[i++];
 
172
                                var c3 = b[i++];
 
173
                                s += fcc( ((c & 0x0F) << 18) | ((c2 & 0x7F) << 12) | ((c3 << 6) & 0x7F) | (b[i++] & 0x7F) );
 
174
                        }
 
175
                }
 
176
                return s;
 
177
                #end
 
178
        }
 
179
 
 
180
        public function toString() : String {
 
181
                #if neko
 
182
                return new String(untyped __dollar__ssub(b,0,length));
 
183
                #elseif flash9
 
184
                b.position = 0;
 
185
                return b.readUTFBytes(length);
 
186
                #elseif php
 
187
                // TODO: test me
 
188
                return cast b;
 
189
//              return untyped __call__("call_user_func_array", "pack", __call__("array_merge", __call__("array", "C*"), b.__a));
 
190
                #else
 
191
                return readString(0,length);
 
192
                #end
 
193
        }
 
194
 
 
195
        public inline function getData() : BytesData {
 
196
                return b;
 
197
        }
 
198
 
 
199
        public static function alloc( length : Int ) : Bytes {
 
200
                #if neko
 
201
                return new Bytes(length,untyped __dollar__smake(length));
 
202
                #elseif flash9
 
203
                var b = new flash.utils.ByteArray();
 
204
                b.length = length;
 
205
                return new Bytes(length,b);
 
206
                #elseif php
 
207
                // TODO: test me
 
208
                return new Bytes(length, untyped __call__("str_repeat", __call__("chr", 0), length));
 
209
                /*
 
210
                if(length > 0)
 
211
                        return new Bytes(length, untyped __call__("new _hx_array", __call__("array_fill", 0, length, 0)));
 
212
                else
 
213
                        return new Bytes(0, untyped __call__("new _hx_array", __call__("array")));
 
214
                */
 
215
                #else
 
216
                var a = new Array();
 
217
                for( i in 0...length )
 
218
                        a.push(0);
 
219
                return new Bytes(length,a);
 
220
                #end
 
221
        }
 
222
 
 
223
        public static function ofString( s : String ) : Bytes {
 
224
                #if neko
 
225
                return new Bytes(s.length,untyped __dollar__ssub(s.__s,0,s.length));
 
226
                #elseif flash9
 
227
                var b = new flash.utils.ByteArray();
 
228
                b.writeUTFBytes(s);
 
229
                return new Bytes(b.length,b);
 
230
                #elseif php
 
231
                return new Bytes(untyped __call__("strlen", s), cast s);
 
232
//              return ofData(untyped __call__("new _hx_array", __call__("array_values", __call__("unpack", "C*",  s))));
 
233
                #else
 
234
                var a = new Array();
 
235
                // utf8-decode
 
236
                for( i in 0...s.length ) {
 
237
                        var c : Int = untyped s["cca"](i);
 
238
                        if( c <= 0x7F )
 
239
                                a.push(c);
 
240
                        else if( c <= 0x7FF ) {
 
241
                                a.push( 0xC0 | (c >> 6) );
 
242
                                a.push( 0x80 | (c & 63) );
 
243
                        } else if( c <= 0xFFFF ) {
 
244
                                a.push( 0xE0 | (c >> 12) );
 
245
                                a.push( 0x80 | ((c >> 6) & 63) );
 
246
                                a.push( 0x80 | (c & 63) );
 
247
                        } else {
 
248
                                a.push( 0xF0 | (c >> 18) );
 
249
                                a.push( 0x80 | ((c >> 12) & 63) );
 
250
                                a.push( 0x80 | ((c >> 6) & 63) );
 
251
                                a.push( 0x80 | (c & 63) );
 
252
                        }
 
253
                }
 
254
                return new Bytes(a.length,a);
 
255
                #end
 
256
        }
 
257
 
 
258
        public static function ofData( b : BytesData ) {
 
259
                #if flash9
 
260
                return new Bytes(b.length,b);
 
261
                #elseif neko
 
262
                return new Bytes(untyped __dollar__ssize(b),b);
 
263
                #elseif php
 
264
                return new Bytes(untyped __call__("strlen", b), b);
 
265
                #else
 
266
                return new Bytes(b.length,b);
 
267
                #end
 
268
        }
 
269
 
 
270
}