Skip to content
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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

freshFruict
Copy link
Contributor

  • cista::generic_string: add internal_change_capacity method
  • cista::string: add resize, reserve and shrink_to_fit methods

@felixguendling
Copy link
Owner

The idea of self_allocated_ was previously that the string knows if it needs to free or not - especially if you deserialize a buffer and modify the string. In this case it might allocate and will then switch from self_allocated=false to self_allocated=true. Will this still work?

@freshFruict
Copy link
Contributor Author

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

@freshFruict
Copy link
Contributor Author

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) {
Copy link
Owner

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)) { /* ... */ }

Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants