pykot/strings

String manipulations.

Consts

PkString: JabbaString = (ascii_letters: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
                         ascii_lowercase: "abcdefghijklmnopqrstuvwxyz",
                         ascii_uppercase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
                         digits: "0123456789",
                         hexdigits: "0123456789abcdefABCDEF",
                         whitespace: " \t\n\r\v\f")

Mimics Python's string module. Since string is a built-in type in Nim, it was renamed to PkString (PyKot String).

Usage example: PkString.ascii_lowercase. PkString is a tuple.

Procs

func `*`(cs: char | string; n: int): string
Repeat the cs character/string n times.

Example:

doAssert '-' * 3 == "---"
doAssert "ab" * 2 == "abab"
func `+`(s, t: string): string {....raises: [], tags: [], forbids: [].}

Concatenates s and t into a string.

Mimicking Python.

Example:

doAssert "py" + "thon" == "python"
func isPalindrome(s: string): bool {....raises: [], tags: [], forbids: [].}

Decide if a string is a palindrome or not.

It works with Unicode strings too.

Example:

doAssert "abba".isPalindrome == true
doAssert "".isPalindrome == true
doAssert "abcd".isPalindrome == false
doAssert "รก..รก".isPalindrome == true
func isPalindromeAscii(s: string): bool {....raises: [], tags: [], forbids: [].}

Decide if a string is a palindrome or not.

It works with ASCII strings only.

Example:

doAssert "abba".isPalindromeAscii == true
doAssert "".isPalindromeAscii == true
doAssert "abcd".isPalindromeAscii == false
doAssert "รก..รก".isPalindromeAscii == false
func lastAscii(s: string): char {....raises: [], tags: [], forbids: [].}

Return the last character of a string. The returned value is a char (byte).

If the last character is a non-ASCII character, use the function lastRune.

Example:

let name = "Nim"
doAssert name.lastAscii == 'm'
func lastIndexAscii(s: string): int {....raises: [], tags: [], forbids: [].}

Return the index of the last character of a string.

It works correctly only with an ASCII string.

Example:

let
  s = "Alice"
doAssert s.lastIndexAscii == 4
func lastRune(s: string): Rune {....raises: [], tags: [], forbids: [].}
func lchop(s, sub: string): string {....raises: [], tags: [], forbids: [].}
Remove sub from the beginning of s.

Example:

let s = "ABcde"
doAssert s.lchop("AB") == "cde"
doAssert s.lchop("XXX") == "ABcde"
func lstrip(s: string; chars: set[char] = Whitespace): string {....raises: [],
    tags: [], forbids: [].}

Strips leading chars from s and returns the resulting string.

If chars are not specified, leading whitespaces are removed. Specify chars as a set of chars (bitset).

Example:

doAssert "  ab  ".lstrip() == "ab  "
func lstrip(s: string; chars: string): string {....raises: [], tags: [],
    forbids: [].}

Strips leading chars from s and returns the resulting string.

Specify chars in a string. The string is treated as a set of chars, just like Python's lstrip().

Example:

doAssert "\nab ".lstrip("\n") == "ab "
doAssert "\t\nab ".lstrip("\n") == "\t\nab "
doAssert "\t\nab ".lstrip("\n\t") == "ab "
doAssert "\t\nab".lstrip("\t\n") == "ab"
doAssert "\t\n ab ".lstrip("\t\n") == " ab "
func rchop(s, sub: string): string {....raises: [], tags: [], forbids: [].}
Remove sub from the end of s.

Example:

let fname = "stuff.nim"
doAssert fname.rchop(".nim") == "stuff"
doAssert fname.rchop(".jpg") == "stuff.nim"
let longer = "nim is a great language"
doAssert longer.rchop("uage") == "nim is a great lang"
func reversedAscii(s: string): string {....raises: [], tags: [], forbids: [].}

Reverse an ASCII string.

It returns a reversed copy.

Example:

doAssert "hello".reversedAscii == "olleh"
func rstrip(s: string; chars: set[char] = Whitespace): string {....raises: [],
    tags: [], forbids: [].}

Strips trailing chars from s and returns the resulting string.

If chars are not specified, trailing whitespaces are removed. Specify chars as a set of chars (bitset).

Example:

doAssert "  ab  ".rstrip() == "  ab"
func rstrip(s: string; chars: string): string {....raises: [], tags: [],
    forbids: [].}

Strips trailing chars from s and returns the resulting string.

Specify chars in a string. The string is treated as a set of chars, just like Python's rstrip().

Example:

doAssert "  ab\n".rstrip("\n") == "  ab"
doAssert "  ab\t\n".rstrip("\n") == "  ab\t"
doAssert "  ab\t\n".rstrip("\n\t") == "  ab"
doAssert "  ab\t\n".rstrip("\t\n") == "  ab"
doAssert "  ab  \t\n".rstrip("\t\n") == "  ab  "

Iterators

iterator indicesAscii(s: string): int {....raises: [], tags: [], forbids: [].}
An iterator over the indices of an ASCII string.

Example:

import sequtils
let
  lang = "Nim"
  res = toSeq(lang.indicesAscii)
doAssert res == @[0, 1, 2]