Skip to content

Conversation

@rokups
Copy link
Contributor

@rokups rokups commented Jun 16, 2021

No description provided.

Str.h Outdated

void Str::replace(const char* find, const char* repl)
{
STR_ASSERT(Owned == 1);
Copy link
Owner

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

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?

Copy link
Contributor Author

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

Copy link
Owner

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.

@ocornut
Copy link
Owner

ocornut commented Jul 6, 2021

When num_matches == 0 in the repl_diff > 0 we could early out to avoid owning/copying a non-owned string.

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