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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/include/OpenImageIO/strutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,12 @@ std::string join (const Sequence& seq, string_view sep /*= ""*/, size_t len)
return out.str();
}

/// Concatenate two strings, returning a std::string, implemented carefully
/// to not perform any redundant copies or allocations. This is
/// semantically equivalent to `Strutil::sprintf("%s%s", s, t)`, but is
/// more efficient.
std::string OIIO_API concat(string_view s, string_view t);

/// Repeat a string formed by concatenating str n times.
std::string OIIO_API repeat (string_view str, int n);

Expand Down
6 changes: 6 additions & 0 deletions src/include/OpenImageIO/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,12 @@ class OIIO_API ustring {
return ustring(Strutil::format(fmt, args...));
}

/// Concatenate two strings, returning a ustring, implemented carefully
/// to not perform any redundant copies or allocations. This is
/// semantically equivalent to `ustring::sprintf("%s%s", s, t)`, but is
/// more efficient.
static ustring concat(string_view s, string_view t);

/// Generic stream output of a ustring.
friend std::ostream& operator<<(std::ostream& out, const ustring& str)
{
Expand Down
36 changes: 32 additions & 4 deletions src/libutil/strutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,13 +614,41 @@ Strutil::split(string_view str, std::vector<string_view>& result,



std::string
Strutil::concat(string_view s, string_view t)
{
size_t sl = s.size();
size_t tl = t.size();
size_t len = sl + tl;
std::unique_ptr<char[]> heap_buf;
char local_buf[256];
char* buf = local_buf;
if (len > sizeof(local_buf)) {
heap_buf.reset(new char[len]);
buf = heap_buf.get();
}
memcpy(buf, s.data(), sl);
memcpy(buf + sl, t.data(), tl);
return std::string(buf, len);
}



std::string
Strutil::repeat(string_view str, int n)
{
std::ostringstream out;
while (n-- > 0)
out << str;
return out.str();
size_t sl = str.size();
size_t len = sl * std::max(0, n);
std::unique_ptr<char[]> heap_buf;
char local_buf[256];
char* buf = local_buf;
if (len > sizeof(local_buf)) {
heap_buf.reset(new char[len]);
buf = heap_buf.get();
}
for (int i = 0; i < n; ++i)
memcpy(buf + i * sl, str.data(), sl);
return std::string(buf, len);
}


Expand Down
18 changes: 18 additions & 0 deletions src/libutil/strutil_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,21 @@ test_join()



void
test_concat()
{
std::cout << "Testing concat\n";
OIIO_CHECK_EQUAL(Strutil::concat("foo", "bar"), "foobar");
OIIO_CHECK_EQUAL(Strutil::concat("foo", ""), "foo");
OIIO_CHECK_EQUAL(Strutil::concat("", "foo"), "foo");
OIIO_CHECK_EQUAL(Strutil::concat("", ""), "");
std::string longstring(Strutil::repeat("01234567890", 100));
OIIO_CHECK_EQUAL(Strutil::concat(longstring, longstring),
Strutil::sprintf("%s%s", longstring, longstring));
}



void
test_repeat()
{
Expand All @@ -422,6 +437,8 @@ test_repeat()
OIIO_CHECK_EQUAL(Strutil::repeat("foo", 1), "foo");
OIIO_CHECK_EQUAL(Strutil::repeat("foo", 0), "");
OIIO_CHECK_EQUAL(Strutil::repeat("foo", -1), "");
OIIO_CHECK_EQUAL(Strutil::repeat("0123456789", 100),
Strutil::repeat("01234567890123456789", 50));
}


Expand Down Expand Up @@ -1045,6 +1062,7 @@ main(int /*argc*/, char* /*argv*/[])
test_strip();
test_split();
test_join();
test_concat();
test_repeat();
test_replace();
test_excise_string_after_head();
Expand Down
22 changes: 22 additions & 0 deletions src/libutil/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,28 @@ ustring::make_unique(string_view strref)
return result ? result : table.insert(strref, hash);
}



ustring
ustring::concat(string_view s, string_view t)
{
size_t sl = s.size();
size_t tl = t.size();
size_t len = sl + tl;
std::unique_ptr<char[]> heap_buf;
char local_buf[256];
char* buf = local_buf;
if (len > sizeof(local_buf)) {
heap_buf.reset(new char[len]);
buf = heap_buf.get();
}
memcpy(buf, s.data(), sl);
memcpy(buf + sl, t.data(), tl);
return ustring(buf, len);
}



std::string
ustring::getstats(bool verbose)
{
Expand Down
8 changes: 8 additions & 0 deletions src/libutil/ustring_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ main(int argc, char* argv[])
OIIO_CHECK_ASSERT(ustring("bar") != ustring("foo"));
ustring foo("foo");
OIIO_CHECK_ASSERT(foo.string() == "foo");
ustring bar("bar");
OIIO_CHECK_EQUAL(ustring::concat(foo, bar), "foobar");
OIIO_CHECK_EQUAL(ustring::concat(foo, "bar"), "foobar");
OIIO_CHECK_EQUAL(ustring::concat(foo, ""), "foo");
OIIO_CHECK_EQUAL(ustring::concat("", foo), "foo");
ustring longstring(Strutil::repeat("01234567890", 100));
OIIO_CHECK_EQUAL(ustring::concat(longstring, longstring),
ustring::sprintf("%s%s", longstring, longstring));

const int nhw_threads = Sysutil::hardware_concurrency();
std::cout << "hw threads = " << nhw_threads << "\n";
Expand Down