-
Notifications
You must be signed in to change notification settings - Fork 126
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
string: add length management functions #243
base: master
Are you sure you want to change the base?
Conversation
freshFruict
commented
Dec 28, 2024
- cista::generic_string: add internal_change_capacity method
- cista::string: add resize, reserve and shrink_to_fit methods
The idea of |
It's supposed to behave the same way. The idea is that string stores the capacity of the buffer it owns, and the capacity of non-owning string is set to 0, so calling any size modification method will result in the original string being copied, with memory allocation if required Although looking now, I see I've misused h.reserved_ on lines 221 and 224, it won't work properly on BE systems. I'll fix that, and probably refactor the new code slightly |
Now I think I've got nothing to add to this PR |
@@ -496,6 +560,27 @@ struct basic_string : public generic_string<Ptr> { | |||
base::set_owning(s); | |||
return *this; | |||
} | |||
|
|||
void resize(msize_t new_size) { | |||
if (new_size <= base::short_length_limit) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean that the allocation will be erased if the string has been allocated (ie. was longer than short length limit at one point)?
I am thinking about use cases similar to this one where you want that it allocates a few times until it has a size that will never require new allocations anymore.
std::string s;
while (std::getline(s)) { /* ... */ }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, that is likely not the best decision. I will look into it