diff options
author | Amjad Ben Hedhili <amjadhedhili@outlook.com> | 2023-09-13 19:43:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-13 20:43:25 +0200 |
commit | 325341866f6b82cba5d81db8e39ca98b0d96fd4d (patch) | |
tree | c1c65237cddd8c1f4d9a143f830e54eb9f122aff | |
parent | 8f5b90f886501862bd27d4e0e8244e2f7f0ebc2a (diff) | |
download | Nim-325341866f6b82cba5d81db8e39ca98b0d96fd4d.tar.gz |
Make capacity work with refc [backport] (#22697)
followup of #19771.
-rw-r--r-- | lib/system/seqs_v2.nim | 7 | ||||
-rw-r--r-- | lib/system/strs_v2.nim | 8 | ||||
-rw-r--r-- | 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 |