diff options
-rw-r--r-- | lib/core/seqs.nim | 14 | ||||
-rw-r--r-- | lib/system.nim | 13 |
2 files changed, 15 insertions, 12 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: diff --git a/lib/system.nim b/lib/system.nim index 2e4862629..3d2d9ab48 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -1992,18 +1992,7 @@ when not defined(JS) and not defined(nimscript) and hostOS != "standalone": when not defined(JS) and not defined(nimscript) and hasAlloc and not defined(gcDestructors): proc addChar(s: NimString, c: char): NimString {.compilerProc, benign.} -when defined(gcDestructors): - proc add*[T](x: var seq[T], y: 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 xl = x.len - setLen(x, xl + 1) - x[xl] = y -else: +when not defined(gcDestructors): proc add*[T](x: var seq[T], y: T) {.magic: "AppendSeqElem", noSideEffect.} ## Generic proc for adding a data item `y` to a container `x`. ## |