mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-12 22:33:49 +00:00
Make capacity work with refc [backport] (#22697)
followup of #19771.
(cherry picked from commit 325341866f)
This commit is contained in:
committed by
narimiran
parent
1080f5eba2
commit
81d58d2cc8
@@ -140,8 +140,6 @@ proc newSeq[T](s: var seq[T], len: Natural) =
|
||||
setLen(s, len)
|
||||
|
||||
|
||||
template capacityImpl(sek: NimSeqV2): int =
|
||||
if sek.p != nil: (sek.p.cap and not strlitFlag) else: 0
|
||||
|
||||
func capacity*[T](self: seq[T]): int {.inline.} =
|
||||
## Returns the current capacity of the seq.
|
||||
@@ -151,9 +149,8 @@ func capacity*[T](self: seq[T]): int {.inline.} =
|
||||
lst.add "Nim"
|
||||
assert lst.capacity == 42
|
||||
|
||||
{.cast(noSideEffect).}:
|
||||
let sek = unsafeAddr self
|
||||
result = capacityImpl(cast[ptr NimSeqV2[T]](sek)[])
|
||||
let sek = cast[ptr NimSeqV2[T]](unsafeAddr self)
|
||||
result = if sek.p != nil: (sek.p.cap and not strlitFlag) else: 0
|
||||
|
||||
|
||||
{.pop.} # See https://github.com/nim-lang/Nim/issues/21401
|
||||
|
||||
@@ -177,9 +177,6 @@ proc prepareMutation*(s: var string) {.inline.} =
|
||||
nimPrepareStrMutationV2(cast[ptr NimStringV2](s)[])
|
||||
|
||||
|
||||
template capacityImpl(str: NimStringV2): int =
|
||||
if str.p != nil: str.p.cap else: 0
|
||||
|
||||
func capacity*(self: string): int {.inline.} =
|
||||
## Returns the current capacity of the string.
|
||||
# See https://github.com/nim-lang/RFCs/issues/460
|
||||
@@ -188,6 +185,5 @@ func capacity*(self: string): int {.inline.} =
|
||||
str.add "Nim"
|
||||
assert str.capacity == 42
|
||||
|
||||
{.cast(noSideEffect).}:
|
||||
let str = unsafeAddr self
|
||||
result = capacityImpl(cast[ptr NimStringV2](str)[])
|
||||
let str = cast[ptr NimStringV2](unsafeAddr self)
|
||||
result = if str.p != nil: str.p.cap else: 0
|
||||
|
||||
@@ -340,3 +340,25 @@ proc setLengthSeqV2(s: PGenericSeq, typ: PNimType, newLen: int): PGenericSeq {.
|
||||
result = s
|
||||
zeroMem(dataPointer(result, elemAlign, elemSize, result.len), (newLen-%result.len) *% elemSize)
|
||||
result.len = newLen
|
||||
|
||||
func capacity*(self: string): int {.inline.} =
|
||||
## Returns the current capacity of the string.
|
||||
# See https://github.com/nim-lang/RFCs/issues/460
|
||||
runnableExamples:
|
||||
var str = newStringOfCap(cap = 42)
|
||||
str.add "Nim"
|
||||
assert str.capacity == 42
|
||||
|
||||
let str = cast[NimString](self)
|
||||
result = if str != nil: str.reserved else: 0
|
||||
|
||||
func capacity*[T](self: seq[T]): int {.inline.} =
|
||||
## Returns the current capacity of the seq.
|
||||
# See https://github.com/nim-lang/RFCs/issues/460
|
||||
runnableExamples:
|
||||
var lst = newSeqOfCap[string](cap = 42)
|
||||
lst.add "Nim"
|
||||
assert lst.capacity == 42
|
||||
|
||||
let sek = cast[PGenericSeq](self)
|
||||
result = if sek != nil: (sek.reserved and not strlitFlag) else: 0
|
||||
|
||||
Reference in New Issue
Block a user