summary refs log tree commit diff stats
path: root/lib/std
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2021-03-01 07:51:12 -0800
committerGitHub <noreply@github.com>2021-03-01 07:51:12 -0800
commit0cb02fbbeeba6d4bd526ccf6cfff2eda05990bd5 (patch)
tree8ee83f3d11f1554daca694baac5d3ece4ee6b9b0 /lib/std
parentdd6b0f81efe54cbc17a79e5c3d7aa7aaf34357f6 (diff)
downloadNim-0cb02fbbeeba6d4bd526ccf6cfff2eda05990bd5.tar.gz
add overload `add(a: var string, b: openArray[char])` (#15951)
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/strbasics.nim16
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/std/strbasics.nim b/lib/std/strbasics.nim
index ce061adca..357e56208 100644
--- a/lib/std/strbasics.nim
+++ b/lib/std/strbasics.nim
@@ -8,9 +8,25 @@
 #
 
 ## This module provides some high performance string operations.
+##
+## Experimental API, subject to change.
 
 const whitespaces = {' ', '\t', '\v', '\r', '\l', '\f'}
 
+proc add*(x: var string, y: openArray[char]) =
+  ## Concatenates `x` and `y` in place. `y` must not overlap with `x` to
+  ## allow future `memcpy` optimizations.
+  # Use `{.noalias.}` ?
+  let n = x.len
+  x.setLen n + y.len
+    # pending https://github.com/nim-lang/Nim/issues/14655#issuecomment-643671397
+    # use x.setLen(n + y.len, isInit = false)
+  var i = 0
+  while i < y.len:
+    x[n + i] = y[i]
+    i.inc
+  # xxx use `nimCopyMem(x[n].addr, y[0].addr, y.len)` after some refactoring
+
 func stripSlice(s: openArray[char], leading = true, trailing = true, chars: set[char] = whitespaces): Slice[int] =
   ## Returns the slice range of `s` which is stripped `chars`.
   runnableExamples: