diff options
author | Juan Carlos <juancarlospaco@gmail.com> | 2022-09-21 05:37:13 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-21 10:37:13 +0200 |
commit | ebb1b7af231e492da3e2c101adfd4d482e488fa4 (patch) | |
tree | efbbd6abcca82715d53c3c954887cd218c67ab20 /lib/system | |
parent | e0c1159fb3f1f392698f071485d3e43e1e7f1a36 (diff) | |
download | Nim-ebb1b7af231e492da3e2c101adfd4d482e488fa4.tar.gz |
RFC-460 implemented (#19771)
* RFC-460 implemented * RFC-460 implemented * RFC-460 implemented * Fix dumb GitHub autoupdate on changelog
Diffstat (limited to 'lib/system')
-rw-r--r-- | lib/system/seqs_v2.nim | 16 | ||||
-rw-r--r-- | lib/system/strs_v2.nim | 16 |
2 files changed, 32 insertions, 0 deletions
diff --git a/lib/system/seqs_v2.nim b/lib/system/seqs_v2.nim index 1194f40ef..42d9938c5 100644 --- a/lib/system/seqs_v2.nim +++ b/lib/system/seqs_v2.nim @@ -129,3 +129,19 @@ proc setLen[T](s: var seq[T], newlen: Natural) = proc newSeq[T](s: var seq[T], len: Natural) = shrink(s, 0) setLen(s, len) + + +template capacityImpl(sek: NimSeqV2): int = + if sek.p != nil: sek.p.cap 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 + + {.cast(noSideEffect).}: + let sek = unsafeAddr self + result = capacityImpl(cast[ptr NimSeqV2](sek)[]) diff --git a/lib/system/strs_v2.nim b/lib/system/strs_v2.nim index 6944cdc58..74b9e7cd9 100644 --- a/lib/system/strs_v2.nim +++ b/lib/system/strs_v2.nim @@ -175,3 +175,19 @@ proc prepareMutation*(s: var string) {.inline.} = {.cast(noSideEffect).}: let s = unsafeAddr s 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 + runnableExamples: + var str = newStringOfCap(cap = 42) + str.add "Nim" + assert str.capacity == 42 + + {.cast(noSideEffect).}: + let str = unsafeAddr self + result = capacityImpl(cast[ptr NimStringV2](str)[]) |