Browse Source

CPP: Adding strrmm to stringutil.

pull/567/head
Philip Liard 15 years ago
committed by Mihaela Rosca
parent
commit
e578692076
3 changed files with 47 additions and 12 deletions
  1. +23
    -12
      cpp/src/stringutil.cc
  2. +3
    -0
      cpp/src/stringutil.h
  3. +21
    -0
      cpp/src/stringutil_test.cc

+ 23
- 12
cpp/src/stringutil.cc View File

@ -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 <typename T> template <typename T>
void GenericAtoi(const string& s, T* out) { void GenericAtoi(const string& s, T* out) {
stringstream stream; stringstream stream;
@ -79,19 +94,15 @@ void safe_strtou64(const string& s, uint64 *n) {
GenericAtoi(s, 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 // StringHolder class


+ 3
- 0
cpp/src/stringutil.h View File

@ -51,6 +51,9 @@ void safe_strto32(const string& s, int32 *n);
// Converts string to uint64. // Converts string to uint64.
void safe_strtou64(const string& s, uint64 *n); 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 // Holds a reference to a std::string or C string. It can also be constructed
// from an integer which is converted to a string. // from an integer which is converted to a string.
class StringHolder { class StringHolder {


+ 21
- 0
cpp/src/stringutil_test.cc View File

@ -90,6 +90,27 @@ TEST(StringUtilTest, safe_strtou64) {
EXPECT_EQ(18446744073709551615UL, n); 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 the StringHolder class.
TEST(StringUtilTest, StringHolder) { TEST(StringUtilTest, StringHolder) {
// Test with C string. // Test with C string.


Loading…
Cancel
Save