Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 42f8e11

Browse filesBrowse files
authored
Merge pull request #21 from leg0/proper_string_move
Make String move constructor move instead of copy.
2 parents e2d2f20 + 41599a3 commit 42f8e11
Copy full SHA for 42f8e11

File tree

4 files changed

+56
-32
lines changed
Filter options

4 files changed

+56
-32
lines changed

‎api/String.cpp

Copy file name to clipboardExpand all lines: api/String.cpp
+18-30Lines changed: 18 additions & 30 deletions
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

‎api/String.h

Copy file name to clipboardExpand all lines: api/String.h
-2Lines changed: 0 additions & 2 deletions
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)

‎test/CMakeLists.txt

Copy file name to clipboardExpand all lines: test/CMakeLists.txt
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ set(TEST_SRCS
6666
src/String/test_indexOf.cpp
6767
src/String/test_lastIndexOf.cpp
6868
src/String/test_length.cpp
69+
src/String/test_move.cpp
6970
src/String/test_remove.cpp
7071
src/String/test_replace.cpp
7172
src/String/test_String.cpp

‎test/src/String/test_move.cpp

Copy file name to clipboard
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <catch.hpp>
2+
3+
#include <String.h>
4+
5+
#include "StringPrinter.h"
6+
7+
#include <utility>
8+
9+
TEST_CASE("Testing String move constructor", "[String-move-01]")
10+
{
11+
arduino::String a("src");
12+
char const* const a_str = a.c_str();
13+
arduino::String b(std::move(a));
14+
REQUIRE(a.length() == 0);
15+
REQUIRE(a.c_str() == nullptr);
16+
REQUIRE(b.c_str() == a_str);
17+
REQUIRE(b.length() == 3);
18+
}
19+
20+
TEST_CASE("Testing String move assignment", "[String-move-02]")
21+
{
22+
arduino::String a("src");
23+
char const* const a_str = a.c_str();
24+
arduino::String b;
25+
b = std::move(a);
26+
REQUIRE(a.length() == 0);
27+
REQUIRE(a.c_str() == nullptr);
28+
REQUIRE(b == arduino::String("src"));
29+
REQUIRE(b.c_str() == a_str);
30+
}
31+
32+
TEST_CASE("Testing String move self assignment", "[String-move-03]")
33+
{
34+
arduino::String a("src");
35+
a = std::move(a);
36+
REQUIRE(a == "src");
37+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.