Empty check in shallow [backport] (#9676)
This commit is contained in:
parent
b78699917e
commit
6ff596d4f8
2 changed files with 25 additions and 0 deletions
|
|
@ -4017,6 +4017,7 @@ proc shallow*[T](s: var seq[T]) {.noSideEffect, inline.} =
|
||||||
## marks a sequence `s` as `shallow`:idx:. Subsequent assignments will not
|
## marks a sequence `s` as `shallow`:idx:. Subsequent assignments will not
|
||||||
## perform deep copies of `s`. This is only useful for optimization
|
## perform deep copies of `s`. This is only useful for optimization
|
||||||
## purposes.
|
## purposes.
|
||||||
|
if s.len == 0: return
|
||||||
when not defined(JS) and not defined(nimscript):
|
when not defined(JS) and not defined(nimscript):
|
||||||
var s = cast[PGenericSeq](s)
|
var s = cast[PGenericSeq](s)
|
||||||
s.reserved = s.reserved or seqShallowFlag
|
s.reserved = s.reserved or seqShallowFlag
|
||||||
|
|
|
||||||
|
|
@ -170,6 +170,30 @@ block tshallowseq:
|
||||||
xxx()
|
xxx()
|
||||||
|
|
||||||
|
|
||||||
|
block tshallowemptyseq:
|
||||||
|
proc test() =
|
||||||
|
var nilSeq: seq[int] = @[]
|
||||||
|
var emptySeq: seq[int] = newSeq[int]()
|
||||||
|
block:
|
||||||
|
var t = @[1,2,3]
|
||||||
|
shallow(nilSeq)
|
||||||
|
t = nilSeq
|
||||||
|
doAssert t == @[]
|
||||||
|
block:
|
||||||
|
var t = @[1,2,3]
|
||||||
|
shallow(emptySeq)
|
||||||
|
t = emptySeq
|
||||||
|
doAssert t == @[]
|
||||||
|
block:
|
||||||
|
var t = @[1,2,3]
|
||||||
|
shallowCopy(t, nilSeq)
|
||||||
|
doAssert t == @[]
|
||||||
|
block:
|
||||||
|
var t = @[1,2,3]
|
||||||
|
shallowCopy(t, emptySeq)
|
||||||
|
doAssert t == @[]
|
||||||
|
test()
|
||||||
|
|
||||||
|
|
||||||
import strutils
|
import strutils
|
||||||
block ttoseq:
|
block ttoseq:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue