Added iterator for utf8 strings

This commit is contained in:
Hans Raaf
2016-07-13 00:24:21 +02:00
parent 6111d2ec21
commit 3cea6e8a96

View File

@@ -1572,7 +1572,7 @@ proc isTitle*(s: string): bool {.noSideEffect, procvar,
firstRune = true
iterator runes*(s: string): Rune =
## Iterates over any unicode character of the string ``s``
## Iterates over any unicode character of the string ``s`` returning runes
var
i = 0
result: Rune
@@ -1580,6 +1580,14 @@ iterator runes*(s: string): Rune =
fastRuneAt(s, i, result, true)
yield result
iterator utf8*(s: string): string =
## Iterates over any unicode character of the string ``s`` returning utf8 values
var o = 0
while o < s.len:
let n = runeLenAt(s, o)
yield s[o.. (o+n-1)]
o += n
proc toRunes*(s: string): seq[Rune] =
## Obtains a sequence containing the Runes in ``s``
result = newSeq[Rune]()
@@ -1777,6 +1785,12 @@ when isMainModule:
# test for rune positioning and runeSubStr()
let s = "Hänsel ««: 10,00€"
var t = ""
for c in s.utf8:
t.add c
doAssert(s == t)
doAssert(runeReverseOffset(s, 1) == (20, 18))
doAssert(runeReverseOffset(s, 19) == (-1, 18))