Selaa lähdekoodia

added haxe.Utf8

Nicolas Cannasse 13 vuotta sitten
vanhempi
commit
cbd1ee892d
6 muutettua tiedostoa jossa 149 lisäystä ja 28 poistoa
  1. 1 0
      doc/CHANGES.txt
  2. 5 4
      std/cpp/_std/haxe/Utf8.hx
  3. 116 0
      std/haxe/Utf8.hx
  4. 17 16
      std/neko/_std/haxe/Utf8.hx
  5. 9 8
      std/php/_std/haxe/Utf8.hx
  6. 1 0
      typeload.ml

+ 1 - 0
doc/CHANGES.txt

@@ -25,6 +25,7 @@
 	all : added --cache, --wait and --cwd
 	all : fixed completion in macros calls arguments
 	all : fixed DCE removing empty but still used interfaces/superclasses
+	all : added haxe.Utf8 (crossplatform)
 
 2011-09-25: 2.08
 	js : added js.JQuery

+ 5 - 4
std/cpp/Utf8.hx → std/cpp/_std/haxe/Utf8.hx

@@ -1,16 +1,17 @@
-package cpp;
+package haxe;
 
+@:core_api
 class Utf8
 {
    var __s:Array<Int>;
 
-	public function new( ?size : Null<Int> ) {
+	public function new( ?size : Null<Int> ) : Void {
       __s = new Array<Int>();
       if (size!=null && size>0)
          __s[size-1] = 0;
 	}
 
-	public function addChar( c : Int ) {
+	public function addChar( c : Int ) : Void {
       __s.push(c);
 	}
 
@@ -30,7 +31,7 @@ class Utf8
 		return untyped __global__.__hxcpp_utf8_string_to_char_bytes(s);
 	}
 
-	public static function iter( s : String, chars : Int -> Void ) {
+	public static function iter( s : String, chars : Int -> Void ) : Void {
       var array:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(s);
       for(a in array)
          chars(a);

+ 116 - 0
std/haxe/Utf8.hx

@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2005-2012, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package haxe;
+
+/**
+	Since all platforms does not guarantee that String always uses UTF-8 encoding, you
+	can use this crossplatform API to perform operations on such strings.
+**/
+class Utf8 {
+
+	var __b : String;
+
+	/**
+		Allocate a new Utf8 buffer using an optional bytes size.
+	**/
+	public function new( ?size : Int ) {
+		__b = "";
+	}
+
+	/**
+		Add the given UTF8 character code to the buffer.
+	**/
+	public inline function addChar( c : Int ) : Void {
+		__b += String.fromCharCode(c);
+	}
+
+	/**
+		Returns the buffer converted to a String;
+	**/
+	public inline function toString() : String {
+		return __b;
+	}
+
+	/**
+		Call the [chars] function for each UTF8 char of the string.
+	**/
+	public static function iter( s : String, chars : Int -> Void ) {
+		for( i in 0...s.length )
+			chars(s.charCodeAt(i));
+	}
+
+	/**
+		Encode the input ISO string into the corresponding UTF8 one.
+	**/
+	public static function encode( s : String ) : String {
+		throw "Not implemented";
+		return s;
+	}
+
+	/**
+		Decode an UTF8 string back to an ISO string.
+		Throw an exception if a given UTF8 character is not supported by the decoder.
+	**/
+	public static function decode( s : String ) : String {
+		throw "Not implemented";
+		return s;
+	}
+
+	/**
+		Similar to [String.charCodeAt] but uses the UTF8 character position.
+	**/
+	public static inline function charCodeAt( s : String, index : Int ) : Int {
+		return s.charCodeAt(index);
+	}
+
+	/**
+		Tells if the String is correctly encoded as UTF8.
+	**/
+	public static inline function validate( s : String ) : Bool {
+		return true;
+	}
+
+	/**
+		Returns the number of UTF8 chars of the String.
+	**/
+	public static inline function length( s : String ) : Int {
+		return s.length;
+	}
+
+	/**
+		Compare two UTF8 strings, character by character.
+	**/
+	public static function compare( a : String, b : String ) : Int {
+		return a > b ? 1 : (a == b ? 0 : -1);
+	}
+
+	/**
+		This is similar to [String.substr] but the [pos] and [len] parts are considering UTF8 characters.
+	**/
+	public static inline function sub( s : String, pos : Int, len : Int ) : String {
+		return s.substr(pos,len);
+	}
+
+}

+ 17 - 16
std/neko/Utf8.hx → std/neko/_std/haxe/Utf8.hx

@@ -22,21 +22,22 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  * DAMAGE.
  */
-package neko;
+package haxe;
 
+@:core_api
 class Utf8 {
 
 	var __b : Void;
 
-	public function new( ?size : Int ) {
+	public function new( ?size : Int ) : Void {
 		__b = utf8_buf_alloc(if( size == null ) 1 else size);
 	}
 
-	public function addChar( c : Int ) {
+	public function addChar( c : Int ) : Void {
 		utf8_buf_add(__b,c);
 	}
 
-	public function toString() {
+	public function toString() : String {
 		return new String(utf8_buf_content(__b));
 	}
 
@@ -70,7 +71,7 @@ class Utf8 {
 		return new String( untyped __dollar__ssub(ret,0,i) );
 	}
 
-	public static function iter( s : String, chars : Int -> Void ) {
+	public static function iter( s : String, chars : Int -> Void ) : Void {
 		utf8_iter(untyped s.__s,chars);
 	}
 
@@ -94,16 +95,16 @@ class Utf8 {
 		return new String(utf8_sub(untyped s.__s,pos,len));
 	}
 
-	static var utf8_buf_alloc = Lib.load("std","utf8_buf_alloc",1);
-	static var utf8_buf_add = Lib.load("std","utf8_buf_add",2);
-	static var utf8_buf_content = Lib.load("std","utf8_buf_content",1);
-	static var utf8_buf_length = Lib.load("std","utf8_buf_length",1);
-	static var utf8_iter = Lib.load("std","utf8_iter",2);
-
-	static var utf8_get = Lib.load("std","utf8_get",2);
-	static var utf8_validate = Lib.load("std","utf8_validate",1);
-	static var utf8_length = Lib.load("std","utf8_length",1);
-	static var utf8_compare = Lib.load("std","utf8_compare",2);
-	static var utf8_sub = Lib.load("std","utf8_sub",3);
+	static var utf8_buf_alloc = neko.Lib.load("std","utf8_buf_alloc",1);
+	static var utf8_buf_add = neko.Lib.load("std","utf8_buf_add",2);
+	static var utf8_buf_content = neko.Lib.load("std","utf8_buf_content",1);
+	static var utf8_buf_length = neko.Lib.load("std","utf8_buf_length",1);
+	static var utf8_iter = neko.Lib.load("std","utf8_iter",2);
+
+	static var utf8_get = neko.Lib.load("std","utf8_get",2);
+	static var utf8_validate = neko.Lib.load("std","utf8_validate",1);
+	static var utf8_length = neko.Lib.load("std","utf8_length",1);
+	static var utf8_compare = neko.Lib.load("std","utf8_compare",2);
+	static var utf8_sub = neko.Lib.load("std","utf8_sub",3);
 
 }

+ 9 - 8
std/php/Utf8.hx → std/php/_std/haxe/Utf8.hx

@@ -22,17 +22,18 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  * DAMAGE.
  */
-package php;
+package haxe;
 
+@:core_api
 class Utf8 {
 
 	var __b : String;
 
-	public function new() {
+	public function new() : Void {
 		__b = '';
 	}
 
-	public function addChar( c : Int ) {
+	public function addChar( c : Int ) : Void {
 		__b += uchr(c);
 	}
 
@@ -48,7 +49,7 @@ class Utf8 {
 		return untyped __call__("utf8_decode", s);
 	}
 
-	public static function iter(s : String, chars : Int -> Void ) {
+	public static function iter(s : String, chars : Int -> Void ) : Void {
 		var len = length(s);
 		for(i in 0...len)
 			chars(charCodeAt(s, i));
@@ -58,15 +59,15 @@ class Utf8 {
 		return uord(sub(s, index, 1));
 	}
 
-	public static function uchr(i : Int) : String {
+	static function uchr(i : Int) : String {
 		return untyped __php__("mb_convert_encoding(pack('N',$i), 'UTF-8', 'UCS-4BE')");
 	}
 
-	public static function uord(s : String) : Int untyped {
+	static function uord(s : String) : Int untyped {
 		var c : Array<Int> = untyped __php__("unpack('N', mb_convert_encoding($s, 'UCS-4BE', 'UTF-8'))");
 		return c[1];
 	}
-	
+
 	public static function validate( s : String ) : Bool {
 		return untyped __call__("mb_check_encoding", s, enc);
 	}
@@ -82,6 +83,6 @@ class Utf8 {
 	public static function sub( s : String, pos : Int, len : Int ) : String {
 		return untyped __call__("mb_substr", s, pos, len, enc);
 	}
-	
+
 	private static inline var enc = "UTF-8";
 }

+ 1 - 0
typeload.ml

@@ -639,6 +639,7 @@ let init_core_api ctx c =
 				if f2.cf_kind <> f.cf_kind then begin
 					match f2.cf_kind, f.cf_kind with
 					| Method MethInline, Method MethNormal -> () (* allow to add 'inline' *)
+					| Method MethNormal, Method MethInline -> () (* allow to disable 'inline' *)
 					| _ ->
 						error ("Field " ^ i ^ " has different property access than core type") p;
 				end;