Browse Source

[php] optimize String.charAt(), charCodeAt(), StringTools.fastCodeAt()

Alexander Kuzmenko 7 years ago
parent
commit
de32a47059
3 changed files with 8 additions and 15 deletions
  1. 6 13
      std/php/Boot.hx
  2. 1 1
      std/php/_std/String.hx
  3. 1 1
      std/php/_std/StringTools.hx

+ 6 - 13
std/php/Boot.hx

@@ -519,15 +519,6 @@ class Boot {
 		return @:privateAccess new HxDynamicStr(str);
 		return @:privateAccess new HxDynamicStr(str);
 	}
 	}
 
 
-	static public function utf8CharAt(str:String, index:Int):Null<String> {
-		if (index < 0 || index >= str.length) {
-			return null;
-		}
-		//preg_split() is faster than mb_substr()
-		var chars = Global.preg_split('//u', str, -1, Const.PREG_SPLIT_NO_EMPTY);
-		return chars == false ? null : (chars:NativeArray)[index];
-	}
-
 	/**
 	/**
 		Creates Haxe-compatible closure of an instance method.
 		Creates Haxe-compatible closure of an instance method.
 		@param obj - any object
 		@param obj - any object
@@ -677,13 +668,15 @@ private class HxString {
 	}
 	}
 
 
 	public static function charAt( str:String, index:Int) : String {
 	public static function charAt( str:String, index:Int) : String {
-		return Syntax.coalesce(Boot.utf8CharAt(str, index), '');
+		return index < 0 ? '' : Global.mb_substr(str, index, 1);
 	}
 	}
 
 
 	public static function charCodeAt( str:String, index:Int) : Null<Int> {
 	public static function charCodeAt( str:String, index:Int) : Null<Int> {
-		var char = Boot.utf8CharAt(str, index);
-		if(char == null) return null;
-		return Global.mb_ord(char, 'UTF-8');
+		if(index < 0) {
+			return null;
+		}
+		var char = Global.mb_substr(str, index, 1, 'UTF-8');
+		return char == '' ? null : Global.mb_ord(char, 'UTF-8');
 	}
 	}
 
 
 	public static function indexOf( str:String, search:String, startIndex:Int = null ) : Int {
 	public static function indexOf( str:String, search:String, startIndex:Int = null ) : Int {

+ 1 - 1
std/php/_std/String.hx

@@ -37,7 +37,7 @@ import php.*;
 	}
 	}
 
 
 	@:pure @:runtime inline function charAt(index : Int) : String {
 	@:pure @:runtime inline function charAt(index : Int) : String {
-		return Syntax.coalesce(Boot.utf8CharAt(this, index), '');
+		return index < 0 ? '' : Global.mb_substr(this, index, 1, 'UTF-8');
 	}
 	}
 
 
 	@:pure function charCodeAt( index : Int) : Null<Int>;
 	@:pure function charCodeAt( index : Int) : Null<Int>;

+ 1 - 1
std/php/_std/StringTools.hx

@@ -95,7 +95,7 @@ import php.*;
 	}
 	}
 
 
 	public static inline function fastCodeAt( s : String, index : Int ) : Int {
 	public static inline function fastCodeAt( s : String, index : Int ) : Int {
-		return (s.length == index ? 0 : Global.mb_ord(Boot.utf8CharAt(s, index), 'UTF-8'));
+		return (s.length == index ? 0 : Global.mb_ord(Global.mb_substr(s, index, 1, 'UTF-8'), 'UTF-8'));
 	}
 	}
 
 
 	public static inline function isEof( c : Int ) : Bool {
 	public static inline function isEof( c : Int ) : Bool {