Selaa lähdekoodia

[cpp] Expose some native utf8 functions. Closes #4440

Hugh 9 vuotta sitten
vanhempi
commit
dd5d9a1d98
3 muutettua tiedostoa jossa 37 lisäystä ja 43 poistoa
  1. 4 4
      std/cpp/ConstPointer.hx
  2. 20 1
      std/cpp/NativeString.hx
  3. 13 38
      std/cpp/_std/haxe/Utf8.hx

+ 4 - 4
std/cpp/ConstPointer.hx

@@ -35,10 +35,10 @@ extern class ConstPointer<T>
    @:analyzer(no_simplification)
    public function get_value() : T;
 
-	public function lt(inOther:Pointer<T>):Bool;
-	public function leq(inOther:Pointer<T>):Bool;
-	public function gt(inOther:Pointer<T>):Bool;
-	public function geq(inOther:Pointer<T>):Bool;
+	public function lt(inOther:ConstPointer<T>):Bool;
+	public function leq(inOther:ConstPointer<T>):Bool;
+	public function gt(inOther:ConstPointer<T>):Bool;
+	public function geq(inOther:ConstPointer<T>):Bool;
 
    public static function fromRaw<T>(ptr:RawConstPointer<T>) : ConstPointer<T>;
 

+ 20 - 1
std/cpp/NativeString.hx

@@ -33,7 +33,26 @@ extern class NativeString {
       return untyped __global__.String(inPtr.ptr);
    }
 
+   @:native("_hx_string_compare")
+   public static function compare(inString0:String, inString1:String) : Int return 0;
+
+   @:native("_hx_utf8_char_code_at")
+   public static function utf8CharCodeAt(inString:String, inIndex:Int) : Int return 0;
+
+   @:native("_hx_utf8_length")
+   public static function utf8Length(inString:String) : Int return 1;
+
+   @:native("_hx_utf8_is_valid")
+   public static function utf8IsValid(inString:String) : Bool return false;
+
+   @:native("_hx_utf8_sub")
+   public static function utf8Sub(inString:String,charStart:Int, inLen:Int) : String return null;
+
    @:native("_hx_string_create")
-	public static function fromPointerLen(inPtr:ConstPointer<Char>, len:Int ) : String;
+   public static function fromPointerLen(inPtr:ConstPointer<Char>, len:Int ) : String;
+
+   @:native("_hx_utf8_decode_advance")
+   public static function utf8DecodeAdvance(reference:Char) : Int  return 0;
+
 }
 

+ 13 - 38
std/cpp/_std/haxe/Utf8.hx

@@ -21,6 +21,8 @@
  */
 package haxe;
 
+using cpp.NativeString;
+
 @:coreApi
 class Utf8
 {
@@ -52,61 +54,34 @@ class Utf8
 		return untyped __global__.__hxcpp_utf8_string_to_char_bytes(s);
 	}
 
-	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);
+	public #if !cppia inline #end static function iter( s : String, chars : Int -> Void ) : Void {
+      var src = s.c_str();
+      var end = src.add( s.length );
+
+      while(src.lt(end))
+         chars(src.ptr.utf8DecodeAdvance());
 	}
 
 	public static function charCodeAt( s : String, index : Int ) : Int {
-      var array:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(s);
-		return array[index];
+      return s.utf8CharCodeAt(index);
 	}
 
 	public static function validate( s : String ) : Bool {
-      try {
-          untyped __global__.__hxcpp_utf8_string_to_char_array(s);
-          return true;
-      } catch(e:Dynamic) { }
-      return false;
+      return s.utf8IsValid();
 	}
 
 	public static function length( s : String ) : Int {
-      var array:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(s);
-      return array.length;
+      return s.utf8Length();
 	}
 
 	public static function compare( a : String, b : String ) : Int {
-      var a_:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(a);
-      var b_:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(b);
-      var min = a_.length < b_.length ? a_.length : b_.length;
-      for(i in 0...min)
-      {
-         if (a_[i] < b_[i]) return -1;
-         if (a_[i] > b_[i]) return 1;
-      }
-      return a_.length==b_.length ? 0 : a_.length<b_.length ? -1 : 1;
+      return a.compare(b);
 	}
 
 	public static function sub( s : String, pos : Int, len : Int ) : String {
-      var array:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(s);
-      var last = len < 0 ? array.length : pos+len;
-      if (last>array.length) last = array.length;
-      var sub = array.slice(pos,last);
-		return untyped __global__.__hxcpp_char_array_to_utf8_string(sub);
+      return s.utf8Sub(pos,len);
 	}
 
-
-
-
-
-
-
-
-
-
-
-
 }