Source Edit

This module provides support to handle the Unicode UTF-8 encoding.

There are no specialized insert, delete, add and contains procedures for seq[Rune] in this module because the generic variants of these procedures in the system module already work with it.

The current version is compatible with Unicode v12.0.0.

See also:

Imports

strbasics

Types

  1. Rune = distinct RuneImpl

Type that can hold a single Unicode code point.

A Rune may be composed with other Runes to a character on the screen. RuneImpl is the underlying type used to store Runes, currently int32.

Source Edit

Procs

  1. proc `$`(rune: Rune): string {....raises: [], tags: [], forbids: [].}

An alias for toUTF8.

See also:

Source Edit

  1. proc `$`(runes: seq[Rune]): string {....raises: [], tags: [], forbids: [].}

Converts a sequence of Runes to a string.

See also:

Example:

  1. let
  2. someString = "öÑ"
  3. someRunes = toRunes(someString)
  4. doAssert $someRunes == someString

Source Edit

  1. proc `<%`(a, b: Rune): bool {....raises: [], tags: [], forbids: [].}

Checks if code point of a is smaller than code point of b.

Example:

  1. let
  2. a = "ú".runeAt(0)
  3. b = "ü".runeAt(0)
  4. doAssert a <% b

Source Edit

  1. proc `<=%`(a, b: Rune): bool {....raises: [], tags: [], forbids: [].}

Checks if code point of a is smaller or equal to code point of b.

Example:

  1. let
  2. a = "ú".runeAt(0)
  3. b = "ü".runeAt(0)
  4. doAssert a <=% b

Source Edit

  1. proc `==`(a, b: Rune): bool {....raises: [], tags: [], forbids: [].}

Checks if two runes are equal. Source Edit

  1. proc add(s: var string; c: Rune) {....raises: [], tags: [], forbids: [].}

Adds a rune c to a string s.

Example:

  1. var s = "abc"
  2. let c = "ä".runeAt(0)
  3. s.add(c)
  4. doAssert s == "abcä"

Source Edit

  1. proc align(s: openArray[char]; count: Natural; padding = ' '.Rune): string {.
  2. noSideEffect, ...gcsafe, extern: "nucAlignString", raises: [], tags: [],
  3. forbids: [].}

Aligns a unicode string s with padding, so that it has a rune-length of count.

padding characters (by default spaces) are added before s resulting in right alignment. If s.runelen >= count, no spaces are added and s is returned unchanged. If you need to left align a string use the alignLeft proc.

Example:

  1. assert align("abc", 4) == " abc"
  2. assert align("a", 0) == "a"
  3. assert align("1232", 6) == " 1232"
  4. assert align("1232", 6, '#'.Rune) == "##1232"
  5. assert align("Åge", 5) == " Åge"
  6. assert align("×", 4, '_'.Rune) == "___×"

Source Edit

  1. proc align(s: string; count: Natural; padding = ' '.Rune): string {.
  2. noSideEffect, inline, ...raises: [], tags: [], forbids: [].}

Aligns a unicode string s with padding, so that it has a rune-length of count.

padding characters (by default spaces) are added before s resulting in right alignment. If s.runelen >= count, no spaces are added and s is returned unchanged. If you need to left align a string use the alignLeft proc.

Example:

  1. assert align("abc", 4) == " abc"
  2. assert align("a", 0) == "a"
  3. assert align("1232", 6) == " 1232"
  4. assert align("1232", 6, '#'.Rune) == "##1232"
  5. assert align("Åge", 5) == " Åge"
  6. assert align("×", 4, '_'.Rune) == "___×"

Source Edit

  1. proc alignLeft(s: openArray[char]; count: Natural; padding = ' '.Rune): string {.
  2. noSideEffect, ...raises: [], tags: [], forbids: [].}

Left-aligns a unicode string s with padding, so that it has a rune-length of count.

padding characters (by default spaces) are added after s resulting in left alignment. If s.runelen >= count, no spaces are added and s is returned unchanged. If you need to right align a string use the align proc.

Example:

  1. assert alignLeft("abc", 4) == "abc "
  2. assert alignLeft("a", 0) == "a"
  3. assert alignLeft("1232", 6) == "1232 "
  4. assert alignLeft("1232", 6, '#'.Rune) == "1232##"
  5. assert alignLeft("Åge", 5) == "Åge "
  6. assert alignLeft("×", 4, '_'.Rune) == "×___"

Source Edit

  1. proc alignLeft(s: string; count: Natural; padding = ' '.Rune): string {.
  2. noSideEffect, inline, ...raises: [], tags: [], forbids: [].}

Left-aligns a unicode string s with padding, so that it has a rune-length of count.

padding characters (by default spaces) are added after s resulting in left alignment. If s.runelen >= count, no spaces are added and s is returned unchanged. If you need to right align a string use the align proc.

Example:

  1. assert alignLeft("abc", 4) == "abc "
  2. assert alignLeft("a", 0) == "a"
  3. assert alignLeft("1232", 6) == "1232 "
  4. assert alignLeft("1232", 6, '#'.Rune) == "1232##"
  5. assert alignLeft("Åge", 5) == "Åge "
  6. assert alignLeft("×", 4, '_'.Rune) == "×___"

Source Edit

  1. proc capitalize(s: openArray[char]): string {.noSideEffect, ...gcsafe,
  2. extern: "nuc$1", raises: [], tags: [], forbids: [].}

Converts the first character of s into an upper-case rune.

Example:

  1. doAssert capitalize("βeta") == "Βeta"

Source Edit

  1. proc capitalize(s: string): string {.noSideEffect, ...raises: [], tags: [],
  2. forbids: [].}

Converts the first character of s into an upper-case rune.

Example:

  1. doAssert capitalize("βeta") == "Βeta"

Source Edit

  1. proc cmpRunesIgnoreCase(a, b: openArray[char]): int {....gcsafe, extern: "nuc$1",
  2. raises: [], tags: [], forbids: [].}

Compares two UTF-8 strings and ignores the case. Returns:

0 if a == b
< 0 if a < b
> 0 if a > b

Source Edit

  1. proc cmpRunesIgnoreCase(a, b: string): int {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Compares two UTF-8 strings and ignores the case. Returns:

0 if a == b
< 0 if a < b
> 0 if a > b

Source Edit

  1. proc graphemeLen(s: openArray[char]; i: Natural): Natural {....raises: [],
  2. tags: [], forbids: [].}

The number of bytes belonging to byte index s[i], including following combining code units.

Example:

  1. let a = "añyóng"
  2. doAssert a.graphemeLen(1) == 2 ## ñ
  3. doAssert a.graphemeLen(2) == 1
  4. doAssert a.graphemeLen(4) == 2 ## ó

Source Edit

  1. proc graphemeLen(s: string; i: Natural): Natural {.inline, ...raises: [], tags: [],
  2. forbids: [].}

The number of bytes belonging to byte index s[i], including following combining code unit.

Example:

  1. let a = "añyóng"
  2. doAssert a.graphemeLen(1) == 2 ## ñ
  3. doAssert a.graphemeLen(2) == 1
  4. doAssert a.graphemeLen(4) == 2 ## ó

Source Edit

  1. proc isAlpha(c: Rune): bool {....gcsafe, extern: "nuc$1", raises: [], tags: [],
  2. forbids: [].}

Returns true if c is an alpha rune (i.e., a letter).

See also:

Source Edit

  1. proc isAlpha(s: openArray[char]): bool {.noSideEffect, ...gcsafe,
  2. extern: "nuc$1Str", raises: [], tags: [], forbids: [].}

Returns true if s contains all alphabetic runes.

Example:

  1. let a = "añyóng"
  2. doAssert a.isAlpha

Source Edit

  1. proc isAlpha(s: string): bool {.noSideEffect, inline, ...raises: [], tags: [],
  2. forbids: [].}

Returns true if s contains all alphabetic runes.

Example:

  1. let a = "añyóng"
  2. doAssert a.isAlpha

Source Edit

  1. proc isCombining(c: Rune): bool {....gcsafe, extern: "nuc$1", raises: [], tags: [],
  2. forbids: [].}

Returns true if c is a Unicode combining code unit.

See also:

Source Edit

  1. proc isLower(c: Rune): bool {....gcsafe, extern: "nuc$1", raises: [], tags: [],
  2. forbids: [].}

Returns true if c is a lower case rune.

If possible, prefer isLower over isUpper.

See also:

Source Edit

  1. proc isSpace(s: openArray[char]): bool {.noSideEffect, ...gcsafe,
  2. extern: "nuc$1Str", raises: [], tags: [], forbids: [].}

Returns true if s contains all whitespace runes.

Example:

  1. let a = "\t\l \v\r\f"
  2. doAssert a.isSpace

Source Edit

  1. proc isSpace(s: string): bool {.noSideEffect, inline, ...raises: [], tags: [],
  2. forbids: [].}

Returns true if s contains all whitespace runes.

Example:

  1. let a = "\t\l \v\r\f"
  2. doAssert a.isSpace

Source Edit

  1. proc isTitle(c: Rune): bool {....gcsafe, extern: "nuc$1", raises: [], tags: [],
  2. forbids: [].}

Returns true if c is a Unicode titlecase code point.

See also:

Source Edit

  1. proc isUpper(c: Rune): bool {....gcsafe, extern: "nuc$1", raises: [], tags: [],
  2. forbids: [].}

Returns true if c is a upper case rune.

If possible, prefer isLower over isUpper.

See also:

Source Edit

  1. proc isWhiteSpace(c: Rune): bool {....gcsafe, extern: "nuc$1", raises: [],
  2. tags: [], forbids: [].}

Returns true if c is a Unicode whitespace code point.

See also:

Source Edit

  1. proc lastRune(s: openArray[char]; last: int): (Rune, int) {....raises: [],
  2. tags: [], forbids: [].}

Length of the last rune in s[0..last]. Returns the rune and its length in bytes. Source Edit

  1. proc lastRune(s: string; last: int): (Rune, int) {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Length of the last rune in s[0..last]. Returns the rune and its length in bytes. Source Edit

  1. proc repeat(c: Rune; count: Natural): string {.noSideEffect, ...gcsafe,
  2. extern: "nucRepeatRune", raises: [], tags: [], forbids: [].}

Returns a string of count Runes c.

The returned string will have a rune-length of count.

Example:

  1. let a = "ñ".runeAt(0)
  2. doAssert a.repeat(5) == "ñññññ"

Source Edit

  1. proc reversed(s: openArray[char]): string {....raises: [], tags: [], forbids: [].}

Returns the reverse of s, interpreting it as runes.

Unicode combining characters are correctly interpreted as well.

Example:

  1. assert reversed("Reverse this!") == "!siht esreveR"
  2. assert reversed("先秦兩漢") == "漢兩秦先"
  3. assert reversed("as⃝df̅") == "f̅ds⃝a"
  4. assert reversed("a⃞b⃞c⃞") == "c⃞b⃞a⃞"

Source Edit

  1. proc reversed(s: string): string {.inline, ...raises: [], tags: [], forbids: [].}

Returns the reverse of s, interpreting it as runes.

Unicode combining characters are correctly interpreted as well.

Example:

  1. assert reversed("Reverse this!") == "!siht esreveR"
  2. assert reversed("先秦兩漢") == "漢兩秦先"
  3. assert reversed("as⃝df̅") == "f̅ds⃝a"
  4. assert reversed("a⃞b⃞c⃞") == "c⃞b⃞a⃞"

Source Edit

  1. proc runeAt(s: openArray[char]; i: Natural): Rune {....raises: [], tags: [],
  2. forbids: [].}

Returns the rune in s at byte index i.

See also:

Example:

  1. let a = "añyóng"
  2. doAssert a.runeAt(1) == "ñ".runeAt(0)
  3. doAssert a.runeAt(2) == "ñ".runeAt(1)
  4. doAssert a.runeAt(3) == "y".runeAt(0)

Source Edit

  1. proc runeAt(s: string; i: Natural): Rune {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Returns the rune in s at byte index i.

See also:

Example:

  1. let a = "añyóng"
  2. doAssert a.runeAt(1) == "ñ".runeAt(0)
  3. doAssert a.runeAt(2) == "ñ".runeAt(1)
  4. doAssert a.runeAt(3) == "y".runeAt(0)

Source Edit

  1. proc runeAtPos(s: openArray[char]; pos: int): Rune {....raises: [], tags: [],
  2. forbids: [].}

Returns the rune at position 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:

Source Edit

  1. proc runeAtPos(s: string; pos: int): Rune {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Returns the rune at position 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:

Source Edit

  1. proc runeLen(s: openArray[char]): int {....gcsafe, extern: "nuc$1", raises: [],
  2. tags: [], forbids: [].}

Returns the number of runes of the string s.

Example:

  1. let a = "añyóng"
  2. doAssert a.runeLen == 6
  3. ## note: a.len == 8

Source Edit

  1. proc runeLen(s: string): int {.inline, ...raises: [], tags: [], forbids: [].}

Returns the number of runes of the string s.

Example:

  1. let a = "añyóng"
  2. doAssert a.runeLen == 6
  3. ## note: a.len == 8

Source Edit

  1. proc runeLenAt(s: openArray[char]; i: Natural): int {....raises: [], tags: [],
  2. forbids: [].}

Returns the number of bytes the rune starting at s[i] takes.

See also:

Example:

  1. let a = "añyóng"
  2. doAssert a.runeLenAt(0) == 1
  3. doAssert a.runeLenAt(1) == 2

Source Edit

  1. proc runeLenAt(s: string; i: Natural): int {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Returns the number of bytes the rune starting at s[i] takes.

See also:

Example:

  1. let a = "añyóng"
  2. doAssert a.runeLenAt(0) == 1
  3. doAssert a.runeLenAt(1) == 2

Source Edit

  1. proc runeOffset(s: openArray[char]; pos: Natural; start: Natural = 0): int {.
  2. ...raises: [], tags: [], forbids: [].}

Returns the byte position of rune at position pos in s with an optional start byte position. Returns the special value -1 if it runs out of the string.

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:

Example:

  1. let a = "añyóng"
  2. doAssert a.runeOffset(1) == 1
  3. doAssert a.runeOffset(3) == 4
  4. doAssert a.runeOffset(4) == 6

Source Edit

  1. proc runeOffset(s: string; pos: Natural; start: Natural = 0): int {.inline,
  2. ...raises: [], tags: [], forbids: [].}

Returns the byte position of rune at position pos in s with an optional start byte position. Returns the special value -1 if it runs out of the string.

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:

Example:

  1. let a = "añyóng"
  2. doAssert a.runeOffset(1) == 1
  3. doAssert a.runeOffset(3) == 4
  4. doAssert a.runeOffset(4) == 6

Source Edit

  1. proc runeReverseOffset(s: openArray[char]; rev: Positive): (int, int) {.
  2. ...raises: [], tags: [], forbids: [].}

Returns a tuple with the byte offset of the rune at position rev in s, 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:

Source Edit

  1. proc runeReverseOffset(s: string; rev: Positive): (int, int) {.inline,
  2. ...raises: [], tags: [], forbids: [].}

Returns a tuple with the byte offset of the rune at position rev in s, 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:

Source Edit

  1. proc runeStrAtPos(s: openArray[char]; pos: Natural): string {....raises: [],
  2. tags: [], forbids: [].}

Returns the rune at position pos as UTF8 String.

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:

Source Edit

  1. proc runeStrAtPos(s: string; pos: Natural): string {.inline, ...raises: [],
  2. tags: [], forbids: [].}

Returns the rune at position pos as UTF8 String.

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:

Source Edit

  1. proc runeSubStr(s: openArray[char]; pos: int; len: int = int.high): string {.
  2. ...raises: [], tags: [], forbids: [].}

Returns the UTF-8 substring starting at code point pos with len code points.

If pos or len is negative they count from the end of the string. If len is not given it means the longest possible string.

Example:

  1. let s = "Hänsel ««: 10,00€"
  2. doAssert(runeSubStr(s, 0, 2) == "Hä")
  3. doAssert(runeSubStr(s, 10, 1) == ":")
  4. doAssert(runeSubStr(s, -6) == "10,00€")
  5. doAssert(runeSubStr(s, 10) == ": 10,00€")
  6. doAssert(runeSubStr(s, 12, 5) == "10,00")
  7. doAssert(runeSubStr(s, -6, 3) == "10,")

Source Edit

  1. proc runeSubStr(s: string; pos: int; len: int = int.high): string {.inline,
  2. ...raises: [], tags: [], forbids: [].}

Returns the UTF-8 substring starting at code point pos with len code points.

If pos or len is negative they count from the end of the string. If len is not given it means the longest possible string.

Example:

  1. let s = "Hänsel ««: 10,00€"
  2. doAssert(runeSubStr(s, 0, 2) == "Hä")
  3. doAssert(runeSubStr(s, 10, 1) == ":")
  4. doAssert(runeSubStr(s, -6) == "10,00€")
  5. doAssert(runeSubStr(s, 10) == ": 10,00€")
  6. doAssert(runeSubStr(s, 12, 5) == "10,00")
  7. doAssert(runeSubStr(s, -6, 3) == "10,")

Source Edit

  1. proc size(r: Rune): int {.noSideEffect, ...raises: [], tags: [], forbids: [].}

Returns the number of bytes the rune r takes.

Example:

  1. let a = toRunes "aá"
  2. doAssert size(a[0]) == 1
  3. doAssert size(a[1]) == 2

Source Edit

  1. proc split(s: openArray[char]; sep: Rune; maxsplit: int = -1): seq[string] {.
  2. noSideEffect, ...gcsafe, extern: "nucSplitRune", raises: [], tags: [],
  3. forbids: [].}

The same as the split iterator, but is a proc that returns a sequence of substrings. Source Edit

  1. proc split(s: openArray[char]; seps: openArray[Rune] = unicodeSpaces;
  2. maxsplit: int = -1): seq[string] {.noSideEffect, ...gcsafe,
  3. extern: "nucSplitRunes", raises: [], tags: [], forbids: [].}

The same as the split iterator, but is a proc that returns a sequence of substrings. Source Edit

  1. proc split(s: string; sep: Rune; maxsplit: int = -1): seq[string] {.
  2. noSideEffect, inline, ...raises: [], tags: [], forbids: [].}

The same as the split iterator, but is a proc that returns a sequence of substrings. Source Edit

  1. proc split(s: string; seps: openArray[Rune] = unicodeSpaces; maxsplit: int = -1): seq[
  2. string] {.noSideEffect, inline, ...raises: [], tags: [], forbids: [].}

The same as the split iterator, but is a proc that returns a sequence of substrings. Source Edit

  1. proc splitWhitespace(s: openArray[char]): seq[string] {.noSideEffect, ...gcsafe,
  2. extern: "ncuSplitWhitespace", raises: [], tags: [], forbids: [].}

The same as the splitWhitespace iterator, but is a proc that returns a sequence of substrings. Source Edit

  1. proc splitWhitespace(s: string): seq[string] {.noSideEffect, inline, ...raises: [],
  2. tags: [], forbids: [].}

The same as the splitWhitespace iterator, but is a proc that returns a sequence of substrings. Source Edit

  1. proc strip(s: openArray[char]; leading = true; trailing = true;
  2. runes: openArray[Rune] = unicodeSpaces): string {.noSideEffect,
  3. ...gcsafe, extern: "nucStrip", raises: [], tags: [], forbids: [].}

Strips leading or trailing runes from s and returns the resulting string.

If leading is true (default), leading runes are stripped. If trailing is true (default), trailing runes are stripped. If both are false, the string is returned unchanged.

Example:

  1. let a = "\táñyóng "
  2. doAssert a.strip == "áñyóng"
  3. doAssert a.strip(leading = false) == "\táñyóng"
  4. doAssert a.strip(trailing = false) == "áñyóng "

Source Edit

  1. proc strip(s: string; leading = true; trailing = true;
  2. runes: openArray[Rune] = unicodeSpaces): string {.noSideEffect,
  3. inline, ...raises: [], tags: [], forbids: [].}

Strips leading or trailing runes from s and returns the resulting string.

If leading is true (default), leading runes are stripped. If trailing is true (default), trailing runes are stripped. If both are false, the string is returned unchanged.

Example:

  1. let a = "\táñyóng "
  2. doAssert a.strip == "áñyóng"
  3. doAssert a.strip(leading = false) == "\táñyóng"
  4. doAssert a.strip(trailing = false) == "áñyóng "

Source Edit

  1. proc swapCase(s: openArray[char]): string {.noSideEffect, ...gcsafe,
  2. extern: "nuc$1", raises: [], tags: [], forbids: [].}

Swaps the case of runes in s.

Returns a new string such that the cases of all runes are swapped if possible.

Example:

  1. doAssert swapCase("Αlpha Βeta Γamma") == "αLPHA βETA γAMMA"

Source Edit

  1. proc swapCase(s: string): string {.noSideEffect, inline, ...raises: [], tags: [],
  2. forbids: [].}

Swaps the case of runes in s.

Returns a new string such that the cases of all runes are swapped if possible.

Example:

  1. doAssert swapCase("Αlpha Βeta Γamma") == "αLPHA βETA γAMMA"

Source Edit

  1. proc title(s: openArray[char]): string {.noSideEffect, ...gcsafe, extern: "nuc$1",
  2. raises: [], tags: [], forbids: [].}

Converts s to a unicode title.

Returns a new string such that the first character in each word inside s is capitalized.

Example:

  1. doAssert title("αlpha βeta γamma") == "Αlpha Βeta Γamma"

Source Edit

  1. proc title(s: string): string {.noSideEffect, inline, ...raises: [], tags: [],
  2. forbids: [].}

Converts s to a unicode title.

Returns a new string such that the first character in each word inside s is capitalized.

Example:

  1. doAssert title("αlpha βeta γamma") == "Αlpha Βeta Γamma"

Source Edit

  1. proc toLower(c: Rune): Rune {....gcsafe, extern: "nuc$1", raises: [], tags: [],
  2. forbids: [].}

Converts c into lower case. This works for any rune.

If possible, prefer toLower over toUpper.

See also:

Source Edit

  1. proc toLower(s: openArray[char]): string {.noSideEffect, ...gcsafe,
  2. extern: "nuc$1Str", raises: [], tags: [], forbids: [].}

Converts s into lower-case runes.

Example:

  1. doAssert toLower("ABΓ") == "abγ"

Source Edit

  1. proc toLower(s: string): string {.noSideEffect, inline, ...raises: [], tags: [],
  2. forbids: [].}

Converts s into lower-case runes.

Example:

  1. doAssert toLower("ABΓ") == "abγ"

Source Edit

  1. proc toRunes(s: openArray[char]): seq[Rune] {....raises: [], tags: [], forbids: [].}

Obtains a sequence containing the Runes in s.

See also:

  • $ proc for a reverse operation

Example:

  1. let a = toRunes("aáä")
  2. doAssert a == @["a".runeAt(0), "á".runeAt(0), "ä".runeAt(0)]

Source Edit

  1. proc toRunes(s: string): seq[Rune] {.inline, ...raises: [], tags: [], forbids: [].}

Obtains a sequence containing the Runes in s.

See also:

  • $ proc for a reverse operation

Example:

  1. let a = toRunes("aáä")
  2. doAssert a == @["a".runeAt(0), "á".runeAt(0), "ä".runeAt(0)]

Source Edit

  1. proc toTitle(c: Rune): Rune {....gcsafe, extern: "nuc$1", raises: [], tags: [],
  2. forbids: [].}

Converts c to title case.

See also:

Source Edit

  1. proc toUpper(c: Rune): Rune {....gcsafe, extern: "nuc$1", raises: [], tags: [],
  2. forbids: [].}

Converts c into upper case. This works for any rune.

If possible, prefer toLower over toUpper.

See also:

Source Edit

  1. proc toUpper(s: openArray[char]): string {.noSideEffect, ...gcsafe,
  2. extern: "nuc$1Str", raises: [], tags: [], forbids: [].}

Converts s into upper-case runes.

Example:

  1. doAssert toUpper("abγ") == "ABΓ"

Source Edit

  1. proc toUpper(s: string): string {.noSideEffect, inline, ...raises: [], tags: [],
  2. forbids: [].}

Converts s into upper-case runes.

Example:

  1. doAssert toUpper("abγ") == "ABΓ"

Source Edit

  1. proc toUTF8(c: Rune): string {....gcsafe, extern: "nuc$1", raises: [], tags: [],
  2. forbids: [].}

Converts a rune into its UTF-8 representation.

See also:

Example:

  1. let a = "añyóng"
  2. doAssert a.runeAt(1).toUTF8 == "ñ"

Source Edit

  1. proc translate(s: openArray[char]; replacements: proc (key: string): string): string {.
  2. ...gcsafe, extern: "nuc$1", effectsOf: replacements, ...raises: [], tags: [],
  3. forbids: [].}

Translates words in a string using the replacements proc to substitute words inside s with their replacements.

replacements is any proc that takes a word and returns a new word to fill it’s place.

Example:

  1. proc wordToNumber(s: string): string =
  2. case s
  3. of "one": "1"
  4. of "two": "2"
  5. else: s
  6. let a = "one two three four"
  7. doAssert a.translate(wordToNumber) == "1 2 three four"

Source Edit

  1. proc translate(s: string; replacements: proc (key: string): string): string {.
  2. effectsOf: replacements, inline, ...raises: [], tags: [], forbids: [].}

Translates words in a string using the replacements proc to substitute words inside s with their replacements.

replacements is any proc that takes a word and returns a new word to fill it’s place.

Example:

  1. proc wordToNumber(s: string): string =
  2. case s
  3. of "one": "1"
  4. of "two": "2"
  5. else: s
  6. let a = "one two three four"
  7. doAssert a.translate(wordToNumber) == "1 2 three four"

Source Edit

  1. proc validateUtf8(s: openArray[char]): int {....raises: [], tags: [], forbids: [].}

Returns the position of the invalid byte in s if the string s does not hold valid UTF-8 data. Otherwise -1 is returned.

See also:

Source Edit

  1. proc validateUtf8(s: string): int {.inline, ...raises: [], tags: [], forbids: [].}

Returns the position of the invalid byte in s if the string s does not hold valid UTF-8 data. Otherwise -1 is returned.

See also:

Source Edit

Iterators

  1. iterator runes(s: openArray[char]): Rune {....raises: [], tags: [], forbids: [].}

Iterates over any rune of the string s returning runes. Source Edit

  1. iterator runes(s: string): Rune {....raises: [], tags: [], forbids: [].}

Iterates over any rune of the string s returning runes. Source Edit

  1. iterator split(s: openArray[char]; sep: Rune; maxsplit: int = -1): string {.
  2. ...raises: [], tags: [], forbids: [].}

Splits the unicode string s into substrings using a single separator. Substrings are separated by the rune sep.

Example:

  1. import std/sequtils
  2. assert toSeq(split(";;hÃllo;this;is;an;;example;;;是", ";".runeAt(0))) ==
  3. @["", "", "hÃllo", "this", "is", "an", "", "example", "", "", "是"]

Source Edit

  1. iterator split(s: openArray[char]; seps: openArray[Rune] = unicodeSpaces;
  2. maxsplit: int = -1): string {....raises: [], tags: [], forbids: [].}

Splits the unicode string s into substrings using a group of separators.

Substrings are separated by a substring containing only seps.

Example:

  1. import std/sequtils
  2. assert toSeq("hÃllo\lthis\lis an\texample\l是".split) ==
  3. @["hÃllo", "this", "is", "an", "example", "是"]
  4. # And the following code splits the same string using a sequence of Runes.
  5. assert toSeq(split("añyóng:hÃllo;是$example", ";:$".toRunes)) ==
  6. @["añyóng", "hÃllo", "是", "example"]
  7. # example with a `Rune` separator and unused one `;`:
  8. assert toSeq(split("ab是de:f:", ";:是".toRunes)) == @["ab", "de", "f", ""]
  9. # Another example that splits a string containing a date.
  10. let date = "2012-11-20T22:08:08.398990"
  11. assert toSeq(split(date, " -:T".toRunes)) ==
  12. @["2012", "11", "20", "22", "08", "08.398990"]

Source Edit

  1. iterator split(s: string; sep: Rune; maxsplit: int = -1): string {....raises: [],
  2. tags: [], forbids: [].}

Splits the unicode string s into substrings using a single separator. Substrings are separated by the rune sep.

Example:

  1. import std/sequtils
  2. assert toSeq(split(";;hÃllo;this;is;an;;example;;;是", ";".runeAt(0))) ==
  3. @["", "", "hÃllo", "this", "is", "an", "", "example", "", "", "是"]

Source Edit

  1. iterator split(s: string; seps: openArray[Rune] = unicodeSpaces;
  2. maxsplit: int = -1): string {....raises: [], tags: [], forbids: [].}

Splits the unicode string s into substrings using a group of separators.

Substrings are separated by a substring containing only seps.

Example:

  1. import std/sequtils
  2. assert toSeq("hÃllo\lthis\lis an\texample\l是".split) ==
  3. @["hÃllo", "this", "is", "an", "example", "是"]
  4. # And the following code splits the same string using a sequence of Runes.
  5. assert toSeq(split("añyóng:hÃllo;是$example", ";:$".toRunes)) ==
  6. @["añyóng", "hÃllo", "是", "example"]
  7. # example with a `Rune` separator and unused one `;`:
  8. assert toSeq(split("ab是de:f:", ";:是".toRunes)) == @["ab", "de", "f", ""]
  9. # Another example that splits a string containing a date.
  10. let date = "2012-11-20T22:08:08.398990"
  11. assert toSeq(split(date, " -:T".toRunes)) ==
  12. @["2012", "11", "20", "22", "08", "08.398990"]

Source Edit

  1. iterator splitWhitespace(s: openArray[char]): string {....raises: [], tags: [],
  2. forbids: [].}

Splits a unicode string at whitespace runes. Source Edit

  1. iterator splitWhitespace(s: string): string {....raises: [], tags: [], forbids: [].}

Splits a unicode string at whitespace runes. Source Edit

  1. iterator utf8(s: openArray[char]): string {....raises: [], tags: [], forbids: [].}

Iterates over any rune of the string s returning utf8 values.

See also:

Source Edit

  1. iterator utf8(s: string): string {....raises: [], tags: [], forbids: [].}

Iterates over any rune of the string s returning utf8 values.

See also:

Source Edit

Templates

  1. template fastRuneAt(s: openArray[char] or string; i: int; result: untyped;
  2. doInc = true)

Returns the rune s[i] in result.

If doInc == true (default), i is incremented by the number of bytes that have been processed.

Source Edit

  1. template fastToUTF8Copy(c: Rune; s: var string; pos: int; doInc = true)

Copies UTF-8 representation of c into the preallocated string s starting at position pos.

If doInc == true (default), pos is incremented by the number of bytes that have been processed.

To be the most efficient, make sure s is preallocated with an additional amount equal to the byte length of c.

See also:

Source Edit