diff options
author | Max Horn | 2005-02-06 19:00:59 +0000 |
---|---|---|
committer | Max Horn | 2005-02-06 19:00:59 +0000 |
commit | 4e66139a4e3d33fce662ad572fca26f49a5d9de4 (patch) | |
tree | 412e33e4e4b28365070fa322db5e93da660ccf30 /common | |
parent | a1b058897d0fcfe1ae7a372ed6d779fdacaeeaa3 (diff) | |
download | scummvm-rg350-4e66139a4e3d33fce662ad572fca26f49a5d9de4.tar.gz scummvm-rg350-4e66139a4e3d33fce662ad572fca26f49a5d9de4.tar.bz2 scummvm-rg350-4e66139a4e3d33fce662ad572fca26f49a5d9de4.zip |
Added String::hasSuffix and hasPrefix
svn-id: r16744
Diffstat (limited to 'common')
-rw-r--r-- | common/str.cpp | 29 | ||||
-rw-r--r-- | common/str.h | 5 |
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(); |