Forráskód Böngészése

[eval] fix lastIndexOf if startIndex >= length

closes #7455

How the fuck was this not in our tests...
Simon Krajewski 7 éve
szülő
commit
7012dfb84e

+ 1 - 1
src/macro/eval/evalStdLib.ml

@@ -1980,7 +1980,7 @@ module StdString = struct
 				vint (max 0 (min i this.slength))
 			end else begin
 				let i = default_int startIndex (this.slength - 1) in
-				if i >= this.slength || i < 0 then raise Not_found;
+				let i = if i < 0 then raise Not_found else if i >= this.slength then this.slength - 1 else i in
 				let s = this.sstring in
 				if this.sascii then
 					vint (Str.search_backward (Str.regexp_string str.sstring) s i)

+ 2 - 0
tests/unit/src/unitstd/String.unit.hx

@@ -96,6 +96,8 @@ s.lastIndexOf("bar", 12) == 12;
 s.lastIndexOf("bar", 11) == 9;
 s.lastIndexOf("bar", 9) == 9;
 s.lastIndexOf("bar", 8) == -1;
+s.lastIndexOf("a", s.length) == 13;
+s.lastIndexOf("a", s.length + 9000) == 13;
 
 // split
 var s = "xfooxfooxxbarxbarxx";

+ 6 - 1
tests/unit/src/unitstd/Unicode.unit.hx

@@ -19,7 +19,12 @@ s.indexOf("a")==0;
 s.lastIndexOf("a")==1;
 s.indexOf("😂")>0;
 s.lastIndexOf("😂")>0;
-s.lastIndexOf("é")==s.length-1;
+s.lastIndexOf("é") == s.length-1;
+s.lastIndexOf("a", s.length) == 1;
+s.lastIndexOf("a", s.length + 9000) == 1;
+s.lastIndexOf("é", s.length) == s.length-1;
+s.lastIndexOf("é", s.length + 9000) == s.length-1;
+
 var s = "abc";
 s.indexOf("éé")<0;
 s.lastIndexOf("éé")<0;