Skip to content

Commit

Permalink
Make capacity work with refc [backport] (#22697)
Browse files Browse the repository at this point in the history
followup of #19771.
  • Loading branch information
AmjadHD committed Sep 13, 2023
1 parent 8f5b90f commit 3253418
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
7 changes: 2 additions & 5 deletions lib/system/seqs_v2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
8 changes: 2 additions & 6 deletions lib/system/strs_v2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
22 changes: 22 additions & 0 deletions lib/system/sysstr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3253418

Please sign in to comment.