| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303 |
- //
- // Copyright (c) 2008-2017 the Urho3D project.
- //
- // 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 "../Precompiled.h"
- #include "../IO/Log.h"
- #include <cstdio>
- #include "../DebugNew.h"
- #ifdef _MSC_VER
- #pragma warning(disable:6293)
- #endif
- namespace Urho3D
- {
- char String::endZero = 0;
- const String String::EMPTY;
- String::String(const WString& str) :
- length_(0),
- capacity_(0),
- buffer_(&endZero)
- {
- SetUTF8FromWChar(str.CString());
- }
- 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(long value) :
- length_(0),
- capacity_(0),
- buffer_(&endZero)
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%ld", value);
- *this = tempBuffer;
- }
- String::String(long long value) :
- length_(0),
- capacity_(0),
- buffer_(&endZero)
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%lld", 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(unsigned long value) :
- length_(0),
- capacity_(0),
- buffer_(&endZero)
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%lu", value);
- *this = tempBuffer;
- }
- String::String(unsigned long long value) :
- length_(0),
- capacity_(0),
- buffer_(&endZero)
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%llu", 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, "%.15g", 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 +=(long rhs)
- {
- return *this += String(rhs);
- }
- String& String::operator +=(long long 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 +=(unsigned long rhs)
- {
- return *this += String(rhs);
- }
- String& String::operator +=(unsigned long long 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, bool caseSensitive)
- {
- if (caseSensitive)
- {
- for (unsigned i = 0; i < length_; ++i)
- {
- if (buffer_[i] == replaceThis)
- buffer_[i] = replaceWith;
- }
- }
- else
- {
- replaceThis = (char)tolower(replaceThis);
- for (unsigned i = 0; i < length_; ++i)
- {
- if (tolower(buffer_[i]) == replaceThis)
- buffer_[i] = replaceWith;
- }
- }
- }
- void String::Replace(const String& replaceThis, const String& replaceWith, bool caseSensitive)
- {
- unsigned nextPos = 0;
- while (nextPos < length_)
- {
- unsigned pos = Find(replaceThis, nextPos, caseSensitive);
- if (pos == NPOS)
- break;
- Replace(pos, replaceThis.length_, replaceWith);
- nextPos = pos + replaceWith.length_;
- }
- }
- void String::Replace(unsigned pos, unsigned length, const String& replaceWith)
- {
- // If substring is illegal, do nothing
- if (pos + length > length_)
- return;
- Replace(pos, length, replaceWith.buffer_, replaceWith.length_);
- }
- void String::Replace(unsigned pos, unsigned length, const char* replaceWith)
- {
- // If substring is illegal, do nothing
- if (pos + length > length_)
- return;
- Replace(pos, length, replaceWith, CStringLength(replaceWith));
- }
- String::Iterator String::Replace(const String::Iterator& start, const String::Iterator& end, const String& replaceWith)
- {
- unsigned pos = (unsigned)(start - Begin());
- if (pos >= length_)
- return End();
- auto length = (unsigned)(end - start);
- Replace(pos, length, replaceWith);
- return Begin() + pos;
- }
- String String::Replaced(char replaceThis, char replaceWith, bool caseSensitive) const
- {
- String ret(*this);
- ret.Replace(replaceThis, replaceWith, caseSensitive);
- return ret;
- }
- String String::Replaced(const String& replaceThis, const String& replaceWith, bool caseSensitive) const
- {
- String ret(*this);
- ret.Replace(replaceThis, replaceWith, caseSensitive);
- return ret;
- }
- String& String::Append(const String& str)
- {
- return *this += str;
- }
- String& String::Append(const char* str)
- {
- return *this += str;
- }
- String& String::Append(char c)
- {
- return *this += c;
- }
- String& String::Append(const char* str, unsigned length)
- {
- if (str)
- {
- unsigned oldLength = length_;
- Resize(oldLength + length);
- CopyChars(&buffer_[oldLength], str, length);
- }
- return *this;
- }
- 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 = (unsigned)(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 = (unsigned)(dest - Begin());
- if (pos > length_)
- pos = length_;
- auto length = (unsigned)(end - start);
- Replace(pos, 0, &(*start), length);
- return Begin() + pos;
- }
- String::Iterator String::Insert(const String::Iterator& dest, char c)
- {
- unsigned pos = (unsigned)(dest - Begin());
- if (pos > length_)
- pos = length_;
- Insert(pos, c);
- return Begin() + pos;
- }
- void String::Erase(unsigned pos, unsigned length)
- {
- Replace(pos, length, String::EMPTY);
- }
- String::Iterator String::Erase(const String::Iterator& it)
- {
- unsigned pos = (unsigned)(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 = (unsigned)(start - Begin());
- if (pos >= length_)
- return End();
- auto length = (unsigned)(end - start);
- Erase(pos, length);
- return Begin() + pos;
- }
- void String::Resize(unsigned newLength)
- {
- if (!capacity_)
- {
- // If zero length requested, do not allocate buffer yet
- if (!newLength)
- return;
- // 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;
- auto* 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;
- auto* 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)
- {
- Urho3D::Swap(length_, str.length_);
- Urho3D::Swap(capacity_, str.capacity_);
- Urho3D::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] = (char)tolower(buffer_[i]);
- return ret;
- }
- String String::ToUpper() const
- {
- String ret(*this);
- for (unsigned i = 0; i < ret.length_; ++i)
- ret[i] = (char)toupper(buffer_[i]);
- return ret;
- }
- Vector<String> String::Split(char separator, bool keepEmptyStrings) const
- {
- return Split(CString(), separator, keepEmptyStrings);
- }
- void String::Join(const Vector<String>& subStrings, const String& glue)
- {
- *this = Joined(subStrings, glue);
- }
- unsigned String::Find(char c, unsigned startPos, bool caseSensitive) const
- {
- if (caseSensitive)
- {
- for (unsigned i = startPos; i < length_; ++i)
- {
- if (buffer_[i] == c)
- return i;
- }
- }
- else
- {
- c = (char)tolower(c);
- for (unsigned i = startPos; i < length_; ++i)
- {
- if (tolower(buffer_[i]) == c)
- return i;
- }
- }
- return NPOS;
- }
- unsigned String::Find(const String& str, unsigned startPos, bool caseSensitive) const
- {
- if (!str.length_ || str.length_ > length_)
- return NPOS;
- char first = str.buffer_[0];
- if (!caseSensitive)
- first = (char)tolower(first);
- for (unsigned i = startPos; i <= length_ - str.length_; ++i)
- {
- char c = buffer_[i];
- if (!caseSensitive)
- c = (char)tolower(c);
- if (c == first)
- {
- unsigned skip = NPOS;
- bool found = true;
- for (unsigned j = 1; j < str.length_; ++j)
- {
- c = buffer_[i + j];
- char d = str.buffer_[j];
- if (!caseSensitive)
- {
- c = (char)tolower(c);
- d = (char)tolower(d);
- }
- if (skip == NPOS && c == first)
- skip = i + j - 1;
- if (c != d)
- {
- found = false;
- if (skip != NPOS)
- i = skip;
- break;
- }
- }
- if (found)
- return i;
- }
- }
- return NPOS;
- }
- unsigned String::FindLast(char c, unsigned startPos, bool caseSensitive) const
- {
- if (startPos >= length_)
- startPos = length_ - 1;
- if (caseSensitive)
- {
- for (unsigned i = startPos; i < length_; --i)
- {
- if (buffer_[i] == c)
- return i;
- }
- }
- else
- {
- c = (char)tolower(c);
- for (unsigned i = startPos; i < length_; --i)
- {
- if (tolower(buffer_[i]) == c)
- return i;
- }
- }
- return NPOS;
- }
- unsigned String::FindLast(const String& str, unsigned startPos, bool caseSensitive) const
- {
- if (!str.length_ || str.length_ > length_)
- return NPOS;
- if (startPos > length_ - str.length_)
- startPos = length_ - str.length_;
- char first = str.buffer_[0];
- if (!caseSensitive)
- first = (char)tolower(first);
- for (unsigned i = startPos; i < length_; --i)
- {
- char c = buffer_[i];
- if (!caseSensitive)
- c = (char)tolower(c);
- if (c == first)
- {
- bool found = true;
- for (unsigned j = 1; j < str.length_; ++j)
- {
- c = buffer_[i + j];
- char d = str.buffer_[j];
- if (!caseSensitive)
- {
- c = (char)tolower(c);
- d = (char)tolower(d);
- }
- if (c != d)
- {
- found = false;
- break;
- }
- }
- if (found)
- return i;
- }
- }
- return NPOS;
- }
- bool String::StartsWith(const String& str, bool caseSensitive) const
- {
- return Find(str, 0, caseSensitive) == 0;
- }
- bool String::EndsWith(const String& str, bool caseSensitive) const
- {
- unsigned pos = FindLast(str, Length() - 1, caseSensitive);
- return pos != NPOS && pos == Length() - str.Length();
- }
- int String::Compare(const String& str, bool caseSensitive) const
- {
- return Compare(CString(), str.CString(), caseSensitive);
- }
- int String::Compare(const char* str, bool caseSensitive) const
- {
- return Compare(CString(), str, caseSensitive);
- }
- void String::SetUTF8FromLatin1(const char* str)
- {
- char temp[7];
- Clear();
- if (!str)
- return;
- while (*str)
- {
- char* dest = temp;
- EncodeUTF8(dest, (unsigned)*str++);
- *dest = 0;
- Append(temp);
- }
- }
- void String::SetUTF8FromWChar(const wchar_t* str)
- {
- char temp[7];
- Clear();
- if (!str)
- return;
- #ifdef _WIN32
- while (*str)
- {
- unsigned unicodeChar = DecodeUTF16(str);
- char* dest = temp;
- EncodeUTF8(dest, unicodeChar);
- *dest = 0;
- Append(temp);
- }
- #else
- while (*str)
- {
- char* dest = temp;
- EncodeUTF8(dest, (unsigned)*str++);
- *dest = 0;
- Append(temp);
- }
- #endif
- }
- unsigned String::LengthUTF8() const
- {
- unsigned ret = 0;
- const char* src = buffer_;
- if (!src)
- return ret;
- const char* end = buffer_ + length_;
- while (src < end)
- {
- DecodeUTF8(src);
- ++ret;
- }
- return ret;
- }
- unsigned String::ByteOffsetUTF8(unsigned index) const
- {
- unsigned byteOffset = 0;
- unsigned utfPos = 0;
- while (utfPos < index && byteOffset < length_)
- {
- NextUTF8Char(byteOffset);
- ++utfPos;
- }
- return byteOffset;
- }
- unsigned String::NextUTF8Char(unsigned& byteOffset) const
- {
- if (!buffer_)
- return 0;
- const char* src = buffer_ + byteOffset;
- unsigned ret = DecodeUTF8(src);
- byteOffset = (unsigned)(src - buffer_);
- return ret;
- }
- unsigned String::AtUTF8(unsigned index) const
- {
- unsigned byteOffset = ByteOffsetUTF8(index);
- return NextUTF8Char(byteOffset);
- }
- void String::ReplaceUTF8(unsigned index, unsigned unicodeChar)
- {
- unsigned utfPos = 0;
- unsigned byteOffset = 0;
- while (utfPos < index && byteOffset < length_)
- {
- NextUTF8Char(byteOffset);
- ++utfPos;
- }
- if (utfPos < index)
- return;
- unsigned beginCharPos = byteOffset;
- NextUTF8Char(byteOffset);
- char temp[7];
- char* dest = temp;
- EncodeUTF8(dest, unicodeChar);
- *dest = 0;
- Replace(beginCharPos, byteOffset - beginCharPos, temp, (unsigned)(dest - temp));
- }
- String& String::AppendUTF8(unsigned unicodeChar)
- {
- char temp[7];
- char* dest = temp;
- EncodeUTF8(dest, unicodeChar);
- *dest = 0;
- return Append(temp);
- }
- String String::SubstringUTF8(unsigned pos) const
- {
- unsigned utf8Length = LengthUTF8();
- unsigned byteOffset = ByteOffsetUTF8(pos);
- String ret;
- while (pos < utf8Length)
- {
- ret.AppendUTF8(NextUTF8Char(byteOffset));
- ++pos;
- }
- return ret;
- }
- String String::SubstringUTF8(unsigned pos, unsigned length) const
- {
- unsigned utf8Length = LengthUTF8();
- unsigned byteOffset = ByteOffsetUTF8(pos);
- unsigned endPos = pos + length;
- String ret;
- while (pos < endPos && pos < utf8Length)
- {
- ret.AppendUTF8(NextUTF8Char(byteOffset));
- ++pos;
- }
- return ret;
- }
- void String::EncodeUTF8(char*& dest, unsigned unicodeChar)
- {
- if (unicodeChar < 0x80)
- *dest++ = unicodeChar;
- else if (unicodeChar < 0x800)
- {
- dest[0] = (char)(0xc0 | ((unicodeChar >> 6) & 0x1f));
- dest[1] = (char)(0x80 | (unicodeChar & 0x3f));
- dest += 2;
- }
- else if (unicodeChar < 0x10000)
- {
- dest[0] = (char)(0xe0 | ((unicodeChar >> 12) & 0xf));
- dest[1] = (char)(0x80 | ((unicodeChar >> 6) & 0x3f));
- dest[2] = (char)(0x80 | (unicodeChar & 0x3f));
- dest += 3;
- }
- else if (unicodeChar < 0x200000)
- {
- dest[0] = (char)(0xf0 | ((unicodeChar >> 18) & 0x7));
- dest[1] = (char)(0x80 | ((unicodeChar >> 12) & 0x3f));
- dest[2] = (char)(0x80 | ((unicodeChar >> 6) & 0x3f));
- dest[3] = (char)(0x80 | (unicodeChar & 0x3f));
- dest += 4;
- }
- else if (unicodeChar < 0x4000000)
- {
- dest[0] = (char)(0xf8 | ((unicodeChar >> 24) & 0x3));
- dest[1] = (char)(0x80 | ((unicodeChar >> 18) & 0x3f));
- dest[2] = (char)(0x80 | ((unicodeChar >> 12) & 0x3f));
- dest[3] = (char)(0x80 | ((unicodeChar >> 6) & 0x3f));
- dest[4] = (char)(0x80 | (unicodeChar & 0x3f));
- dest += 5;
- }
- else
- {
- dest[0] = (char)(0xfc | ((unicodeChar >> 30) & 0x1));
- dest[1] = (char)(0x80 | ((unicodeChar >> 24) & 0x3f));
- dest[2] = (char)(0x80 | ((unicodeChar >> 18) & 0x3f));
- dest[3] = (char)(0x80 | ((unicodeChar >> 12) & 0x3f));
- dest[4] = (char)(0x80 | ((unicodeChar >> 6) & 0x3f));
- dest[5] = (char)(0x80 | (unicodeChar & 0x3f));
- dest += 6;
- }
- }
- #define GET_NEXT_CONTINUATION_BYTE(ptr) *ptr; if ((unsigned char)*ptr < 0x80 || (unsigned char)*ptr >= 0xc0) return '?'; else ++ptr;
- unsigned String::DecodeUTF8(const char*& src)
- {
- if (src == nullptr)
- return 0;
- unsigned char char1 = *src++;
- // Check if we are in the middle of a UTF8 character
- if (char1 >= 0x80 && char1 < 0xc0)
- {
- while ((unsigned char)*src >= 0x80 && (unsigned char)*src < 0xc0)
- ++src;
- return '?';
- }
- if (char1 < 0x80)
- return char1;
- else if (char1 < 0xe0)
- {
- unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
- return (unsigned)((char2 & 0x3f) | ((char1 & 0x1f) << 6));
- }
- else if (char1 < 0xf0)
- {
- unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
- unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
- return (unsigned)((char3 & 0x3f) | ((char2 & 0x3f) << 6) | ((char1 & 0xf) << 12));
- }
- else if (char1 < 0xf8)
- {
- unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
- unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
- unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
- return (unsigned)((char4 & 0x3f) | ((char3 & 0x3f) << 6) | ((char2 & 0x3f) << 12) | ((char1 & 0x7) << 18));
- }
- else if (char1 < 0xfc)
- {
- unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
- unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
- unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
- unsigned char char5 = GET_NEXT_CONTINUATION_BYTE(src);
- return (unsigned)((char5 & 0x3f) | ((char4 & 0x3f) << 6) | ((char3 & 0x3f) << 12) | ((char2 & 0x3f) << 18) |
- ((char1 & 0x3) << 24));
- }
- else
- {
- unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
- unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
- unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
- unsigned char char5 = GET_NEXT_CONTINUATION_BYTE(src);
- unsigned char char6 = GET_NEXT_CONTINUATION_BYTE(src);
- return (unsigned)((char6 & 0x3f) | ((char5 & 0x3f) << 6) | ((char4 & 0x3f) << 12) | ((char3 & 0x3f) << 18) |
- ((char2 & 0x3f) << 24) | ((char1 & 0x1) << 30));
- }
- }
- #ifdef _WIN32
- void String::EncodeUTF16(wchar_t*& dest, unsigned unicodeChar)
- {
- if (unicodeChar < 0x10000)
- *dest++ = unicodeChar;
- else
- {
- unicodeChar -= 0x10000;
- *dest++ = 0xd800 | ((unicodeChar >> 10) & 0x3ff);
- *dest++ = 0xdc00 | (unicodeChar & 0x3ff);
- }
- }
- unsigned String::DecodeUTF16(const wchar_t*& src)
- {
- if (src == nullptr)
- return 0;
- unsigned short word1 = *src++;
- // Check if we are at a low surrogate
- if (word1 >= 0xdc00 && word1 < 0xe000)
- {
- while (*src >= 0xdc00 && *src < 0xe000)
- ++src;
- return '?';
- }
- if (word1 < 0xd800 || word1 >= 0xe000)
- return word1;
- else
- {
- unsigned short word2 = *src++;
- if (word2 < 0xdc00 || word2 >= 0xe000)
- {
- --src;
- return '?';
- }
- else
- return (((word1 & 0x3ff) << 10) | (word2 & 0x3ff)) + 0x10000;
- }
- }
- #endif
- Vector<String> String::Split(const char* str, char separator, bool keepEmptyStrings)
- {
- Vector<String> ret;
- const char* strEnd = str + String::CStringLength(str);
- for (const char* splitEnd = str; splitEnd != strEnd; ++splitEnd)
- {
- if (*splitEnd == separator)
- {
- const ptrdiff_t splitLen = splitEnd - str;
- if (splitLen > 0 || keepEmptyStrings)
- ret.Push(String(str, splitLen));
- str = splitEnd + 1;
- }
- }
- const ptrdiff_t splitLen = strEnd - str;
- if (splitLen > 0 || keepEmptyStrings)
- ret.Push(String(str, splitLen));
- return ret;
- }
- String String::Joined(const Vector<String>& subStrings, const String& glue)
- {
- if (subStrings.Empty())
- return String();
- String joinedString(subStrings[0]);
- for (unsigned i = 1; i < subStrings.Size(); ++i)
- joinedString.Append(glue).Append(subStrings[i]);
- return joinedString;
- }
- String& String::AppendWithFormat(const char* formatString, ...)
- {
- va_list args;
- va_start(args, formatString);
- AppendWithFormatArgs(formatString, args);
- va_end(args);
- return *this;
- }
- String& String::AppendWithFormatArgs(const char* formatString, va_list args)
- {
- int pos = 0, lastPos = 0;
- auto length = (int)strlen(formatString);
- while (true)
- {
- // Scan the format string and find %a argument where a is one of d, f, s ...
- while (pos < length && formatString[pos] != '%') pos++;
- Append(formatString + lastPos, (unsigned)(pos - lastPos));
- if (pos >= length)
- return *this;
- char format = formatString[pos + 1];
- pos += 2;
- lastPos = pos;
- switch (format)
- {
- // Integer
- case 'd':
- case 'i':
- {
- int arg = va_arg(args, int);
- Append(String(arg));
- break;
- }
- // Unsigned
- case 'u':
- {
- unsigned arg = va_arg(args, unsigned);
- Append(String(arg));
- break;
- }
- // Unsigned long
- case 'l':
- {
- unsigned long arg = va_arg(args, unsigned long);
- Append(String(arg));
- break;
- }
- // Real
- case 'f':
- {
- double arg = va_arg(args, double);
- Append(String(arg));
- break;
- }
- // Character
- case 'c':
- {
- int arg = va_arg(args, int);
- Append((char)arg);
- break;
- }
- // C string
- case 's':
- {
- char* arg = va_arg(args, char*);
- Append(arg);
- break;
- }
- // Hex
- case 'x':
- {
- char buf[CONVERSION_BUFFER_LENGTH];
- int arg = va_arg(args, int);
- int arglen = ::sprintf(buf, "%x", arg);
- Append(buf, (unsigned)arglen);
- break;
- }
- // Pointer
- case 'p':
- {
- char buf[CONVERSION_BUFFER_LENGTH];
- int arg = va_arg(args, int);
- int arglen = ::sprintf(buf, "%p", reinterpret_cast<void*>(arg));
- Append(buf, (unsigned)arglen);
- break;
- }
- case '%':
- {
- Append("%", 1);
- break;
- }
- default:
- URHO3D_LOGWARNINGF("Unsupported format specifier: '%c'", format);
- break;
- }
- }
- }
- int String::Compare(const char* lhs, const char* rhs, bool caseSensitive)
- {
- if (!lhs || !rhs)
- return lhs ? 1 : (rhs ? -1 : 0);
- if (caseSensitive)
- return strcmp(lhs, rhs);
- else
- {
- for (;;)
- {
- auto l = (char)tolower(*lhs);
- auto r = (char)tolower(*rhs);
- if (!l || !r)
- return l ? 1 : (r ? -1 : 0);
- if (l < r)
- return -1;
- if (l > r)
- return 1;
- ++lhs;
- ++rhs;
- }
- }
- }
- 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 - delta);
- }
- }
- else
- Resize(length_ + delta);
- CopyChars(buffer_ + pos, srcStart, srcLength);
- }
- WString::WString() :
- length_(0),
- buffer_(nullptr)
- {
- }
- WString::WString(const String& str) :
- length_(0),
- buffer_(nullptr)
- {
- #ifdef _WIN32
- unsigned neededSize = 0;
- wchar_t temp[3];
- unsigned byteOffset = 0;
- while (byteOffset < str.Length())
- {
- wchar_t* dest = temp;
- String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
- neededSize += dest - temp;
- }
- Resize(neededSize);
- byteOffset = 0;
- wchar_t* dest = buffer_;
- while (byteOffset < str.Length())
- String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
- #else
- Resize(str.LengthUTF8());
- unsigned byteOffset = 0;
- wchar_t* dest = buffer_;
- while (byteOffset < str.Length())
- *dest++ = (wchar_t)str.NextUTF8Char(byteOffset);
- #endif
- }
- WString::~WString()
- {
- delete[] buffer_;
- }
- void WString::Resize(unsigned newLength)
- {
- if (!newLength)
- {
- delete[] buffer_;
- buffer_ = nullptr;
- length_ = 0;
- }
- else
- {
- auto* newBuffer = new wchar_t[newLength + 1];
- if (buffer_)
- {
- unsigned copyLength = length_ < newLength ? length_ : newLength;
- memcpy(newBuffer, buffer_, copyLength * sizeof(wchar_t));
- delete[] buffer_;
- }
- newBuffer[newLength] = 0;
- buffer_ = newBuffer;
- length_ = newLength;
- }
- }
- }
|