aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/str.cpp29
-rw-r--r--common/str.h5
2 files changed, 33 insertions, 1 deletions
diff --git a/common/str.cpp b/common/str.cpp
index cbfdc4f029..231272e82a 100644
--- a/common/str.cpp
+++ b/common/str.cpp
@@ -136,6 +136,35 @@ String &String::operator += (char c) {
return *this;
}
+bool String::hasPrefix(const char *x) const {
+ assert(x != 0);
+ // Compare x with the start of _str.
+ const char *y = c_str();
+ while (*x && *x == *y) {
+ ++x;
+ ++y;
+ }
+ // It's a prefix, if and only if all letters in x are 'used up' before
+ // _str ends.
+ return *x == 0;
+}
+
+bool String::hasSuffix(const char *x) const {
+ assert(x != 0);
+ // Compare x with the end of _str.
+ const int x_len = strlen(x);
+ if (x_len > _len)
+ return false;
+ const char *y = c_str() + _len - x_len;
+ while (*x && *x == *y) {
+ ++x;
+ ++y;
+ }
+ // It's a suffix, if and only if all letters in x are 'used up' before
+ // _str ends.
+ return *x == 0;
+}
+
void String::deleteLastChar() {
if (_len > 0) {
ensureCapacity(_len - 1, true);
diff --git a/common/str.h b/common/str.h
index 5a96f7939e..4ca744193d 100644
--- a/common/str.h
+++ b/common/str.h
@@ -60,6 +60,9 @@ public:
bool operator >(const String &x) const;
bool operator >=(const String &x) const;
+ bool hasSuffix(const char *x) const;
+ bool hasPrefix(const char *x) const;
+
const char *c_str() const { return _str ? _str : ""; }
uint size() const { return _len; }
@@ -75,7 +78,7 @@ public:
assert(_str && idx >= 0 && idx < _len);
return _str[idx];
}
-
+
void deleteLastChar();
void deleteChar(int p);
void clear();