std/strbasics

  Source   Edit

This module provides some high performance string operations.

Experimental API, subject to change.

Procs

proc add(x: var string; y: openArray[char]) {....raises: [], tags: [].}
Concatenates x and y in place. y must not overlap with x to allow future memcpy optimizations.   Source   Edit
func setSlice(s: var string; slice: Slice[int]) {....raises: [], tags: [].}
Inplace version of substr.

Example:

import std/sugar

var a = "Hello, Nim!"
doAssert a.dup(setSlice(7 .. 9)) == "Nim"
doAssert a.dup(setSlice(0 .. 0)) == "H"
doAssert a.dup(setSlice(0 .. 1)) == "He"
doAssert a.dup(setSlice(0 .. 10)) == a
doAssert a.dup(setSlice(1 .. 0)).len == 0
doAssert a.dup(setSlice(20 .. -1)).len == 0


doAssertRaises(AssertionDefect):
  discard a.dup(setSlice(-1 .. 1))

doAssertRaises(AssertionDefect):
  discard a.dup(setSlice(1 .. 11))
  Source   Edit
func strip(a: var string; leading = true; trailing = true;
           chars: set[char] = whitespaces) {.inline, ...raises: [], tags: [].}

Inplace version of strip. Strips leading or trailing chars (default: whitespace characters).

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

Example:

var a = "  vhellov   "
strip(a)
assert a == "vhellov"

a = "  vhellov   "
a.strip(leading = false)
assert a == "  vhellov"

a = "  vhellov   "
a.strip(trailing = false)
assert a == "vhellov   "

var c = "blaXbla"
c.strip(chars = {'b', 'a'})
assert c == "laXbl"
c = "blaXbla"
c.strip(chars = {'b', 'a', 'l'})
assert c == "X"
  Source   Edit