summaryrefslogtreecommitdiff
path: root/src/mongo/base
diff options
context:
space:
mode:
authorAlberto Lerner <alerner@10gen.com>2012-12-07 17:56:35 -0500
committerAlberto Lerner <alerner@10gen.com>2012-12-11 10:47:16 -0500
commit0215858e3d79efdf013c605ce1a7ca028252104e (patch)
tree4ca86ee6f6756e78f7f88b6d5ac52f69b39ad12e /src/mongo/base
parent4c3f61e55a81c98da1692d79ee6e27876cc209d2 (diff)
downloadmongo-0215858e3d79efdf013c605ce1a7ca028252104e.tar.gz
SERVER-6399 Allowed creation of empty string data's.
Diffstat (limited to 'src/mongo/base')
-rw-r--r--src/mongo/base/string_data.h12
-rw-r--r--src/mongo/base/string_data_test.cpp13
2 files changed, 20 insertions, 5 deletions
diff --git a/src/mongo/base/string_data.h b/src/mongo/base/string_data.h
index bcb144e966a..1990138704c 100644
--- a/src/mongo/base/string_data.h
+++ b/src/mongo/base/string_data.h
@@ -37,20 +37,22 @@ namespace mongo {
*
* + The object StringData wraps around must be alive while the StringData is.
*
- * + Because strings accept null characters, we allow them in StringData. But this use is
- * *strongly* discouraged. One problem this case may encounter is when asking for data()
- * out of a StringData that was feed with, say "a\0b". If interpreted as a c-string,
- * the null character would cut the original string short.
+ * + Because string data can be used to pass a substring around, one should never assume a
+ * rawData() terminates with a null.
*/
class StringData {
public:
+ /** Constructs an empty string data */
+ StringData()
+ : _data(NULL), _size(0) {}
+
/**
* Constructs a StringData, for the case where the length of string is not known. 'c'
* must be a pointer to a null-terminated string.
*/
StringData( const char* c )
- : _data(c), _size(string::npos){}
+ : _data(c), _size((c == NULL) ? 0 : string::npos) {}
/**
* Constructs a StringData explicitly, for the case where the length of the string is
diff --git a/src/mongo/base/string_data_test.cpp b/src/mongo/base/string_data_test.cpp
index 2dffa88f1ad..14b5ac5e471 100644
--- a/src/mongo/base/string_data_test.cpp
+++ b/src/mongo/base/string_data_test.cpp
@@ -24,6 +24,12 @@ namespace {
using mongo::StringData;
using std::string;
+ TEST(Construction, Empty) {
+ StringData strData;
+ ASSERT_EQUALS(strData.size(), 0U);
+ ASSERT_TRUE(strData.rawData() == NULL);
+ }
+
TEST(Construction, FromStdString) {
std::string base("aaa");
StringData strData(base);
@@ -38,6 +44,13 @@ namespace {
ASSERT_EQUALS(strData.toString(), base);
}
+ TEST(Construction, FromNullCString) {
+ char* c = NULL;
+ StringData strData(c);
+ ASSERT_EQUALS(strData.size(), 0U);
+ ASSERT_TRUE(strData.rawData() == NULL);
+ }
+
TEST(Construction, FromLiteral) {
StringData strData("ccc", StringData::LiteralTag());
ASSERT_EQUALS(strData.size(), 3U);