commit c0b074c7b9bc037f41a29d75c5d12882e92cb0e4 parent e9387b648e91996f7d61825857224fc282b97c61 Author: Drew DeVault <sir@cmpwn.com> Date: Sun, 7 Nov 2021 10:53:55 +0100 strings: add strings::runes Signed-off-by: Drew DeVault <sir@cmpwn.com> Diffstat:
M | strings/iter.ha | | | 15 | +++++++++++++++ |
1 file changed, 15 insertions(+), 0 deletions(-)
diff --git a/strings/iter.ha b/strings/iter.ha @@ -117,3 +117,18 @@ export fn iter_str(iter: *iterator) str = { assert(prev(&s) is void); assert(next(&s) as rune == 'に'); }; + +// Returns a slice of runes for a string in O(n). The caller must free the +// return value. +export fn runes(s: str) []rune = { + let sl: []rune = alloc([], len(s)); + let iter = iter(s); + for (true) { + match (next(&iter)) { + case void => break; + case r: rune => + append(sl, r); + }; + }; + return sl; +};