2
* Copyright (c) 2005-2008, The haXe Project Contributors
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
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.
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
29
public var length(default,null) : Int;
32
function new(length,b) {
37
public inline function get( pos : Int ) : Int {
39
return untyped __dollar__sget(b,pos);
43
return untyped __call__("ord", b[pos]);
49
public inline function set( pos : Int, v : Int ) : Void {
51
untyped __dollar__sset(b,pos,v);
55
b[pos] = untyped __call__("chr", v);
61
public function blit( pos : Int, src : Bytes, srcpos : Int, len : Int ) : Void {
63
if( pos < 0 || srcpos < 0 || len < 0 || pos + len > length || srcpos + len > src.length ) throw Error.OutsideBounds;
66
try untyped __dollar__sblit(b,pos,src.b,srcpos,len) catch( e : Dynamic ) throw Error.OutsideBounds;
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);
72
b.writeBytes(src.b,srcpos,len);
76
if( b1 == b2 && pos > srcpos ) {
80
b1[i + pos] = b2[i + srcpos];
85
b1[i+pos] = b2[i+srcpos];
89
public function sub( pos : Int, len : Int ) : Bytes {
91
if( pos < 0 || len < 0 || pos + len > length ) throw Error.OutsideBounds;
94
return try new Bytes(len,untyped __dollar__ssub(b,pos,len)) catch( e : Dynamic ) throw Error.OutsideBounds;
97
var b2 = new flash.utils.ByteArray();
98
b.readBytes(b2,0,len);
99
return new Bytes(len,b2);
102
return new Bytes(len, untyped __call__("substr", b, pos, len));
104
return new Bytes(len,b.slice(pos,pos+len));
108
public function compare( other : Bytes ) : Int {
110
return untyped __dollar__compare(b,other.b);
112
var len = (length < other.length) ? length : other.length;
117
for( i in 0...len>>2 )
118
if( b1.readUnsignedInt() != b2.readUnsignedInt() ) {
121
return b1.readUnsignedInt() - b2.readUnsignedInt();
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;
128
return untyped __php__("$this->b < $other->b ? -1 : ($this->b == $other->b ? 0 : 1)");
132
var len = (length < other.length) ? length : other.length;
135
return b1[i] - b2[i];
136
return length - other.length;
140
public function readString( pos : Int, len : Int ) : String {
142
if( pos < 0 || len < 0 || pos + len > length ) throw Error.OutsideBounds;
145
return try new String(untyped __dollar__ssub(b,pos,len)) catch( e : Dynamic ) throw Error.OutsideBounds;
148
return b.readUTFBytes(len);
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)));
156
var fcc = String.fromCharCode;
165
} else if( c < 0xE0 )
166
s += fcc( ((c & 0x3F) << 6) | (b[i++] & 0x7F) );
167
else if( c < 0xF0 ) {
169
s += fcc( ((c & 0x1F) << 12) | ((c2 & 0x7F) << 6) | (b[i++] & 0x7F) );
173
s += fcc( ((c & 0x0F) << 18) | ((c2 & 0x7F) << 12) | ((c3 << 6) & 0x7F) | (b[i++] & 0x7F) );
180
public function toString() : String {
182
return new String(untyped __dollar__ssub(b,0,length));
185
return b.readUTFBytes(length);
189
// return untyped __call__("call_user_func_array", "pack", __call__("array_merge", __call__("array", "C*"), b.__a));
191
return readString(0,length);
195
public inline function getData() : BytesData {
199
public static function alloc( length : Int ) : Bytes {
201
return new Bytes(length,untyped __dollar__smake(length));
203
var b = new flash.utils.ByteArray();
205
return new Bytes(length,b);
208
return new Bytes(length, untyped __call__("str_repeat", __call__("chr", 0), length));
211
return new Bytes(length, untyped __call__("new _hx_array", __call__("array_fill", 0, length, 0)));
213
return new Bytes(0, untyped __call__("new _hx_array", __call__("array")));
217
for( i in 0...length )
219
return new Bytes(length,a);
223
public static function ofString( s : String ) : Bytes {
225
return new Bytes(s.length,untyped __dollar__ssub(s.__s,0,s.length));
227
var b = new flash.utils.ByteArray();
229
return new Bytes(b.length,b);
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))));
236
for( i in 0...s.length ) {
237
var c : Int = untyped s["cca"](i);
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) );
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) );
254
return new Bytes(a.length,a);
258
public static function ofData( b : BytesData ) {
260
return new Bytes(b.length,b);
262
return new Bytes(untyped __dollar__ssize(b),b);
264
return new Bytes(untyped __call__("strlen", b), b);
266
return new Bytes(b.length,b);