summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndy Schwerin <schwerin@10gen.com>2013-01-09 11:52:00 -0500
committerAndy Schwerin <schwerin@10gen.com>2013-01-09 16:19:47 -0500
commit5ffba5f721f6f17b41b9bd8782b3c79567fd27d3 (patch)
tree766ac9c6b5c7d3bec8cf4bdefa253ff84d1d878c /src
parent82d7498b6a362847c0367a17fb5ebe07d9c9cb5a (diff)
downloadmongo-5ffba5f721f6f17b41b9bd8782b3c79567fd27d3.tar.gz
Add startsWith and endsWith predicates to StringData.
Diffstat (limited to 'src')
-rw-r--r--src/mongo/base/string_data-inl.h16
-rw-r--r--src/mongo/base/string_data.h10
-rw-r--r--src/mongo/base/string_data_test.cpp33
3 files changed, 58 insertions, 1 deletions
diff --git a/src/mongo/base/string_data-inl.h b/src/mongo/base/string_data-inl.h
index 635def4bb18..a99a78d230e 100644
--- a/src/mongo/base/string_data-inl.h
+++ b/src/mongo/base/string_data-inl.h
@@ -97,4 +97,18 @@ namespace mongo {
return StringData( _data + pos, n );
}
-}
+ inline bool StringData::startsWith( const StringData& prefix ) const {
+ // TODO: Investigate an optimized implementation.
+ return substr(0, prefix.size()) == prefix;
+ }
+
+ inline bool StringData::endsWith( const StringData& suffix ) const {
+ // TODO: Investigate an optimized implementation.
+ const size_t thisSize = size();
+ const size_t suffixSize = suffix.size();
+ if (suffixSize > thisSize)
+ return false;
+ return substr(thisSize - suffixSize) == suffix;
+ }
+
+} // namespace mongo
diff --git a/src/mongo/base/string_data.h b/src/mongo/base/string_data.h
index a30a7398efa..86f9fd3d672 100644
--- a/src/mongo/base/string_data.h
+++ b/src/mongo/base/string_data.h
@@ -99,6 +99,16 @@ namespace mongo {
size_t find( char c ) const;
size_t find( const StringData& needle ) const;
+ /**
+ * Returns true if 'prefix' is a substring of this instance, anchored at position 0.
+ */
+ bool startsWith( const StringData& prefix ) const;
+
+ /**
+ * Returns true if 'suffix' is a substring of this instance, anchored at the end.
+ */
+ bool endsWith( const StringData& suffix ) const;
+
//
// accessors
//
diff --git a/src/mongo/base/string_data_test.cpp b/src/mongo/base/string_data_test.cpp
index 14b5ac5e471..eecda842c25 100644
--- a/src/mongo/base/string_data_test.cpp
+++ b/src/mongo/base/string_data_test.cpp
@@ -148,4 +148,37 @@ namespace {
ASSERT( !StringData( "ABC" ).equalCaseInsensitive( "AdC" ) );
}
+ TEST(StartsWith, Simple) {
+ ASSERT(StringData("").startsWith(""));
+ ASSERT(!StringData("").startsWith("x"));
+ ASSERT(StringData("abcde").startsWith(""));
+ ASSERT(StringData("abcde").startsWith("a"));
+ ASSERT(StringData("abcde").startsWith("ab"));
+ ASSERT(StringData("abcde").startsWith("abc"));
+ ASSERT(StringData("abcde").startsWith("abcd"));
+ ASSERT(StringData("abcde").startsWith("abcde"));
+ ASSERT(!StringData("abcde").startsWith("abcdef"));
+ ASSERT(!StringData("abcde").startsWith("abdce"));
+ ASSERT(StringData("abcde").startsWith(StringData("abcdeXXXX").substr(0, 4)));
+ ASSERT(!StringData("abcde").startsWith(StringData("abdef").substr(0, 4)));
+ ASSERT(!StringData("abcde").substr(0, 3).startsWith("abcd"));
+ }
+
+ TEST(EndsWith, Simple) {
+ //ASSERT(StringData("").endsWith(""));
+ ASSERT(!StringData("").endsWith("x"));
+ //ASSERT(StringData("abcde").endsWith(""));
+ ASSERT(StringData("abcde").endsWith(StringData("e", 0)));
+ ASSERT(StringData("abcde").endsWith("e"));
+ ASSERT(StringData("abcde").endsWith("de"));
+ ASSERT(StringData("abcde").endsWith("cde"));
+ ASSERT(StringData("abcde").endsWith("bcde"));
+ ASSERT(StringData("abcde").endsWith("abcde"));
+ ASSERT(!StringData("abcde").endsWith("0abcde"));
+ ASSERT(!StringData("abcde").endsWith("abdce"));
+ ASSERT(StringData("abcde").endsWith(StringData("bcdef").substr(0, 4)));
+ ASSERT(!StringData("abcde").endsWith(StringData("bcde", 3)));
+ ASSERT(!StringData("abcde").substr(0, 3).endsWith("cde"));
+ }
+
} // unnamed namespace