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
+18
View File
@@ -129,6 +129,24 @@ negative to count backwards from the end of the string.
It is a runtime error if `search` is not a string or `start` is not an integer
index within the string's byte length.
### **split**(seperator)
Returns a list of one or more strings seperated by `seperator`.
:::wren
var string = "abc abc abc"
System.print(string.split(" ")) //> [abc, abc, abc]
It is a runtime error if `seperator` is not a string or is an empty string.
### **replace**(old, swap)
Returns a new string with all occurences of `old` replaced with `swap`.
:::wren
var string = "abc abc abc"
System.print(string.replace(" ", "")) //> abcabcabc
### **iterate**(iterator), **iteratorValue**(iterator)
Implements the [iterator protocol](../../control-flow.html#the-iterator-protocol)