summary refs log tree commit diff stats
path: root/lib/core/seqs.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/core/seqs.nim')
-rw-r--r--lib/core/seqs.nim14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/core/seqs.nim b/lib/core/seqs.nim
index 5483c0ebc..9c88040ba 100644
--- a/lib/core/seqs.nim
+++ b/lib/core/seqs.nim
@@ -153,6 +153,20 @@ proc grow*[T](x: var seq[T]; newLen: Natural; value: T) =
   for i in oldLen .. newLen-1:
     xu.p.data[i] = value
 
+proc add*[T](x: var seq[T]; value: sink T) {.magic: "AppendSeqElem", noSideEffect.} =
+  ## Generic proc for adding a data item `y` to a container `x`.
+  ##
+  ## For containers that have an order, `add` means *append*. New generic
+  ## containers should also call their adding proc `add` for consistency.
+  ## Generic code becomes much easier to write if the Nim naming scheme is
+  ## respected.
+  let oldLen = x.len
+  var xu = cast[ptr NimSeqV2[T]](addr x)
+  if xu.p == nil or xu.p.cap < oldLen+1:
+    xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, 1, sizeof(T)))
+  xu.len = oldLen+1
+  xu.p.data[oldLen] = value
+
 proc setLen[T](s: var seq[T], newlen: Natural) =
   {.noSideEffect.}:
     if newlen < s.len: