Răsfoiți Sursa

Add `StringTools.contains` (#7608)

* Add `StringTools.eregReplace` and `StringTools.contains`

* Update StringTools.hx

* Add StringTools.eregReplace / StringTools.contains for PHP

* Remove eregReplace and null check

* Don't forget about PHP

* Minor wording fix
Mark Knol 6 ani în urmă
părinte
comite
d555619eff

+ 9 - 0
std/StringTools.hx

@@ -183,6 +183,15 @@ class StringTools {
 		return s.split("&gt;").join(">").split("&lt;").join("<").split("&quot;").join('"').split("&#039;").join("'").split("&amp;").join("&");
 	}
 
+	/**
+		Returns `true` if `s` contains `value` and  `false` otherwise.
+
+		When `value` is `null`, the result is unspecified.
+	**/
+	public static inline function contains(s : String, value : String) : Bool {
+		return s.indexOf(value) != -1;
+	}
+
 	/**
 		Tells if the string `s` starts with the string `start`.
 

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

@@ -40,6 +40,10 @@ import php.*;
 		return Global.htmlspecialchars_decode(s, Const.ENT_QUOTES);
 	}
 
+	public inline static function contains(s : String, value : String) : Bool {
+		return s.indexOf(value) != -1;
+	}
+
 	public static function startsWith( s : String, start : String ) : Bool {
 		return start == '' || Global.strpos(s, start) == 0;
 	}
@@ -219,5 +223,4 @@ import php.*;
 			return argument;
 		}
 	}
-
 }

+ 6 - 0
tests/unit/src/unitstd/StringTools.unit.hx

@@ -109,6 +109,12 @@ StringTools.hex(0xABCDEF, 7) == "0ABCDEF";
 StringTools.hex( -1, 8) == "FFFFFFFF";
 StringTools.hex( -481400000, 8) == "E34E6B40";
 
+// contains
+var s = "foo1bar";
+StringTools.contains(s, '') == true;
+StringTools.contains(s, 'bar') == true;
+StringTools.contains(s, 'test') == false;
+
 // fastCodeAt
 var s = "foo1bar";
 StringTools.fastCodeAt(s, 0) == 102;