hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit a04c25428ea6c7c072ce6232e1077999b7262886
parent 55c85ddc6687ec6a5d630ca5f062178c9a762914
Author: Byron Torres <b@torresjrjr.com>
Date:   Sat, 14 Jan 2023 23:14:46 +0000

strings: add runelen()

Signed-off-by: Byron Torres <b@torresjrjr.com>

Diffstat:
Mstrings/runes.ha | 15+++++++++++++++
1 file changed, 15 insertions(+), 0 deletions(-)

diff --git a/strings/runes.ha b/strings/runes.ha @@ -24,6 +24,21 @@ export fn fromrunes(runes: []rune) str = { return fromutf8_unsafe(bytes); }; +// Returns a string's rune-length; the number of runes encoded in a string. +export fn runelen(s: str) size = { + let it = iter(s); + let n = 0z; + for (true) { + match (next(&it)) { + case rune => + n += 1; + case done => + break; + }; + }; + return n; +}; + @test fn fromrunes() void = { const tests: [_](str, []rune) = [ ("Harriet", ['H', 'a', 'r', 'r', 'i', 'e', 't']),