diff options
Diffstat (limited to 'common/str.cpp')
-rw-r--r-- | common/str.cpp | 97 |
1 files changed, 67 insertions, 30 deletions
diff --git a/common/str.cpp b/common/str.cpp index 2ef67175cd..468d1a6f53 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -28,7 +28,7 @@ namespace Common { -MemoryPool *g_refCountPool = 0; // FIXME: This is never freed right now +MemoryPool *g_refCountPool = nullptr; // FIXME: This is never freed right now static uint32 computeCapacity(uint32 len) { // By default, for the capacity we use the next multiple of 32 @@ -36,7 +36,7 @@ static uint32 computeCapacity(uint32 len) { } String::String(const char *str) : _size(0), _str(_storage) { - if (str == 0) { + if (str == nullptr) { _storage[0] = 0; _size = 0; } else @@ -63,10 +63,10 @@ void String::initWithCStr(const char *str, uint32 len) { if (len >= _builtinCapacity) { // Not enough internal storage, so allocate more - _extern._capacity = computeCapacity(len+1); - _extern._refCount = 0; + _extern._capacity = computeCapacity(len + 1); + _extern._refCount = nullptr; _str = new char[_extern._capacity]; - assert(_str != 0); + assert(_str != nullptr); } // Copy the string into the storage area @@ -87,7 +87,7 @@ String::String(const String &str) _extern._capacity = str._extern._capacity; _str = str._str; } - assert(_str != 0); + assert(_str != nullptr); } String::String(char c) @@ -165,15 +165,15 @@ void String::ensureCapacity(uint32 new_size, bool keep_old) { // Set the ref count & capacity if we use an external storage. // It is important to do this *after* copying any old content, // else we would override data that has not yet been copied! - _extern._refCount = 0; + _extern._refCount = nullptr; _extern._capacity = newCapacity; } } void String::incRefCount() const { assert(!isStorageIntern()); - if (_extern._refCount == 0) { - if (g_refCountPool == 0) { + if (_extern._refCount == nullptr) { + if (g_refCountPool == nullptr) { g_refCountPool = new MemoryPool(sizeof(int)); assert(g_refCountPool); } @@ -290,7 +290,7 @@ bool String::hasPrefix(const String &x) const { } bool String::hasPrefix(const char *x) const { - assert(x != 0); + assert(x != nullptr); // Compare x with the start of _str. const char *y = c_str(); while (*x && *x == *y) { @@ -302,12 +302,29 @@ bool String::hasPrefix(const char *x) const { return *x == 0; } +bool String::hasPrefixIgnoreCase(const String &x) const { + return hasPrefixIgnoreCase(x.c_str()); +} + +bool String::hasPrefixIgnoreCase(const char *x) const { + assert(x != nullptr); + // Compare x with the start of _str. + const char *y = c_str(); + while (*x && tolower(*x) == tolower(*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 String &x) const { return hasSuffix(x.c_str()); } bool String::hasSuffix(const char *x) const { - assert(x != 0); + assert(x != nullptr); // Compare x with the end of _str. const uint32 x_size = strlen(x); if (x_size > _size) @@ -322,17 +339,37 @@ bool String::hasSuffix(const char *x) const { return *x == 0; } +bool String::hasSuffixIgnoreCase(const String &x) const { + return hasSuffixIgnoreCase(x.c_str()); +} + +bool String::hasSuffixIgnoreCase(const char *x) const { + assert(x != nullptr); + // Compare x with the end of _str. + const uint32 x_size = strlen(x); + if (x_size > _size) + return false; + const char *y = c_str() + _size - x_size; + while (*x && tolower(*x) == tolower(*y)) { + ++x; + ++y; + } + // It's a suffix, if and only if all letters in x are 'used up' before + // _str ends. + return *x == 0; +} + bool String::contains(const String &x) const { - return strstr(c_str(), x.c_str()) != NULL; + return strstr(c_str(), x.c_str()) != nullptr; } bool String::contains(const char *x) const { - assert(x != 0); - return strstr(c_str(), x) != NULL; + assert(x != nullptr); + return strstr(c_str(), x) != nullptr; } bool String::contains(char x) const { - return strchr(c_str(), x) != NULL; + return strchr(c_str(), x) != nullptr; } uint64 String::asUint64() const { @@ -593,7 +630,7 @@ String String::vformat(const char *fmt, va_list args) { // vsnprintf didn't have enough space, so grow buffer output.ensureCapacity(len, false); scumm_va_copy(va, args); - int len2 = vsnprintf(output._str, len+1, fmt, va); + int len2 = vsnprintf(output._str, len + 1, fmt, va); va_end(va); assert(len == len2); output._size = len2; @@ -610,7 +647,7 @@ bool String::operator==(const String &x) const { } bool String::operator==(const char *x) const { - assert(x != 0); + assert(x != nullptr); return equals(x); } @@ -619,7 +656,7 @@ bool String::operator!=(const String &x) const { } bool String::operator !=(const char *x) const { - assert(x != 0); + assert(x != nullptr); return !equals(x); } @@ -656,7 +693,7 @@ bool String::equals(const String &x) const { } bool String::equals(const char *x) const { - assert(x != 0); + assert(x != nullptr); return (0 == compareTo(x)); } @@ -665,7 +702,7 @@ bool String::equalsIgnoreCase(const String &x) const { } bool String::equalsIgnoreCase(const char *x) const { - assert(x != 0); + assert(x != nullptr); return (0 == compareToIgnoreCase(x)); } @@ -674,7 +711,7 @@ int String::compareTo(const String &x) const { } int String::compareTo(const char *x) const { - assert(x != 0); + assert(x != nullptr); return strcmp(c_str(), x); } @@ -683,7 +720,7 @@ int String::compareToIgnoreCase(const String &x) const { } int String::compareToIgnoreCase(const char *x) const { - assert(x != 0); + assert(x != nullptr); return scumm_stricmp(c_str(), x); } @@ -741,7 +778,7 @@ String lastPathComponent(const String &path, const char sep) { const char *last = str + path.size(); // Skip over trailing slashes - while (last > str && *(last-1) == sep) + while (last > str && *(last - 1) == sep) --last; // Path consisted of only slashes -> return empty string @@ -815,13 +852,13 @@ bool matchString(const char *str, const char *pat, bool ignoreCase, bool pathMod assert(str); assert(pat); - const char *p = 0; - const char *q = 0; + const char *p = nullptr; + const char *q = nullptr; for (;;) { if (pathMode && *str == '/') { - p = 0; - q = 0; + p = nullptr; + q = nullptr; if (*pat == '?') return false; } @@ -837,8 +874,8 @@ bool matchString(const char *str, const char *pat, bool ignoreCase, bool pathMod // NB: We can't simply check if pat also ended here, because // the pattern might end with any number of *s. ++pat; - p = 0; - q = 0; + p = nullptr; + q = nullptr; } // If pattern ended with * -> match if (!*pat) @@ -1010,7 +1047,7 @@ int scumm_strnicmp(const char *s1, const char *s2, uint n) { byte l1, l2; do { if (n-- == 0) - return 0; // no difference found so far -> signal equality + return 0; // no difference found so far -> signal equality // Don't use ++ inside tolower, in case the macro uses its // arguments more than once. |