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/str.cpp | |
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/str.cpp')
-rw-r--r-- | common/str.cpp | 29 |
1 files changed, 29 insertions, 0 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); |