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

Add concat function to ustring and Strutil #2478

Merged
merged 2 commits into from
Jan 28, 2020

Conversation

lgritz
Copy link
Collaborator

@lgritz lgritz commented Jan 27, 2020

concat(s,t) is semantically equivalent to sprintf("%s%s",s,t), but
concat carefully avoids copying any of the characters more than once,
and it performs at most one temporary allocation (typically it does
not allocate temp memory from the heap at all, if the sum of the
strings' lengths are not too long).

Added both a std::string-returning and a ustring-returning version.

Also reworked the implementation of Strutil::repeat() to similarly
perform at most one one temporary allocation (typically none) and
not perform any unnecessary copies.

Inspired by work Chris Kulla did in OSL.

}
memcpy(buf, s.data(), sl);
memcpy(buf + sl, t.data(), tl);
return std::string(local_buf, len);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want buf here, not local_buf

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And ahem it looks like I was the one who wrote the code incorrectly in the first place :(

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will teach me an important lesson about cut and paste from untrusted sources...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I am similarly embarrassed about not only failing to notice it while reviewing your code, but failing TWICE MORE when putting the equivalent into OIIO, and then one final failure by actually making test cases, but that do not cover the "long string" case.

}
for (int i = 0; i < n; ++i)
memcpy(buf + i * sl, str.data(), sl);
return std::string(local_buf, len);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right I hate to tell you this, Chris, but... can you guess?

`concat(s,t)` is semantically equivalent to `sprintf("%s%s",s,t)`, but
concat carefully avoids copying any of the characters more than once,
and it performs at most one temporary allocation (typically it does
not allocate temp memory from the heap at all, if the sum of the
strings' lengths are not too long).

Added both a std::string-returning and a ustring-returning version.

Also reworked the implementation of `Strutil::repeat()` to similarly
perform at most one one temporary allocation (typically none) and
not perform any unnecessary copies.

Inspired by work Chris Kulla did in OSL.
@lgritz
Copy link
Collaborator Author

lgritz commented Jan 27, 2020

Updated.

"Untested code is buggy code" has proven itself once again.

@fpsunflower
Copy link
Contributor

LGTM!

@lgritz lgritz merged commit 8730b62 into AcademySoftwareFoundation:master Jan 28, 2020
@lgritz lgritz deleted the lg-ustring branch January 28, 2020 05:11
lgritz added a commit to lgritz/OpenImageIO that referenced this pull request Feb 5, 2020
…#2478)

`concat(s,t)` is semantically equivalent to `sprintf("%s%s",s,t)`, but
concat carefully avoids copying any of the characters more than once,
and it performs at most one temporary allocation (typically it does
not allocate temp memory from the heap at all, if the sum of the
strings' lengths are not too long).

Added both a std::string-returning and a ustring-returning version.

Also reworked the implementation of `Strutil::repeat()` to similarly
perform at most one one temporary allocation (typically none) and
not perform any unnecessary copies.

Inspired by work Chris Kulla did in OSL.
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.

None yet

2 participants