unicode
nimony/lib/std/unicode.nim
type Rune = distinct int32\ Type that can hold a single Unicode code point.
A Rune may be composed with other Runes to a character on the screen.
RuneImplis the underlying type used to store Runes, currentlyint32.func runeLen(s: openArray): int64Returns the number of runes (code points) in
s, not the UTF-8 byte length (len(s)).func runeLenAt(s: openArray; i: int64): int64template fastRuneAt(s: openArray; i: int64; result: untyped; doInc: bool): untypedReturns the rune
s[i]inresult.If
doInc == true(default),iis incremented by the number of bytes that have been processed.func runeAt(s: openArray; i: int64): Runefunc validateUtf8(s: openArray): int64Returns the position of the invalid byte in
sif the stringsdoes not hold valid UTF-8 data. Otherwise-1is returned.See also:
- toUTF8 func
- $ func alias for
toUTF8 - fastToUTF8Copy template
template fastToUTF8Copy(c: Rune; s: var string; pos: int64; doInc: bool): untypedCopies UTF-8 representation of
cinto the preallocated stringsstarting at positionpos.If
doInc == true(default),posis incremented by the number of bytes that have been processed.To be the most efficient, make sure
sis preallocated with an additional amount equal to the byte length ofc.See also:
- validateUtf8 func
- toUTF8 func
- $ func alias for
toUTF8
func toUTF8(c: Rune): stringConverts a rune into its UTF-8 representation.
See also:
- validateUtf8 func
- $ func alias for
toUTF8 - utf8 iterator
- fastToUTF8Copy template
func add(s: var string; c: Rune)Adds a rune
cto a strings.func $(rune: Rune): stringfunc $(runes: seq): stringfunc runeOffset(s: openArray; pos: int64; start: int64): int64Returns the byte offset in
swhere code-point indexposbegins, optionally skipping ahead tostartbytes first. Returns-1ifposis past the last code point.Beware: This can lead to unoptimized code and slow execution! Most problems can be solved more efficiently by using an iterator or conversion to a seq of Rune.
See also:
func runeReverseOffset(s: openArray; rev: int64): tuple[int64, int64]Returns a tuple with the byte offset of the rune at position
revins, counting from the end (starting with 1) and the total number of runes in the string.Returns a negative value for offset if there are too few runes in the string to satisfy the request.
Beware: This can lead to unoptimized code and slow execution! Most problems can be solved more efficiently by using an iterator or conversion to a seq of Rune.
See also:
func runeAtPos(s: openArray; pos: int64): RuneReturns the rune at code-point index
pos(not a UTF-8 byte index; seeruneAt).Beware: This can lead to unoptimized code and slow execution! Most problems can be solved more efficiently by using an iterator or conversion to a seq of Rune.
See also:
func runeStrAtPos(s: openArray; pos: int64): stringReturns the UTF-8 substring for the rune at code-point index
pos.Beware: This can lead to unoptimized code and slow execution! Most problems can be solved more efficiently by using an iterator or conversion to a seq of Rune.
See also:
func runeSubStr(s: openArray; pos: int64; len: int64): stringReturns the UTF-8 substring starting at code point
poswithlencode points.If
posorlenis negative they count from the end of the string. Iflenis not given it means the longest possible string.func <=%(a: Rune; b: Rune): boolChecks if code point of
ais smaller or equal to code point ofb.func <%(a: Rune; b: Rune): boolChecks if code point of
ais smaller than code point ofb.func ==(a: Rune; b: Rune): boolChecks if two runes are equal.
func toLower(c: Rune): RuneConverts
cinto lower case. This works for any rune.If possible, prefer
toLowerovertoUpper.See also:
func toUpper(c: Rune): RuneConverts
cinto upper case. This works for any rune.If possible, prefer
toLowerovertoUpper.See also:
func toTitle(c: Rune): Runefunc isLower(c: Rune): boolReturns true if
cis a lower case rune.If possible, prefer
isLoweroverisUpper.See also:
func isUpper(c: Rune): boolReturns true if
cis a upper case rune.If possible, prefer
isLoweroverisUpper.See also:
func isAlpha(c: Rune): boolReturns true if
cis an alpha rune (i.e., a letter).See also:
func isTitle(c: Rune): boolReturns true if
cis a Unicode titlecase code point.See also:
func isWhiteSpace(c: Rune): boolReturns true if
cis a Unicode whitespace code point.See also:
func isCombining(c: Rune): boolReturns true if
cis a Unicode combining code unit.See also:
func isAlpha(s: openArray): boolReturns true if
scontains all alphabetic runes.func isSpace(s: openArray): boolReturns true if
scontains all whitespace runes.func toUpper(s: openArray): stringConverts
sinto upper-case runes.func toLower(s: openArray): stringConverts
sinto lower-case runes.func swapCase(s: openArray): stringSwaps the case of runes in
s.Returns a new string such that the cases of all runes are swapped if possible.
func capitalize(s: openArray): stringConverts the first character of
sinto an upper-case rune.func translate(s: openArray; replacements: proc (key: string): string {.noSideEffect.}): stringTranslates words in a string using the
replacementsfunc to substitute words insideswith their replacements.replacementsis any func that takes a word and returns a new word to fill it's place.func title(s: openArray): stringConverts
sto a unicode title.Returns a new string such that the first character in each word inside
sis capitalized.iterator runes(s: openArray): RuneIterates over any rune of the string
sreturning runes.iterator utf8(s: openArray): stringIterates over any rune of the string
sreturning utf8 values.See also:
- validateUtf8 func
- toUTF8 func
- $ func alias for
toUTF8 - fastToUTF8Copy template
func toRunes(s: openArray): seqfunc cmpRunesIgnoreCase(a: openArray; b: openArray): int64Compares two UTF-8 strings and ignores the case. Returns:
|
0if a == b|
< 0if a < b|
> 0if a > bfunc reversed(s: openArray): stringReturns the reverse of
s, interpreting it as runes.Unicode combining characters are correctly interpreted as well.
func graphemeLen(s: openArray; i: int64): int64The number of bytes belonging to byte index
s[i], including following combining code units.func lastRune(s: openArray; last: int64): tuple[Rune, int64]Treating
lastas the inclusive byte index of the final UTF-8 byte of interest, finds that code unit's starting byte (walking backward through continuation bytes), decodes oneRune, and returns(rune, byteLength)for that encoding.func size(r: Rune): int64Returns the number of bytes the rune
rtakes.iterator split(s: openArray; seps: openArray; maxsplit: int64): stringSplits the unicode string
sinto substrings using a group of separators.Substrings are separated by a substring containing only
seps.iterator splitWhitespace(s: openArray): stringSplits a unicode string at whitespace runes.
func splitWhitespace(s: openArray): seqThe same as the splitWhitespace iterator, but is a func that returns a sequence of substrings.
iterator split(s: openArray; sep: Rune; maxsplit: int64): stringSplits the unicode string
sinto substrings using a single separator. Substrings are separated by the runesep.func split(s: openArray; seps: openArray; maxsplit: int64): seqThe same as the split iterator, but is a func that returns a sequence of substrings.
func split(s: openArray; sep: Rune; maxsplit: int64): seqThe same as the split iterator, but is a func that returns a sequence of substrings.
func strip(s: openArray; leading: bool; trailing: bool; runes: openArray): stringStrips leading or trailing
runesfromsand returns the resulting string.If
leadingis true (default), leadingrunesare stripped. Iftrailingis true (default), trailingrunesare stripped. If both are false, the string is returned unchanged.func repeat(c: Rune; count: int64): stringReturns a string of
countRunesc.The returned string will have a rune-length of
count.func align(s: openArray; count: int64; padding: Rune): stringAligns a unicode string
swithpadding, so that it has a rune-length ofcount.paddingcharacters (by default spaces) are added beforesresulting in right alignment. Ifs.runelen >= count, no spaces are added andsis returned unchanged. If you need to left align a string use the alignLeft func.func alignLeft(s: openArray; count: int64; padding: Rune): stringLeft-aligns a unicode string
swithpadding, so that it has a rune-length ofcount.paddingcharacters (by default spaces) are added aftersresulting in left alignment. Ifs.runelen >= count, no spaces are added andsis returned unchanged. If you need to right align a string use the align func.func runeLen(s: string): int64Returns the number of runes (code points) in
s, nots.len(bytes).func runeLenAt(s: string; i: int64): int64func runeAt(s: string; i: int64): Runefunc validateUtf8(s: string): int64Returns the position of the invalid byte in
sif the stringsdoes not hold valid UTF-8 data. Otherwise-1is returned.See also:
- toUTF8 func
- $ func alias for
toUTF8 - fastToUTF8Copy template
func runeOffset(s: string; pos: int64; start: int64): int64Returns the byte offset in
swhere code-point indexposbegins, optionally skipping ahead tostartbytes first. Returns-1ifposis past the last code point.Beware: This can lead to unoptimized code and slow execution! Most problems can be solved more efficiently by using an iterator or conversion to a seq of Rune.
See also:
func runeReverseOffset(s: string; rev: int64): tuple[int64, int64]Returns a tuple with the byte offset of the rune at position
revins, counting from the end (starting with 1) and the total number of runes in the string.Returns a negative value for offset if there are too few runes in the string to satisfy the request.
Beware: This can lead to unoptimized code and slow execution! Most problems can be solved more efficiently by using an iterator or conversion to a seq of Rune.
See also:
func runeAtPos(s: string; pos: int64): RuneReturns the rune at code-point index
pos(not a UTF-8 byte index; seeruneAt).Beware: This can lead to unoptimized code and slow execution! Most problems can be solved more efficiently by using an iterator or conversion to a seq of Rune.
See also:
func runeStrAtPos(s: string; pos: int64): stringReturns the UTF-8 substring for the rune at code-point index
pos.Beware: This can lead to unoptimized code and slow execution! Most problems can be solved more efficiently by using an iterator or conversion to a seq of Rune.
See also:
func runeSubStr(s: string; pos: int64; len: int64): stringReturns the UTF-8 substring starting at code point
poswithlencode points.If
posorlenis negative they count from the end of the string. Iflenis not given it means the longest possible string.func isAlpha(s: string): boolReturns true if
scontains all alphabetic runes.func isSpace(s: string): boolReturns true if
scontains all whitespace runes.func toUpper(s: string): stringConverts
sinto upper-case runes.func toLower(s: string): stringConverts
sinto lower-case runes.func swapCase(s: string): stringSwaps the case of runes in
s.Returns a new string such that the cases of all runes are swapped if possible.
func capitalize(s: string): stringConverts the first character of
sinto an upper-case rune.func translate(s: string; replacements: proc (key: string): string {.noSideEffect.}): stringTranslates words in a string using the
replacementsfunc to substitute words insideswith their replacements.replacementsis any func that takes a word and returns a new word to fill it's place.func title(s: string): stringConverts
sto a unicode title.Returns a new string such that the first character in each word inside
sis capitalized.iterator runes(s: string): RuneIterates over any rune of the string
sreturning runes.iterator utf8(s: string): stringIterates over any rune of the string
sreturning utf8 values.See also:
- validateUtf8 func
- toUTF8 func
- $ func alias for
toUTF8 - fastToUTF8Copy template
func toRunes(s: string): seqfunc cmpRunesIgnoreCase(a: string; b: string): int64Compares two UTF-8 strings and ignores the case. Returns:
|
0if a == b|
< 0if a < b|
> 0if a > bfunc reversed(s: string): stringReturns the reverse of
s, interpreting it as runes.Unicode combining characters are correctly interpreted as well.
func graphemeLen(s: string; i: int64): int64The number of bytes belonging to byte index
s[i], including following combining code units.func lastRune(s: string; last: int64): tuple[Rune, int64]Same as
lastRune(openArray[char], int)—lastis the inclusive byte index of the final UTF-8 byte examined.iterator split(s: string; seps: openArray; maxsplit: int64): stringSplits the unicode string
sinto substrings using a group of separators.Substrings are separated by a substring containing only
seps.iterator splitWhitespace(s: string): stringSplits a unicode string at whitespace runes.
func splitWhitespace(s: string): seqThe same as the splitWhitespace iterator, but is a func that returns a sequence of substrings.
iterator split(s: string; sep: Rune; maxsplit: int64): stringSplits the unicode string
sinto substrings using a single separator. Substrings are separated by the runesep.func split(s: string; seps: openArray; maxsplit: int64): seqThe same as the split iterator, but is a func that returns a sequence of substrings.
func split(s: string; sep: Rune; maxsplit: int64): seqThe same as the split iterator, but is a func that returns a sequence of substrings.
func strip(s: string; leading: bool; trailing: bool; runes: openArray): stringStrips leading or trailing
runesfromsand returns the resulting string.If
leadingis true (default), leadingrunesare stripped. Iftrailingis true (default), trailingrunesare stripped. If both are false, the string is returned unchanged.func align(s: string; count: int64; padding: Rune): stringAligns a unicode string
swithpadding, so that it has a rune-length ofcount.paddingcharacters (by default spaces) are added beforesresulting in right alignment. Ifs.runelen >= count, no spaces are added andsis returned unchanged. If you need to left align a string use the alignLeft func.func alignLeft(s: string; count: int64; padding: Rune): stringLeft-aligns a unicode string
swithpadding, so that it has a rune-length ofcount.paddingcharacters (by default spaces) are added aftersresulting in left alignment. Ifs.runelen >= count, no spaces are added andsis returned unchanged. If you need to right align a string use the align func.