| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412 |
- // Copyright (c) 2008-2023 the Urho3D project
- // License: MIT
- #include "../Precompiled.h"
- #include "../IO/Log.h"
- #include <cstdio>
- #include "../DebugNew.h"
- namespace Urho3D
- {
- const String String::EMPTY;
- String::String(const WString& str)
- : String()
- {
- SetUTF8FromWChar(str.CString());
- }
- String::String(int value)
- : String()
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%d", value);
- *this = tempBuffer;
- }
- String::String(short value)
- : String()
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%d", value);
- *this = tempBuffer;
- }
- String::String(long value)
- : String()
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%ld", value);
- *this = tempBuffer;
- }
- String::String(long long value)
- : String()
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%lld", value);
- *this = tempBuffer;
- }
- String::String(unsigned value)
- : String()
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%u", value);
- *this = tempBuffer;
- }
- String::String(unsigned short value)
- : String()
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%u", value);
- *this = tempBuffer;
- }
- String::String(unsigned long value)
- : String()
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%lu", value);
- *this = tempBuffer;
- }
- String::String(unsigned long long value)
- : String()
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%llu", value);
- *this = tempBuffer;
- }
- String::String(float value)
- : String()
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%g", value);
- *this = tempBuffer;
- }
- String::String(double value)
- : String()
- {
- char tempBuffer[CONVERSION_BUFFER_LENGTH];
- sprintf(tempBuffer, "%.15g", value);
- *this = tempBuffer;
- }
- String::String(bool value)
- : String()
- {
- if (value)
- *this = "true";
- else
- *this = "false";
- }
- String::String(char value)
- : String()
- {
- Resize(1);
- GetBuffer()[0] = value;
- }
- String::String(char value, i32 length)
- : String()
- {
- Resize(length);
- char* buffer = GetBuffer();
- for (i32 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)
- {
- i32 length = Length();
- char* buffer = GetBuffer();
- if (caseSensitive)
- {
- for (i32 i = 0; i < length; ++i)
- {
- if (buffer[i] == replaceThis)
- buffer[i] = replaceWith;
- }
- }
- else
- {
- replaceThis = (char)tolower(replaceThis);
- for (i32 i = 0; i < length; ++i)
- {
- if (tolower(buffer[i]) == replaceThis)
- buffer[i] = replaceWith;
- }
- }
- }
- void String::Replace(const String& replaceThis, const String& replaceWith, bool caseSensitive)
- {
- i32 nextPos = 0;
- i32 length = Length();
- i32 replaceThisLength = replaceThis.Length();
- i32 replaceWithLength = replaceWith.Length();
- while (nextPos < length)
- {
- i32 pos = Find(replaceThis, nextPos, caseSensitive);
- if (pos == NPOS)
- break;
- Replace(pos, replaceThisLength, replaceWith);
- nextPos = pos + replaceWithLength;
- }
- }
- void String::Replace(i32 pos, i32 length, const String& replaceWith)
- {
- // If substring is illegal, do nothing
- if (pos + length > Length())
- return;
- Replace(pos, length, replaceWith.GetBuffer(), replaceWith.Length());
- }
- void String::Replace(i32 pos, i32 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)
- {
- i32 pos = (i32)(start - Begin());
- if (pos >= Length())
- return End();
- i32 length = (i32)(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, i32 length)
- {
- if (str)
- {
- i32 oldLength = Length();
- Resize(oldLength + length);
- CopyChars(&GetBuffer()[oldLength], str, length);
- }
- return *this;
- }
- void String::Insert(i32 pos, const String& str)
- {
- assert(pos >= 0);
- i32 length = Length();
- if (pos > length)
- pos = length;
- if (pos == length)
- (*this) += str;
- else
- Replace(pos, 0, str);
- }
- void String::Insert(i32 pos, char c)
- {
- assert(pos >= 0);
- i32 length = Length();
- if (pos > length)
- pos = length;
- if (pos == length)
- {
- (*this) += c;
- }
- else
- {
- i32 oldLength = length;
- Resize(length + 1);
- MoveRange(pos + 1, pos, oldLength - pos);
- GetBuffer()[pos] = c;
- }
- }
- String::Iterator String::Insert(const String::Iterator& dest, const String& str)
- {
- i32 length = Length();
- i32 pos = (i32)(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)
- {
- i32 pos = (i32)(dest - Begin());
- if (pos > Length())
- pos = Length();
- i32 length = (i32)(end - start);
- Replace(pos, 0, &(*start), length);
- return Begin() + pos;
- }
- String::Iterator String::Insert(const String::Iterator& dest, char c)
- {
- i32 length = Length();
- i32 pos = (i32)(dest - Begin());
- if (pos > length)
- pos = length;
- Insert(pos, c);
- return Begin() + pos;
- }
- void String::Erase(i32 pos, i32 length)
- {
- Replace(pos, length, String::EMPTY);
- }
- String::Iterator String::Erase(const String::Iterator& it)
- {
- i32 pos = (i32)(it - Begin());
- if (pos >= Length())
- return End();
- Erase(pos);
- return Begin() + pos;
- }
- String::Iterator String::Erase(const String::Iterator& start, const String::Iterator& end)
- {
- i32 pos = (i32)(start - Begin());
- if (pos >= Length())
- return End();
- i32 length = (i32)(end - start);
- Erase(pos, length);
- return Begin() + pos;
- }
- void String::Resize(i32 newLength)
- {
- assert(newLength >= 0);
- i32 capacity = Capacity();
- if (newLength && capacity < newLength + 1) // Need to increase capacity
- {
- // Increase the capacity with half each time it is exceeded
- while (capacity < newLength + 1)
- capacity += (capacity + 1) >> 1u;
- // SHORT_STRING_CAPACITY is minimal possible capacity
- assert(capacity > SHORT_STRING_CAPACITY);
- char* newBuffer = new char[capacity];
- // Move the existing data to the new buffer
- i32 oldLength = Length();
- if (oldLength)
- CopyChars(newBuffer, GetBuffer(), oldLength);
- // Delete the old buffer if in heap
- if (!IsShort())
- delete[] data_.longString_.buffer_;
- newBuffer[newLength] = '\0';
- data_.longString_.buffer_ = newBuffer;
- data_.longString_.capacity_ = capacity;
- data_.longString_.length_ = newLength;
- }
- else // Old buffer is used
- {
- if (IsShort())
- {
- data_.shortString_.buffer_[newLength] = '\0';
- SetShortStringLength((u8)newLength);
- }
- else
- {
- data_.longString_.buffer_[newLength] = '\0';
- data_.longString_.length_ = newLength;
- }
- }
- }
- void String::Reserve(i32 newCapacity)
- {
- assert(newCapacity >= 0);
- i32 length = Length();
- if (newCapacity < length + 1)
- newCapacity = length + 1;
- if (newCapacity < SHORT_STRING_CAPACITY)
- newCapacity = SHORT_STRING_CAPACITY;
- if (newCapacity == Capacity())
- return;
- if (newCapacity > SHORT_STRING_CAPACITY) // New buffer in heap
- {
- char* newBuffer = new char[newCapacity];
- // Move the existing data to the new buffer
- CopyChars(newBuffer, GetBuffer(), length + 1);
- // Delete the old buffer if in heap
- if (!IsShort())
- delete[] data_.longString_.buffer_;
- data_.longString_.buffer_ = newBuffer;
- data_.longString_.capacity_ = newCapacity;
- data_.longString_.length_ = length;
- }
- else // New buffer in stack
- {
- if (!IsShort()) // Old buffer in heap
- {
- // Pointer will be will be overwritten in CopyChars()
- char* oldBuffer = data_.longString_.buffer_;
- // Move the existing data from heap to stack
- CopyChars(data_.shortString_.buffer_, oldBuffer, length + 1);
- // Delete the old buffer if in heap
- delete[] oldBuffer;
- }
- SetShortStringLength((u8)length);
- }
- }
- void String::Compact()
- {
- Reserve(Length() + 1);
- }
- void String::Clear()
- {
- Resize(0);
- }
- void String::Swap(String& str)
- {
- std::swap(data_, str.data_);
- }
- String String::Substring(i32 pos) const
- {
- i32 length = Length();
- if (pos < length)
- {
- String ret;
- ret.Resize(length - pos);
- CopyChars(ret.GetBuffer(), GetBuffer() + pos, ret.Length());
- return ret;
- }
- else
- return String();
- }
- String String::Substring(i32 pos, i32 length) const
- {
- i32 thisLength = Length();
- if (pos < thisLength)
- {
- String ret;
- if (pos + length > thisLength)
- length = thisLength - pos;
- ret.Resize(length);
- CopyChars(ret.GetBuffer(), GetBuffer() + pos, ret.Length());
- return ret;
- }
- else
- return String();
- }
- String String::Trimmed() const
- {
- i32 trimStart = 0;
- i32 trimEnd = Length();
- const char* buffer = GetBuffer();
- 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;
- i32 byte_offset = 0;
- while (byte_offset < Length())
- {
- c32 cp = NextUTF8Char(byte_offset);
- // Первые символы Юникода (0 - 127) совпадают с 7-ми битной кодировкой US-ASCII
- // Коды кириллицы: https://ru.wikipedia.org/wiki/Кириллица_(блок_Юникода)
- if ((cp >= 0x41 && cp <= 0x5a) // Английские A - Z
- || (cp >= 0x410 && cp <= 0x42f)) // Русские А - Я без Ё
- {
- ret.AppendUTF8(cp + 32);
- }
- else if (cp >= 0x400 && cp <= 0x40f) // Другие большие буквы кириллицы
- {
- ret.AppendUTF8(cp + 80);
- }
- else
- {
- ret.AppendUTF8(cp);
- }
- }
- return ret;
- }
- String String::ToUpper() const
- {
- String ret;
- i32 byte_offset = 0;
- while (byte_offset < Length())
- {
- c32 cp = NextUTF8Char(byte_offset);
- // Первые символы Юникода (0 - 127) совпадают с 7-ми битной кодировкой US-ASCII
- // Коды кириллицы: https://ru.wikipedia.org/wiki/Кириллица_(блок_Юникода)
- if ((cp >= 0x61 && cp <= 0x7a) // Английские a - z
- || (cp >= 0x430 && cp <= 0x44f)) // Русские а - я без ё
- {
- ret.AppendUTF8(cp - 32);
- }
- else if (cp >= 0x450 && cp <= 0x45f) // Другие маленькие буквы кириллицы
- {
- ret.AppendUTF8(cp - 80);
- }
- else
- {
- ret.AppendUTF8(cp);
- }
- }
- 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);
- }
- i32 String::Find(char c, i32 startPos, bool caseSensitive) const
- {
- assert(startPos >= 0);
- const char* buffer = GetBuffer();
- i32 length = Length();
- if (caseSensitive)
- {
- for (i32 i = startPos; i < length; ++i)
- {
- if (buffer[i] == c)
- return i;
- }
- }
- else
- {
- c = (char)tolower(c);
- for (i32 i = startPos; i < length; ++i)
- {
- if (tolower(buffer[i]) == c)
- return i;
- }
- }
- return NPOS;
- }
- i32 String::Find(const String& str, i32 startPos, bool caseSensitive) const
- {
- assert(startPos >= 0);
- i32 length = Length();
- i32 strLength = str.Length();
- if (!strLength || strLength > length)
- return NPOS;
- const char* buffer = GetBuffer();
- const char* strBuffer = str.GetBuffer();
- char first = strBuffer[0];
- if (!caseSensitive)
- first = (char)tolower(first);
- for (i32 i = startPos; i <= length - strLength; ++i)
- {
- char c = buffer[i];
- if (!caseSensitive)
- c = (char)tolower(c);
- if (c == first)
- {
- i32 skip = NPOS;
- bool found = true;
- for (i32 j = 1; j < strLength; ++j)
- {
- c = buffer[i + j];
- char d = strBuffer[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;
- }
- i32 String::FindLast(char c, i32 startPos, bool caseSensitive) const
- {
- i32 length = Length();
- const char* buffer = GetBuffer();
- if (startPos == NPOS || startPos >= length)
- startPos = length - 1;
- if (caseSensitive)
- {
- for (i32 i = startPos; i >= 0; --i)
- {
- if (buffer[i] == c)
- return i;
- }
- }
- else
- {
- c = (char)tolower(c);
- for (i32 i = startPos; i >= 0; --i)
- {
- if (tolower(buffer[i]) == c)
- return i;
- }
- }
- return NPOS;
- }
- i32 String::FindLast(const String& str, i32 startPos, bool caseSensitive) const
- {
- i32 length = Length();
- i32 strLength = str.Length();
- if (!strLength || strLength > length)
- return NPOS;
- if (startPos == NPOS || startPos > length - strLength)
- startPos = length - strLength;
- const char* buffer = GetBuffer();
- const char* strBuffer = str.GetBuffer();
- char first = strBuffer[0];
- if (!caseSensitive)
- first = (char)tolower(first);
- for (i32 i = startPos; i >= 0; --i)
- {
- char c = buffer[i];
- if (!caseSensitive)
- c = (char)tolower(c);
- if (c == first)
- {
- bool found = true;
- for (i32 j = 1; j < strLength; ++j)
- {
- c = buffer[i + j];
- char d = strBuffer[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
- {
- i32 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, (c32)*str++);
- *dest = 0;
- Append(temp);
- }
- }
- void String::SetUTF8FromWChar(const wchar_t* str)
- {
- char temp[7];
- Clear();
- if (!str)
- return;
- #ifdef _WIN32
- while (*str)
- {
- c32 unicodeChar = DecodeUTF16(str);
- char* dest = temp;
- EncodeUTF8(dest, unicodeChar);
- *dest = 0;
- Append(temp);
- }
- #else
- while (*str)
- {
- char* dest = temp;
- EncodeUTF8(dest, (c32)*str++);
- *dest = 0;
- Append(temp);
- }
- #endif
- }
- i32 String::LengthUTF8() const
- {
- i32 ret = 0;
- const char* buffer = GetBuffer();
- const char* src = buffer;
- if (!src)
- return ret;
- const char* end = buffer + Length();
- while (src < end)
- {
- DecodeUTF8(src);
- ++ret;
- }
- return ret;
- }
- i32 String::ByteOffsetUTF8(i32 index) const
- {
- i32 byteOffset = 0;
- i32 utfPos = 0;
- i32 length = Length();
- while (utfPos < index && byteOffset < length)
- {
- NextUTF8Char(byteOffset);
- ++utfPos;
- }
- return byteOffset;
- }
- c32 String::NextUTF8Char(i32& byteOffset) const
- {
- const char* buffer = GetBuffer();
- const char* src = buffer + byteOffset;
- c32 ret = DecodeUTF8(src);
- byteOffset = (i32)(src - buffer);
- return ret;
- }
- c32 String::AtUTF8(i32 index) const
- {
- i32 byteOffset = ByteOffsetUTF8(index);
- return NextUTF8Char(byteOffset);
- }
- void String::ReplaceUTF8(i32 index, c32 unicodeChar)
- {
- i32 utfPos = 0;
- i32 byteOffset = 0;
- i32 length = Length();
- while (utfPos < index && byteOffset < length)
- {
- NextUTF8Char(byteOffset);
- ++utfPos;
- }
- if (utfPos < index)
- return;
- i32 beginCharPos = byteOffset;
- NextUTF8Char(byteOffset);
- char temp[7];
- char* dest = temp;
- EncodeUTF8(dest, unicodeChar);
- *dest = 0;
- Replace(beginCharPos, byteOffset - beginCharPos, temp, (i32)(dest - temp));
- }
- String& String::AppendUTF8(c32 unicodeChar)
- {
- char temp[7];
- char* dest = temp;
- EncodeUTF8(dest, unicodeChar);
- *dest = 0;
- return Append(temp);
- }
- String String::SubstringUTF8(i32 pos) const
- {
- i32 utf8Length = LengthUTF8();
- i32 byteOffset = ByteOffsetUTF8(pos);
- String ret;
- while (pos < utf8Length)
- {
- ret.AppendUTF8(NextUTF8Char(byteOffset));
- ++pos;
- }
- return ret;
- }
- String String::SubstringUTF8(i32 pos, i32 length) const
- {
- i32 utf8Length = LengthUTF8();
- i32 byteOffset = ByteOffsetUTF8(pos);
- i32 endPos = pos + length;
- String ret;
- while (pos < endPos && pos < utf8Length)
- {
- ret.AppendUTF8(NextUTF8Char(byteOffset));
- ++pos;
- }
- return ret;
- }
- void String::EncodeUTF8(char*& dest, c32 unicodeChar)
- {
- if (unicodeChar < 0x80)
- *dest++ = (char)unicodeChar;
- else if (unicodeChar < 0x800)
- {
- dest[0] = (char)(0xc0u | ((unicodeChar >> 6u) & 0x1fu));
- dest[1] = (char)(0x80u | (unicodeChar & 0x3fu));
- dest += 2;
- }
- else if (unicodeChar < 0x10000)
- {
- dest[0] = (char)(0xe0u | ((unicodeChar >> 12u) & 0xfu));
- dest[1] = (char)(0x80u | ((unicodeChar >> 6u) & 0x3fu));
- dest[2] = (char)(0x80u | (unicodeChar & 0x3fu));
- dest += 3;
- }
- else if (unicodeChar < 0x200000)
- {
- dest[0] = (char)(0xf0u | ((unicodeChar >> 18u) & 0x7u));
- dest[1] = (char)(0x80u | ((unicodeChar >> 12u) & 0x3fu));
- dest[2] = (char)(0x80u | ((unicodeChar >> 6u) & 0x3fu));
- dest[3] = (char)(0x80u | (unicodeChar & 0x3fu));
- dest += 4;
- }
- else if (unicodeChar < 0x4000000)
- {
- dest[0] = (char)(0xf8u | ((unicodeChar >> 24u) & 0x3u));
- dest[1] = (char)(0x80u | ((unicodeChar >> 18u) & 0x3fu));
- dest[2] = (char)(0x80u | ((unicodeChar >> 12u) & 0x3fu));
- dest[3] = (char)(0x80u | ((unicodeChar >> 6u) & 0x3fu));
- dest[4] = (char)(0x80u | (unicodeChar & 0x3fu));
- dest += 5;
- }
- else
- {
- dest[0] = (char)(0xfcu | ((unicodeChar >> 30u) & 0x1u));
- dest[1] = (char)(0x80u | ((unicodeChar >> 24u) & 0x3fu));
- dest[2] = (char)(0x80u | ((unicodeChar >> 18u) & 0x3fu));
- dest[3] = (char)(0x80u | ((unicodeChar >> 12u) & 0x3fu));
- dest[4] = (char)(0x80u | ((unicodeChar >> 6u) & 0x3fu));
- dest[5] = (char)(0x80u | (unicodeChar & 0x3fu));
- dest += 6;
- }
- }
- #define GET_NEXT_CONTINUATION_BYTE(ptr) *(ptr); if ((u8)*(ptr) < 0x80 || (u8)*(ptr) >= 0xc0) return '?'; else ++(ptr);
- c32 String::DecodeUTF8(const char*& src)
- {
- if (src == nullptr)
- return 0;
- u8 char1 = *src++;
- // Check if we are in the middle of a UTF8 character
- if (char1 >= 0x80 && char1 < 0xc0)
- {
- while ((u8)*src >= 0x80 && (u8)*src < 0xc0)
- ++src;
- return '?';
- }
- if (char1 < 0x80)
- return char1;
- else if (char1 < 0xe0)
- {
- u8 char2 = GET_NEXT_CONTINUATION_BYTE(src);
- return (c32)((char2 & 0x3fu) | ((char1 & 0x1fu) << 6u));
- }
- else if (char1 < 0xf0)
- {
- u8 char2 = GET_NEXT_CONTINUATION_BYTE(src);
- u8 char3 = GET_NEXT_CONTINUATION_BYTE(src);
- return (c32)((char3 & 0x3fu) | ((char2 & 0x3fu) << 6u) | ((char1 & 0xfu) << 12u));
- }
- else if (char1 < 0xf8)
- {
- u8 char2 = GET_NEXT_CONTINUATION_BYTE(src);
- u8 char3 = GET_NEXT_CONTINUATION_BYTE(src);
- u8 char4 = GET_NEXT_CONTINUATION_BYTE(src);
- return (c32)((char4 & 0x3fu) | ((char3 & 0x3fu) << 6u) | ((char2 & 0x3fu) << 12u) | ((char1 & 0x7u) << 18u));
- }
- else if (char1 < 0xfc)
- {
- u8 char2 = GET_NEXT_CONTINUATION_BYTE(src);
- u8 char3 = GET_NEXT_CONTINUATION_BYTE(src);
- u8 char4 = GET_NEXT_CONTINUATION_BYTE(src);
- u8 char5 = GET_NEXT_CONTINUATION_BYTE(src);
- return (c32)((char5 & 0x3fu) | ((char4 & 0x3fu) << 6u) | ((char3 & 0x3fu) << 12u) | ((char2 & 0x3fu) << 18u) |
- ((char1 & 0x3u) << 24u));
- }
- else
- {
- u8 char2 = GET_NEXT_CONTINUATION_BYTE(src);
- u8 char3 = GET_NEXT_CONTINUATION_BYTE(src);
- u8 char4 = GET_NEXT_CONTINUATION_BYTE(src);
- u8 char5 = GET_NEXT_CONTINUATION_BYTE(src);
- u8 char6 = GET_NEXT_CONTINUATION_BYTE(src);
- return (c32)((char6 & 0x3fu) | ((char5 & 0x3fu) << 6u) | ((char4 & 0x3fu) << 12u) | ((char3 & 0x3fu) << 18u) |
- ((char2 & 0x3fu) << 24u) | ((char1 & 0x1u) << 30u));
- }
- }
- #ifdef _WIN32
- void String::EncodeUTF16(wchar_t*& dest, c32 unicodeChar)
- {
- if (unicodeChar < 0x10000)
- *dest++ = (wchar_t)unicodeChar;
- else
- {
- unicodeChar -= 0x10000;
- *dest++ = 0xd800 | ((unicodeChar >> 10) & 0x3ff);
- *dest++ = 0xdc00 | (unicodeChar & 0x3ff);
- }
- }
- c32 String::DecodeUTF16(const wchar_t*& src)
- {
- if (src == nullptr)
- return 0;
- u16 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
- {
- u16 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, (i32)splitLen));
- str = splitEnd + 1;
- }
- }
- const ptrdiff_t splitLen = strEnd - str;
- if (splitLen > 0 || keepEmptyStrings)
- ret.Push(String(str, (i32)splitLen));
- return ret;
- }
- String String::Joined(const Vector<String>& subStrings, const String& glue)
- {
- if (subStrings.Empty())
- return {};
- i32 glueLen = glue.Length();
- i32 commonLen = -glueLen;
- for (const auto& s : subStrings)
- commonLen += s.Length() + glueLen;
- String joinedString;
- if (commonLen)
- {
- joinedString.Reserve(commonLen);
- joinedString.Resize(commonLen);
- const char* strGlue = glue.GetBuffer();
- char* dest = joinedString.GetBuffer();
- for (i32 i = 0; i < subStrings.Size(); ++i)
- {
- i32 l = subStrings[i].Length();
- if (l)
- {
- memcpy(dest, subStrings[i].GetBuffer(), l);
- dest += l;
- }
- if (glueLen && i != subStrings.Size() - 1)
- {
- memcpy(dest, strGlue, glueLen);
- dest += glueLen;
- }
- }
- }
- 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;
- i32 length = (i32)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, (i32)(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, 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, 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(i32 pos, i32 length, const char* srcStart, i32 srcLength)
- {
- i32 delta = srcLength - length;
- i32 thisLength = Length();
- if (pos + length < thisLength)
- {
- if (delta < 0)
- {
- MoveRange(pos + srcLength, pos + length, thisLength - pos - length);
- Resize(thisLength + delta);
- }
- if (delta > 0)
- {
- Resize(thisLength + delta);
- thisLength = Length();
- MoveRange(pos + srcLength, pos + length, thisLength - pos - length - delta);
- }
- }
- else
- {
- Resize(thisLength + delta);
- }
- CopyChars(GetBuffer() + pos, srcStart, srcLength);
- }
- WString::WString() :
- length_(0),
- buffer_(nullptr)
- {
- }
- WString::WString(const String& str) :
- length_(0),
- buffer_(nullptr)
- {
- #ifdef _WIN32
- i32 neededSize = 0;
- wchar_t temp[3];
- i32 byteOffset = 0;
- while (byteOffset < str.Length())
- {
- wchar_t* dest = temp;
- String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
- neededSize += (i32)(dest - temp);
- }
- Resize(neededSize);
- byteOffset = 0;
- wchar_t* dest = buffer_;
- while (byteOffset < str.Length())
- String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
- #else
- Resize(str.LengthUTF8());
- i32 byteOffset = 0;
- wchar_t* dest = buffer_;
- while (byteOffset < str.Length())
- *dest++ = (wchar_t)str.NextUTF8Char(byteOffset);
- #endif
- }
- WString::~WString()
- {
- delete[] buffer_;
- }
- void WString::Resize(i32 newLength)
- {
- if (!newLength)
- {
- delete[] buffer_;
- buffer_ = nullptr;
- length_ = 0;
- }
- else
- {
- auto* newBuffer = new wchar_t[newLength + 1];
- if (buffer_)
- {
- i32 copyLength = length_ < newLength ? length_ : newLength;
- memcpy(newBuffer, buffer_, copyLength * sizeof(wchar_t));
- delete[] buffer_;
- }
- newBuffer[newLength] = 0;
- buffer_ = newBuffer;
- length_ = newLength;
- }
- }
- }
|