From e578692076c5371665edbdec4e0e54cd0f85bad9 Mon Sep 17 00:00:00 2001 From: Philip Liard Date: Tue, 26 Apr 2011 09:04:58 +0000 Subject: [PATCH] CPP: Adding strrmm to stringutil. --- cpp/src/stringutil.cc | 35 +++++++++++++++++++++++------------ cpp/src/stringutil.h | 3 +++ cpp/src/stringutil_test.cc | 21 +++++++++++++++++++++ 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/cpp/src/stringutil.cc b/cpp/src/stringutil.cc index 7f52012e7..70ec01062 100644 --- a/cpp/src/stringutil.cc +++ b/cpp/src/stringutil.cc @@ -64,6 +64,21 @@ void StripString(string* s, const char* remove, char replacewith) { } } +bool TryStripPrefixString(const string& in, const string& prefix, string* out) { + assert(out); + const bool has_prefix = in.compare(0, prefix.length(), prefix) == 0; + out->assign(has_prefix ? in.substr(prefix.length()) : in); + + return has_prefix; +} + +bool HasSuffixString(const string& s, const string& suffix) { + if (s.length() < suffix.length()) { + return false; + } + return s.compare(s.length() - suffix.length(), suffix.length(), suffix) == 0; +} + template void GenericAtoi(const string& s, T* out) { stringstream stream; @@ -79,19 +94,15 @@ void safe_strtou64(const string& s, uint64 *n) { GenericAtoi(s, n); } -bool TryStripPrefixString(const string& in, const string& prefix, string* out) { - assert(out); - const bool has_prefix = in.compare(0, prefix.length(), prefix) == 0; - out->assign(has_prefix ? in.substr(prefix.length()) : in); - - return has_prefix; -} - -bool HasSuffixString(const string& s, const string& suffix) { - if (s.length() < suffix.length()) { - return false; +void strrmm(string* s, const string& chars) { + for (string::iterator it = s->begin(); it != s->end(); ) { + const char current_char = *it; + if (chars.find(current_char) != string::npos) { + it = s->erase(it); + } else { + ++it; + } } - return s.compare(s.length() - suffix.length(), suffix.length(), suffix) == 0; } // StringHolder class diff --git a/cpp/src/stringutil.h b/cpp/src/stringutil.h index bca35647a..a9ba6fc31 100644 --- a/cpp/src/stringutil.h +++ b/cpp/src/stringutil.h @@ -51,6 +51,9 @@ void safe_strto32(const string& s, int32 *n); // Converts string to uint64. void safe_strtou64(const string& s, uint64 *n); +// Remove all occurrences of a given set of characters from a string. +void strrmm(string* s, const string& chars); + // Holds a reference to a std::string or C string. It can also be constructed // from an integer which is converted to a string. class StringHolder { diff --git a/cpp/src/stringutil_test.cc b/cpp/src/stringutil_test.cc index 3696d3a4c..14c2cea06 100644 --- a/cpp/src/stringutil_test.cc +++ b/cpp/src/stringutil_test.cc @@ -90,6 +90,27 @@ TEST(StringUtilTest, safe_strtou64) { EXPECT_EQ(18446744073709551615UL, n); } +// Test strrmm. +TEST(StringUtilTest, strrmm) { + string input("hello"); + + strrmm(&input, ""); + EXPECT_EQ(input, input); + + string empty; + strrmm(&empty, ""); + EXPECT_EQ("", empty); + + strrmm(&empty, "aa"); + EXPECT_EQ("", empty); + + strrmm(&input, "h"); + EXPECT_EQ("ello", input); + + strrmm(&input, "el"); + EXPECT_EQ("o", input); +} + // Test the StringHolder class. TEST(StringUtilTest, StringHolder) { // Test with C string.