-
Notifications
You must be signed in to change notification settings - Fork 39
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
Add Str::replace(find, repl) function. #15
base: master
Are you sure you want to change the base?
Conversation
Str.h
Outdated
@@ -651,6 +652,49 @@ int Str::appendf(const char* fmt, ...) | |||
return len; | |||
} | |||
|
|||
void Str::replace(const char* find, const char* repl) | |||
{ | |||
STR_ASSERT(Owned == 1); |
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.
Can take ownership after we know that replacements are needed (ideally without a double rewrite)
for (char* p = Data, *end = Data + length(); p != NULL && p < end;) | ||
{ | ||
p = (char*)memmem(p, end - p, find, find_len); | ||
if (p) |
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.
Maybe avoid looping fully if no match or end when last match?
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.
That was already the case. memmem()
would return NULL
and loop would not continue due to p != NULL
check in for
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.
By definition if memmem returns null it means the string has been read entirely. Whereas you already have a match counter you can use to early out.
…locating too small buffer when buffer resize is needed.
When |
No description provided.