8000 Make String move constructor move instead of copy. by leg0 · Pull Request #21 · arduino/ArduinoCore-API · GitHub
[go: up one dir, main page]

Skip to content

Make String move constructor move instead of copy. #21

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

Merged
merged 2 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Make String move constructor move instead of copy.
The move constructor String::String(String&&) and String::operator=(String&&)
now perform move instead of copy.

Remove String(StringSumHelper&&) constructor because having it makes no sense:
String(String&&) takes care of it - you can pass either String&& or
StringSumHelper&& to this constructor. StringSumHelper is derived from String
and has no data members other than those inherited from String. Even if it did
have some extra data members, truncation would have to happen during move, and
normally that is something you don't want.
  • Loading branch information
8000 leg0 committed Apr 22, 2021
commit 9668aa626d6f1f3427b88aed06175bc312a1ce84
48 changes: 18 additions & 30 deletions api/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,13 @@ String::String(const __FlashStringHelper *pstr)

#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
String::String(String &&rval)
: buffer(rval.buffer)
, capacity(rval.capacity)
, len(rval.len)
{
init();
move(rval);
}
String::String(StringSumHelper &&rval)
{
init();
move(rval);
rval.buffer = NULL;
rval.capacity = 0;
rval.len = 0;
}
#endif

Expand Down Expand Up @@ -217,23 +216,18 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
void String::move(String &rhs)
{
if (buffer) {
if (rhs && capacity >= rhs.len) {
memcpy(buffer, rhs.buffer, rhs.len);
len = rhs.len;
buffer[len] = '\0';
rhs.len = 0;
return;
} else {
free(buffer);
}
if (this != &rhs)
{
free(buffer);

buffer = rhs.buffer;
len = rhs.len;
capacity = rhs.capacity;

rhs.buffer = NULL;
rhs.len = 0;
rhs.capacity = 0;
}
buffer = rhs.buffer;
capacity = rhs.capacity;
len = rhs.len;
rhs.buffer = NULL;
rhs.capacity = 0;
rhs.len = 0;
}
#endif

Expand All @@ -250,13 +244,7 @@ String & String::operator = (const String &rhs)
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
String & String::operator = (String &&rval)
{
if (this != &rval) move(rval);
return *this;
}

String & String::operator = (StringSumHelper &&rval)
{
if (this != &rval) move(rval);
move(rval);
return *this;
}
#endif
Expand Down
2 changes: 0 additions & 2 deletions api/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class String
String(const __FlashStringHelper *str);
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
String(String &&rval);
String(StringSumHelper &&rval);
#endif
explicit String(char c);
explicit String(unsigned char, unsigned char base=10);
Expand All @@ -101,7 +100,6 @@ class String
String & operator = (const __FlashStringHelper *str);
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
String & operator = (String &&rval);
String & operator = (StringSumHelper &&rval);
#endif

// concatenate (works w/ built-in types)
Expand Down
0