strbasics.nim 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2021 Nim Contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module provides some high performance string operations.
  10. ##
  11. ## Experimental API, subject to change.
  12. const whitespaces = {' ', '\t', '\v', '\r', '\l', '\f'}
  13. proc add*(x: var string, y: openArray[char]) =
  14. ## Concatenates `x` and `y` in place. `y` must not overlap with `x` to
  15. ## allow future `memcpy` optimizations.
  16. # Use `{.noalias.}` ?
  17. let n = x.len
  18. x.setLen n + y.len
  19. # pending https://github.com/nim-lang/Nim/issues/14655#issuecomment-643671397
  20. # use x.setLen(n + y.len, isInit = false)
  21. var i = 0
  22. while i < y.len:
  23. x[n + i] = y[i]
  24. i.inc
  25. # xxx use `nimCopyMem(x[n].addr, y[0].addr, y.len)` after some refactoring
  26. func stripSlice(s: openArray[char], leading = true, trailing = true, chars: set[char] = whitespaces): Slice[int] =
  27. ## Returns the slice range of `s` which is stripped `chars`.
  28. runnableExamples:
  29. assert stripSlice(" abc ") == 1 .. 3
  30. var
  31. first = 0
  32. last = high(s)
  33. if leading:
  34. while first <= last and s[first] in chars: inc(first)
  35. if trailing:
  36. while last >= first and s[last] in chars: dec(last)
  37. result = first .. last
  38. func setSlice*(s: var string, slice: Slice[int]) =
  39. ## Inplace version of `substr`.
  40. runnableExamples:
  41. import std/sugar
  42. var a = "Hello, Nim!"
  43. doassert a.dup(setSlice(7 .. 9)) == "Nim"
  44. doAssert a.dup(setSlice(0 .. 0)) == "H"
  45. doAssert a.dup(setSlice(0 .. 1)) == "He"
  46. doAssert a.dup(setSlice(0 .. 10)) == a
  47. doAssert a.dup(setSlice(1 .. 0)).len == 0
  48. doAssert a.dup(setSlice(20 .. -1)).len == 0
  49. doAssertRaises(AssertionDefect):
  50. discard a.dup(setSlice(-1 .. 1))
  51. doAssertRaises(AssertionDefect):
  52. discard a.dup(setSlice(1 .. 11))
  53. let first = slice.a
  54. let last = slice.b
  55. assert first >= 0
  56. assert last <= s.high
  57. if first > last:
  58. s.setLen(0)
  59. return
  60. template impl =
  61. for index in first .. last:
  62. s[index - first] = s[index]
  63. if first > 0:
  64. when nimvm: impl()
  65. else:
  66. # not JS and not Nimscript
  67. when not declared(moveMem):
  68. impl()
  69. else:
  70. when defined(nimSeqsV2):
  71. prepareMutation(s)
  72. moveMem(addr s[0], addr s[first], last - first + 1)
  73. s.setLen(last - first + 1)
  74. func strip*(a: var string, leading = true, trailing = true, chars: set[char] = whitespaces) {.inline.} =
  75. ## Inplace version of `strip`. Strips leading or
  76. ## trailing `chars` (default: whitespace characters).
  77. ##
  78. ## If `leading` is true (default), leading `chars` are stripped.
  79. ## If `trailing` is true (default), trailing `chars` are stripped.
  80. ## If both are false, the string is unchanged.
  81. runnableExamples:
  82. var a = " vhellov "
  83. strip(a)
  84. assert a == "vhellov"
  85. a = " vhellov "
  86. a.strip(leading = false)
  87. assert a == " vhellov"
  88. a = " vhellov "
  89. a.strip(trailing = false)
  90. assert a == "vhellov "
  91. var c = "blaXbla"
  92. c.strip(chars = {'b', 'a'})
  93. assert c == "laXbl"
  94. c = "blaXbla"
  95. c.strip(chars = {'b', 'a', 'l'})
  96. assert c == "X"
  97. setSlice(a, stripSlice(a, leading, trailing, chars))