feat: add split and replace methods to String class with tests and docs

Implement String.split(delim) and String.replace(from, to) methods in the core Wren VM. split returns a list of substrings separated by the given delimiter, while replace returns a new string with all occurrences of the old substring replaced by the new one. Both methods validate arguments (must be non-empty strings) and abort the fiber on invalid input. Include comprehensive test suites covering basic usage, multiple matches, non-ASCII characters, 8-bit clean data, and error cases. Update the string.markdown documentation with usage examples for both methods.
This commit is contained in:
Bob Nystrom
2017-03-15 14:04:56 +00:00
10 changed files with 158 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
System.print("something".replace("some", "no")) // expect: nothing
System.print("something".replace("thing", "one")) // expect: someone
System.print("something".replace("ometh", "umm")) // expect: summing
System.print("something".replace("math", "ton")) // expect: something
// Multiple.
System.print("somethingsomething".replace("some", "no")) // expect: nothingnothing
System.print("abc abc abc".replace(" ", "")) // expect: abcabcabc
System.print("abcabcabc".replace("abc", "")) // expect:
// Non-ASCII.
System.print("søméthîng".replace("sømé", "nø")) // expect: nøthîng
System.print("søméthîng".replace("meth", "ton")) // expect: søméthîng
// 8-bit clean.
System.print("a\0b\0c".replace("\0", "")) // expect: abc
System.print("a\0b\0c".replace("b", "") == "a\0\0c") // expect: true
+1
View File
@@ -0,0 +1 @@
"foo".replace("", "f") // expect runtime error: From must be a non-empty string.
@@ -0,0 +1 @@
"foo".replace("o", 1) // expect runtime error: To must be a string.
@@ -0,0 +1 @@
"foo".replace(1, "o") // expect runtime error: From must be a non-empty string.
+16
View File
@@ -0,0 +1,16 @@
System.print("something".split("meth")) // expect: [so, ing]
System.print("something".split("some")) // expect: [, thing]
System.print("something".split("ing")) // expect: [someth, ]
System.print("something".split("math")) // expect: [something]
// Multiple.
System.print("somethingsomething".split("meth")) // expect: [so, ingso, ing]
System.print("abc abc abc".split(" ")) // expect: [abc, abc, abc]
System.print("abcabcabc".split("abc")) // expect: [, , , ]
// Non-ASCII.
System.print("søméthîng".split("méth")) // expect: [sø, îng]
System.print("søméthîng".split("meth")) // expect: [søméthîng]
// 8-bit clean.
System.print("a\0b\0c".split("\0")) // expect: [a, b, c]
@@ -0,0 +1 @@
"foo".split(1) // expect runtime error: Argument must be a non-empty string.
@@ -0,0 +1 @@
"foo".split("") // expect runtime error: Argument must be a non-empty string.