aboutsummaryrefslogtreecommitdiff
path: root/common/ustr.cpp
diff options
context:
space:
mode:
authorPaul Gilbert2019-01-01 00:40:17 -0800
committerPaul Gilbert2019-01-01 00:40:17 -0800
commit7643382ff4c19bf69ac36c015ca9e8e046c20c77 (patch)
tree14ae1e4846d518597045eacb98ce6c306f003060 /common/ustr.cpp
parent0d1f5bc2d0c828165abd422a2e0a47ad3d9ae8c9 (diff)
downloadscummvm-rg350-7643382ff4c19bf69ac36c015ca9e8e046c20c77.tar.gz
scummvm-rg350-7643382ff4c19bf69ac36c015ca9e8e046c20c77.tar.bz2
scummvm-rg350-7643382ff4c19bf69ac36c015ca9e8e046c20c77.zip
COMMON: Suport String in U32String assignment & equality operators
Diffstat (limited to 'common/ustr.cpp')
-rw-r--r--common/ustr.cpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/common/ustr.cpp b/common/ustr.cpp
index ecc91bb189..7f68f8a37a 100644
--- a/common/ustr.cpp
+++ b/common/ustr.cpp
@@ -72,6 +72,28 @@ U32String::U32String(const U32String &str)
assert(_str != nullptr);
}
+U32String::U32String(const char *str) : _size(0), _str(_storage) {
+ if (str == nullptr) {
+ _storage[0] = 0;
+ _size = 0;
+ } else {
+ initWithCStr(str, strlen(str));
+ }
+}
+
+U32String::U32String(const char *str, uint32 len) : _size(0), _str(_storage) {
+ initWithCStr(str, len);
+}
+
+U32String::U32String(const char *beginP, const char *endP) : _size(0), _str(_storage) {
+ assert(endP >= beginP);
+ initWithCStr(beginP, endP - beginP);
+}
+
+U32String::U32String(const String &str) : _size(0) {
+ initWithCStr(str.c_str(), str.size());
+}
+
U32String::~U32String() {
decRefCount(_extern._refCount);
}
@@ -98,6 +120,20 @@ U32String &U32String::operator=(const U32String &str) {
return *this;
}
+U32String &U32String::operator=(const String &str) {
+ initWithCStr(str.c_str(), str.size());
+ return *this;
+}
+
+U32String &U32String::operator=(const value_type *str) {
+ return U32String::operator=(U32String(str));
+}
+
+U32String &U32String::operator=(const char *str) {
+ initWithCStr(str, strlen(str));
+ return *this;
+}
+
U32String &U32String::operator+=(const U32String &str) {
if (&str == this) {
return operator+=(U32String(str));
@@ -122,6 +158,38 @@ U32String &U32String::operator+=(value_type c) {
return *this;
}
+bool U32String::operator==(const U32String &x) const {
+ return equals(x);
+}
+
+bool U32String::operator==(const String &x) const {
+ return equals(x);
+}
+
+bool U32String::operator==(const value_type *x) const {
+ return equals(U32String(x));
+}
+
+bool U32String::operator==(const char *x) const {
+ return equals(x);
+}
+
+bool U32String::operator!=(const U32String &x) const {
+ return !equals(x);
+}
+
+bool U32String::operator!=(const String &x) const {
+ return !equals(x);
+}
+
+bool U32String::operator!=(const value_type *x) const {
+ return !equals(U32String(x));
+}
+
+bool U32String::operator!=(const char *x) const {
+ return !equals(x);
+}
+
bool U32String::equals(const U32String &x) const {
if (this == &x || _str == x._str) {
return true;
@@ -134,6 +202,17 @@ bool U32String::equals(const U32String &x) const {
return !memcmp(_str, x._str, _size * sizeof(value_type));
}
+bool U32String::equals(const String &x) const {
+ if (x.size() != _size)
+ return false;
+
+ for (size_t idx = 0; idx < _size; ++idx)
+ if (_str[idx] != x[idx])
+ return false;
+
+ return true;
+}
+
bool U32String::contains(value_type x) const {
for (uint32 i = 0; i < _size; ++i) {
if (_str[i] == x) {
@@ -327,6 +406,28 @@ void U32String::initWithCStr(const value_type *str, uint32 len) {
_str[len] = 0;
}
+void U32String::initWithCStr(const char *str, uint32 len) {
+ assert(str);
+
+ _storage[0] = 0;
+
+ _size = len;
+
+ if (len >= _builtinCapacity) {
+ // Not enough internal storage, so allocate more
+ _extern._capacity = computeCapacity(len + 1);
+ _extern._refCount = nullptr;
+ _str = new value_type[_extern._capacity];
+ assert(_str != nullptr);
+ }
+
+ // Copy the string into the storage area
+ for (size_t idx = 0; idx < len; ++idx, ++str)
+ _str[idx] = *str;
+
+ _str[len] = 0;
+}
+
// This is a quick and dirty converter.
//
// More comprehensive one lives in wintermute/utils/convert_utf.cpp