diff options
-rw-r--r-- | vespalib/src/tests/stllike/string_test.cpp | 6 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/stllike/string.h | 41 |
2 files changed, 21 insertions, 26 deletions
diff --git a/vespalib/src/tests/stllike/string_test.cpp b/vespalib/src/tests/stllike/string_test.cpp index 3ca2549b7c3..fcce1386ebc 100644 --- a/vespalib/src/tests/stllike/string_test.cpp +++ b/vespalib/src/tests/stllike/string_test.cpp @@ -73,13 +73,15 @@ void verify_move_operator(string org) { string copy(org); EXPECT_EQUAL(org, copy); string moved_into_short("short movable string"); + EXPECT_LESS(moved_into_short.size(), string().capacity()); EXPECT_NOT_EQUAL(org, moved_into_short); moved_into_short = std::move(copy); EXPECT_EQUAL(org, moved_into_short); EXPECT_NOT_EQUAL(org, copy); EXPECT_EQUAL(string(), copy); - string moved_into_long("longer movable string than the 47 bytes that can be held inthe short string optimization."); + string moved_into_long("longer movable string than the 47 bytes that can be held in the short string optimization."); + EXPECT_GREATER(moved_into_long.size(), string().capacity()); EXPECT_NOT_EQUAL(org, moved_into_long); moved_into_long = std::move(moved_into_short); EXPECT_EQUAL(org, moved_into_long); @@ -94,7 +96,7 @@ void verify_move(string org) { TEST("test move constructor") { TEST_DO(verify_move("short string")); - TEST_DO(verify_move("longer string than the 47 bytes that can be held inthe short string optimization.")); + TEST_DO(verify_move("longer string than the 47 bytes that can be held in the short string optimization.")); } TEST("testStringAlloc") { diff --git a/vespalib/src/vespa/vespalib/stllike/string.h b/vespalib/src/vespa/vespalib/stllike/string.h index 38728da0780..ba37c57a0f9 100644 --- a/vespalib/src/vespa/vespalib/stllike/string.h +++ b/vespalib/src/vespa/vespalib/stllike/string.h @@ -180,18 +180,7 @@ public: small_string(small_string && rhs) noexcept : _sz(rhs.size()), _bufferSize(rhs._bufferSize) { - if (rhs.isAllocated()) { - _buf = rhs._buf; - rhs._buf = rhs._stack; - rhs._sz = 0; - rhs._bufferSize = sizeof(rhs._stack); - rhs._stack[0] = 0; - } else { - _buf = _stack; - memcpy(_stack, rhs._stack, sizeof(_stack)); - rhs._sz = 0; - rhs._stack[0] = 0; - } + move(std::move(rhs)); } small_string(const small_string & rhs) noexcept : _buf(_stack), _sz(rhs.size()) { init(rhs.data()); } small_string(const small_string & rhs, size_type pos, size_type sz=npos) noexcept @@ -216,22 +205,12 @@ public: free(buffer()); } } + small_string& operator= (small_string && rhs) noexcept { reset(); _sz = rhs._sz; _bufferSize = rhs._bufferSize; - if (rhs.isAllocated()) { - _buf = rhs._buf; - rhs._buf = rhs._stack; - rhs._sz = 0; - rhs._bufferSize = sizeof(rhs._stack); - rhs._stack[0] = 0; - } else { - _buf = _stack; - memcpy(_stack, rhs._stack, sizeof(_stack)); - rhs._sz = 0; - rhs._stack[0] = 0; - } + move(std::move(rhs)); return *this; } small_string& operator= (const small_string &rhs) noexcept { @@ -558,6 +537,20 @@ private: _reserveBytes(newBufferSize); } } + void move(small_string && rhs) { + if (rhs.isAllocated()) { + _buf = rhs._buf; + rhs._buf = rhs._stack; + rhs._sz = 0; + rhs._bufferSize = sizeof(rhs._stack); + rhs._stack[0] = 0; + } else { + _buf = _stack; + memcpy(_stack, rhs._stack, sizeof(_stack)); + rhs._sz = 0; + rhs._stack[0] = 0; + } + } typedef uint32_t isize_type; bool needAlloc(isize_type add) const { return (add + _sz + 1) > _bufferSize; } bool isAllocated() const { return _buf != _stack; } |