Browse Source

[php] optimize String functions by less checking str.length

Alexander Kuzmenko 7 years ago
parent
commit
7976942e42
3 changed files with 11 additions and 9 deletions
  1. 6 7
      std/php/Boot.hx
  2. 3 1
      std/php/_std/String.hx
  3. 2 1
      std/php/_std/StringTools.hx

+ 6 - 7
std/php/Boot.hx

@@ -720,18 +720,17 @@ private class HxString {
 	}
 
 	public static function substr( str:String, pos:Int, ?len:Int ) : String {
-		if (pos < -str.length) {
-			pos = 0;
-		} else if (pos >= str.length) {
-			return '';
-		}
 		return Global.mb_substr(str, pos, len, 'UTF-8');
 	}
 
 	public static function substring( str:String, startIndex:Int, ?endIndex:Int ) : String {
 		if (endIndex == null) {
-			endIndex = str.length;
-		} else if (endIndex < 0) {
+			if(startIndex < 0) {
+				startIndex = 0;
+			}
+			return Global.mb_substr(str, startIndex, null, 'UTF-8');
+		}
+		if (endIndex < 0) {
 			endIndex = 0;
 		}
 		if (startIndex < 0) {

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

@@ -48,7 +48,9 @@ import php.*;
 
 	@:pure function split( delimiter : String ) : Array<String>;
 
-	@:pure function substr( pos : Int, ?len : Int ) : String;
+	@:pure @:runtime inline function substr( pos : Int, ?len : Int ) : String {
+		return Global.mb_substr(this, pos, len, 'UTF-8');
+	}
 
 	@:pure function substring( startIndex : Int, ?endIndex : Int ) : String;
 

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

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