diff options
author | HÃ¥vard Pettersen <3535158+havardpe@users.noreply.github.com> | 2021-12-14 15:23:23 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-14 15:23:23 +0100 |
commit | 6fb22f75be2bf950e4bfa32f94c63729875af14b (patch) | |
tree | d8e974faf2f9bed7f7c947db783dc4d9e7214f68 | |
parent | 5f671b4a7b518bd4ef811a2a8d603a4b852ee1c9 (diff) | |
parent | d65e09cce107478f2da65d7fdf9a0fdcabfdb059 (diff) |
Merge pull request #20510 from vespa-engine/havardpe/shared-empty-string
a shared empty string
-rw-r--r-- | vespalib/src/tests/stllike/string_test.cpp | 5 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/stllike/string.cpp | 5 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/stllike/string.h | 3 |
3 files changed, 13 insertions, 0 deletions
diff --git a/vespalib/src/tests/stllike/string_test.cpp b/vespalib/src/tests/stllike/string_test.cpp index 95846e765f6..de384bce9ad 100644 --- a/vespalib/src/tests/stllike/string_test.cpp +++ b/vespalib/src/tests/stllike/string_test.cpp @@ -484,4 +484,9 @@ TEST("test that operator<() works with stringref versus string") { EXPECT_FALSE(sb < sb); } +TEST("test that empty_string is shared and empty") { + EXPECT_TRUE(&empty_string() == &empty_string()); + EXPECT_EQUAL(empty_string(), ""); +} + TEST_MAIN() { TEST_RUN_ALL(); } diff --git a/vespalib/src/vespa/vespalib/stllike/string.cpp b/vespalib/src/vespa/vespalib/stllike/string.cpp index 67e89d013cc..460bb0d39c6 100644 --- a/vespalib/src/vespa/vespalib/stllike/string.cpp +++ b/vespalib/src/vespa/vespalib/stllike/string.cpp @@ -100,4 +100,9 @@ template string operator + (stringref a, const string & b); template string operator + (const string & a, const char * b); template string operator + (const char * a, const string & b); +const string &empty_string() { + static string empty; + return empty; +} + } diff --git a/vespalib/src/vespa/vespalib/stllike/string.h b/vespalib/src/vespa/vespalib/stllike/string.h index 79c9d50b164..2c0bb9a85d3 100644 --- a/vespalib/src/vespa/vespalib/stllike/string.h +++ b/vespalib/src/vespa/vespalib/stllike/string.h @@ -675,6 +675,9 @@ inline bool ends_with(stringref text, stringref key) { return false; } +// returns a reference to a shared empty string +const string &empty_string(); + /** * Utility function to format an unsigned integer into a new * string instance. |