mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-05 11:24:08 +00:00
Some procs to deal with Rune position base indexing.
It can't be perfect but at least one can index on rune position efficiently.
This commit is contained in:
@@ -183,6 +183,25 @@ proc `$`*(runes: seq[Rune]): string =
|
||||
result = ""
|
||||
for rune in runes: result.add(rune.toUTF8)
|
||||
|
||||
proc runeOffset*(s: string, pos:int): int =
|
||||
## Returns the byte position of unicode character at position in s
|
||||
var
|
||||
i = 0
|
||||
o = 0
|
||||
while i < pos:
|
||||
o += runeLenAt(s, o)
|
||||
inc i
|
||||
o
|
||||
|
||||
proc rune*(s: string, pos:int): Rune =
|
||||
## Returns the unicode character at position pos
|
||||
fastRuneAt(s, runeOffset(s, pos), result, false)
|
||||
|
||||
proc runeStr*(s: string, pos:int): string =
|
||||
## Returns the unicode character at position pos as UTF8 String
|
||||
let o = runeOffset(s, pos)
|
||||
s[o.. (o+runeLenAt(s, o)-1)]
|
||||
|
||||
const
|
||||
alphaRanges = [
|
||||
0x00d8, 0x00f6, # -
|
||||
|
||||
Reference in New Issue
Block a user