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

Containers: implement also StringView and char concatenation #134

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
30 changes: 30 additions & 0 deletions src/Corrade/Containers/StringView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,36 @@ String operator+(const StringView a, const StringView b) {
return result;
}

String operator+(const char a, const StringView b) {
/* Not using the size() accessor to speed up debug builds */
const std::size_t bSize = b._sizePlusFlags & ~Implementation::StringViewSizeMask;

String result{Corrade::NoInit, 1 + bSize};

/* Apparently memcpy() can't be called with null pointers, even if size is
zero. I call that bullying. */
char* out = result.data();
out[0] = a;
if(bSize) std::memcpy(out + 1, b._data, bSize);

return result;
}

String operator+(const StringView a, const char b) {
/* Not using the size() accessor to speed up debug builds */
const std::size_t aSize = a._sizePlusFlags & ~Implementation::StringViewSizeMask;

String result{Corrade::NoInit, aSize + 1};

/* Apparently memcpy() can't be called with null pointers, even if size is
zero. I call that bullying. */
char* out = result.data();
if(aSize) std::memcpy(out, a._data, aSize);
out[aSize] = b;

return result;
}

Utility::Debug& operator<<(Utility::Debug& debug, const StringViewFlag value) {
debug << "Containers::StringViewFlag" << Utility::Debug::nospace;

Expand Down
14 changes: 14 additions & 0 deletions src/Corrade/Containers/StringView.h
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,8 @@ template<class T> class CORRADE_UTILITY_EXPORT BasicStringView {
friend CORRADE_UTILITY_EXPORT bool operator>=(StringView, StringView);
friend CORRADE_UTILITY_EXPORT bool operator>(StringView, StringView);
friend CORRADE_UTILITY_EXPORT String operator+(StringView, StringView);
friend CORRADE_UTILITY_EXPORT String operator+(char, StringView);
friend CORRADE_UTILITY_EXPORT String operator+(StringView, char);

/* Used by the char* constructor, delinlined because it calls into
std::strlen() */
Expand Down Expand Up @@ -1158,6 +1160,18 @@ needless temporary allocations.
*/
CORRADE_UTILITY_EXPORT String operator+(StringView a, StringView b);

/**
* @overload
* @m_since_latest
*/
CORRADE_UTILITY_EXPORT String operator+(char a, StringView b);

/**
* @overload
* @m_since_latest
*/
CORRADE_UTILITY_EXPORT String operator+(StringView a, char b);

/* operator<<(Debug&, StringView) implemented directly in Debug */

namespace Literals {
Expand Down
17 changes: 17 additions & 0 deletions src/Corrade/Containers/Test/StringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ struct StringTest: TestSuite::Tester {

void add();
void addNullViews();
void addArrayViews();

void join();
void joinNullViews();
Expand Down Expand Up @@ -292,6 +293,7 @@ StringTest::StringTest() {

&StringTest::add,
&StringTest::addNullViews,
&StringTest::addArrayViews,

&StringTest::join,
&StringTest::joinNullViews,
Expand Down Expand Up @@ -1842,6 +1844,12 @@ void StringTest::add() {
CORRADE_COMPARE("hello"_s + ""_s, "hello");
CORRADE_COMPARE(""_s + "hello"_s, "hello");
CORRADE_COMPARE("hello"_s + "world"_s, "helloworld");

/* Single char */
CORRADE_COMPARE(""_s + '!', "!");
CORRADE_COMPARE('?' + ""_s, "?");
CORRADE_COMPARE("hello"_s + '!', "hello!");
CORRADE_COMPARE('?' + "hello"_s, "?hello");
}

void StringTest::addNullViews() {
Expand All @@ -1851,6 +1859,15 @@ void StringTest::addNullViews() {
CORRADE_COMPARE(StringView{} + StringView{}, "");
CORRADE_COMPARE("hello"_s + nullptr, "hello");
CORRADE_COMPARE(nullptr + "hello"_s, "hello");

/* Single char */
CORRADE_COMPARE(StringView{} + '!', "!");
CORRADE_COMPARE('!' + StringView{}, "!");
}

void StringTest::addArrayViews() {
// TODO ambiguous
Containers::ArrayView<const char>{} + 3;
}

void StringTest::join() {
Expand Down