123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // 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 <stdarg.h>
- #include <stdio.h>
- #include "platform/platform.h"
- // Sigh... guess what compiler needs this...
- namespace DictHash { U32 hash( String::StringData* ); }
- namespace KeyCmp
- {
- template< typename Key > bool equals( const Key&, const Key& );
- template<> bool equals<>( String::StringData* const&, String::StringData* const& );
- }
- #include "core/util/str.h"
- #include "core/util/tDictionary.h"
- #include "core/strings/stringFunctions.h"
- #include "core/strings/unicode.h"
- #include "core/util/hashFunction.h"
- #include "core/util/autoPtr.h"
- #include "core/util/tVector.h"
- #include "core/dataChunker.h"
- #include "console/console.h"
- #include "console/engineAPI.h"
- #include "math/mMathFn.h"
- #include "platform/platform.h"
- #include "platform/profiler.h"
- #include "platform/platformIntrinsics.h"
- #include "platform/threads/mutex.h"
- #ifndef TORQUE_DISABLE_MEMORY_MANAGER
- # undef new
- #else
- # define _new new
- #endif
- const String::SizeType String::NPos = U32(~0);
- const String String::EmptyString;
- /// A delete policy for the AutoPtr class
- struct DeleteString
- {
- template<class T>
- static void destroy(T *ptr) { dFree(ptr); }
- };
- //-----------------------------------------------------------------------------
- /// Search for a character.
- /// Search for the position of the needle in the haystack.
- /// Default mode is StrCase | StrLeft, mode also accepts StrNoCase and StrRight.
- /// If pos is non-zero, then in mode StrLeft the search starts at (hay + pos) and
- /// in mode StrRight the search starts at (hay + pos - 1)
- /// @return Returns a pointer to the location of the character in the haystack or 0
- static const char* StrFind(const char* hay, char needle, S32 pos, U32 mode)
- {
- if (mode & String::Right)
- {
- // Go to the end first, then search backwards
- const char *he = hay;
- if (pos)
- {
- he += pos - 1;
- }
- else
- {
- while (*he)
- he++;
- }
- if (mode & String::NoCase)
- {
- needle = dTolower(needle);
- for (; he >= hay; he--)
- {
- if (dTolower(*he) == needle)
- return he;
- }
- }
- else
- {
- for (; he >= hay; he--)
- {
- if (*he == needle)
- return he;
- }
- }
- return 0;
- }
- else
- {
- if (mode & String::NoCase)
- {
- needle = dTolower(needle);
- for (hay += pos; *hay && dTolower(*hay) != needle;)
- hay++;
- }
- else
- {
- for (hay += pos; *hay && *hay != needle;)
- hay++;
- }
- return *hay ? hay : 0;
- }
- }
- /// Search for a StringData.
- /// Search for the position of the needle in the haystack.
- /// Default mode is StrCase | StrLeft, mode also accepts StrNoCase and StrRight.
- /// If pos is non-zero, then in mode StrLeft the search starts at (hay + pos) and
- /// in mode StrRight the search starts at (hay + pos - 1)
- /// @return Returns a pointer to the StringData in the haystack or 0
- static const char* StrFind(const char* hay, const char* needle, S32 pos, U32 mode)
- {
- if (mode & String::Right)
- {
- const char *he = hay;
- if (pos)
- {
- he += pos - 1;
- }
- else
- {
- while (*he)
- he++;
- }
- if (mode & String::NoCase)
- {
- AutoPtr<char,DeleteString> ln(dStrlwr(dStrdup(needle)));
- for (; he >= hay; he--)
- {
- if (dTolower(*he) == *ln)
- {
- U32 i = 0;
- while (ln[i] && ln[i] == dTolower(he[i]))
- i++;
- if (!ln[i])
- return he;
- if (!hay[i])
- return 0;
- }
- }
- }
- else
- {
- for (; he >= hay; he--)
- {
- if (*he == *needle)
- {
- U32 i = 0;
- while (needle[i] && needle[i] == he[i])
- i++;
- if (!needle[i])
- return he;
- if (!hay[i])
- return 0;
- }
- }
- }
- return 0;
- }
- else
- {
- if (mode & String::NoCase)
- {
- AutoPtr<char,DeleteString> ln(dStrlwr(dStrdup(needle)));
- for (hay += pos; *hay; hay++)
- {
- if (dTolower(*hay) == *ln)
- {
- U32 i = 0;
- while (ln[i] && ln[i] == dTolower(hay[i]))
- i++;
- if (!ln[i])
- return hay;
- if (!hay[i])
- return 0;
- }
- }
- }
- else
- {
- for (hay += pos; *hay; hay++)
- {
- if (*hay == *needle)
- {
- U32 i = 0;
- while (needle[i] && needle[i] == hay[i])
- i++;
- if (!needle[i])
- return hay;
- if (!hay[i])
- return 0;
- }
- }
- }
- }
- return 0;
- }
- //-----------------------------------------------------------------------------
- /// Struct with String::StringData's field so we can initialize
- /// this without a constructor.
- struct StringDataImpl
- {
- #ifdef TORQUE_DEBUG
- StringChar* mString; ///< so we can inspect data in a debugger
- #endif
- U32 mRefCount; ///< String reference count; string is not refcounted if this is U32_MAX (necessary for thread-safety of interned strings and the empty string).
- U32 mLength; ///< String length in bytes excluding null.
- mutable U32 mNumChars; ///< Character count; varies from byte count for strings with multi-bytes characters.
- mutable U32 mHashCase; ///< case-sensitive hash
- mutable U32 mHashNoCase; ///< case-insensitive hash
- mutable UTF16* mUTF16;
- bool mIsInterned; ///< If true, this string is interned in the string table.
- StringChar mData[1]; ///< Start of string data
- };
- ///
- class String::StringData : protected StringDataImpl
- {
- public:
- ///
- StringData( const StringChar* data, U32 length, bool interned = false )
- {
- mRefCount = 1;
- mNumChars = U32_MAX;
- mHashCase = U32_MAX;
- mHashNoCase = U32_MAX;
- mUTF16 = NULL;
- mLength = length;
- mIsInterned = interned;
-
- if( data )
- {
- dMemcpy( mData, data, sizeof( StringChar ) * mLength );
- mData[ mLength ] = '\0';
- }
-
- #ifdef TORQUE_DEBUG
- mString = &mData[0];
- #endif
- if( mIsInterned )
- mRefCount = U32_MAX;
- }
- ~StringData()
- {
- AssertFatal( mRefCount == 0, "StringData::~StringData invalid refcount" );
- if( mUTF16 )
- delete [] mUTF16;
- }
-
- static StringData* Create(const StringChar* data, U32 len, bool interned = false)
- {
- void* memory = dMalloc(sizeof(StringData) + sizeof(StringChar) * len);
- StringData* result = new(memory) StringData(data, len, interned);
- return result;
- }
-
- static StringData* Create(const StringChar* data, U32 len, DataChunker& chunker, bool interned = false)
- {
- void* memory = chunker.alloc( sizeof(StringData) + len * sizeof(StringChar));
- StringData* result = new(memory) StringData(data, len, interned);
- return result;
- }
-
- bool isShared() const
- {
- return ( mRefCount > 1 );
- }
- void addRef()
- {
- if( mRefCount != U32_MAX )
- mRefCount ++;
- }
- void release()
- {
- if( mRefCount != U32_MAX )
- {
- -- mRefCount;
- if( !mRefCount )
- delete this;
- }
- }
- U32 getLength() const
- {
- return mLength;
- }
- U32 getDataSize() const
- {
- return ( mLength + 1 );
- }
- U32 getDataSizeUTF16() const
- {
- return ( mLength * sizeof( UTF16 ) );
- }
- UTF8 operator []( U32 index ) const
- {
- AssertFatal( index < mLength, "String::StringData::operator []() - index out of range" );
- return mData[ index ];
- }
- UTF8* utf8()
- {
- return mData;
- }
- const UTF8* utf8() const
- {
- return mData;
- }
- UTF16* utf16() const
- {
- if( !mUTF16 )
- {
- // Do this atomically to protect interned strings.
-
- UTF16* utf16 = createUTF16string( mData );
- if( !dCompareAndSwap( mUTF16,( UTF16* ) NULL, utf16 ) )
- delete [] utf16;
- }
- return mUTF16;
- }
- U32 getHashCase() const
- {
- return mHashCase;
- }
- U32 getOrCreateHashCase() const
- {
- if( mHashCase == U32_MAX )
- {
- PROFILE_SCOPE(StringData_getOrCreateHashCase);
- mHashCase = Torque::hash((const U8 *)(mData), mLength, 0);
- }
- return mHashCase;
- }
- U32 getHashNoCase() const
- {
- return mHashNoCase;
- }
- U32 getOrCreateHashNoCase() const
- {
- if( mHashNoCase == U32_MAX)
- {
- PROFILE_SCOPE(StringData_getOrCreateHashNoCase);
- UTF8 *lower = new UTF8[ mLength + 1 ];
- dStrncpy( lower, utf8(), mLength );
- lower[ mLength ] = 0;
- dStrlwr( lower );
- mHashNoCase = Torque::hash( (const U8*)lower, mLength, 0 );
- delete [] lower;
- }
- return mHashNoCase;
- }
- U32 getNumChars() const
- {
- if( mNumChars == U32_MAX )
- mNumChars = dStrlen( utf16() );
-
- return mNumChars;
- }
- bool isInterned() const
- {
- return mIsInterned;
- }
- static StringData* Empty()
- {
- static UTF16 emptyUTF16[ 1 ] = { 0 };
- static StringDataImpl empty =
- {
- #ifdef TORQUE_DEBUG
- "", // mString
- #endif
-
- U32_MAX, // mRefCount
- 0, // mLength
- 0, // mNumChars
- 0, // mHashCase
- 0, // mHashNoCase
- emptyUTF16, // mUTF16
- true, // mIsInterned
- { 0 } // mData
- };
-
- return ( StringData* ) ∅
- }
- };
- //-----------------------------------------------------------------------------
- namespace DictHash
- {
- inline U32 hash( String::StringData* data )
- {
- return data->getOrCreateHashCase();
- }
- }
- namespace KeyCmp
- {
- template<>
- inline bool equals<>( String::StringData* const& d1, String::StringData* const& d2 )
- {
- return ( String::compare( d1->utf8(), d2->utf8() ) == 0 );
- }
- }
- /// Type for the intern string table. We don't want String instances directly
- /// on the table so that destructors don't run when the table is destroyed. This
- /// is because we really shouldn't depend on dtor ordering within this file and thus
- /// we can't tell whether the intern string memory is freed before or after the
- /// table is destroyed.
- struct StringInternTable : public HashTable< String::StringData*, String::StringData* >
- {
- Mutex mMutex;
- DataChunker mChunker;
- };
- static StringInternTable* sInternTable;
- struct KillInternTable
- {
- ~KillInternTable()
- {
- if( sInternTable )
- delete sInternTable;
- }
- };
- static KillInternTable sKillInternTable;
- //-----------------------------------------------------------------------------
- #ifdef TORQUE_DEBUG
- /// Tracks the number of bytes allocated for strings.
- /// @bug This currently does not include UTF16 allocations.
- static U32 sgStringMemBytes;
- /// Tracks the number of Strings which are currently instantiated.
- static U32 sgStringInstances;
- #endif
- DefineEngineFunction( dumpStringMemStats, void, (), , "()"
- "@brief Dumps information about String memory usage\n\n"
- "@ingroup Debugging\n"
- "@ingroup Strings\n")
- {
- #ifdef TORQUE_DEBUG
- Con::printf( "String Data: %i instances, %i bytes", sgStringInstances, sgStringMemBytes );
- #endif
- }
- //-----------------------------------------------------------------------------
- String::String()
- {
- PROFILE_SCOPE(String_default_constructor);
- _string = StringData::Empty();
- }
- String::String(const String &str)
- {
- PROFILE_SCOPE(String_String_constructor);
- _string = str._string;
- _string->addRef();
- }
- String::String(const StringChar *str)
- {
- PROFILE_SCOPE(String_char_constructor);
- if( str && *str )
- {
- U32 len = dStrlen(str);
- _string = StringData::Create(str, len);
- }
- else
- _string = StringData::Empty();
- }
- String::String(const StringChar *str, SizeType len)
- {
- PROFILE_SCOPE(String_char_len_constructor);
- if (str && *str && len!=0)
- {
- _string = StringData::Create(str, len);
- }
- else
- _string = StringData::Empty();
- }
- String::String(const UTF16 *str)
- {
- PROFILE_SCOPE(String_UTF16_constructor);
- if( str && str[ 0 ] )
- {
- UTF8* utf8 = createUTF8string( str );
- U32 len = dStrlen( utf8 );
- _string = StringData::Create(utf8, len);
- delete [] utf8;
- }
- else
- _string = StringData::Empty();
- }
- String::~String()
- {
- if (_string && _string != StringData::Empty())
- _string->release();
- }
- //-----------------------------------------------------------------------------
- String String::intern() const
- {
- if( isInterned() )
- return *this;
-
- // Create the intern table, if we haven't already.
-
- if( !sInternTable )
- sInternTable = new StringInternTable;
-
- // Lock the string table.
-
- MutexHandle mutex;
- mutex.lock( &sInternTable->mMutex );
-
- // Lookup.
-
- StringInternTable::Iterator iter = sInternTable->find( _string );
- if( iter != sInternTable->end() )
- return ( *iter ).value;
-
- // Create new.
-
- StringData* data = StringData::Create(c_str(), length(), sInternTable->mChunker, true);
- iter = sInternTable->insertUnique( data, data );
-
- return ( *iter ).value;
- }
- //-----------------------------------------------------------------------------
- const StringChar* String::c_str() const
- {
- return _string->utf8();
- }
- const UTF16 *String::utf16() const
- {
- return _string->utf16();
- }
- String::SizeType String::length() const
- {
- return _string->getLength();
- }
- String::SizeType String::size() const
- {
- return _string->getDataSize();
- }
- String::SizeType String::numChars() const
- {
- return _string->getNumChars();
- }
- bool String::isEmpty() const
- {
- return ( _string == StringData::Empty() );
- }
- bool String::isEmpty(const char* str)
- {
- return str == 0 || str[0] == '\0';
- }
- bool String::isShared() const
- {
- return _string->isShared();
- }
- bool String::isSame( const String& str ) const
- {
- return ( _string == str._string );
- }
- bool String::isInterned() const
- {
- return ( _string->isInterned() );
- }
- U32 String::getHashCaseSensitive() const
- {
- return _string->getOrCreateHashCase();
- }
- U32 String::getHashCaseInsensitive() const
- {
- return _string->getOrCreateHashNoCase();
- }
- //-----------------------------------------------------------------------------
- String::SizeType String::find(const String &str, SizeType pos, U32 mode) const
- {
- return find(str._string->utf8(), pos, mode);
- }
- String& String::insert(SizeType pos, const String &str)
- {
- return insert(pos, str._string->utf8());
- }
- String& String::replace(SizeType pos, SizeType len, const String &str)
- {
- return replace(pos, len, str._string->utf8());
- }
- //-----------------------------------------------------------------------------
- String& String::operator=(StringChar c)
- {
- _string->release();
- _string = StringData::Create(NULL, 2);
- _string->utf8()[ 0 ] = c;
- _string->utf8()[ 1 ] = '\0';
- return *this;
- }
- String& String::operator+=(StringChar c)
- {
- // Append the given string into a new string
- U32 len = _string->getLength();
- StringData* sub = StringData::Create(NULL, len + 1);
-
- copy( sub->utf8(), _string->utf8(), len );
- sub->utf8()[len] = c;
- sub->utf8()[len+1] = 0;
- _string->release();
- _string = sub;
- return *this;
- }
- //-----------------------------------------------------------------------------
- String& String::operator=(const StringChar *str)
- {
- // Protect against self assignment which is not only a
- // waste of time, but can also lead to the string being
- // freed before it can be reassigned.
- if ( _string->utf8() == str )
- return *this;
- _string->release();
- if (str && *str)
- {
- U32 len = dStrlen(str);
- _string = StringData::Create(str, len);
- }
- else
- _string = StringData::Empty();
- return *this;
- }
- String& String::operator=(const String &src)
- {
- // Inc src first to avoid assignment to self problems.
- src._string->addRef();
- _string->release();
- _string = src._string;
- return *this;
- }
- String& String::operator+=(const StringChar *src)
- {
- if( src == NULL || !*src )
- return *this;
- // Append the given string into a new string
- U32 lena = _string->getLength();
- U32 lenb = dStrlen(src);
- U32 newlen = lena + lenb;
- StringData* sub;
- if( !newlen )
- sub = StringData::Empty();
- else
- {
- sub = StringData::Create(NULL, newlen);
- copy(sub->utf8(),_string->utf8(),lena);
- copy(sub->utf8() + lena,src,lenb + 1);
- }
- _string->release();
- _string = sub;
- return *this;
- }
- String& String::operator+=(const String &src)
- {
- if( src.isEmpty() )
- return *this;
- // Append the given string into a new string
- U32 lena = _string->getLength();
- U32 lenb = src._string->getLength();
- U32 newlen = lena + lenb;
- StringData* sub;
- if( !newlen )
- sub = StringData::Empty();
- else
- {
- sub = StringData::Create(NULL, newlen);
- copy(sub->utf8(),_string->utf8(),lena);
- copy(sub->utf8() + lena,src._string->utf8(),lenb + 1);
- }
- _string->release();
- _string = sub;
- return *this;
- }
- //-----------------------------------------------------------------------------
- String operator+(const String &a, const String &b)
- {
- PROFILE_SCOPE( String_String_plus_String );
-
- if( a.isEmpty() )
- return b;
- else if( b.isEmpty() )
- return a;
- U32 lena = a.length();
- U32 lenb = b.length();
- String::StringData* sub = String::StringData::Create(NULL, lena + lenb);
- String::copy(sub->utf8(),a._string->utf8(),lena);
- String::copy(sub->utf8() + lena,b._string->utf8(),lenb + 1);
- return String(sub);
- }
- String operator+(const String &a, StringChar c)
- {
- //PROFILE_SCOPE( String_String_plus_Char );
- U32 lena = a.length();
- String::StringData* sub = String::StringData::Create(NULL, lena + 1);
- String::copy(sub->utf8(),a._string->utf8(),lena);
- sub->utf8()[lena] = c;
- sub->utf8()[lena+1] = 0;
- return String(sub);
- }
- String operator+(StringChar c, const String &a)
- {
- //PROFILE_SCOPE( String_Char_plus_String );
- U32 lena = a.length();
- String::StringData* sub = String::StringData::Create(NULL, lena + 1);
- String::copy(sub->utf8() + 1,a._string->utf8(),lena + 1);
- sub->utf8()[0] = c;
- return String(sub);
- }
- String operator+(const String &a, const StringChar *b)
- {
- //PROFILE_SCOPE( String_String_plus_CString );
- AssertFatal(b,"String:: Invalid null ptr argument");
- if( a.isEmpty() )
- return String( b );
- U32 lena = a.length();
- U32 lenb = dStrlen(b);
- if( !lenb )
- return a;
- String::StringData* sub = String::StringData::Create(NULL, lena + lenb);
- String::copy(sub->utf8(),a._string->utf8(),lena);
- String::copy(sub->utf8() + lena,b,lenb + 1);
- return String(sub);
- }
- String operator+(const StringChar *a, const String &b)
- {
- //PROFILE_SCOPE( String_CString_plus_String );
- AssertFatal(a,"String:: Invalid null ptr argument");
- if( b.isEmpty() )
- return String( a );
- U32 lena = dStrlen(a);
- if( !lena )
- return b;
- U32 lenb = b.length();
- String::StringData* sub = String::StringData::Create(NULL, lena + lenb);
- String::copy(sub->utf8(),a,lena);
- String::copy(sub->utf8() + lena,b._string->utf8(),lenb + 1);
- return String(sub);
- }
- bool String::operator==(const String &str) const
- {
- //PROFILE_SCOPE( String_op_equal );
- if( str._string == _string )
- return true;
- else if( str._string->isInterned() && _string->isInterned() )
- return false;
- else if( str.length() != length() )
- return false;
- else if( str._string->getHashCase() != U32_MAX
- && _string->getHashCase() != U32_MAX
- && str._string->getHashCase() != _string->getHashCase() )
- return false;
- else
- return ( dMemcmp( str._string->utf8(), _string->utf8(), _string->getLength() ) == 0 );
- }
- bool String::operator==( StringChar c ) const
- {
- if( !_string || _string->getLength() != 1 )
- return false;
- else
- return ( _string->utf8()[ 0 ] == c );
- }
- bool String::operator<(const String &str) const
- {
- return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) < 0 );
- }
- bool String::operator>(const String &str) const
- {
- return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) > 0 );
- }
- bool String::operator<=(const String &str) const
- {
- return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) <= 0 );
- }
- bool String::operator>=(const String &str) const
- {
- return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) >= 0 );
- }
- //-----------------------------------------------------------------------------
- // Base functions for string comparison
- S32 String::compare(const StringChar *str, SizeType len, U32 mode) const
- {
- PROFILE_SCOPE( String_compare );
-
- AssertFatal(str,"String:: Invalid null ptr argument");
- const StringChar *p1 = _string->utf8();
- const StringChar *p2 = str;
- if (p1 == p2)
- return 0;
- if( mode & String::Right )
- {
- U32 n = len;
- if( n > length() )
- n = length();
- p1 += length() - n;
- p2 += dStrlen( str ) - n;
- }
- if (mode & String::NoCase)
- {
- if (len)
- {
- for (;--len; p1++,p2++)
- {
- if (dTolower(*p1) != dTolower(*p2) || !*p1)
- break;
- }
- }
- else
- {
- while (dTolower(*p1) == dTolower(*p2) && *p1)
- {
- p1++;
- p2++;
- }
- }
- return dTolower(*p1) - dTolower(*p2);
- }
- if (len)
- return dMemcmp(p1,p2,len);
- while (*p1 == *p2 && *p1)
- {
- p1++;
- p2++;
- }
- return *p1 - *p2;
- }
- S32 String::compare(const String &str, SizeType len, U32 mode) const
- {
- if ( str._string == _string )
- return 0;
- return compare( str.c_str(), len, mode );
- }
- S32 String::compare(const char *str1, const char *str2)
- {
- return strcmp(str1, str2);
- }
- S32 String::compare(const UTF16 *str1, const UTF16 *str2)
- {
- #if defined(TORQUE_OS_WIN) || defined(TORQUE_OS_XBOX) || defined(TORQUE_OS_XENON)
- return wcscmp(reinterpret_cast<const wchar_t *>(str1), reinterpret_cast<const wchar_t *>(str2));
- #else
- S32 ret;
- const UTF16 *a, *b;
- a = str1;
- b = str2;
- while (((ret = *a - *b) == 0) && *a && *b)
- a++, b++;
- return ret;
- #endif
- }
- bool String::equal(const String &str, U32 mode) const
- {
- if( !mode )
- return ( *this == str );
- else
- {
- if( _string == str._string )
- return true;
- else if( _string->isInterned() && str._string->isInterned() )
- return false;
- else if( length() != str.length() )
- return false;
- else if( _string->getHashNoCase() != U32_MAX
- && str._string->getHashNoCase() != U32_MAX
- && _string->getHashNoCase() != str._string->getHashNoCase() )
- return false;
- else
- return ( compare( str.c_str(), length(), mode ) == 0 );
- }
- }
- //-----------------------------------------------------------------------------
- String::SizeType String::find(StringChar c, SizeType pos, U32 mode) const
- {
- const StringChar* ptr = StrFind(_string->utf8(),c,pos,mode);
- return ptr? SizeType(ptr - _string->utf8()): NPos;
- }
- String::SizeType String::find(const StringChar *str, SizeType pos, U32 mode) const
- {
- AssertFatal(str,"String:: Invalid null ptr argument");
- const StringChar* ptr = StrFind(_string->utf8(),str,pos,mode);
- return ptr? SizeType(ptr - _string->utf8()): NPos;
- }
- //-----------------------------------------------------------------------------
- String& String::insert(SizeType pos, const StringChar *str)
- {
- AssertFatal(str,"String:: Invalid null ptr argument");
- return insert(pos,str,dStrlen(str));
- }
- ///@todo review for error checking
- String& String::insert(SizeType pos, const StringChar *str, SizeType len)
- {
- if( !len )
- return *this;
- AssertFatal( str, "String:: Invalid null ptr argument" );
-
- SizeType lena = length();
- AssertFatal((pos <= lena),"Calling String::insert with position greater than length");
- U32 newlen = lena + len;
- StringData *sub;
- if( !newlen )
- sub = StringData::Empty();
- else
- {
- sub = StringData::Create(NULL, newlen);
- String::copy(sub->utf8(),_string->utf8(),pos);
- String::copy(sub->utf8() + pos,str,len);
- String::copy(sub->utf8() + pos + len,_string->utf8() + pos,lena - pos + 1);
- }
- _string->release();
- _string = sub;
- return *this;
- }
- String& String::erase(SizeType pos, SizeType len)
- {
- AssertFatal( len != 0, "String::erase() - Calling String::erase with 0 length" );
- AssertFatal( ( pos + len ) <= length(), "String::erase() - Invalid string region" );
- if( !len )
- return *this;
- SizeType slen = length();
- U32 newlen = slen - len;
- StringData *sub;
- if( !newlen )
- sub = StringData::Empty();
- else
- {
- sub = StringData::Create(NULL, newlen);
- if (pos > 0)
- String::copy(sub->utf8(),_string->utf8(),pos);
- String::copy(sub->utf8() + pos, _string->utf8() + pos + len, slen - (pos + len) + 1);
- }
- _string->release();
- _string = sub;
- return *this;
- }
- ///@todo review for error checking
- String& String::replace(SizeType pos, SizeType len, const StringChar *str)
- {
- AssertFatal( str, "String::replace() - Invalid null ptr argument" );
- AssertFatal( len != 0, "String::replace() - Zero length" );
- AssertFatal( ( pos + len ) <= length(), "String::replace() - Invalid string region" );
- SizeType slen = length();
- SizeType rlen = dStrlen(str);
- U32 newlen = slen - len + rlen;
- StringData *sub;
- if( !newlen )
- sub = StringData::Empty();
- else
- {
- sub = StringData::Create(NULL, newlen);
- String::copy(sub->utf8(),_string->utf8(), pos);
- String::copy(sub->utf8() + pos,str,rlen);
- String::copy(sub->utf8() + pos + rlen,_string->utf8() + pos + len,slen - pos - len + 1);
- }
- _string->release();
- _string = sub;
- return *this;
- }
- String& String::replace( StringChar c1, StringChar c2 )
- {
- if( isEmpty() )
- return *this;
-
- // Create the new string lazily so that we don't needlessly
- // dup strings when there is nothing to replace.
- StringData* sub = NULL;
- bool foundReplacement = false;
- StringChar* c = _string->utf8();
- while( *c )
- {
- if( *c == c1 )
- {
- if( !foundReplacement )
- {
- sub = StringData::Create(_string->utf8(), length());
- c = &sub->utf8()[ c - _string->utf8() ];
- foundReplacement = true;
- }
-
- *c = c2;
- }
- c++;
- }
-
- if( foundReplacement )
- {
- _string->release();
- _string = sub;
- }
- return *this;
- }
- String &String::replace(const String &s1, const String &s2)
- {
- // Find number of occurrences of s1 and
- // Calculate length of the new string...
- const U32 &s1len = s1.length();
- const U32 &s2len = s2.length();
- U32 pos = 0;
- Vector<U32> indices;
- StringChar *walk = _string->utf8();
- while ( walk )
- {
- // Casting away the const... was there a better way?
- walk = (StringChar*)StrFind( _string->utf8(), s1.c_str(), pos, Case|Left );
- if ( walk )
- {
- pos = SizeType(walk - _string->utf8());
- indices.push_back( pos );
- pos += s1len;
- }
- }
- // Early-out, no StringDatas found.
- if ( indices.size() == 0 )
- return *this;
- U32 newSize = size() - ( indices.size() * s1len ) + ( indices.size() * s2len );
- StringData *sub;
- if( newSize == 1 )
- sub = StringData::Empty();
- else
- {
- sub = StringData::Create(NULL, newSize - 1);
- // Now assemble the new string from the pieces of the old...
- // Index into the old string
- pos = 0;
- // Index into the new string
- U32 newPos = 0;
- // Used to store a character count to be memcpy'd
- U32 copyCharCount = 0;
- for ( U32 i = 0; i < indices.size(); i++ )
- {
- const U32 &index = indices[i];
- // Number of chars (if any) before the next indexed StringData
- copyCharCount = index - pos;
- // Copy chars before the StringData if we have any.
- if ( copyCharCount > 0 )
- {
- dMemcpy( sub->utf8() + newPos, _string->utf8() + pos, copyCharCount * sizeof(StringChar) );
- newPos += copyCharCount;
- }
- // Copy over the replacement string.
- if ( s2len > 0 )
- dMemcpy( sub->utf8() + newPos, s2._string->utf8(), s2len * sizeof(StringChar) );
- newPos += s2len;
- pos = index + s1len;
- }
- // There could be characters left in the original string after the last
- // StringData occurrence, which we need to copy now - outside the loop.
- copyCharCount = length() - indices.last() - s1len;
- if ( copyCharCount != 0 )
- dMemcpy( sub->utf8() + newPos, _string->utf8() + pos, copyCharCount * sizeof(StringChar) );
- // Null terminate it!
- sub->utf8()[newSize-1] = 0;
- }
- _string->release();
- _string = sub;
- return *this;
- }
- //-----------------------------------------------------------------------------
- String String::substr(SizeType pos, SizeType len) const
- {
- //PROFILE_SCOPE( String_substr );
-
- AssertFatal( pos <= length(), "String::substr - Invalid position!" );
- if ( len == -1 )
- len = length() - pos;
- AssertFatal( len + pos <= length(), "String::substr - Invalid length!" );
- StringData* sub;
- if( !len )
- sub = StringData::Empty();
- else
- sub = StringData::Create(_string->utf8() + pos, len);
- return sub;
- }
- //-----------------------------------------------------------------------------
- String String::trim() const
- {
- if( isEmpty() )
- return *this;
-
- const StringChar* start = _string->utf8();
- while( *start && dIsspace( *start ) )
- start ++;
-
- const StringChar* end = _string->utf8() + length() - 1;
- while( end > start && dIsspace( *end ) )
- end --;
- end ++;
-
- const U32 len = end - start;
- if( len == length() )
- return *this;
-
- StringData* sub;
- if( !len )
- sub = StringData::Empty();
- else
- sub = StringData::Create(start, len);
- return sub;
- }
- //-----------------------------------------------------------------------------
- String String::expandEscapes() const
- {
- char* tmp = ( char* ) dMalloc( length() * 2 + 1 ); // worst-case situation.
- expandEscape( tmp, c_str() );
- String str( tmp );
- dFree( tmp );
- return str;
- }
- //-----------------------------------------------------------------------------
- String String::collapseEscapes() const
- {
- char* tmp = dStrdup( c_str() );
- collapseEscape( tmp );
- String str( tmp );
- dFree( tmp );
- return str;
- }
- //-----------------------------------------------------------------------------
- void String::split( const char* delimiter, Vector< String >& outElements ) const
- {
- const char* ptr = _string->utf8();
-
- const char* start = ptr;
- while( *ptr )
- {
- // Search for start of delimiter.
-
- if( *ptr != delimiter[ 0 ] )
- ptr ++;
- else
- {
- // Skip delimiter.
-
- const char* end = ptr;
- const char* del = delimiter;
- while( *del && *del == *ptr )
- {
- ptr ++;
- del ++;
- }
-
- // If we didn't match all of delimiter,
- // continue with search.
-
- if( *del != '\0' )
- continue;
-
- // Extract component.
-
- outElements.push_back( String( start, end - start ) );
- start = ptr;
- }
- }
-
- // Add rest of string if there is any.
-
- if( start != ptr )
- outElements.push_back( start );
- }
- //-----------------------------------------------------------------------------
- bool String::startsWith( const char* text ) const
- {
- return dStrStartsWith( _string->utf8(), text );
- }
- //-----------------------------------------------------------------------------
- bool String::endsWith( const char* text ) const
- {
- return dStrEndsWith( _string->utf8(), text );
- }
- //-----------------------------------------------------------------------------
- void String::copy(StringChar* dst, const StringChar *src, U32 len)
- {
- dMemcpy(dst, src, len * sizeof(StringChar));
- }
- //-----------------------------------------------------------------------------
- #if defined(TORQUE_OS_WIN)
- // This standard function is not defined when compiling with VC7...
- #define vsnprintf _vsnprintf
- #endif
- String::StrFormat::~StrFormat()
- {
- if( _dynamicBuffer )
- dFree( _dynamicBuffer );
- }
- S32 String::StrFormat::format( const char *format, va_list args )
- {
- _len=0;
- return formatAppend(format,args);
- }
- S32 String::StrFormat::formatAppend( const char *format, va_list args )
- {
- // Format into the fixed buffer first.
- S32 startLen = _len;
- if (_dynamicBuffer == NULL)
- {
- _len += vsnprintf(_fixedBuffer + _len, sizeof(_fixedBuffer) - _len, format, args);
- if (_len >= 0 && _len < sizeof(_fixedBuffer))
- return _len;
- // Start off the dynamic buffer at twice fixed buffer size
- _len = startLen;
- _dynamicSize = sizeof(_fixedBuffer) * 2;
- _dynamicBuffer = (char*)dMalloc(_dynamicSize);
- dMemcpy(_dynamicBuffer, _fixedBuffer, _len + 1);
- }
- // Format into the dynamic buffer, if the buffer is not large enough, then
- // keep doubling it's size until it is. The buffer is not reallocated
- // using reallocate() to avoid unnecessary buffer copying.
- _len += vsnprintf(_dynamicBuffer + _len, _dynamicSize - _len, format, *(va_list*)args);
- while (_len < 0 || _len >= _dynamicSize)
- {
- _len = startLen;
- _dynamicBuffer = (char*)dRealloc(_dynamicBuffer, _dynamicSize *= 2);
- _len += vsnprintf(_dynamicBuffer + _len, _dynamicSize - _len, format, *(va_list*)args);
- }
- return _len;
- }
- S32 String::StrFormat::append(const char * str, S32 len)
- {
- if (_dynamicBuffer == NULL)
- {
- if (_len+len >= 0 && _len+len < sizeof(_fixedBuffer))
- {
- dMemcpy(_fixedBuffer + _len, str, len);
- _len += len;
- _fixedBuffer[_len] = '\0';
- return _len;
- }
- _dynamicSize = sizeof(_fixedBuffer) * 2;
- _dynamicBuffer = (char*)dMalloc(_dynamicSize);
- dMemcpy(_dynamicBuffer, _fixedBuffer, _len + 1);
- }
- S32 newSize = _dynamicSize;
- while (newSize < _len+len)
- newSize *= 2;
- if (newSize != _dynamicSize)
- _dynamicBuffer = (char*) dRealloc(_dynamicBuffer, newSize);
- _dynamicSize = newSize;
- dMemcpy(_dynamicBuffer + _len, str, len);
- _len += len;
- _dynamicBuffer[_len] = '\0';
- return _len;
- }
- S32 String::StrFormat::append(const char * str)
- {
- return append(str, dStrlen(str));
- }
- char* String::StrFormat::copy( char *buffer ) const
- {
- dMemcpy(buffer, _dynamicBuffer? _dynamicBuffer: _fixedBuffer, _len+1);
- return buffer;
- }
- //-----------------------------------------------------------------------------
- String String::ToString( bool value )
- {
- static String sTrue = "true";
- static String sFalse = "false";
-
- if( value )
- return sTrue;
- return sFalse;
- }
- String String::ToString(const char *str, ...)
- {
- AssertFatal(str,"String:: Invalid null ptr argument");
- // Use the format object
- va_list args;
- va_start(args, str);
- String ret = VToString(str, args);
- va_end(args);
- return ret;
- }
- String String::VToString(const char* str, va_list args)
- {
- StrFormat format(str,args);
- // Copy it into a string
- U32 len = format.length();
- StringData* sub;
- if( !len )
- sub = StringData::Empty();
- else
- {
- sub = StringData::Create(NULL, len);
- format.copy( sub->utf8() );
- sub->utf8()[ len ] = 0;
- }
- return sub;
- }
- String String::SpanToString(const char *start, const char *end)
- {
- if ( end == start )
- return String();
-
- AssertFatal( end > start, "Invalid arguments to String::SpanToString - end is before start" );
- U32 len = U32(end - start);
- String::StringData* sub = StringData::Create(start, len);
- return sub;
- }
- String String::ToLower(const String &string)
- {
- if ( string.isEmpty() )
- return String();
- String::StringData* sub = StringData::Create(string, string.length());
- dStrlwr( sub->utf8() );
- return sub;
- }
- String String::ToUpper(const String &string)
- {
- if ( string.isEmpty() )
- return String();
- String::StringData* sub = StringData::Create(string, string.length());
- dStrupr( sub->utf8() );
- return sub;
- }
- String String::GetTrailingNumber(const char* str, S32& number)
- {
- // Check for trivial strings
- if (!str || !str[0])
- return String::EmptyString;
- // Find the number at the end of the string
- String base(str);
- const char* p = base.c_str() + base.length() - 1;
- // Ignore trailing whitespace
- while ((p != base.c_str()) && dIsspace(*p))
- p--;
- // Need at least one digit!
- if (!isdigit(*p))
- return base;
- // Back up to the first non-digit character
- while ((p != base.c_str()) && isdigit(*p))
- p--;
- // Convert number => allow negative numbers, treat '_' as '-' for Maya
- if ((*p == '-') || (*p == '_'))
- number = -dAtoi(p + 1);
- else
- number = (isdigit(*p) ? dAtoi(p) : dAtoi(++p));
- // Remove space between the name and the number
- while ((p > base.c_str()) && dIsspace(*(p-1)))
- p--;
- return base.substr(0, p - base.c_str());
- }
- String String::GetFirstNumber(const char* str, U32& startPos, U32& endPos)
- {
- // Check for trivial strings
- if (!str || !str[0])
- return String::EmptyString;
- // Find the number at the end of the string
- String base(str);
- const char* p = base.c_str();
- const char* end = base.c_str() + base.length() - 1;
- bool dec = false;
- startPos = 0;
- //Check if we are just a digit
- if(p == end && isdigit(*p))
- return base;
- //Look for the first digit
- while ((p != end) && (dIsspace(*p) || !isdigit(*p)))
- {
- p++;
- startPos++;
- }
- //Handle if we are at the end and found nothing
- if(p == end && !isdigit(*p))
- return "";
- //update our end position at least to the start of our number
- endPos = startPos;
- //Backup our ptr
- const char* backup = p;
- //Check for any negative or decimal values
- if(startPos > 0)
- {
- p--;
- startPos--;
- if(*p == '.')
- {
- dec = true;
- //ignore any duplicate periods
- while ((p != base.c_str()) && (*p == '.'))
- {
- p--;
- startPos--;
- }
- //Found a decimal lets still check for negative sign
- if(startPos > 0)
- {
- p--;
- startPos--;
- if((*p != '-') && (*p != '_'))
- {
- startPos++;
- p++;
- }
- }
- }
- else if((*p != '-') && (*p != '_'))
- {
- //go back to where we where cause no decimal or negative sign found
- startPos++;
- p++;
- }
- }
- //Restore where we were
- p = backup;
- //look for the end of the digits
- bool justFoundDec = false;
- while (p != end)
- {
- if(*p == '.')
- {
- if(dec && !justFoundDec)
- break;
- else
- {
- dec = true;
- justFoundDec = true;
- }
- }
- else if(!isdigit(*p))
- break;
- else if(justFoundDec)
- justFoundDec = false;
- p++;
- endPos++;
- }
- U32 len = (!isdigit(*p)) ? endPos - startPos : (endPos + 1) - startPos;
- return base.substr(startPos, len);
- }
|