Browse Source

[eval] specify indexOf with negative startIndex

Simon Krajewski 6 years ago
parent
commit
caa3a4aaec
2 changed files with 13 additions and 4 deletions
  1. 5 0
      src/macro/eval/evalStdLib.ml
  2. 8 4
      std/String.hx

+ 5 - 0
src/macro/eval/evalStdLib.ml

@@ -2055,6 +2055,11 @@ module StdString = struct
 			if str.slength = 0 then
 			if str.slength = 0 then
 				vint (max 0 (min i this.slength))
 				vint (max 0 (min i this.slength))
 			else begin
 			else begin
+				let i =
+					if i >= this.slength then raise Not_found
+					else if i < 0 then max (this.slength + i) 0
+					else i
+				in
 				let b = get_offset this i in
 				let b = get_offset this i in
 				let offset,_,_ = find_substring this str false i b in
 				let offset,_,_ = find_substring this str false i b in
 				vint offset
 				vint offset

+ 8 - 4
std/String.hx

@@ -29,7 +29,7 @@
 
 
 	String can be concatenated by using the `+` operator. If an operand is not a
 	String can be concatenated by using the `+` operator. If an operand is not a
 	String, it is passed through `Std.string()` first.
 	String, it is passed through `Std.string()` first.
-	
+
 	@see https://haxe.org/manual/std-String.html
 	@see https://haxe.org/manual/std-String.html
 **/
 **/
 extern class String {
 extern class String {
@@ -78,9 +78,13 @@ extern class String {
 		String.
 		String.
 
 
 		If `startIndex` is given, the search is performed within the substring
 		If `startIndex` is given, the search is performed within the substring
-		of `this` String starting from `startIndex`. Otherwise the search is
-		performed within `this` String. In either case, the returned position
-		is relative to the beginning of `this` String.
+		of `this` String starting from `startIndex` (if `startIndex` is posivite
+		or 0) or `max(this.length + startIndex, 0)` (if `startIndex` is negative).
+
+		If `startIndex` exceeds `this.length`, -1 is returned.
+
+		Otherwise the search is performed within `this` String. In either case,
+		the returned position is relative to the beginning of `this` String.
 
 
 		If `str` cannot be found, -1 is returned.
 		If `str` cannot be found, -1 is returned.
 	**/
 	**/