PageRenderTime 34ms CodeModel.GetById 11ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/std/neko/Utf8.hx

http://github.com/Yoomee/clippy
Haxe | 109 lines | 69 code | 16 blank | 24 comment | 9 complexity | 6719c3f9008528237f21778d9224357a MD5 | raw file
  1/*
  2 * Copyright (c) 2005, 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 neko;
 26
 27class Utf8 {
 28
 29	var __b : Void;
 30
 31	public function new( ?size : Int ) {
 32		__b = utf8_buf_alloc(if( size == null ) 1 else size);
 33	}
 34
 35	public function addChar( c : Int ) {
 36		utf8_buf_add(__b,c);
 37	}
 38
 39	public function toString() {
 40		return new String(utf8_buf_content(__b));
 41	}
 42
 43	public static function encode( s : String ) : String {
 44		s = untyped s.__s;
 45		var sl = untyped __dollar__ssize(s);
 46		var buf = utf8_buf_alloc( sl );
 47		var i = 0;
 48		while( i < sl ) {
 49			utf8_buf_add(buf,untyped __dollar__sget(s,i));
 50			i += 1;
 51		}
 52		return new String( utf8_buf_content(buf) );
 53	}
 54
 55	public static function decode( s : String ) : String {
 56		s = untyped s.__s;
 57		var sl = untyped __dollar__ssize(s);
 58		var ret = untyped __dollar__smake(sl);
 59		var i = 0;
 60		utf8_iter(s,function(c) {
 61			if( c == 8364 ) // euro symbol
 62				c = 164;
 63			else if( c == 0xFEFF ) // BOM
 64				return;
 65			else if( c > 255 )
 66				throw "Utf8::decode invalid character ("+c+")";
 67			untyped __dollar__sset(ret,i,c);
 68			i += 1;
 69		});
 70		return new String( untyped __dollar__ssub(ret,0,i) );
 71	}
 72
 73	public static function iter( s : String, chars : Int -> Void ) {
 74		utf8_iter(untyped s.__s,chars);
 75	}
 76
 77	public static function charCodeAt( s : String, index : Int ) : Int {
 78		return utf8_get(untyped s.__s,index);
 79	}
 80
 81	public static function validate( s : String ) : Bool {
 82		return utf8_validate(untyped s.__s);
 83	}
 84
 85	public static function length( s : String ) : Int {
 86		return utf8_length(untyped s.__s);
 87	}
 88
 89	public static function compare( a : String, b : String ) : Int {
 90		return utf8_compare(untyped a.__s,untyped b.__s);
 91	}
 92
 93	public static function sub( s : String, pos : Int, len : Int ) : String {
 94		return new String(utf8_sub(untyped s.__s,pos,len));
 95	}
 96
 97	static var utf8_buf_alloc = Lib.load("std","utf8_buf_alloc",1);
 98	static var utf8_buf_add = Lib.load("std","utf8_buf_add",2);
 99	static var utf8_buf_content = Lib.load("std","utf8_buf_content",1);
100	static var utf8_buf_length = Lib.load("std","utf8_buf_length",1);
101	static var utf8_iter = Lib.load("std","utf8_iter",2);
102
103	static var utf8_get = Lib.load("std","utf8_get",2);
104	static var utf8_validate = Lib.load("std","utf8_validate",1);
105	static var utf8_length = Lib.load("std","utf8_length",1);
106	static var utf8_compare = Lib.load("std","utf8_compare",2);
107	static var utf8_sub = Lib.load("std","utf8_sub",3);
108
109}