2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2002-09-08 01:08:12 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2014-02-18 01:34:18 +00:00
|
|
|
*
|
2002-09-08 01:08:12 +00:00
|
|
|
*/
|
|
|
|
|
2006-07-30 12:21:54 +00:00
|
|
|
#include "common/hash-str.h"
|
2011-06-03 15:33:07 +00:00
|
|
|
#include "common/list.h"
|
2008-09-24 20:29:29 +00:00
|
|
|
#include "common/memorypool.h"
|
2011-06-03 15:33:07 +00:00
|
|
|
#include "common/str.h"
|
|
|
|
#include "common/util.h"
|
2008-09-24 20:29:29 +00:00
|
|
|
|
2003-10-02 17:43:02 +00:00
|
|
|
namespace Common {
|
2002-09-08 01:08:12 +00:00
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
MemoryPool *g_refCountPool = 0; // FIXME: This is never freed right now
|
2008-09-24 20:29:29 +00:00
|
|
|
|
2008-09-03 17:39:18 +00:00
|
|
|
static uint32 computeCapacity(uint32 len) {
|
2008-09-03 18:38:01 +00:00
|
|
|
// By default, for the capacity we use the next multiple of 32
|
2008-09-03 17:39:18 +00:00
|
|
|
return ((len + 32 - 1) & ~0x1F);
|
2006-06-03 16:33:42 +00:00
|
|
|
}
|
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
String::String(const char *str) : _size(0), _str(_storage) {
|
2008-07-20 16:42:56 +00:00
|
|
|
if (str == 0) {
|
|
|
|
_storage[0] = 0;
|
2008-08-20 11:07:16 +00:00
|
|
|
_size = 0;
|
2008-07-20 16:42:56 +00:00
|
|
|
} else
|
|
|
|
initWithCStr(str, strlen(str));
|
|
|
|
}
|
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
String::String(const char *str, uint32 len) : _size(0), _str(_storage) {
|
2008-07-20 16:42:56 +00:00
|
|
|
initWithCStr(str, len);
|
|
|
|
}
|
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
String::String(const char *beginP, const char *endP) : _size(0), _str(_storage) {
|
2008-07-20 16:42:56 +00:00
|
|
|
assert(endP >= beginP);
|
|
|
|
initWithCStr(beginP, endP - beginP);
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::initWithCStr(const char *str, uint32 len) {
|
|
|
|
assert(str);
|
2006-11-27 00:51:14 +00:00
|
|
|
|
|
|
|
// Init _storage member explicitly (ie. without calling its constructor)
|
|
|
|
// for GCC 2.95.x compatibility (see also tracker item #1602879).
|
|
|
|
_storage[0] = 0;
|
2006-09-30 18:55:38 +00:00
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
_size = len;
|
2008-07-20 16:42:56 +00:00
|
|
|
|
|
|
|
if (len >= _builtinCapacity) {
|
|
|
|
// Not enough internal storage, so allocate more
|
2008-09-03 18:38:01 +00:00
|
|
|
_extern._capacity = computeCapacity(len+1);
|
2008-07-20 16:42:56 +00:00
|
|
|
_extern._refCount = 0;
|
2009-05-03 22:45:31 +00:00
|
|
|
_str = new char[_extern._capacity];
|
2008-07-20 16:42:56 +00:00
|
|
|
assert(_str != 0);
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
2008-07-20 16:42:56 +00:00
|
|
|
|
|
|
|
// Copy the string into the storage area
|
|
|
|
memmove(_str, str, len);
|
|
|
|
_str[len] = 0;
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2005-01-15 21:42:59 +00:00
|
|
|
String::String(const String &str)
|
2016-03-02 16:07:50 +00:00
|
|
|
: _size(str._size) {
|
2006-09-30 18:55:38 +00:00
|
|
|
if (str.isStorageIntern()) {
|
|
|
|
// String in internal storage: just copy it
|
2008-09-20 15:53:17 +00:00
|
|
|
memcpy(_storage, str._storage, _builtinCapacity);
|
|
|
|
_str = _storage;
|
2006-09-30 18:55:38 +00:00
|
|
|
} else {
|
|
|
|
// String in external storage: use refcount mechanism
|
2006-06-05 17:36:08 +00:00
|
|
|
str.incRefCount();
|
2006-09-30 18:55:38 +00:00
|
|
|
_extern._refCount = str._extern._refCount;
|
|
|
|
_extern._capacity = str._extern._capacity;
|
2008-09-20 15:53:17 +00:00
|
|
|
_str = str._str;
|
2006-06-05 17:36:08 +00:00
|
|
|
}
|
2006-09-30 18:55:38 +00:00
|
|
|
assert(_str != 0);
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
2007-06-02 12:42:40 +00:00
|
|
|
|
|
|
|
String::String(char c)
|
2016-03-02 16:07:50 +00:00
|
|
|
: _size(0), _str(_storage) {
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-06-02 12:42:40 +00:00
|
|
|
_storage[0] = c;
|
|
|
|
_storage[1] = 0;
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
_size = (c == 0) ? 0 : 1;
|
2007-06-02 12:42:40 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 16:27:06 +00:00
|
|
|
String::~String() {
|
2006-09-30 18:55:38 +00:00
|
|
|
decRefCount(_extern._refCount);
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2008-07-23 16:49:45 +00:00
|
|
|
void String::makeUnique() {
|
2008-08-20 11:07:16 +00:00
|
|
|
ensureCapacity(_size, true);
|
2008-07-23 16:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-08-20 11:07:16 +00:00
|
|
|
* Ensure that enough storage is available to store at least new_size
|
2008-07-23 16:49:45 +00:00
|
|
|
* characters plus a null byte. In addition, if we currently share
|
|
|
|
* the storage with another string, unshare it, so that we can safely
|
|
|
|
* write to the storage.
|
|
|
|
*/
|
2008-08-20 11:07:16 +00:00
|
|
|
void String::ensureCapacity(uint32 new_size, bool keep_old) {
|
2008-07-23 16:49:45 +00:00
|
|
|
bool isShared;
|
|
|
|
uint32 curCapacity, newCapacity;
|
|
|
|
char *newStorage;
|
|
|
|
int *oldRefCount = _extern._refCount;
|
|
|
|
|
|
|
|
if (isStorageIntern()) {
|
|
|
|
isShared = false;
|
2008-09-03 17:39:18 +00:00
|
|
|
curCapacity = _builtinCapacity;
|
2008-07-23 16:49:45 +00:00
|
|
|
} else {
|
|
|
|
isShared = (oldRefCount && *oldRefCount > 1);
|
|
|
|
curCapacity = _extern._capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special case: If there is enough space, and we do not share
|
|
|
|
// the storage, then there is nothing to do.
|
2008-09-03 18:38:01 +00:00
|
|
|
if (!isShared && new_size < curCapacity)
|
2008-07-23 16:49:45 +00:00
|
|
|
return;
|
|
|
|
|
2016-03-02 16:07:50 +00:00
|
|
|
// We need to allocate storage on the heap!
|
|
|
|
|
|
|
|
// Compute a suitable new capacity limit
|
|
|
|
// If the current capacity is sufficient we use the same capacity
|
|
|
|
if (new_size < curCapacity)
|
|
|
|
newCapacity = curCapacity;
|
|
|
|
else
|
|
|
|
newCapacity = MAX(curCapacity * 2, computeCapacity(new_size+1));
|
|
|
|
|
|
|
|
// Allocate new storage
|
|
|
|
newStorage = new char[newCapacity];
|
|
|
|
assert(newStorage);
|
2016-05-28 15:18:17 +00:00
|
|
|
|
2008-07-23 16:49:45 +00:00
|
|
|
|
|
|
|
// Copy old data if needed, elsewise reset the new storage.
|
|
|
|
if (keep_old) {
|
2008-09-03 17:39:18 +00:00
|
|
|
assert(_size < newCapacity);
|
2008-08-20 11:07:16 +00:00
|
|
|
memcpy(newStorage, _str, _size + 1);
|
2008-07-23 16:49:45 +00:00
|
|
|
} else {
|
2008-08-20 11:07:16 +00:00
|
|
|
_size = 0;
|
2008-07-23 16:49:45 +00:00
|
|
|
newStorage[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release hold on the old storage ...
|
|
|
|
decRefCount(oldRefCount);
|
|
|
|
|
|
|
|
// ... in favor of the new storage
|
|
|
|
_str = newStorage;
|
|
|
|
|
|
|
|
if (!isStorageIntern()) {
|
|
|
|
// 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._capacity = newCapacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-03 16:33:42 +00:00
|
|
|
void String::incRefCount() const {
|
2006-09-30 18:55:38 +00:00
|
|
|
assert(!isStorageIntern());
|
|
|
|
if (_extern._refCount == 0) {
|
2009-05-03 22:45:31 +00:00
|
|
|
if (g_refCountPool == 0) {
|
2008-09-24 20:29:29 +00:00
|
|
|
g_refCountPool = new MemoryPool(sizeof(int));
|
2009-05-03 22:45:31 +00:00
|
|
|
assert(g_refCountPool);
|
|
|
|
}
|
2008-09-24 20:29:29 +00:00
|
|
|
|
2008-10-12 22:05:26 +00:00
|
|
|
_extern._refCount = (int *)g_refCountPool->allocChunk();
|
2008-09-24 20:29:29 +00:00
|
|
|
*_extern._refCount = 2;
|
2006-06-03 16:33:42 +00:00
|
|
|
} else {
|
2006-09-30 18:55:38 +00:00
|
|
|
++(*_extern._refCount);
|
2006-06-03 16:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-30 18:55:38 +00:00
|
|
|
void String::decRefCount(int *oldRefCount) {
|
|
|
|
if (isStorageIntern())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (oldRefCount) {
|
|
|
|
--(*oldRefCount);
|
2006-06-05 17:36:08 +00:00
|
|
|
}
|
2006-09-30 18:55:38 +00:00
|
|
|
if (!oldRefCount || *oldRefCount <= 0) {
|
|
|
|
// The ref count reached zero, so we free the string storage
|
|
|
|
// and the ref count storage.
|
2008-09-24 20:29:29 +00:00
|
|
|
if (oldRefCount) {
|
|
|
|
assert(g_refCountPool);
|
2008-10-12 22:05:26 +00:00
|
|
|
g_refCountPool->freeChunk(oldRefCount);
|
2008-09-24 20:29:29 +00:00
|
|
|
}
|
2009-05-04 14:20:17 +00:00
|
|
|
delete[] _str;
|
2006-09-30 18:55:38 +00:00
|
|
|
|
|
|
|
// Even though _str points to a freed memory block now,
|
|
|
|
// we do not change its value, because any code that calls
|
|
|
|
// decRefCount will have to do this afterwards anyway.
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
String &String::operator=(const char *str) {
|
2006-09-30 18:55:38 +00:00
|
|
|
uint32 len = strlen(str);
|
|
|
|
ensureCapacity(len, false);
|
2008-08-20 11:07:16 +00:00
|
|
|
_size = len;
|
2008-07-20 16:42:56 +00:00
|
|
|
memmove(_str, str, len + 1);
|
2002-09-08 01:08:12 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
String &String::operator=(const String &str) {
|
2008-07-20 16:42:56 +00:00
|
|
|
if (&str == this)
|
|
|
|
return *this;
|
|
|
|
|
2006-09-30 18:55:38 +00:00
|
|
|
if (str.isStorageIntern()) {
|
|
|
|
decRefCount(_extern._refCount);
|
2008-08-20 11:07:16 +00:00
|
|
|
_size = str._size;
|
2006-09-30 18:55:38 +00:00
|
|
|
_str = _storage;
|
2008-08-20 11:07:16 +00:00
|
|
|
memcpy(_str, str._str, _size + 1);
|
2006-09-30 18:55:38 +00:00
|
|
|
} else {
|
|
|
|
str.incRefCount();
|
|
|
|
decRefCount(_extern._refCount);
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2006-09-30 18:55:38 +00:00
|
|
|
_extern._refCount = str._extern._refCount;
|
|
|
|
_extern._capacity = str._extern._capacity;
|
2008-08-20 11:07:16 +00:00
|
|
|
_size = str._size;
|
2006-09-30 18:55:38 +00:00
|
|
|
_str = str._str;
|
|
|
|
}
|
2002-09-08 01:08:12 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
String &String::operator=(char c) {
|
2008-07-23 16:33:53 +00:00
|
|
|
decRefCount(_extern._refCount);
|
|
|
|
_str = _storage;
|
2010-07-05 21:29:52 +00:00
|
|
|
|
2004-07-21 14:20:37 +00:00
|
|
|
_str[0] = c;
|
|
|
|
_str[1] = 0;
|
2010-07-05 21:29:52 +00:00
|
|
|
|
|
|
|
_size = (c == 0) ? 0 : 1;
|
2004-07-21 14:20:37 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
String &String::operator+=(const char *str) {
|
2009-06-07 13:14:34 +00:00
|
|
|
if (_str <= str && str <= _str + _size)
|
2011-06-03 15:33:07 +00:00
|
|
|
return operator+=(String(str));
|
2009-06-07 13:14:34 +00:00
|
|
|
|
2002-09-28 19:25:09 +00:00
|
|
|
int len = strlen(str);
|
2002-09-08 01:08:12 +00:00
|
|
|
if (len > 0) {
|
2008-08-20 11:07:16 +00:00
|
|
|
ensureCapacity(_size + len, true);
|
2002-09-08 01:08:12 +00:00
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
memcpy(_str + _size, str, len + 1);
|
|
|
|
_size += len;
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
String &String::operator+=(const String &str) {
|
2009-06-07 13:14:34 +00:00
|
|
|
if (&str == this)
|
2011-06-03 15:33:07 +00:00
|
|
|
return operator+=(String(str));
|
2009-06-07 13:14:34 +00:00
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
int len = str._size;
|
2002-09-08 01:08:12 +00:00
|
|
|
if (len > 0) {
|
2008-08-20 11:07:16 +00:00
|
|
|
ensureCapacity(_size + len, true);
|
2002-09-08 01:08:12 +00:00
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
memcpy(_str + _size, str._str, len + 1);
|
|
|
|
_size += len;
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
String &String::operator+=(char c) {
|
2008-08-20 11:07:16 +00:00
|
|
|
ensureCapacity(_size + 1, true);
|
2003-03-06 16:27:06 +00:00
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
_str[_size++] = c;
|
|
|
|
_str[_size] = 0;
|
2002-09-08 01:08:12 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-06-07 13:04:03 +00:00
|
|
|
bool String::hasPrefix(const String &x) const {
|
|
|
|
return hasPrefix(x.c_str());
|
|
|
|
}
|
|
|
|
|
2005-02-06 19:00:59 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-06-07 13:04:03 +00:00
|
|
|
bool String::hasSuffix(const String &x) const {
|
|
|
|
return hasSuffix(x.c_str());
|
|
|
|
}
|
|
|
|
|
2005-02-06 19:00:59 +00:00
|
|
|
bool String::hasSuffix(const char *x) const {
|
|
|
|
assert(x != 0);
|
|
|
|
// Compare x with the end of _str.
|
2008-08-20 11:07:16 +00:00
|
|
|
const uint32 x_size = strlen(x);
|
|
|
|
if (x_size > _size)
|
2005-02-06 19:00:59 +00:00
|
|
|
return false;
|
2008-08-20 11:07:16 +00:00
|
|
|
const char *y = c_str() + _size - x_size;
|
2005-02-06 19:00:59 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-06-07 13:04:03 +00:00
|
|
|
bool String::contains(const String &x) const {
|
|
|
|
return strstr(c_str(), x.c_str()) != NULL;
|
|
|
|
}
|
|
|
|
|
2007-04-15 18:27:10 +00:00
|
|
|
bool String::contains(const char *x) const {
|
|
|
|
assert(x != 0);
|
|
|
|
return strstr(c_str(), x) != NULL;
|
2008-02-08 04:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool String::contains(char x) const {
|
|
|
|
return strchr(c_str(), x) != NULL;
|
2007-04-15 18:27:10 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 08:06:06 +00:00
|
|
|
uint64 String::asUint64() const {
|
|
|
|
uint64 result = 0;
|
|
|
|
for (uint32 i = 0; i < _size; ++i) {
|
|
|
|
if (_str[i] < '0' || _str[i] > '9') break;
|
|
|
|
result = result * 10L + (_str[i] - '0');
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-09-23 00:14:06 +00:00
|
|
|
bool String::matchString(const char *pat, bool ignoreCase, bool pathMode) const {
|
|
|
|
return Common::matchString(c_str(), pat, ignoreCase, pathMode);
|
2008-09-05 20:07:34 +00:00
|
|
|
}
|
|
|
|
|
2009-09-23 00:14:06 +00:00
|
|
|
bool String::matchString(const String &pat, bool ignoreCase, bool pathMode) const {
|
|
|
|
return Common::matchString(c_str(), pat.c_str(), ignoreCase, pathMode);
|
2008-09-05 20:07:34 +00:00
|
|
|
}
|
|
|
|
|
2002-09-08 11:46:42 +00:00
|
|
|
void String::deleteLastChar() {
|
2008-10-02 17:17:18 +00:00
|
|
|
if (_size > 0)
|
|
|
|
deleteChar(_size - 1);
|
2002-09-08 11:46:42 +00:00
|
|
|
}
|
|
|
|
|
2006-09-30 18:55:38 +00:00
|
|
|
void String::deleteChar(uint32 p) {
|
2008-08-20 11:07:16 +00:00
|
|
|
assert(p < _size);
|
2006-09-30 18:55:38 +00:00
|
|
|
|
2008-07-23 16:33:53 +00:00
|
|
|
makeUnique();
|
2008-08-20 11:07:16 +00:00
|
|
|
while (p++ < _size)
|
2010-01-31 10:22:09 +00:00
|
|
|
_str[p - 1] = _str[p];
|
2008-08-20 11:07:16 +00:00
|
|
|
_size--;
|
2003-01-10 21:33:42 +00:00
|
|
|
}
|
|
|
|
|
2013-06-19 22:27:05 +00:00
|
|
|
void String::erase(uint32 p, uint32 len) {
|
|
|
|
assert(p < _size);
|
|
|
|
|
|
|
|
makeUnique();
|
|
|
|
// If len == npos or p + len is over the end, remove all the way to the end
|
|
|
|
if (len == npos || p + len >= _size) {
|
|
|
|
// Delete char at p as well. So _size = (p - 1) + 1
|
|
|
|
_size = p;
|
|
|
|
// Null terminate
|
|
|
|
_str[_size] = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( ; p + len <= _size; p++) {
|
|
|
|
_str[p] = _str[p + len];
|
|
|
|
}
|
|
|
|
_size -= len;
|
|
|
|
}
|
|
|
|
|
2003-03-06 16:27:06 +00:00
|
|
|
void String::clear() {
|
2006-09-30 18:55:38 +00:00
|
|
|
decRefCount(_extern._refCount);
|
2003-03-06 16:27:06 +00:00
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
_size = 0;
|
2006-09-30 18:55:38 +00:00
|
|
|
_str = _storage;
|
|
|
|
_storage[0] = 0;
|
2002-09-08 11:46:42 +00:00
|
|
|
}
|
|
|
|
|
2008-03-30 18:37:09 +00:00
|
|
|
void String::setChar(char c, uint32 p) {
|
2013-03-20 20:10:13 +00:00
|
|
|
assert(p < _size);
|
2008-03-30 18:37:09 +00:00
|
|
|
|
2008-07-23 16:33:53 +00:00
|
|
|
makeUnique();
|
2008-03-30 18:37:09 +00:00
|
|
|
_str[p] = c;
|
|
|
|
}
|
|
|
|
|
2006-09-30 18:55:38 +00:00
|
|
|
void String::insertChar(char c, uint32 p) {
|
2008-08-20 11:07:16 +00:00
|
|
|
assert(p <= _size);
|
2006-09-30 18:55:38 +00:00
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
ensureCapacity(_size + 1, true);
|
|
|
|
_size++;
|
|
|
|
for (uint32 i = _size; i > p; --i)
|
2010-01-31 10:22:09 +00:00
|
|
|
_str[i] = _str[i - 1];
|
2006-09-30 18:55:38 +00:00
|
|
|
_str[p] = c;
|
2003-01-10 21:33:42 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 16:27:06 +00:00
|
|
|
void String::toLowercase() {
|
2008-07-23 16:33:53 +00:00
|
|
|
makeUnique();
|
2008-08-20 11:07:16 +00:00
|
|
|
for (uint32 i = 0; i < _size; ++i)
|
2002-10-08 00:11:41 +00:00
|
|
|
_str[i] = tolower(_str[i]);
|
|
|
|
}
|
|
|
|
|
2003-03-06 16:27:06 +00:00
|
|
|
void String::toUppercase() {
|
2008-07-23 16:33:53 +00:00
|
|
|
makeUnique();
|
2008-08-20 11:07:16 +00:00
|
|
|
for (uint32 i = 0; i < _size; ++i)
|
2002-10-08 00:11:41 +00:00
|
|
|
_str[i] = toupper(_str[i]);
|
|
|
|
}
|
|
|
|
|
2008-07-22 14:39:26 +00:00
|
|
|
void String::trim() {
|
2008-08-20 11:07:16 +00:00
|
|
|
if (_size == 0)
|
2008-07-22 14:39:26 +00:00
|
|
|
return;
|
|
|
|
|
2008-07-23 16:33:53 +00:00
|
|
|
makeUnique();
|
2008-07-23 09:53:29 +00:00
|
|
|
|
2008-07-22 14:39:26 +00:00
|
|
|
// Trim trailing whitespace
|
2012-01-28 00:15:49 +00:00
|
|
|
while (_size >= 1 && isSpace(_str[_size - 1]))
|
2010-01-31 10:22:09 +00:00
|
|
|
--_size;
|
2008-08-20 11:07:16 +00:00
|
|
|
_str[_size] = 0;
|
2008-07-22 14:39:26 +00:00
|
|
|
|
|
|
|
// Trim leading whitespace
|
|
|
|
char *t = _str;
|
2012-01-28 00:15:49 +00:00
|
|
|
while (isSpace(*t))
|
2008-07-22 14:39:26 +00:00
|
|
|
t++;
|
|
|
|
|
|
|
|
if (t != _str) {
|
2008-08-20 11:07:16 +00:00
|
|
|
_size -= t - _str;
|
|
|
|
memmove(_str, t, _size + 1);
|
2008-07-22 14:39:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-30 12:17:51 +00:00
|
|
|
uint String::hash() const {
|
|
|
|
return hashit(c_str());
|
|
|
|
}
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2016-03-02 16:07:50 +00:00
|
|
|
void String::replace(uint32 pos, uint32 count, const String &str) {
|
|
|
|
replace(pos, count, str, 0, str._size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::replace(uint32 pos, uint32 count, const char *str) {
|
|
|
|
replace(pos, count, str, 0, strlen(str));
|
|
|
|
}
|
|
|
|
|
2016-05-28 15:18:17 +00:00
|
|
|
void String::replace(iterator begin_, iterator end_, const String &str) {
|
|
|
|
replace(begin_ - _str, end_ - begin_, str._str, 0, str._size);
|
2016-03-02 16:07:50 +00:00
|
|
|
}
|
|
|
|
|
2016-05-28 15:18:17 +00:00
|
|
|
void String::replace(iterator begin_, iterator end_, const char *str) {
|
|
|
|
replace(begin_ - _str, end_ - begin_, str, 0, strlen(str));
|
2016-03-02 16:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void String::replace(uint32 posOri, uint32 countOri, const String &str,
|
|
|
|
uint32 posDest, uint32 countDest) {
|
|
|
|
replace(posOri, countOri, str._str, posDest, countDest);
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::replace(uint32 posOri, uint32 countOri, const char *str,
|
|
|
|
uint32 posDest, uint32 countDest) {
|
|
|
|
|
|
|
|
ensureCapacity(_size + countDest - countOri, true);
|
|
|
|
|
|
|
|
// Prepare string for the replaced text.
|
|
|
|
if (countOri < countDest) {
|
|
|
|
uint32 offset = countDest - countOri; ///< Offset to copy the characters
|
|
|
|
uint32 newSize = _size + offset;
|
|
|
|
_size = newSize;
|
|
|
|
|
|
|
|
// Push the old characters to the end of the string
|
2016-05-28 15:18:17 +00:00
|
|
|
for (uint32 i = _size; i >= posOri + countDest; i--)
|
2016-03-02 16:07:50 +00:00
|
|
|
_str[i] = _str[i - offset];
|
|
|
|
|
|
|
|
} else if (countOri > countDest){
|
|
|
|
uint32 offset = countOri - countDest; ///< Number of positions that we have to pull back
|
|
|
|
|
|
|
|
// Pull the remainder string back
|
2016-05-28 15:18:17 +00:00
|
|
|
for (uint32 i = posOri + countDest; i < _size; i++)
|
|
|
|
_str[i] = _str[i + offset];
|
2016-03-02 16:07:50 +00:00
|
|
|
|
2016-05-28 15:18:17 +00:00
|
|
|
_size -= offset;
|
2016-03-02 16:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the replaced part of the string
|
2016-05-28 15:18:17 +00:00
|
|
|
for (uint32 i = 0; i < countDest; i++)
|
2016-03-02 16:07:50 +00:00
|
|
|
_str[posOri + i] = str[posDest + i];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-07-25 10:25:57 +00:00
|
|
|
// static
|
2010-11-01 16:02:28 +00:00
|
|
|
String String::format(const char *fmt, ...) {
|
2009-07-25 10:25:57 +00:00
|
|
|
String output;
|
|
|
|
|
|
|
|
va_list va;
|
2010-10-18 20:20:14 +00:00
|
|
|
va_start(va, fmt);
|
2011-03-18 13:30:57 +00:00
|
|
|
output = String::vformat(fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
String String::vformat(const char *fmt, va_list args) {
|
|
|
|
String output;
|
|
|
|
assert(output.isStorageIntern());
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
scumm_va_copy(va, args);
|
2009-07-25 10:25:57 +00:00
|
|
|
int len = vsnprintf(output._str, _builtinCapacity, fmt, va);
|
2010-10-18 20:20:14 +00:00
|
|
|
va_end(va);
|
2009-07-25 10:25:57 +00:00
|
|
|
|
2010-10-10 22:36:03 +00:00
|
|
|
if (len == -1 || len == _builtinCapacity - 1) {
|
|
|
|
// MSVC and IRIX don't return the size the full string would take up.
|
|
|
|
// MSVC returns -1, IRIX returns the number of characters actually written,
|
|
|
|
// which is at the most the size of the buffer minus one, as the string is
|
|
|
|
// truncated to fit.
|
2009-10-01 14:47:52 +00:00
|
|
|
|
|
|
|
// We assume MSVC failed to output the correct, null-terminated string
|
|
|
|
// if the return value is either -1 or size.
|
2010-10-10 22:36:03 +00:00
|
|
|
// For IRIX, because we lack a better mechanism, we assume failure
|
|
|
|
// if the return value equals size - 1.
|
|
|
|
// The downside to this is that whenever we try to format a string where the
|
|
|
|
// size is 1 below the built-in capacity, the size is needlessly increased.
|
|
|
|
|
|
|
|
// Try increasing the size of the string until it fits.
|
2009-10-01 14:47:52 +00:00
|
|
|
int size = _builtinCapacity;
|
|
|
|
do {
|
|
|
|
size *= 2;
|
2010-01-31 10:22:09 +00:00
|
|
|
output.ensureCapacity(size - 1, false);
|
2009-10-03 20:26:30 +00:00
|
|
|
assert(!output.isStorageIntern());
|
|
|
|
size = output._extern._capacity;
|
|
|
|
|
2011-03-18 13:30:57 +00:00
|
|
|
scumm_va_copy(va, args);
|
2009-10-01 14:47:52 +00:00
|
|
|
len = vsnprintf(output._str, size, fmt, va);
|
2010-10-18 20:20:14 +00:00
|
|
|
va_end(va);
|
2010-10-10 22:36:03 +00:00
|
|
|
} while (len == -1 || len >= size - 1);
|
2009-10-12 11:51:28 +00:00
|
|
|
output._size = len;
|
2009-10-01 14:47:52 +00:00
|
|
|
} else if (len < (int)_builtinCapacity) {
|
2009-07-25 10:25:57 +00:00
|
|
|
// vsnprintf succeeded
|
|
|
|
output._size = len;
|
|
|
|
} else {
|
|
|
|
// vsnprintf didn't have enough space, so grow buffer
|
|
|
|
output.ensureCapacity(len, false);
|
2011-03-18 13:30:57 +00:00
|
|
|
scumm_va_copy(va, args);
|
2009-07-25 10:25:57 +00:00
|
|
|
int len2 = vsnprintf(output._str, len+1, fmt, va);
|
2010-10-18 20:20:14 +00:00
|
|
|
va_end(va);
|
2009-07-25 10:25:57 +00:00
|
|
|
assert(len == len2);
|
|
|
|
output._size = len2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-08 11:46:42 +00:00
|
|
|
#pragma mark -
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
bool String::operator==(const String &x) const {
|
2006-07-30 12:17:51 +00:00
|
|
|
return equals(x);
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
bool String::operator==(const char *x) const {
|
2003-11-07 00:02:03 +00:00
|
|
|
assert(x != 0);
|
2006-07-30 12:17:51 +00:00
|
|
|
return equals(x);
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
bool String::operator!=(const String &x) const {
|
2006-07-30 12:17:51 +00:00
|
|
|
return !equals(x);
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2005-01-15 21:42:59 +00:00
|
|
|
bool String::operator !=(const char *x) const {
|
2003-11-07 00:02:03 +00:00
|
|
|
assert(x != 0);
|
2006-07-30 12:17:51 +00:00
|
|
|
return !equals(x);
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
bool String::operator<(const String &x) const {
|
2006-07-30 12:17:51 +00:00
|
|
|
return compareTo(x) < 0;
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
bool String::operator<=(const String &x) const {
|
2006-07-30 12:17:51 +00:00
|
|
|
return compareTo(x) <= 0;
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
bool String::operator>(const String &x) const {
|
2006-07-30 12:17:51 +00:00
|
|
|
return compareTo(x) > 0;
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
bool String::operator>=(const String &x) const {
|
2006-07-30 12:17:51 +00:00
|
|
|
return compareTo(x) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
bool operator==(const char* y, const String &x) {
|
2006-07-30 12:17:51 +00:00
|
|
|
return (x == y);
|
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
bool operator!=(const char* y, const String &x) {
|
2006-07-30 12:17:51 +00:00
|
|
|
return x != y;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
bool String::equals(const String &x) const {
|
|
|
|
return (0 == compareTo(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String::equals(const char *x) const {
|
|
|
|
assert(x != 0);
|
|
|
|
return (0 == compareTo(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String::equalsIgnoreCase(const String &x) const {
|
|
|
|
return (0 == compareToIgnoreCase(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String::equalsIgnoreCase(const char *x) const {
|
|
|
|
assert(x != 0);
|
|
|
|
return (0 == compareToIgnoreCase(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
int String::compareTo(const String &x) const {
|
|
|
|
return compareTo(x.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
int String::compareTo(const char *x) const {
|
|
|
|
assert(x != 0);
|
|
|
|
return strcmp(c_str(), x);
|
|
|
|
}
|
|
|
|
|
|
|
|
int String::compareToIgnoreCase(const String &x) const {
|
|
|
|
return compareToIgnoreCase(x.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
int String::compareToIgnoreCase(const char *x) const {
|
|
|
|
assert(x != 0);
|
|
|
|
return scumm_stricmp(c_str(), x);
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2004-06-27 23:58:41 +00:00
|
|
|
#pragma mark -
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
String operator+(const String &x, const String &y) {
|
2004-06-27 23:58:41 +00:00
|
|
|
String temp(x);
|
|
|
|
temp += y;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
String operator+(const char *x, const String &y) {
|
2004-06-27 23:58:41 +00:00
|
|
|
String temp(x);
|
|
|
|
temp += y;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
String operator+(const String &x, const char *y) {
|
2004-06-27 23:58:41 +00:00
|
|
|
String temp(x);
|
|
|
|
temp += y;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
String operator+(char x, const String &y) {
|
2007-06-02 12:42:40 +00:00
|
|
|
String temp(x);
|
|
|
|
temp += y;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2010-01-31 10:22:09 +00:00
|
|
|
String operator+(const String &x, char y) {
|
2007-06-02 12:42:40 +00:00
|
|
|
String temp(x);
|
|
|
|
temp += y;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2007-02-13 21:06:57 +00:00
|
|
|
char *ltrim(char *t) {
|
2012-01-28 00:15:49 +00:00
|
|
|
while (isSpace(*t))
|
2007-02-13 21:06:57 +00:00
|
|
|
t++;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *rtrim(char *t) {
|
|
|
|
int l = strlen(t) - 1;
|
2012-01-28 00:15:49 +00:00
|
|
|
while (l >= 0 && isSpace(t[l]))
|
2007-02-13 21:06:57 +00:00
|
|
|
t[l--] = 0;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *trim(char *t) {
|
|
|
|
return rtrim(ltrim(t));
|
|
|
|
}
|
2002-09-08 01:08:12 +00:00
|
|
|
|
2011-06-03 15:33:07 +00:00
|
|
|
String lastPathComponent(const String &path, const char sep) {
|
2008-09-02 11:32:38 +00:00
|
|
|
const char *str = path.c_str();
|
|
|
|
const char *last = str + path.size();
|
|
|
|
|
|
|
|
// Skip over trailing slashes
|
|
|
|
while (last > str && *(last-1) == sep)
|
|
|
|
--last;
|
|
|
|
|
|
|
|
// Path consisted of only slashes -> return empty string
|
|
|
|
if (last == str)
|
2011-06-03 15:33:07 +00:00
|
|
|
return String();
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2008-09-02 11:32:38 +00:00
|
|
|
// Now scan the whole component
|
|
|
|
const char *first = last - 1;
|
2010-08-16 16:01:31 +00:00
|
|
|
while (first > str && *first != sep)
|
2008-09-02 11:32:38 +00:00
|
|
|
--first;
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2008-09-02 11:32:38 +00:00
|
|
|
if (*first == sep)
|
|
|
|
first++;
|
|
|
|
|
2011-06-03 15:33:07 +00:00
|
|
|
return String(first, last);
|
2008-09-02 11:32:38 +00:00
|
|
|
}
|
|
|
|
|
2011-06-03 15:33:07 +00:00
|
|
|
String normalizePath(const String &path, const char sep) {
|
2008-09-02 11:32:38 +00:00
|
|
|
if (path.empty())
|
|
|
|
return path;
|
|
|
|
|
|
|
|
const char *cur = path.c_str();
|
2011-06-03 15:33:07 +00:00
|
|
|
String result;
|
2008-09-02 11:32:38 +00:00
|
|
|
|
|
|
|
// If there is a leading slash, preserve that:
|
|
|
|
if (*cur == sep) {
|
|
|
|
result += sep;
|
2011-06-03 15:33:07 +00:00
|
|
|
// Skip over multiple leading slashes, so "//" equals "/"
|
2008-09-02 11:32:38 +00:00
|
|
|
while (*cur == sep)
|
|
|
|
++cur;
|
|
|
|
}
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2011-06-03 15:33:07 +00:00
|
|
|
// Scan for path components till the end of the String
|
|
|
|
List<String> comps;
|
2008-09-02 11:32:38 +00:00
|
|
|
while (*cur != 0) {
|
|
|
|
const char *start = cur;
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2008-09-02 11:32:38 +00:00
|
|
|
// Scan till the next path separator resp. the end of the string
|
|
|
|
while (*cur != sep && *cur != 0)
|
|
|
|
cur++;
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2011-06-03 15:33:07 +00:00
|
|
|
const String component(start, cur);
|
2008-09-02 11:32:38 +00:00
|
|
|
|
2011-06-03 15:33:07 +00:00
|
|
|
if (component.empty() || component == ".") {
|
|
|
|
// Skip empty components and dot components
|
|
|
|
} else if (!comps.empty() && component == ".." && comps.back() != "..") {
|
|
|
|
// If stack is non-empty and top is not "..", remove top
|
|
|
|
comps.pop_back();
|
|
|
|
} else {
|
|
|
|
// Add the component to the stack
|
|
|
|
comps.push_back(component);
|
2008-09-02 11:32:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip over separator chars
|
|
|
|
while (*cur == sep)
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
|
2011-06-03 15:33:07 +00:00
|
|
|
// Finally, assemble all components back into a path
|
|
|
|
while (!comps.empty()) {
|
|
|
|
result += comps.front();
|
|
|
|
comps.pop_front();
|
|
|
|
if (!comps.empty())
|
|
|
|
result += sep;
|
|
|
|
}
|
|
|
|
|
2008-09-02 11:32:38 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-09-23 00:14:06 +00:00
|
|
|
bool matchString(const char *str, const char *pat, bool ignoreCase, bool pathMode) {
|
2008-09-05 20:07:34 +00:00
|
|
|
assert(str);
|
|
|
|
assert(pat);
|
|
|
|
|
|
|
|
const char *p = 0;
|
|
|
|
const char *q = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
2009-02-15 18:45:53 +00:00
|
|
|
if (pathMode && *str == '/') {
|
|
|
|
p = 0;
|
|
|
|
q = 0;
|
|
|
|
if (*pat == '?')
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-09-05 20:07:34 +00:00
|
|
|
switch (*pat) {
|
|
|
|
case '*':
|
2010-09-07 11:40:44 +00:00
|
|
|
if (*str) {
|
|
|
|
// Record pattern / string position for backtracking
|
|
|
|
p = ++pat;
|
|
|
|
q = str;
|
|
|
|
} else {
|
|
|
|
// If we've reached the end of str, we can't backtrack further
|
|
|
|
// 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;
|
|
|
|
}
|
2008-09-05 20:29:03 +00:00
|
|
|
// If pattern ended with * -> match
|
|
|
|
if (!*pat)
|
|
|
|
return true;
|
2008-09-05 20:07:34 +00:00
|
|
|
break;
|
|
|
|
|
2016-01-26 01:20:52 +00:00
|
|
|
case '#':
|
|
|
|
if (!isDigit(*str))
|
|
|
|
return false;
|
|
|
|
pat++;
|
|
|
|
str++;
|
|
|
|
break;
|
|
|
|
|
2008-09-05 20:07:34 +00:00
|
|
|
default:
|
2009-09-23 00:14:06 +00:00
|
|
|
if ((!ignoreCase && *pat != *str) ||
|
|
|
|
(ignoreCase && tolower(*pat) != tolower(*str))) {
|
2008-09-05 20:07:34 +00:00
|
|
|
if (p) {
|
2008-09-05 20:29:03 +00:00
|
|
|
// No match, oops -> try to backtrack
|
2008-09-05 20:07:34 +00:00
|
|
|
pat = p;
|
|
|
|
str = ++q;
|
|
|
|
if (!*str)
|
|
|
|
return !*pat;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// fallthrough
|
|
|
|
case '?':
|
|
|
|
if (!*str)
|
|
|
|
return !*pat;
|
|
|
|
pat++;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 09:37:13 +00:00
|
|
|
void replace(Common::String &source, const Common::String &what, const Common::String &with) {
|
|
|
|
const char *cstr = source.c_str();
|
|
|
|
const char *position = strstr(cstr, what.c_str());
|
|
|
|
if (position) {
|
|
|
|
uint32 index = position - cstr;
|
|
|
|
source.replace(index, what.size(), with);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-18 15:07:48 +00:00
|
|
|
String tag2string(uint32 tag) {
|
|
|
|
char str[5];
|
|
|
|
str[0] = (char)(tag >> 24);
|
|
|
|
str[1] = (char)(tag >> 16);
|
|
|
|
str[2] = (char)(tag >> 8);
|
|
|
|
str[3] = (char)tag;
|
|
|
|
str[4] = '\0';
|
|
|
|
// Replace non-printable chars by dot
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
2012-12-13 19:52:09 +00:00
|
|
|
if (!Common::isPrint(str[i]))
|
2010-03-18 15:07:48 +00:00
|
|
|
str[i] = '.';
|
|
|
|
}
|
2011-06-03 15:33:07 +00:00
|
|
|
return String(str);
|
2010-03-18 15:07:48 +00:00
|
|
|
}
|
|
|
|
|
2010-05-05 17:52:59 +00:00
|
|
|
size_t strlcpy(char *dst, const char *src, size_t size) {
|
|
|
|
// Our backup of the source's start, we need this
|
|
|
|
// to calculate the source's length.
|
|
|
|
const char * const srcStart = src;
|
|
|
|
|
|
|
|
// In case a non-empty size was specified we
|
|
|
|
// copy over (size - 1) bytes at max.
|
|
|
|
if (size != 0) {
|
|
|
|
// Copy over (size - 1) bytes at max.
|
|
|
|
while (--size != 0) {
|
|
|
|
if ((*dst++ = *src) == 0)
|
|
|
|
break;
|
|
|
|
++src;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case the source string was longer than the
|
|
|
|
// destination, we need to add a terminating
|
|
|
|
// zero.
|
|
|
|
if (size == 0)
|
|
|
|
*dst = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move to the terminating zero of the source
|
2012-01-25 14:33:59 +00:00
|
|
|
// string, we need this to determine the length
|
2010-05-05 17:52:59 +00:00
|
|
|
// of the source string.
|
|
|
|
while (*src)
|
|
|
|
++src;
|
|
|
|
|
|
|
|
// Return the source string's length.
|
|
|
|
return src - srcStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t strlcat(char *dst, const char *src, size_t size) {
|
|
|
|
// In case the destination buffer does not contain
|
|
|
|
// space for at least 1 character, we will just
|
|
|
|
// return the source string's length.
|
|
|
|
if (size == 0)
|
|
|
|
return strlen(src);
|
|
|
|
|
|
|
|
// Our backup of the source's start, we need this
|
|
|
|
// to calculate the source's length.
|
|
|
|
const char * const srcStart = src;
|
|
|
|
|
|
|
|
// Our backup of the destination's start, we need
|
|
|
|
// this to calculate the destination's length.
|
|
|
|
const char * const dstStart = dst;
|
|
|
|
|
|
|
|
// Search the end of the destination, but do not
|
|
|
|
// move past the terminating zero.
|
|
|
|
while (size-- != 0 && *dst != 0)
|
|
|
|
++dst;
|
|
|
|
|
|
|
|
// Calculate the destination's length;
|
|
|
|
const size_t dstLength = dst - dstStart;
|
|
|
|
|
|
|
|
// In case we reached the end of the destination
|
|
|
|
// buffer before we had a chance to append any
|
|
|
|
// characters we will just return the destination
|
|
|
|
// length plus the source string's length.
|
|
|
|
if (size == 0)
|
|
|
|
return dstLength + strlen(srcStart);
|
|
|
|
|
|
|
|
// Copy over all of the source that fits
|
|
|
|
// the destination buffer. We also need
|
|
|
|
// to take the terminating zero we will
|
|
|
|
// add into consideration.
|
|
|
|
while (size-- != 0 && *src != 0)
|
|
|
|
*dst++ = *src++;
|
|
|
|
*dst = 0;
|
|
|
|
|
|
|
|
// Move to the terminating zero of the source
|
2012-01-25 14:33:59 +00:00
|
|
|
// string, we need this to determine the length
|
2010-05-05 17:52:59 +00:00
|
|
|
// of the source string.
|
|
|
|
while (*src)
|
|
|
|
++src;
|
|
|
|
|
|
|
|
// Return the total length of the result string
|
|
|
|
return dstLength + (src - srcStart);
|
|
|
|
}
|
|
|
|
|
2016-11-26 18:56:25 +00:00
|
|
|
size_t strnlen(const char *src, size_t maxSize) {
|
|
|
|
size_t counter = 0;
|
|
|
|
while (counter != maxSize && *src++)
|
|
|
|
++counter;
|
|
|
|
return counter;
|
|
|
|
}
|
|
|
|
|
2013-01-26 18:33:27 +00:00
|
|
|
} // End of namespace Common
|
2011-05-23 16:32:42 +00:00
|
|
|
|
|
|
|
// Portable implementation of stricmp / strcasecmp / strcmpi.
|
|
|
|
// TODO: Rename this to Common::strcasecmp
|
|
|
|
int scumm_stricmp(const char *s1, const char *s2) {
|
|
|
|
byte l1, l2;
|
|
|
|
do {
|
|
|
|
// Don't use ++ inside tolower, in case the macro uses its
|
|
|
|
// arguments more than once.
|
|
|
|
l1 = (byte)*s1++;
|
|
|
|
l1 = tolower(l1);
|
|
|
|
l2 = (byte)*s2++;
|
|
|
|
l2 = tolower(l2);
|
|
|
|
} while (l1 == l2 && l1 != 0);
|
|
|
|
return l1 - l2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Portable implementation of strnicmp / strncasecmp / strncmpi.
|
|
|
|
// TODO: Rename this to Common::strncasecmp
|
|
|
|
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
|
|
|
|
|
|
|
|
// Don't use ++ inside tolower, in case the macro uses its
|
|
|
|
// arguments more than once.
|
|
|
|
l1 = (byte)*s1++;
|
|
|
|
l1 = tolower(l1);
|
|
|
|
l2 = (byte)*s2++;
|
|
|
|
l2 = tolower(l2);
|
|
|
|
} while (l1 == l2 && l1 != 0);
|
|
|
|
return l1 - l2;
|
|
|
|
}
|