| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666 |
- //
- // Urho3D Engine
- // Copyright (c) 2008-2012 Lasse Öörni
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "StringBase.h"
- #include "Swap.h"
- #include <cstdio>
- char String::endZero = 0;
- String::String(int value) :
- length_(0),
- capacity_(0),
- buffer_(&endZero)
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%d", value);
- *this = tempBuffer;
- }
- String::String(short value) :
- length_(0),
- capacity_(0),
- buffer_(&endZero)
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%d", value);
- *this = tempBuffer;
- }
- String::String(unsigned value) :
- length_(0),
- capacity_(0),
- buffer_(&endZero)
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%u", value);
- *this = tempBuffer;
- }
- String::String(unsigned short value) :
- length_(0),
- capacity_(0),
- buffer_(&endZero)
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%u", value);
- *this = tempBuffer;
- }
- String::String(float value) :
- length_(0),
- capacity_(0),
- buffer_(&endZero)
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%g", value);
- *this = tempBuffer;
- }
- String::String(double value) :
- length_(0),
- capacity_(0),
- buffer_(&endZero)
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%g", value);
- *this = tempBuffer;
- }
- String::String(bool value) :
- length_(0),
- capacity_(0),
- buffer_(&endZero)
- {
- if (value)
- *this = "true";
- else
- *this = "false";
- }
- String::String(char value) :
- length_(0),
- capacity_(0),
- buffer_(&endZero)
- {
- Resize(1);
- buffer_[0] = value;
- }
- String::String(char value, unsigned length) :
- length_(0),
- capacity_(0),
- buffer_(&endZero)
- {
- Resize(length);
- for (unsigned i = 0; i < length; ++i)
- buffer_[i] = value;
- }
- String& String::operator += (int rhs)
- {
- return *this += String(rhs);
- }
- String& String::operator += (short rhs)
- {
- return *this += String(rhs);
- }
- String& String::operator += (unsigned rhs)
- {
- return *this += String(rhs);
- }
- String& String::operator += (unsigned short rhs)
- {
- return *this += String(rhs);
- }
- String& String::operator += (float rhs)
- {
- return *this += String(rhs);
- }
- String& String::operator += (bool rhs)
- {
- return *this += String(rhs);
- }
- void String::Replace(char replaceThis, char replaceWith)
- {
- for (unsigned i = 0; i < length_; ++i)
- {
- if (buffer_[i] == replaceThis)
- buffer_[i] = replaceWith;
- }
- }
- void String::Replace(const String& replaceThis, const String& replaceWith)
- {
- unsigned nextPos = 0;
-
- while (nextPos < length_)
- {
- unsigned pos = Find(replaceThis, nextPos);
- if (pos == NPOS)
- break;
- Replace(pos, replaceThis.length_, replaceWith);
- nextPos = pos + replaceWith.length_;
- }
- }
- void String::Replace(unsigned pos, unsigned length, const String& str)
- {
- // If substring is illegal, do nothing
- if (pos + length > length_)
- return;
-
- Replace(pos, length, str.buffer_, str.length_);
- }
- String::Iterator String::Replace(const String::Iterator& start, const String::Iterator& end, const String& replaceWith)
- {
- unsigned pos = start - Begin();
- if (pos >= length_)
- return End();
- unsigned length = end - start;
- Replace(pos, length, replaceWith);
-
- return Begin() + pos;
- }
- String String::Replaced(char replaceThis, char replaceWith) const
- {
- String ret(*this);
- ret.Replace(replaceThis, replaceWith);
- return ret;
- }
- String String::Replaced(const String& replaceThis, const String& replaceWith) const
- {
- String ret(*this);
- ret.Replace(replaceThis, replaceWith);
- return ret;
- }
- void String::Append(const String& str)
- {
- *this += str;
- }
- void String::Append(const char* str)
- {
- *this += str;
- }
- void String::Append(char c)
- {
- *this += c;
- }
- void String::Append(const char* str, unsigned length)
- {
- if (!str)
- return;
-
- unsigned oldLength = length_;
- Resize(oldLength + length);
- CopyChars(&buffer_[oldLength], str, length);
- }
- void String::Insert(unsigned pos, const String& str)
- {
- if (pos > length_)
- pos = length_;
-
- if (pos == length_)
- (*this) += str;
- else
- Replace(pos, 0, str);
- }
- void String::Insert(unsigned pos, char c)
- {
- if (pos > length_)
- pos = length_;
-
- if (pos == length_)
- (*this) += c;
- else
- {
- unsigned oldLength = length_;
- Resize(length_ + 1);
- MoveRange(pos + 1, pos, oldLength - pos);
- buffer_[pos] = c;
- }
- }
- String::Iterator String::Insert(const String::Iterator& dest, const String& str)
- {
- unsigned pos = dest - Begin();
- if (pos > length_)
- pos = length_;
- Insert(pos, str);
-
- return Begin() + pos;
- }
- String::Iterator String::Insert(const String::Iterator& dest, const String::Iterator& start, const String::Iterator& end)
- {
- unsigned pos = dest - Begin();
- if (pos > length_)
- pos = length_;
- unsigned length = end - start;
- Replace(pos, 0, &(*start), length);
-
- return Begin() + pos;
- }
- String::Iterator String::Insert(const String::Iterator& dest, char c)
- {
- unsigned pos = dest - Begin();
- if (pos > length_)
- pos = length_;
- Insert(pos, c);
-
- return Begin() + pos;
- }
- void String::Erase(unsigned pos, unsigned length)
- {
- Replace(pos, length, String());
- }
- String::Iterator String::Erase(const String::Iterator& it)
- {
- unsigned pos = it - Begin();
- if (pos >= length_)
- return End();
- Erase(pos);
-
- return Begin() + pos;
- }
- String::Iterator String::Erase(const String::Iterator& start, const String::Iterator& end)
- {
- unsigned pos = start - Begin();
- if (pos >= length_)
- return End();
- unsigned length = end - start;
- Erase(pos, length);
-
- return Begin() + pos;
- }
- void String::Resize(unsigned newLength)
- {
- if (!capacity_)
- {
- // Calculate initial capacity
- capacity_ = newLength + 1;
- if (capacity_ < MIN_CAPACITY)
- capacity_ = MIN_CAPACITY;
-
- buffer_ = new char[capacity_];
- }
- else
- {
- if (newLength && capacity_ < newLength + 1)
- {
- // Increase the capacity with half each time it is exceeded
- while (capacity_ < newLength + 1)
- capacity_ += (capacity_ + 1) >> 1;
-
- char* newBuffer = new char[capacity_];
- // Move the existing data to the new buffer, then delete the old buffer
- if (length_)
- CopyChars(newBuffer, buffer_, length_);
- delete[] buffer_;
-
- buffer_ = newBuffer;
- }
- }
-
- buffer_[newLength] = 0;
- length_ = newLength;
- }
- void String::Reserve(unsigned newCapacity)
- {
- if (newCapacity < length_ + 1)
- newCapacity = length_ + 1;
- if (newCapacity == capacity_)
- return;
-
- char* newBuffer = new char[newCapacity];
- // Move the existing data to the new buffer, then delete the old buffer
- CopyChars(newBuffer, buffer_, length_ + 1);
- if (capacity_)
- delete[] buffer_;
-
- capacity_ = newCapacity;
- buffer_ = newBuffer;
- }
- void String::Compact()
- {
- if (capacity_)
- Reserve(length_ + 1);
- }
- void String::Clear()
- {
- Resize(0);
- }
- void String::Swap(String& str)
- {
- ::Swap(length_, str.length_);
- ::Swap(capacity_, str.capacity_);
- ::Swap(buffer_, str.buffer_);
- }
- String String::Substring(unsigned pos) const
- {
- if (pos < length_)
- {
- String ret;
- ret.Resize(length_ - pos);
- CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
-
- return ret;
- }
- else
- return String();
- }
- String String::Substring(unsigned pos, unsigned length) const
- {
- if (pos < length_)
- {
- String ret;
- if (pos + length > length_)
- length = length_ - pos;
- ret.Resize(length);
- CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
-
- return ret;
- }
- else
- return String();
- }
- String String::Trimmed() const
- {
- unsigned trimStart = 0;
- unsigned trimEnd = length_;
-
- while (trimStart < trimEnd)
- {
- char c = buffer_[trimStart];
- if (c != ' ' && c != 9)
- break;
- ++trimStart;
- }
- while (trimEnd > trimStart)
- {
- char c = buffer_[trimEnd - 1];
- if (c != ' ' && c != 9)
- break;
- --trimEnd;
- }
-
- return Substring(trimStart, trimEnd - trimStart);
- }
- String String::ToLower() const
- {
- String ret(*this);
- for (unsigned i = 0; i < ret.length_; ++i)
- ret[i] = tolower(buffer_[i]);
-
- return ret;
- }
- String String::ToUpper() const
- {
- String ret(*this);
- for (unsigned i = 0; i < ret.length_; ++i)
- ret[i] = toupper(buffer_[i]);
-
- return ret;
- }
- Vector<String> String::Split(char separator) const
- {
- return Split(CString(), separator);
- }
- unsigned String::Find(char c, unsigned startPos) const
- {
- for (unsigned i = startPos; i < length_; ++i)
- {
- if (buffer_[i] == c)
- return i;
- }
-
- return NPOS;
- }
- unsigned String::Find(const String& str, unsigned startPos) const
- {
- if (!str.length_ || str.length_ > length_)
- return NPOS;
-
- char first = str.buffer_[0];
-
- for (unsigned i = startPos; i <= length_ - str.length_; ++i)
- {
- if (buffer_[i] == first)
- {
- unsigned skip = NPOS;
- bool found = true;
- for (unsigned j = 1; j < str.length_; ++j)
- {
- char c = buffer_[i + j];
- if (skip == NPOS && c == first)
- skip = i + j - 1;
- if (c != str.buffer_[j])
- {
- found = false;
- if (skip != NPOS)
- i = skip;
- break;
- }
- }
- if (found)
- return i;
- }
- }
-
- return NPOS;
- }
- unsigned String::FindLast(char c, unsigned startPos) const
- {
- if (startPos >= length_)
- startPos = length_ - 1;
-
- for (unsigned i = startPos; i < length_; --i)
- {
- if (buffer_[i] == c)
- return i;
- }
-
- return NPOS;
- }
- unsigned String::FindLast(const String& str, unsigned startPos) const
- {
- if (!str.length_ || str.length_ > length_)
- return NPOS;
- if (startPos > length_ - str.length_)
- startPos = length_ - str.length_;
-
- char first = str.buffer_[0];
-
- for (unsigned i = startPos; i < length_; --i)
- {
- if (buffer_[i] == first)
- {
- bool found = true;
- for (unsigned j = 1; j < str.length_; ++j)
- {
- char c = buffer_[i + j];
- if (c != str.buffer_[j])
- {
- found = false;
- break;
- }
- }
- if (found)
- return i;
- }
- }
-
- return NPOS;
- }
- bool String::StartsWith(const String& str) const
- {
- return Find(str) == 0;
- }
- bool String::EndsWith(const String& str) const
- {
- return Find(str) == Length() - str.Length();
- }
- int String::Compare(const String& str, bool caseSensitive) const
- {
- return Compare(str.CString(), caseSensitive);
- }
- int String::Compare(const char* str, bool caseSensitive) const
- {
- const char* lhs = CString();
- const char* rhs = str;
-
- if (caseSensitive)
- return strcmp(lhs, rhs);
- else
- {
- if (!lhs || !rhs)
- return lhs ? 1 : (rhs ? -1 : 0);
-
- for (;;)
- {
- char l = tolower(*lhs);
- char r = tolower(*rhs);
- if (!l || !r)
- return l ? 1 : (r ? -1 : 0);
- if (l < r)
- return -1;
- if (l > r)
- return 1;
-
- ++lhs;
- ++rhs;
- }
- }
- }
- Vector<String> String::Split(const char* str, char separator)
- {
- Vector<String> ret;
- unsigned pos = 0;
- unsigned length = CStringLength(str);
-
- while (pos < length)
- {
- if (str[pos] != separator)
- break;
- ++pos;
- }
-
- while (pos < length)
- {
- unsigned start = pos;
-
- while (start < length)
- {
- if (str[start] == separator)
- break;
-
- ++start;
- }
-
- if (start == length)
- {
- ret.Push(String(&str[pos]));
- break;
- }
-
- unsigned end = start;
-
- while (end < length)
- {
- if (str[end] != separator)
- break;
-
- ++end;
- }
-
- ret.Push(String(&str[pos], start - pos));
- pos = end;
- }
-
- return ret;
- }
- void String::Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength)
- {
- int delta = (int)srcLength - (int)length;
-
- if (pos + length < length_)
- {
- if (delta < 0)
- {
- MoveRange(pos + srcLength, pos + length, length_ - pos - length);
- Resize(length_ + delta);
- }
- if (delta > 0)
- {
- Resize(length_ + delta);
- MoveRange(pos + srcLength, pos + length, length_ - pos - length);
- }
- }
- else
- Resize(length_ + delta);
-
- CopyChars(buffer_ + pos, srcStart, srcLength);
- }
|