From 325341866f6b82cba5d81db8e39ca98b0d96fd4d Mon Sep 17 00:00:00 2001 From: Amjad Ben Hedhili Date: Wed, 13 Sep 2023 19:43:25 +0100 Subject: Make capacity work with refc [backport] (#22697) followup of #19771. --- lib/system/seqs_v2.nim | 7 ++----- lib/system/strs_v2.nim | 8 ++------ lib/system/sysstr.nim | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/system/seqs_v2.nim b/lib/system/seqs_v2.nim index c3e48019c..9cf403229 100644 --- a/lib/system/seqs_v2.nim +++ b/lib/system/seqs_v2.nim @@ -183,8 +183,6 @@ proc newSeq[T](s: var seq[T], len: Natural) = proc sameSeqPayload(x: pointer, y: pointer): bool {.compilerproc, inline.} = result = cast[ptr NimRawSeq](x)[].p == cast[ptr NimRawSeq](y)[].p -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. @@ -194,9 +192,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 diff --git a/lib/system/strs_v2.nim b/lib/system/strs_v2.nim index e161172b2..a9a104d3d 100644 --- a/lib/system/strs_v2.nim +++ b/lib/system/strs_v2.nim @@ -202,9 +202,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 @@ -213,6 +210,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 diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim index b8dc7101d..4acf1780b 100644 --- a/lib/system/sysstr.nim +++ b/lib/system/sysstr.nim @@ -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 -- cgit 1.4.1-2-gfad0