feat: add startIndex parameter to string indexOf and update internal find function

Add a new `indexOf(_,_)` primitive that accepts a start index for searching within strings, and modify the existing `wrenStringFind` function to accept and use a start index parameter. The existing `indexOf(_)` and `contains(_)` methods now pass 0 as the start index to maintain backward compatibility. The Boyer-Moore-Horspool search algorithm is updated to offset comparisons by the start index and return the absolute position in the original string. New test cases verify correct behavior with various start index values including edge cases where the start index exceeds string length.
This commit is contained in:
Bob Nystrom
2016-08-04 04:51:46 +00:00
4 changed files with 31 additions and 9 deletions
+6
View File
@@ -5,6 +5,12 @@ System.print("abcd".indexOf("abcd")) // expect: 0
System.print("abcd".indexOf("abcde")) // expect: -1
System.print("abab".indexOf("ab")) // expect: 0
System.print("abcd".indexOf("cd", 0)) // expect: 2
System.print("abcd".indexOf("cd", 1)) // expect: 2
System.print("abcd".indexOf("cd", 2)) // expect: 2
System.print("abcd".indexOf("cd", 3)) // expect: -1
System.print("abcd".indexOf("cd", 10)) // expect: -1
// More complex cases.
System.print("abcdefabcdefg".indexOf("defg")) // expect: 9
System.print("abcdabcdabcd".indexOf("dab")) // expect: 3