-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[request] setCap and getCap for strings and seqs #97
Comments
Somewhat related: On the topic of |
also, why not use the more natural way: let cap = myseq.capacity
myseq.capacity = 10
myseq.capacity.inc likewise with |
|
i was poking around nim's code and i stumbled upon core/seqs and in it |
Yes I think so but that should probably an RFC of its own. |
unrelated, but what is the purpose of the core modules? |
in the spirit only having strictly essential stuff in system.nim, why not another module? (eg sequtils or memory) |
Just had a need for this, so +1 from me :) A way to reverse engineer it is like so: var x: seq[uint8]
for i in 0 ..< 3482:
x.add(i.uint8)
echo(x.len)
type
TGenericSeq = object
len, reserved: int
echo(cast[ptr TGenericSeq](x).reserved) |
@dom96 this won't work for new runtime, just saying, a more generic code which works for both would be: var x: seq[uint8]
for i in 0 ..< 3482:
x.add(i.uint8)
echo(x.len)
when defined(nimV2):
type
SeqData = object
cap: int
SeqHeader = object
len: int
data: ptr SeqData
proc getCap[T](x: seq[T]): int =
cast[SeqHeader](x).data[].cap
else:
type
SeqHeader = object
len, cap: int
proc getCap[T](x: seq[T]): int =
cast[ptr SeqHeader](x).cap
echo x.getCap() |
the question is where to add
there is a lot of functionality shared between string and seq so it wouldn't make sense to duplicate API's in |
What's the problem with |
|
The rationale is that |
would it be possible to add procedures for getting and setting the capacity of containers? you can already create strings and seq with a capacity, but there is currently no way of getting that capacity back.
The text was updated successfully, but these errors were encountered: