PageRenderTime 34ms CodeModel.GetById 14ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/bin/std/haxe/io/Bytes.hx

http://github.com/Yoomee/clippy
Haxe | 290 lines | 235 code | 15 blank | 40 comment | 41 complexity | 38ba62611f8dbc91952782fca10d89b4 MD5 | raw file
  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 */
 25package haxe.io;
 26
 27class 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		#elseif cpp
 45		return untyped b[pos];
 46		#else
 47		return b[pos];
 48		#end
 49	}
 50
 51	public inline function set( pos : Int, v : Int ) : Void {
 52		#if neko
 53		untyped __dollar__sset(b,pos,v);
 54		#elseif flash9
 55		b[pos] = v;
 56		#elseif php
 57		b[pos] = untyped __call__("chr", v);
 58		#elseif cpp
 59		untyped b[pos] = v;
 60		#else
 61		b[pos] = v & 0xFF;
 62		#end
 63	}
 64
 65	public function blit( pos : Int, src : Bytes, srcpos : Int, len : Int ) : Void {
 66		#if !neko
 67		if( pos < 0 || srcpos < 0 || len < 0 || pos + len > length || srcpos + len > src.length ) throw Error.OutsideBounds;
 68		#end
 69		#if neko
 70		try untyped __dollar__sblit(b,pos,src.b,srcpos,len) catch( e : Dynamic ) throw Error.OutsideBounds;
 71		#elseif php
 72		// TODO: test me
 73		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);
 74		#elseif flash9
 75		b.position = pos;
 76		b.writeBytes(src.b,srcpos,len);
 77		#else
 78		var b1 = b;
 79		var b2 = src.b;
 80		if( b1 == b2 && pos > srcpos ) {
 81			var i = len;
 82			while( i > 0 ) {
 83				i--;
 84				b1[i + pos] = b2[i + srcpos];
 85			}
 86			return;
 87		}
 88		for( i in 0...len )
 89			b1[i+pos] = b2[i+srcpos];
 90		#end
 91	}
 92
 93	public function sub( pos : Int, len : Int ) : Bytes {
 94		#if !neko
 95		if( pos < 0 || len < 0 || pos + len > length ) throw Error.OutsideBounds;
 96		#end
 97		#if neko
 98		return try new Bytes(len,untyped __dollar__ssub(b,pos,len)) catch( e : Dynamic ) throw Error.OutsideBounds;
 99		#elseif flash9
100		b.position = pos;
101		var b2 = new flash.utils.ByteArray();
102		b.readBytes(b2,0,len);
103		return new Bytes(len,b2);
104		#elseif php
105		// TODO: test me
106		return new Bytes(len, untyped __call__("substr", b, pos, len));
107		#else
108		return new Bytes(len,b.slice(pos,pos+len));
109		#end
110	}
111
112	public function compare( other : Bytes ) : Int {
113		#if neko
114		return untyped __dollar__compare(b,other.b);
115		#elseif flash9
116		var len = (length < other.length) ? length : other.length;
117		var b1 = b;
118		var b2 = other.b;
119		b1.position = 0;
120		b2.position = 0;
121		for( i in 0...len>>2 )
122			if( b1.readUnsignedInt() != b2.readUnsignedInt() ) {
123				b1.position -= 4;
124				b2.position -= 4;
125				return b1.readUnsignedInt() - b2.readUnsignedInt();
126			}
127		for( i in 0...len & 3 )
128			if( b1.readUnsignedByte() != b2.readUnsignedByte() )
129				return b1[b1.position-1] - b2[b2.position-1];
130		return length - other.length;
131		#elseif php
132		return untyped __php__("$this->b < $other->b ? -1 : ($this->b == $other->b ? 0 : 1)");
133		#else
134		var b1 = b;
135		var b2 = other.b;
136		var len = (length < other.length) ? length : other.length;
137		for( i in 0...len )
138			if( b1[i] != b2[i] )
139				#if cpp
140				return untyped b1[i] - untyped b2[i];
141				#else
142				return b1[i] - b2[i];
143				#end
144		return length - other.length;
145		#end
146	}
147
148	public function readString( pos : Int, len : Int ) : String {
149		#if !neko
150		if( pos < 0 || len < 0 || pos + len > length ) throw Error.OutsideBounds;
151		#end
152		#if neko
153		return try new String(untyped __dollar__ssub(b,pos,len)) catch( e : Dynamic ) throw Error.OutsideBounds;
154		#elseif flash9
155		b.position = pos;
156		return b.readUTFBytes(len);
157		#elseif php
158		// TODO: test me
159		return untyped __call__("substr", b, pos, len);
160//		return untyped __call__("call_user_func_array", "pack", __call__("array_merge", __call__("array", "C*"), __call__("array_slice", b.�a, pos, len)));
161		#elseif cpp
162		var result:String="";
163		untyped __global__.__hxcpp_string_of_bytes(b,result,pos,len);
164		return result;
165		#else
166		var s = "";
167		var b = b;
168		var fcc = String.fromCharCode;
169		var i = pos;
170		var max = pos+len;
171		// utf8-encode
172		while( i < max ) {
173			var c = b[i++];
174			if( c < 0x80 ) {
175				if( c == 0 ) break;
176				s += fcc(c);
177			} else if( c < 0xE0 )
178				s += fcc( ((c & 0x3F) << 6) | (b[i++] & 0x7F) );
179			else if( c < 0xF0 ) {
180				var c2 = b[i++];
181				s += fcc( ((c & 0x1F) << 12) | ((c2 & 0x7F) << 6) | (b[i++] & 0x7F) );
182			} else {
183				var c2 = b[i++];
184				var c3 = b[i++];
185				s += fcc( ((c & 0x0F) << 18) | ((c2 & 0x7F) << 12) | ((c3 << 6) & 0x7F) | (b[i++] & 0x7F) );
186			}
187		}
188		return s;
189		#end
190	}
191
192	public function toString() : String {
193		#if neko
194		return new String(untyped __dollar__ssub(b,0,length));
195		#elseif flash9
196		b.position = 0;
197		return b.readUTFBytes(length);
198		#elseif php
199		// TODO: test me
200		return cast b;
201//		return untyped __call__("call_user_func_array", "pack", __call__("array_merge", __call__("array", "C*"), b.�a));
202		#else
203		return readString(0,length);
204		#end
205	}
206
207	public inline function getData() : BytesData {
208		return b;
209	}
210
211	public static function alloc( length : Int ) : Bytes {
212		#if neko
213		return new Bytes(length,untyped __dollar__smake(length));
214		#elseif flash9
215		var b = new flash.utils.ByteArray();
216		b.length = length;
217		return new Bytes(length,b);
218		#elseif php
219		// TODO: test me
220		return new Bytes(length, untyped __call__("str_repeat", __call__("chr", 0), length));
221		/*
222		if(length > 0)
223			return new Bytes(length, untyped __call__("new _hx_array", __call__("array_fill", 0, length, 0)));
224		else
225			return new Bytes(0, untyped __call__("new _hx_array", __call__("array")));
226		*/
227		#elseif cpp
228		var a = new BytesData();
229		if (length>0) a[length-1] = untyped 0;
230		return new Bytes(length,a);
231		#else
232		var a = new Array();
233		for( i in 0...length )
234			a.push(0);
235		return new Bytes(length,a);
236		#end
237	}
238
239	public static function ofString( s : String ) : Bytes {
240		#if neko
241		return new Bytes(s.length,untyped __dollar__ssub(s.__s,0,s.length));
242		#elseif flash9
243		var b = new flash.utils.ByteArray();
244		b.writeUTFBytes(s);
245		return new Bytes(b.length,b);
246		#elseif php
247		return new Bytes(untyped __call__("strlen", s), cast s);
248//		return ofData(untyped __call__("new _hx_array", __call__("array_values", __call__("unpack", "C*",  s))));
249		#elseif cpp
250		var a = new BytesData();
251		untyped __global__.__hxcpp_bytes_of_string(a,s);
252		return new Bytes(a.length,a);
253		#else
254		var a = new Array();
255		// utf8-decode
256		for( i in 0...s.length ) {
257			var c : Int = untyped s["cca"](i);
258			if( c <= 0x7F )
259				a.push(c);
260			else if( c <= 0x7FF ) {
261				a.push( 0xC0 | (c >> 6) );
262				a.push( 0x80 | (c & 63) );
263			} else if( c <= 0xFFFF ) {
264				a.push( 0xE0 | (c >> 12) );
265				a.push( 0x80 | ((c >> 6) & 63) );
266				a.push( 0x80 | (c & 63) );
267			} else {
268				a.push( 0xF0 | (c >> 18) );
269				a.push( 0x80 | ((c >> 12) & 63) );
270				a.push( 0x80 | ((c >> 6) & 63) );
271				a.push( 0x80 | (c & 63) );
272			}
273		}
274		return new Bytes(a.length,a);
275		#end
276	}
277
278	public static function ofData( b : BytesData ) {
279		#if flash9
280		return new Bytes(b.length,b);
281		#elseif neko
282		return new Bytes(untyped __dollar__ssize(b),b);
283		#elseif php
284		return new Bytes(untyped __call__("strlen", b), b);
285		#else
286		return new Bytes(b.length,b);
287		#end
288	}
289
290}