std/cstrutils

  Source   Edit

This module supports helper routines for working with cstring without having to convert cstring to string, in order to save allocations.

See also

Procs

func cmpIgnoreCase(a, b: cstring): int {....gcsafe, extern: "csuCmpIgnoreCase",
    raises: [], tags: [].}
Compares two strings in a case insensitive manner. Returns:
  • 0 if a == b
  • < 0 if a < b
  • > 0 if a > b

Example:

assert cmpIgnoreCase(cstring"hello", cstring"HeLLo") == 0
assert cmpIgnoreCase(cstring"echo", cstring"hello") < 0
assert cmpIgnoreCase(cstring"yellow", cstring"hello") > 0
  Source   Edit
func cmpIgnoreStyle(a, b: cstring): int {....gcsafe, extern: "csuCmpIgnoreStyle",
    raises: [], tags: [].}
Semantically the same as cmp(normalize($a), normalize($b)). It is just optimized to not allocate temporary strings. This should NOT be used to compare Nim identifier names, use macros.eqIdent for that. Returns:
  • 0 if a == b
  • < 0 if a < b
  • > 0 if a > b

Example:

assert cmpIgnoreStyle(cstring"hello", cstring"H_e_L_Lo") == 0
  Source   Edit
func endsWith(s, suffix: cstring): bool {....gcsafe, extern: "csuEndsWith",
    raises: [], tags: [].}

Returns true if s ends with suffix.

The JS backend uses the native String.prototype.endsWith function.

Example:

assert endsWith(cstring"Hello, Nimion", cstring"Nimion")
assert not endsWith(cstring"Hello, Nimion", cstring"Hello")
assert endsWith(cstring"Hello", cstring"")
  Source   Edit
func startsWith(s, prefix: cstring): bool {....gcsafe, extern: "csuStartsWith",
    raises: [], tags: [].}

Returns true if s starts with prefix.

The JS backend uses the native String.prototype.startsWith function.

Example:

assert startsWith(cstring"Hello, Nimion", cstring"Hello")
assert not startsWith(cstring"Hello, Nimion", cstring"Nimion")
assert startsWith(cstring"Hello", cstring"")
  Source   Edit