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 insert vs assign #960

Open
KammutierSpule opened this issue Sep 25, 2024 · 5 comments
Open

string insert vs assign #960

KammutierSpule opened this issue Sep 25, 2024 · 5 comments

Comments

@KammutierSpule
Copy link

It looks insert does not "repair" the string. should it? or should put on documentation something about it?

@jwellbelove
Copy link
Contributor

What do you mean by "repair"?

@KammutierSpule
Copy link
Author

the test I did, I used insert and it didn't change (update) the size.
Does it expects the repair() to be called?

@jwellbelove
Copy link
Contributor

repair() is called by the user when a string has been moved by a low level C copy like memcpy. It just re-attaches the buffer to the internal pointer.

Can you give an example of an insert not updating the size?

@KammutierSpule
Copy link
Author

#include <iostream>
#include <etl/string.h>

int main() {
    etl::string<20> test;
    char buffer[20 + 1];
    strncpy(buffer, "ABCDE", sizeof(buffer));
    test.insert(0, buffer, buffer + 5);
    std::cout << "test.size:" << test.length() << "\n";
    test.repair();
    std::cout << "test.size:" << test.length() << "\n";

    return 0;
}
test.size:0
test.size:0

Looks I missed the propose of insert, it works on a string already created and doesn't change the original string size?

@jwellbelove
Copy link
Contributor

jwellbelove commented Sep 25, 2024

When you call test.insert(0, buffer, buffer + 5) you are calling this function

template <typename TIterator>
iterator insert(const_iterator position, TIterator first, TIterator last)

It is expecting the first parameter to be the iterator position where you want the string to be inserted. You are sending it a null iterator.

I assume you meant to call test.insert(0, buffer, 5);

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

No branches or pull requests

2 participants