feat: make String iterable over code points with iterate/iteratorValue primitives

Add String as a subclass of Sequence in core.wren and implement the iterator protocol for strings in wren_core.c. The string_iterate native advances through UTF-8 code point boundaries, while string_iteratorValue extracts the full code point at a given byte index. Includes comprehensive test coverage for iteration, edge cases (empty strings, mid-sequence positions), and error handling for invalid iterator types and out-of-bounds indices. Also updates documentation for String, List, and Sequence classes with iterator protocol details and usage examples.
This commit is contained in:
Bob Nystrom
2015-01-23 04:58:22 +00:00
parent 7e52e36a1c
commit 488fb27b91
14 changed files with 141 additions and 21 deletions
+13
View File
@@ -56,6 +56,19 @@ Returns the index of the first byte matching `search` in the string or `-1` if
It is a runtime error if `search` is not a string.
### **iterate**(iterator), **iteratorValue**(iterator)
Implements the [iterator protocol](../control-flow.html#the-iterator-protocol)
for iterating over the *code points* in the string:
:::dart
var codePoints = []
for (c in "(ᵔᴥᵔ)") {
codePoints.add(c)
}
IO.print(codePoints) // ["(", "ᵔ", "ᴥ", "ᵔ", ")"].
### **startsWith**(prefix)
Checks if the string starts with `prefix`.