Skip to content

Commit 9668aa6

Browse files
committed
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.
1 parent e2d2f20 commit 9668aa6

File tree

2 files changed

+18
-32
lines changed

2 files changed

+18
-32
lines changed

Diff for: api/String.cpp

+18-30
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,13 @@ String::String(const __FlashStringHelper *pstr)
6565

6666
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
6767
String::String(String &&rval)
68+
: buffer(rval.buffer)
69+
, capacity(rval.capacity)
70+
, len(rval.len)
6871
{
69-
init();
70-
move(rval);
71-
}
72-
String::String(StringSumHelper &&rval)
73-
{
74-
init();
75-
move(rval);
72+
rval.buffer = NULL;
73+
rval.capacity = 0;
74+
rval.len = 0;
7675
}
7776
#endif
7877

@@ -217,23 +216,18 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
217216
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
218217
void String::move(String &rhs)
219218
{
220-
if (buffer) {
221-
if (rhs && capacity >= rhs.len) {
222-
memcpy(buffer, rhs.buffer, rhs.len);
223-
len = rhs.len;
224-
buffer[len] = '\0';
225-
rhs.len = 0;
226-
return;
227-
} else {
228-
free(buffer);
229-
}
219+
if (this != &rhs)
220+
{
221+
free(buffer);
222+
223+
buffer = rhs.buffer;
224+
len = rhs.len;
225+
capacity = rhs.capacity;
226+
227+
rhs.buffer = NULL;
228+
rhs.len = 0;
229+
rhs.capacity = 0;
230230
}
231-
buffer = rhs.buffer;
232-
capacity = rhs.capacity;
233-
len = rhs.len;
234-
rhs.buffer = NULL;
235-
rhs.capacity = 0;
236-
rhs.len = 0;
237231
}
238232
#endif
239233

@@ -250,13 +244,7 @@ String & String::operator = (const String &rhs)
250244
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
251245
String & String::operator = (String &&rval)
252246
{
253-
if (this != &rval) move(rval);
254-
return *this;
255-
}
256-
257-
String & String::operator = (StringSumHelper &&rval)
258-
{
259-
if (this != &rval) move(rval);
247+
move(rval);
260248
return *this;
261249
}
262250
#endif

Diff for: api/String.h

-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ class String
7474
String(const __FlashStringHelper *str);
7575
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
7676
String(String &&rval);
77-
String(StringSumHelper &&rval);
7877
#endif
7978
explicit String(char c);
8079
explicit String(unsigned char, unsigned char base=10);
@@ -101,7 +100,6 @@ class String
101100
String & operator = (const __FlashStringHelper *str);
102101
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
103102
String & operator = (String &&rval);
104-
String & operator = (StringSumHelper &&rval);
105103
#endif
106104

107105
// concatenate (works w/ built-in types)

0 commit comments

Comments
 (0)