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

Make String move constructor move instead of copy. #21

Merged
merged 2 commits into from
May 25, 2021
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
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
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ set(TEST_SRCS
src/String/test_indexOf.cpp
src/String/test_lastIndexOf.cpp
src/String/test_length.cpp
src/String/test_move.cpp
src/String/test_remove.cpp
src/String/test_replace.cpp
src/String/test_String.cpp
Expand Down
37 changes: 37 additions & 0 deletions test/src/String/test_move.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <catch.hpp>

#include <String.h>

#include "StringPrinter.h"

#include <utility>

TEST_CASE("Testing String move constructor", "[String-move-01]")
{
arduino::String a("src");
char const* const a_str = a.c_str();
arduino::String b(std::move(a));
REQUIRE(a.length() == 0);
REQUIRE(a.c_str() == nullptr);
REQUIRE(b.c_str() == a_str);
REQUIRE(b.length() == 3);
}

TEST_CASE("Testing String move assignment", "[String-move-02]")
{
arduino::String a("src");
char const* const a_str = a.c_str();
arduino::String b;
b = std::move(a);
REQUIRE(a.length() == 0);
REQUIRE(a.c_str() == nullptr);
REQUIRE(b == arduino::String("src"));
REQUIRE(b.c_str() == a_str);
}

TEST_CASE("Testing String move self assignment", "[String-move-03]")
{
arduino::String a("src");
a = std::move(a);
REQUIRE(a == "src");
}