aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@oath.com>2021-12-14 13:52:24 +0000
committerHåvard Pettersen <havardpe@oath.com>2021-12-14 13:52:24 +0000
commitd65e09cce107478f2da65d7fdf9a0fdcabfdb059 (patch)
tree4c3fde3075f6a8d139f613ad33896b602a0089db /vespalib
parent348b05de5ecbd4c36b7b5f694a7c26e69f0ea706 (diff)
a shared empty string
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/tests/stllike/string_test.cpp5
-rw-r--r--vespalib/src/vespa/vespalib/stllike/string.cpp5
-rw-r--r--vespalib/src/vespa/vespalib/stllike/string.h3
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.