Browse Source

[python] use native startswith/endswith in StringTools (closes #6138)

Dan Korostelev 8 năm trước cách đây
mục cha
commit
9f0f0a1f2a
2 tập tin đã thay đổi với 16 bổ sung3 xóa
  1. 6 2
      std/StringTools.hx
  2. 10 1
      std/python/NativeStringTools.hx

+ 6 - 2
std/StringTools.hx

@@ -180,7 +180,7 @@ class StringTools {
 
 		If `start` is the empty String `""`, the result is true.
 	**/
-	public static #if (cs || java) inline #end function startsWith( s : String, start : String ) : Bool {
+	public static #if (cs || java || python) inline #end function startsWith( s : String, start : String ) : Bool {
 		#if java
 		return untyped s.startsWith(start);
 		#elseif cs
@@ -196,6 +196,8 @@ class StringTools {
 		return true;
 		#elseif hl
 		return @:privateAccess (s.length >= start.length && s.bytes.compare(0,start.bytes,0,start.length<<1) == 0);
+		#elseif python
+		return python.NativeStringTools.startswith(s, start);
 		#else
 		return( s.length >= start.length && s.substr(0, start.length) == start );
 		#end
@@ -208,7 +210,7 @@ class StringTools {
 
 		If `end` is the empty String `""`, the result is true.
 	**/
-	public static #if (cs || java) inline #end function endsWith( s : String, end : String ) : Bool {
+	public static #if (cs || java || python) inline #end function endsWith( s : String, end : String ) : Bool {
 		#if java
 		return untyped s.endsWith(end);
 		#elseif cs
@@ -226,6 +228,8 @@ class StringTools {
 		var elen = end.length;
 		var slen = s.length;
 		return @:privateAccess (slen >= elen && s.bytes.compare((slen - elen) << 1, end.bytes, 0, elen << 1) == 0);
+		#elseif python
+		return python.NativeStringTools.endswith(s, end);
 		#else
 		var elen = end.length;
 		var slen = s.length;

+ 10 - 1
std/python/NativeStringTools.hx

@@ -49,4 +49,13 @@ class NativeStringTools {
 		return python.Syntax.field(s, "rpartition")(sep);
 	}
 
-}
+	public static inline function startswith (s:String, prefix:String):Bool
+	{
+		return python.Syntax.field(s, "startswith")(prefix);
+	}
+
+	public static inline function endswith (s:String, suffix:String):Bool
+	{
+		return python.Syntax.field(s, "endswith")(suffix);
+	}
+}