str.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include "platform/platform.h"
  25. // Sigh... guess what compiler needs this...
  26. namespace DictHash { U32 hash( String::StringData* ); }
  27. namespace KeyCmp
  28. {
  29. template< typename Key > bool equals( const Key&, const Key& );
  30. template<> bool equals<>( String::StringData* const&, String::StringData* const& );
  31. }
  32. #include "core/util/str.h"
  33. #include "core/util/tDictionary.h"
  34. #include "core/strings/stringFunctions.h"
  35. #include "core/strings/unicode.h"
  36. #include "core/util/hashFunction.h"
  37. #include "core/util/autoPtr.h"
  38. #include "core/util/tVector.h"
  39. #include "core/dataChunker.h"
  40. #include "console/console.h"
  41. #include "console/engineAPI.h"
  42. #include "math/mMathFn.h"
  43. #include "platform/platform.h"
  44. #include "platform/profiler.h"
  45. #include "platform/platformIntrinsics.h"
  46. #include "platform/threads/mutex.h"
  47. #ifndef TORQUE_DISABLE_MEMORY_MANAGER
  48. # undef new
  49. #else
  50. # define _new new
  51. #endif
  52. const String::SizeType String::NPos = U32(~0);
  53. const String String::EmptyString;
  54. /// A delete policy for the AutoPtr class
  55. struct DeleteString
  56. {
  57. template<class T>
  58. static void destroy(T *ptr) { dFree(ptr); }
  59. };
  60. //-----------------------------------------------------------------------------
  61. /// Search for a character.
  62. /// Search for the position of the needle in the haystack.
  63. /// Default mode is StrCase | StrLeft, mode also accepts StrNoCase and StrRight.
  64. /// If pos is non-zero, then in mode StrLeft the search starts at (hay + pos) and
  65. /// in mode StrRight the search starts at (hay + pos - 1)
  66. /// @return Returns a pointer to the location of the character in the haystack or 0
  67. static const char* StrFind(const char* hay, char needle, S32 pos, U32 mode)
  68. {
  69. if (mode & String::Right)
  70. {
  71. // Go to the end first, then search backwards
  72. const char *he = hay;
  73. if (pos)
  74. {
  75. he += pos - 1;
  76. }
  77. else
  78. {
  79. while (*he)
  80. he++;
  81. }
  82. if (mode & String::NoCase)
  83. {
  84. needle = dTolower(needle);
  85. for (; he >= hay; he--)
  86. {
  87. if (dTolower(*he) == needle)
  88. return he;
  89. }
  90. }
  91. else
  92. {
  93. for (; he >= hay; he--)
  94. {
  95. if (*he == needle)
  96. return he;
  97. }
  98. }
  99. return 0;
  100. }
  101. else
  102. {
  103. if (mode & String::NoCase)
  104. {
  105. needle = dTolower(needle);
  106. for (hay += pos; *hay && dTolower(*hay) != needle;)
  107. hay++;
  108. }
  109. else
  110. {
  111. for (hay += pos; *hay && *hay != needle;)
  112. hay++;
  113. }
  114. return *hay ? hay : 0;
  115. }
  116. }
  117. /// Search for a StringData.
  118. /// Search for the position of the needle in the haystack.
  119. /// Default mode is StrCase | StrLeft, mode also accepts StrNoCase and StrRight.
  120. /// If pos is non-zero, then in mode StrLeft the search starts at (hay + pos) and
  121. /// in mode StrRight the search starts at (hay + pos - 1)
  122. /// @return Returns a pointer to the StringData in the haystack or 0
  123. static const char* StrFind(const char* hay, const char* needle, S32 pos, U32 mode)
  124. {
  125. if (mode & String::Right)
  126. {
  127. const char *he = hay;
  128. if (pos)
  129. {
  130. he += pos - 1;
  131. }
  132. else
  133. {
  134. while (*he)
  135. he++;
  136. }
  137. if (mode & String::NoCase)
  138. {
  139. AutoPtr<char,DeleteString> ln(dStrlwr(dStrdup(needle)));
  140. for (; he >= hay; he--)
  141. {
  142. if (dTolower(*he) == *ln)
  143. {
  144. U32 i = 0;
  145. while (ln[i] && ln[i] == dTolower(he[i]))
  146. i++;
  147. if (!ln[i])
  148. return he;
  149. if (!hay[i])
  150. return 0;
  151. }
  152. }
  153. }
  154. else
  155. {
  156. for (; he >= hay; he--)
  157. {
  158. if (*he == *needle)
  159. {
  160. U32 i = 0;
  161. while (needle[i] && needle[i] == he[i])
  162. i++;
  163. if (!needle[i])
  164. return he;
  165. if (!hay[i])
  166. return 0;
  167. }
  168. }
  169. }
  170. return 0;
  171. }
  172. else
  173. {
  174. if (mode & String::NoCase)
  175. {
  176. AutoPtr<char,DeleteString> ln(dStrlwr(dStrdup(needle)));
  177. for (hay += pos; *hay; hay++)
  178. {
  179. if (dTolower(*hay) == *ln)
  180. {
  181. U32 i = 0;
  182. while (ln[i] && ln[i] == dTolower(hay[i]))
  183. i++;
  184. if (!ln[i])
  185. return hay;
  186. if (!hay[i])
  187. return 0;
  188. }
  189. }
  190. }
  191. else
  192. {
  193. for (hay += pos; *hay; hay++)
  194. {
  195. if (*hay == *needle)
  196. {
  197. U32 i = 0;
  198. while (needle[i] && needle[i] == hay[i])
  199. i++;
  200. if (!needle[i])
  201. return hay;
  202. if (!hay[i])
  203. return 0;
  204. }
  205. }
  206. }
  207. }
  208. return 0;
  209. }
  210. //-----------------------------------------------------------------------------
  211. /// Struct with String::StringData's field so we can initialize
  212. /// this without a constructor.
  213. struct StringDataImpl
  214. {
  215. #ifdef TORQUE_DEBUG
  216. StringChar* mString; ///< so we can inspect data in a debugger
  217. #endif
  218. 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).
  219. U32 mLength; ///< String length in bytes excluding null.
  220. mutable U32 mNumChars; ///< Character count; varies from byte count for strings with multi-bytes characters.
  221. mutable U32 mHashCase; ///< case-sensitive hash
  222. mutable U32 mHashNoCase; ///< case-insensitive hash
  223. mutable UTF16* mUTF16;
  224. bool mIsInterned; ///< If true, this string is interned in the string table.
  225. StringChar mData[1]; ///< Start of string data
  226. };
  227. ///
  228. class String::StringData : protected StringDataImpl
  229. {
  230. public:
  231. ///
  232. StringData( const StringChar* data, U32 length, bool interned = false )
  233. {
  234. mRefCount = 1;
  235. mNumChars = U32_MAX;
  236. mHashCase = U32_MAX;
  237. mHashNoCase = U32_MAX;
  238. mUTF16 = NULL;
  239. mLength = length;
  240. mIsInterned = interned;
  241. // mLength is initialized by operator new()
  242. if( data )
  243. {
  244. dMemcpy( mData, data, sizeof( StringChar ) * mLength );
  245. mData[ mLength ] = '\0';
  246. }
  247. #ifdef TORQUE_DEBUG
  248. mString = &mData[0];
  249. #endif
  250. if( mIsInterned )
  251. mRefCount = U32_MAX;
  252. }
  253. ~StringData()
  254. {
  255. AssertFatal( mRefCount == 0, "StringData::~StringData invalid refcount" );
  256. if( mUTF16 )
  257. delete [] mUTF16;
  258. }
  259. static StringData* Create(const StringChar* data, U32 len, bool interned = false)
  260. {
  261. void* memory = dMalloc(sizeof(StringData) + sizeof(StringChar) * len);
  262. StringData* result = new(memory) StringData(data, len, interned);
  263. return result;
  264. }
  265. static StringData* Create(const StringChar* data, U32 len, DataChunker& chunker, bool interned = false)
  266. {
  267. // StringData *str = static_cast<StringData*>( chunker.alloc( size + len * sizeof(StringChar) ) );
  268. // str->mLength = len;
  269. void* memory = chunker.alloc( sizeof(StringData) + len * sizeof(StringChar)); //dMalloc(sizeof(StringData) + sizeof(Stringchar) * len);
  270. StringData* result = new(memory) StringData(data, len, interned);
  271. return result;
  272. }
  273. // void* operator new(size_t size, U32 len);
  274. // void* operator new( size_t size, U32 len, DataChunker& chunker );
  275. // void operator delete(void *);
  276. bool isShared() const
  277. {
  278. return ( mRefCount > 1 );
  279. }
  280. void addRef()
  281. {
  282. if( mRefCount != U32_MAX )
  283. mRefCount ++;
  284. }
  285. void release()
  286. {
  287. if( mRefCount != U32_MAX )
  288. {
  289. -- mRefCount;
  290. if( !mRefCount )
  291. delete this;
  292. }
  293. }
  294. U32 getLength() const
  295. {
  296. return mLength;
  297. }
  298. U32 getDataSize() const
  299. {
  300. return ( mLength + 1 );
  301. }
  302. U32 getDataSizeUTF16() const
  303. {
  304. return ( mLength * sizeof( UTF16 ) );
  305. }
  306. UTF8 operator []( U32 index ) const
  307. {
  308. AssertFatal( index < mLength, "String::StringData::operator []() - index out of range" );
  309. return mData[ index ];
  310. }
  311. UTF8* utf8()
  312. {
  313. return mData;
  314. }
  315. const UTF8* utf8() const
  316. {
  317. return mData;
  318. }
  319. UTF16* utf16() const
  320. {
  321. if( !mUTF16 )
  322. {
  323. // Do this atomically to protect interned strings.
  324. UTF16* utf16 = createUTF16string( mData );
  325. if( !dCompareAndSwap( mUTF16,( UTF16* ) NULL, utf16 ) )
  326. delete [] utf16;
  327. }
  328. return mUTF16;
  329. }
  330. U32 getHashCase() const
  331. {
  332. return mHashCase;
  333. }
  334. U32 getOrCreateHashCase() const
  335. {
  336. if( mHashCase == U32_MAX )
  337. {
  338. PROFILE_SCOPE(StringData_getOrCreateHashCase);
  339. mHashCase = Torque::hash((const U8 *)(mData), mLength, 0);
  340. }
  341. return mHashCase;
  342. }
  343. U32 getHashNoCase() const
  344. {
  345. return mHashNoCase;
  346. }
  347. U32 getOrCreateHashNoCase() const
  348. {
  349. if( mHashNoCase == U32_MAX)
  350. {
  351. PROFILE_SCOPE(StringData_getOrCreateHashNoCase);
  352. UTF8 *lower = new UTF8[ mLength + 1 ];
  353. dStrncpy( lower, utf8(), mLength );
  354. lower[ mLength ] = 0;
  355. dStrlwr( lower );
  356. mHashNoCase = Torque::hash( (const U8*)lower, mLength, 0 );
  357. delete [] lower;
  358. }
  359. return mHashNoCase;
  360. }
  361. U32 getNumChars() const
  362. {
  363. if( mNumChars == U32_MAX )
  364. mNumChars = dStrlen( utf16() );
  365. return mNumChars;
  366. }
  367. bool isInterned() const
  368. {
  369. return mIsInterned;
  370. }
  371. static StringData* Empty()
  372. {
  373. static UTF16 emptyUTF16[ 1 ] = { 0 };
  374. static StringDataImpl empty =
  375. {
  376. #ifdef TORQUE_DEBUG
  377. "", // mString
  378. #endif
  379. U32_MAX, // mRefCount
  380. 0, // mLength
  381. 0, // mNumChars
  382. 0, // mHashCase
  383. 0, // mHashNoCase
  384. emptyUTF16, // mUTF16
  385. true, // mIsInterned
  386. { 0 } // mData
  387. };
  388. return ( StringData* ) &empty;
  389. }
  390. };
  391. //-----------------------------------------------------------------------------
  392. namespace DictHash
  393. {
  394. inline U32 hash( String::StringData* data )
  395. {
  396. return data->getOrCreateHashCase();
  397. }
  398. }
  399. namespace KeyCmp
  400. {
  401. template<>
  402. inline bool equals<>( String::StringData* const& d1, String::StringData* const& d2 )
  403. {
  404. return ( String::compare( d1->utf8(), d2->utf8() ) == 0 );
  405. }
  406. }
  407. /// Type for the intern string table. We don't want String instances directly
  408. /// on the table so that destructors don't run when the table is destroyed. This
  409. /// is because we really shouldn't depend on dtor ordering within this file and thus
  410. /// we can't tell whether the intern string memory is freed before or after the
  411. /// table is destroyed.
  412. struct StringInternTable : public HashTable< String::StringData*, String::StringData* >
  413. {
  414. Mutex mMutex;
  415. DataChunker mChunker;
  416. };
  417. static StringInternTable* sInternTable;
  418. struct KillInternTable
  419. {
  420. ~KillInternTable()
  421. {
  422. if( sInternTable )
  423. delete sInternTable;
  424. }
  425. };
  426. static KillInternTable sKillInternTable;
  427. //-----------------------------------------------------------------------------
  428. #ifdef TORQUE_DEBUG
  429. /// Tracks the number of bytes allocated for strings.
  430. /// @bug This currently does not include UTF16 allocations.
  431. static U32 sgStringMemBytes;
  432. /// Tracks the number of Strings which are currently instantiated.
  433. static U32 sgStringInstances;
  434. #endif
  435. DefineEngineFunction( dumpStringMemStats, void, (), , "()"
  436. "@brief Dumps information about String memory usage\n\n"
  437. "@ingroup Debugging\n"
  438. "@ingroup Strings\n")
  439. {
  440. #ifdef TORQUE_DEBUG
  441. Con::printf( "String Data: %i instances, %i bytes", sgStringInstances, sgStringMemBytes );
  442. #endif
  443. }
  444. //-----------------------------------------------------------------------------
  445. /*
  446. void* String::StringData::operator new( size_t size, U32 len )
  447. {
  448. AssertFatal( len != 0, "String::StringData::operator new() - string must not be empty" );
  449. StringData *str = static_cast<StringData*>( dMalloc( size + len * sizeof(StringChar) ) );
  450. str->mLength = len;
  451. #ifdef TORQUE_DEBUG
  452. dFetchAndAdd( sgStringMemBytes, size + len * sizeof(StringChar) );
  453. dFetchAndAdd( sgStringInstances, 1 );
  454. #endif
  455. return str;
  456. }
  457. */
  458. /*
  459. void String::StringData::operator delete(void *ptr)
  460. {
  461. StringData* sub = static_cast<StringData *>(ptr);
  462. AssertFatal( sub->mRefCount == 0, "StringData::delete() - invalid refcount" );
  463. #ifdef TORQUE_DEBUG
  464. dFetchAndAdd( sgStringMemBytes, U32( -( S32( sizeof( StringData ) + sub->mLength * sizeof(StringChar) ) ) ) );
  465. dFetchAndAdd( sgStringInstances, U32( -1 ) );
  466. #endif
  467. dFree( ptr );
  468. }
  469. */
  470. /*
  471. void* String::StringData::operator new( size_t size, U32 len, DataChunker& chunker )
  472. {
  473. AssertFatal( len != 0, "String::StringData::operator new() - string must not be empty" );
  474. StringData *str = static_cast<StringData*>( chunker.alloc( size + len * sizeof(StringChar) ) );
  475. str->mLength = len;
  476. #ifdef TORQUE_DEBUG
  477. dFetchAndAdd( sgStringMemBytes, size + len * sizeof(StringChar) );
  478. dFetchAndAdd( sgStringInstances, 1 );
  479. #endif
  480. return str;
  481. }
  482. */
  483. //-----------------------------------------------------------------------------
  484. String::String()
  485. {
  486. PROFILE_SCOPE(String_default_constructor);
  487. _string = StringData::Empty();
  488. }
  489. String::String(const String &str)
  490. {
  491. PROFILE_SCOPE(String_String_constructor);
  492. _string = str._string;
  493. _string->addRef();
  494. }
  495. String::String(const StringChar *str)
  496. {
  497. PROFILE_SCOPE(String_char_constructor);
  498. if( str && *str )
  499. {
  500. U32 len = dStrlen(str);
  501. // _string = new ( len ) StringData( str );
  502. _string = StringData::Create(str, len);
  503. }
  504. else
  505. _string = StringData::Empty();
  506. }
  507. String::String(const StringChar *str, SizeType len)
  508. {
  509. PROFILE_SCOPE(String_char_len_constructor);
  510. if (str && *str && len!=0)
  511. {
  512. _string = StringData::Create(str, len);
  513. }
  514. else
  515. _string = StringData::Empty();
  516. }
  517. String::String(const UTF16 *str)
  518. {
  519. PROFILE_SCOPE(String_UTF16_constructor);
  520. if( str && str[ 0 ] )
  521. {
  522. UTF8* utf8 = createUTF8string( str );
  523. U32 len = dStrlen( utf8 );
  524. _string = StringData::Create(utf8, len);
  525. delete [] utf8;
  526. }
  527. else
  528. _string = StringData::Empty();
  529. }
  530. String::~String()
  531. {
  532. if (_string && _string != StringData::Empty())
  533. _string->release();
  534. }
  535. //-----------------------------------------------------------------------------
  536. String String::intern() const
  537. {
  538. if( isInterned() )
  539. return *this;
  540. // Create the intern table, if we haven't already.
  541. if( !sInternTable )
  542. sInternTable = new StringInternTable;
  543. // Lock the string table.
  544. MutexHandle mutex;
  545. mutex.lock( &sInternTable->mMutex );
  546. // Lookup.
  547. StringInternTable::Iterator iter = sInternTable->find( _string );
  548. if( iter != sInternTable->end() )
  549. return ( *iter ).value;
  550. // Create new.
  551. StringData* data = StringData::Create(c_str(), length(), sInternTable->mChunker);
  552. //StringData* data = new ( length(), sInternTable->mChunker ) StringData( c_str(), true );
  553. iter = sInternTable->insertUnique( data, data );
  554. return ( *iter ).value;
  555. }
  556. //-----------------------------------------------------------------------------
  557. const StringChar* String::c_str() const
  558. {
  559. return _string->utf8();
  560. }
  561. const UTF16 *String::utf16() const
  562. {
  563. return _string->utf16();
  564. }
  565. String::SizeType String::length() const
  566. {
  567. return _string->getLength();
  568. }
  569. String::SizeType String::size() const
  570. {
  571. return _string->getDataSize();
  572. }
  573. String::SizeType String::numChars() const
  574. {
  575. return _string->getNumChars();
  576. }
  577. bool String::isEmpty() const
  578. {
  579. return ( _string == StringData::Empty() );
  580. }
  581. bool String::isEmpty(const char* str)
  582. {
  583. return str == 0 || str[0] == '\0';
  584. }
  585. bool String::isShared() const
  586. {
  587. return _string->isShared();
  588. }
  589. bool String::isSame( const String& str ) const
  590. {
  591. return ( _string == str._string );
  592. }
  593. bool String::isInterned() const
  594. {
  595. return ( _string->isInterned() );
  596. }
  597. U32 String::getHashCaseSensitive() const
  598. {
  599. return _string->getOrCreateHashCase();
  600. }
  601. U32 String::getHashCaseInsensitive() const
  602. {
  603. return _string->getOrCreateHashNoCase();
  604. }
  605. //-----------------------------------------------------------------------------
  606. String::SizeType String::find(const String &str, SizeType pos, U32 mode) const
  607. {
  608. return find(str._string->utf8(), pos, mode);
  609. }
  610. String& String::insert(SizeType pos, const String &str)
  611. {
  612. return insert(pos, str._string->utf8());
  613. }
  614. String& String::replace(SizeType pos, SizeType len, const String &str)
  615. {
  616. return replace(pos, len, str._string->utf8());
  617. }
  618. //-----------------------------------------------------------------------------
  619. String& String::operator=(StringChar c)
  620. {
  621. _string->release();
  622. //_string = new ( 2 ) StringData( 0 );
  623. _string = StringData::Create(NULL, 2);
  624. _string->utf8()[ 0 ] = c;
  625. _string->utf8()[ 1 ] = '\0';
  626. return *this;
  627. }
  628. String& String::operator+=(StringChar c)
  629. {
  630. // Append the given string into a new string
  631. U32 len = _string->getLength();
  632. // StringData* sub = new ( len + 1 ) StringData( NULL );
  633. StringData* sub = StringData::Create(NULL, len + 1);
  634. copy( sub->utf8(), _string->utf8(), len );
  635. sub->utf8()[len] = c;
  636. sub->utf8()[len+1] = 0;
  637. _string->release();
  638. _string = sub;
  639. return *this;
  640. }
  641. //-----------------------------------------------------------------------------
  642. String& String::operator=(const StringChar *str)
  643. {
  644. // Protect against self assignment which is not only a
  645. // waste of time, but can also lead to the string being
  646. // freed before it can be reassigned.
  647. if ( _string->utf8() == str )
  648. return *this;
  649. _string->release();
  650. if (str && *str)
  651. {
  652. U32 len = dStrlen(str);
  653. // _string = new ( len ) StringData( str );
  654. _string = StringData::Create(str, len);
  655. }
  656. else
  657. _string = StringData::Empty();
  658. return *this;
  659. }
  660. String& String::operator=(const String &src)
  661. {
  662. // Inc src first to avoid assignment to self problems.
  663. src._string->addRef();
  664. _string->release();
  665. _string = src._string;
  666. return *this;
  667. }
  668. String& String::operator+=(const StringChar *src)
  669. {
  670. if( src == NULL || !*src )
  671. return *this;
  672. // Append the given string into a new string
  673. U32 lena = _string->getLength();
  674. U32 lenb = dStrlen(src);
  675. U32 newlen = lena + lenb;
  676. StringData* sub;
  677. if( !newlen )
  678. sub = StringData::Empty();
  679. else
  680. {
  681. // sub = new ( newlen ) StringData( NULL );
  682. sub = StringData::Create(NULL, newlen);
  683. copy(sub->utf8(),_string->utf8(),lena);
  684. copy(sub->utf8() + lena,src,lenb + 1);
  685. }
  686. _string->release();
  687. _string = sub;
  688. return *this;
  689. }
  690. String& String::operator+=(const String &src)
  691. {
  692. if( src.isEmpty() )
  693. return *this;
  694. // Append the given string into a new string
  695. U32 lena = _string->getLength();
  696. U32 lenb = src._string->getLength();
  697. U32 newlen = lena + lenb;
  698. StringData* sub;
  699. if( !newlen )
  700. sub = StringData::Empty();
  701. else
  702. {
  703. //sub = new ( newlen ) StringData( NULL );
  704. sub = StringData::Create(NULL, newlen);
  705. copy(sub->utf8(),_string->utf8(),lena);
  706. copy(sub->utf8() + lena,src._string->utf8(),lenb + 1);
  707. }
  708. _string->release();
  709. _string = sub;
  710. return *this;
  711. }
  712. //-----------------------------------------------------------------------------
  713. String operator+(const String &a, const String &b)
  714. {
  715. PROFILE_SCOPE( String_String_plus_String );
  716. if( a.isEmpty() )
  717. return b;
  718. else if( b.isEmpty() )
  719. return a;
  720. U32 lena = a.length();
  721. U32 lenb = b.length();
  722. //String::StringData *sub = new ( lena + lenb ) String::StringData( NULL );
  723. String::StringData* sub = String::StringData::Create(NULL, lena + lenb);
  724. String::copy(sub->utf8(),a._string->utf8(),lena);
  725. String::copy(sub->utf8() + lena,b._string->utf8(),lenb + 1);
  726. return String(sub);
  727. }
  728. String operator+(const String &a, StringChar c)
  729. {
  730. //PROFILE_SCOPE( String_String_plus_Char );
  731. U32 lena = a.length();
  732. // String::StringData *sub = new ( lena + 1 ) String::StringData( NULL );
  733. String::StringData* sub = String::StringData::Create(NULL, lena + 1);
  734. String::copy(sub->utf8(),a._string->utf8(),lena);
  735. sub->utf8()[lena] = c;
  736. sub->utf8()[lena+1] = 0;
  737. return String(sub);
  738. }
  739. String operator+(StringChar c, const String &a)
  740. {
  741. //PROFILE_SCOPE( String_Char_plus_String );
  742. U32 lena = a.length();
  743. // String::StringData *sub = new ( lena + 1 ) String::StringData( NULL );
  744. String::StringData* sub = String::StringData::Create(NULL, lena + 1);
  745. String::copy(sub->utf8() + 1,a._string->utf8(),lena + 1);
  746. sub->utf8()[0] = c;
  747. return String(sub);
  748. }
  749. String operator+(const String &a, const StringChar *b)
  750. {
  751. //PROFILE_SCOPE( String_String_plus_CString );
  752. AssertFatal(b,"String:: Invalid null ptr argument");
  753. if( a.isEmpty() )
  754. return String( b );
  755. U32 lena = a.length();
  756. U32 lenb = dStrlen(b);
  757. if( !lenb )
  758. return a;
  759. // String::StringData *sub = new ( lena + lenb ) String::StringData( NULL );
  760. String::StringData* sub = String::StringData::Create(NULL, lena + lenb);
  761. String::copy(sub->utf8(),a._string->utf8(),lena);
  762. String::copy(sub->utf8() + lena,b,lenb + 1);
  763. return String(sub);
  764. }
  765. String operator+(const StringChar *a, const String &b)
  766. {
  767. //PROFILE_SCOPE( String_CString_plus_String );
  768. AssertFatal(a,"String:: Invalid null ptr argument");
  769. if( b.isEmpty() )
  770. return String( a );
  771. U32 lena = dStrlen(a);
  772. if( !lena )
  773. return b;
  774. U32 lenb = b.length();
  775. //String::StringData* sub = new ( lena + lenb ) String::StringData( NULL );
  776. String::StringData* sub = String::StringData::Create(NULL, lena + lenb);
  777. String::copy(sub->utf8(),a,lena);
  778. String::copy(sub->utf8() + lena,b._string->utf8(),lenb + 1);
  779. return String(sub);
  780. }
  781. bool String::operator==(const String &str) const
  782. {
  783. //PROFILE_SCOPE( String_op_equal );
  784. if( str._string == _string )
  785. return true;
  786. else if( str._string->isInterned() && _string->isInterned() )
  787. return false;
  788. else if( str.length() != length() )
  789. return false;
  790. else if( str._string->getHashCase() != U32_MAX
  791. && _string->getHashCase() != U32_MAX
  792. && str._string->getHashCase() != _string->getHashCase() )
  793. return false;
  794. else
  795. return ( dMemcmp( str._string->utf8(), _string->utf8(), _string->getLength() ) == 0 );
  796. }
  797. bool String::operator==( StringChar c ) const
  798. {
  799. if( !_string || _string->getLength() != 1 )
  800. return false;
  801. else
  802. return ( _string->utf8()[ 0 ] == c );
  803. }
  804. bool String::operator<(const String &str) const
  805. {
  806. return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) < 0 );
  807. }
  808. bool String::operator>(const String &str) const
  809. {
  810. return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) > 0 );
  811. }
  812. bool String::operator<=(const String &str) const
  813. {
  814. return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) <= 0 );
  815. }
  816. bool String::operator>=(const String &str) const
  817. {
  818. return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) >= 0 );
  819. }
  820. //-----------------------------------------------------------------------------
  821. // Base functions for string comparison
  822. S32 String::compare(const StringChar *str, SizeType len, U32 mode) const
  823. {
  824. PROFILE_SCOPE( String_compare );
  825. AssertFatal(str,"String:: Invalid null ptr argument");
  826. const StringChar *p1 = _string->utf8();
  827. const StringChar *p2 = str;
  828. if (p1 == p2)
  829. return 0;
  830. if( mode & String::Right )
  831. {
  832. U32 n = len;
  833. if( n > length() )
  834. n = length();
  835. p1 += length() - n;
  836. p2 += dStrlen( str ) - n;
  837. }
  838. if (mode & String::NoCase)
  839. {
  840. if (len)
  841. {
  842. for (;--len; p1++,p2++)
  843. {
  844. if (dTolower(*p1) != dTolower(*p2) || !*p1)
  845. break;
  846. }
  847. }
  848. else
  849. {
  850. while (dTolower(*p1) == dTolower(*p2) && *p1)
  851. {
  852. p1++;
  853. p2++;
  854. }
  855. }
  856. return dTolower(*p1) - dTolower(*p2);
  857. }
  858. if (len)
  859. return dMemcmp(p1,p2,len);
  860. while (*p1 == *p2 && *p1)
  861. {
  862. p1++;
  863. p2++;
  864. }
  865. return *p1 - *p2;
  866. }
  867. S32 String::compare(const String &str, SizeType len, U32 mode) const
  868. {
  869. if ( str._string == _string )
  870. return 0;
  871. return compare( str.c_str(), len, mode );
  872. }
  873. S32 String::compare(const char *str1, const char *str2)
  874. {
  875. return strcmp(str1, str2);
  876. }
  877. S32 String::compare(const UTF16 *str1, const UTF16 *str2)
  878. {
  879. #if defined(TORQUE_OS_WIN) || defined(TORQUE_OS_XBOX) || defined(TORQUE_OS_XENON)
  880. return wcscmp(reinterpret_cast<const wchar_t *>(str1), reinterpret_cast<const wchar_t *>(str2));
  881. #else
  882. S32 ret;
  883. const UTF16 *a, *b;
  884. a = str1;
  885. b = str2;
  886. while (((ret = *a - *b) == 0) && *a && *b)
  887. a++, b++;
  888. return ret;
  889. #endif
  890. }
  891. bool String::equal(const String &str, U32 mode) const
  892. {
  893. if( !mode )
  894. return ( *this == str );
  895. else
  896. {
  897. if( _string == str._string )
  898. return true;
  899. else if( _string->isInterned() && str._string->isInterned() )
  900. return false;
  901. else if( length() != str.length() )
  902. return false;
  903. else if( _string->getHashNoCase() != U32_MAX
  904. && str._string->getHashNoCase() != U32_MAX
  905. && _string->getHashNoCase() != str._string->getHashNoCase() )
  906. return false;
  907. else
  908. return ( compare( str.c_str(), length(), mode ) == 0 );
  909. }
  910. }
  911. //-----------------------------------------------------------------------------
  912. String::SizeType String::find(StringChar c, SizeType pos, U32 mode) const
  913. {
  914. const StringChar* ptr = StrFind(_string->utf8(),c,pos,mode);
  915. return ptr? SizeType(ptr - _string->utf8()): NPos;
  916. }
  917. String::SizeType String::find(const StringChar *str, SizeType pos, U32 mode) const
  918. {
  919. AssertFatal(str,"String:: Invalid null ptr argument");
  920. const StringChar* ptr = StrFind(_string->utf8(),str,pos,mode);
  921. return ptr? SizeType(ptr - _string->utf8()): NPos;
  922. }
  923. //-----------------------------------------------------------------------------
  924. String& String::insert(SizeType pos, const StringChar *str)
  925. {
  926. AssertFatal(str,"String:: Invalid null ptr argument");
  927. return insert(pos,str,dStrlen(str));
  928. }
  929. ///@todo review for error checking
  930. String& String::insert(SizeType pos, const StringChar *str, SizeType len)
  931. {
  932. if( !len )
  933. return *this;
  934. AssertFatal( str, "String:: Invalid null ptr argument" );
  935. SizeType lena = length();
  936. AssertFatal((pos <= lena),"Calling String::insert with position greater than length");
  937. U32 newlen = lena + len;
  938. StringData *sub;
  939. if( !newlen )
  940. sub = StringData::Empty();
  941. else
  942. {
  943. // sub = new ( newlen ) StringData( NULL );
  944. sub = StringData::Create(NULL, newlen);
  945. String::copy(sub->utf8(),_string->utf8(),pos);
  946. String::copy(sub->utf8() + pos,str,len);
  947. String::copy(sub->utf8() + pos + len,_string->utf8() + pos,lena - pos + 1);
  948. }
  949. _string->release();
  950. _string = sub;
  951. return *this;
  952. }
  953. String& String::erase(SizeType pos, SizeType len)
  954. {
  955. AssertFatal( len != 0, "String::erase() - Calling String::erase with 0 length" );
  956. AssertFatal( ( pos + len ) <= length(), "String::erase() - Invalid string region" );
  957. if( !len )
  958. return *this;
  959. SizeType slen = length();
  960. U32 newlen = slen - len;
  961. StringData *sub;
  962. if( !newlen )
  963. sub = StringData::Empty();
  964. else
  965. {
  966. // sub = new ( newlen ) StringData( NULL );
  967. sub = StringData::Create(NULL, newlen);
  968. if (pos > 0)
  969. String::copy(sub->utf8(),_string->utf8(),pos);
  970. String::copy(sub->utf8() + pos, _string->utf8() + pos + len, slen - (pos + len) + 1);
  971. }
  972. _string->release();
  973. _string = sub;
  974. return *this;
  975. }
  976. ///@todo review for error checking
  977. String& String::replace(SizeType pos, SizeType len, const StringChar *str)
  978. {
  979. AssertFatal( str, "String::replace() - Invalid null ptr argument" );
  980. AssertFatal( len != 0, "String::replace() - Zero length" );
  981. AssertFatal( ( pos + len ) <= length(), "String::replace() - Invalid string region" );
  982. SizeType slen = length();
  983. SizeType rlen = dStrlen(str);
  984. U32 newlen = slen - len + rlen;
  985. StringData *sub;
  986. if( !newlen )
  987. sub = StringData::Empty();
  988. else
  989. {
  990. // sub = new ( newlen ) StringData( NULL );
  991. sub = StringData::Create(NULL, newlen);
  992. String::copy(sub->utf8(),_string->utf8(), pos);
  993. String::copy(sub->utf8() + pos,str,rlen);
  994. String::copy(sub->utf8() + pos + rlen,_string->utf8() + pos + len,slen - pos - len + 1);
  995. }
  996. _string->release();
  997. _string = sub;
  998. return *this;
  999. }
  1000. String& String::replace( StringChar c1, StringChar c2 )
  1001. {
  1002. if( isEmpty() )
  1003. return *this;
  1004. // Create the new string lazily so that we don't needlessly
  1005. // dup strings when there is nothing to replace.
  1006. StringData* sub = NULL;
  1007. bool foundReplacement = false;
  1008. StringChar* c = _string->utf8();
  1009. while( *c )
  1010. {
  1011. if( *c == c1 )
  1012. {
  1013. if( !foundReplacement )
  1014. {
  1015. //sub = new ( length() ) StringData( _string->utf8() );
  1016. sub = StringData::Create(_string->utf8(), length());
  1017. c = &sub->utf8()[ c - _string->utf8() ];
  1018. foundReplacement = true;
  1019. }
  1020. *c = c2;
  1021. }
  1022. c++;
  1023. }
  1024. if( foundReplacement )
  1025. {
  1026. _string->release();
  1027. _string = sub;
  1028. }
  1029. return *this;
  1030. }
  1031. String &String::replace(const String &s1, const String &s2)
  1032. {
  1033. // Find number of occurrences of s1 and
  1034. // Calculate length of the new string...
  1035. const U32 &s1len = s1.length();
  1036. const U32 &s2len = s2.length();
  1037. U32 pos = 0;
  1038. Vector<U32> indices;
  1039. StringChar *walk = _string->utf8();
  1040. while ( walk )
  1041. {
  1042. // Casting away the const... was there a better way?
  1043. walk = (StringChar*)StrFind( _string->utf8(), s1.c_str(), pos, Case|Left );
  1044. if ( walk )
  1045. {
  1046. pos = SizeType(walk - _string->utf8());
  1047. indices.push_back( pos );
  1048. pos += s1len;
  1049. }
  1050. }
  1051. // Early-out, no StringDatas found.
  1052. if ( indices.size() == 0 )
  1053. return *this;
  1054. U32 newSize = size() - ( indices.size() * s1len ) + ( indices.size() * s2len );
  1055. StringData *sub;
  1056. if( newSize == 1 )
  1057. sub = StringData::Empty();
  1058. else
  1059. {
  1060. //sub = new (newSize - 1 ) StringData( NULL );
  1061. sub = StringData::Create(NULL, newSize - 1);
  1062. // Now assemble the new string from the pieces of the old...
  1063. // Index into the old string
  1064. pos = 0;
  1065. // Index into the new string
  1066. U32 newPos = 0;
  1067. // Used to store a character count to be memcpy'd
  1068. U32 copyCharCount = 0;
  1069. for ( U32 i = 0; i < indices.size(); i++ )
  1070. {
  1071. const U32 &index = indices[i];
  1072. // Number of chars (if any) before the next indexed StringData
  1073. copyCharCount = index - pos;
  1074. // Copy chars before the StringData if we have any.
  1075. if ( copyCharCount > 0 )
  1076. {
  1077. dMemcpy( sub->utf8() + newPos, _string->utf8() + pos, copyCharCount * sizeof(StringChar) );
  1078. newPos += copyCharCount;
  1079. }
  1080. // Copy over the replacement string.
  1081. if ( s2len > 0 )
  1082. dMemcpy( sub->utf8() + newPos, s2._string->utf8(), s2len * sizeof(StringChar) );
  1083. newPos += s2len;
  1084. pos = index + s1len;
  1085. }
  1086. // There could be characters left in the original string after the last
  1087. // StringData occurrence, which we need to copy now - outside the loop.
  1088. copyCharCount = length() - indices.last() - s1len;
  1089. if ( copyCharCount != 0 )
  1090. dMemcpy( sub->utf8() + newPos, _string->utf8() + pos, copyCharCount * sizeof(StringChar) );
  1091. // Null terminate it!
  1092. sub->utf8()[newSize-1] = 0;
  1093. }
  1094. _string->release();
  1095. _string = sub;
  1096. return *this;
  1097. }
  1098. //-----------------------------------------------------------------------------
  1099. String String::substr(SizeType pos, SizeType len) const
  1100. {
  1101. //PROFILE_SCOPE( String_substr );
  1102. AssertFatal( pos <= length(), "String::substr - Invalid position!" );
  1103. if ( len == -1 )
  1104. len = length() - pos;
  1105. AssertFatal( len + pos <= length(), "String::substr - Invalid length!" );
  1106. StringData* sub;
  1107. if( !len )
  1108. sub = StringData::Empty();
  1109. else
  1110. sub = StringData::Create(_string->utf8() + pos, len);
  1111. //sub = new ( len ) StringData( _string->utf8() + pos );
  1112. return sub;
  1113. }
  1114. //-----------------------------------------------------------------------------
  1115. String String::trim() const
  1116. {
  1117. if( isEmpty() )
  1118. return *this;
  1119. const StringChar* start = _string->utf8();
  1120. while( *start && dIsspace( *start ) )
  1121. start ++;
  1122. const StringChar* end = _string->utf8() + length() - 1;
  1123. while( end > start && dIsspace( *end ) )
  1124. end --;
  1125. end ++;
  1126. const U32 len = end - start;
  1127. if( len == length() )
  1128. return *this;
  1129. StringData* sub;
  1130. if( !len )
  1131. sub = StringData::Empty();
  1132. else
  1133. sub = StringData::Create(start, len);
  1134. //sub = new ( len ) StringData( start );
  1135. return sub;
  1136. }
  1137. //-----------------------------------------------------------------------------
  1138. String String::expandEscapes() const
  1139. {
  1140. char* tmp = ( char* ) dMalloc( length() * 2 + 1 ); // worst-case situation.
  1141. expandEscape( tmp, c_str() );
  1142. String str( tmp );
  1143. dFree( tmp );
  1144. return str;
  1145. }
  1146. //-----------------------------------------------------------------------------
  1147. String String::collapseEscapes() const
  1148. {
  1149. char* tmp = dStrdup( c_str() );
  1150. collapseEscape( tmp );
  1151. String str( tmp );
  1152. dFree( tmp );
  1153. return str;
  1154. }
  1155. //-----------------------------------------------------------------------------
  1156. void String::split( const char* delimiter, Vector< String >& outElements ) const
  1157. {
  1158. const char* ptr = _string->utf8();
  1159. const char* start = ptr;
  1160. while( *ptr )
  1161. {
  1162. // Search for start of delimiter.
  1163. if( *ptr != delimiter[ 0 ] )
  1164. ptr ++;
  1165. else
  1166. {
  1167. // Skip delimiter.
  1168. const char* end = ptr;
  1169. const char* del = delimiter;
  1170. while( *del && *del == *ptr )
  1171. {
  1172. ptr ++;
  1173. del ++;
  1174. }
  1175. // If we didn't match all of delimiter,
  1176. // continue with search.
  1177. if( *del != '\0' )
  1178. continue;
  1179. // Extract component.
  1180. outElements.push_back( String( start, end - start ) );
  1181. start = ptr;
  1182. }
  1183. }
  1184. // Add rest of string if there is any.
  1185. if( start != ptr )
  1186. outElements.push_back( start );
  1187. }
  1188. //-----------------------------------------------------------------------------
  1189. bool String::startsWith( const char* text ) const
  1190. {
  1191. return dStrStartsWith( _string->utf8(), text );
  1192. }
  1193. //-----------------------------------------------------------------------------
  1194. bool String::endsWith( const char* text ) const
  1195. {
  1196. return dStrEndsWith( _string->utf8(), text );
  1197. }
  1198. //-----------------------------------------------------------------------------
  1199. void String::copy(StringChar* dst, const StringChar *src, U32 len)
  1200. {
  1201. dMemcpy(dst, src, len * sizeof(StringChar));
  1202. }
  1203. //-----------------------------------------------------------------------------
  1204. #if defined(TORQUE_OS_WIN)
  1205. // This standard function is not defined when compiling with VC7...
  1206. #define vsnprintf _vsnprintf
  1207. #endif
  1208. String::StrFormat::~StrFormat()
  1209. {
  1210. if( _dynamicBuffer )
  1211. dFree( _dynamicBuffer );
  1212. }
  1213. S32 String::StrFormat::format( const char *format, va_list args )
  1214. {
  1215. _len=0;
  1216. return formatAppend(format,args);
  1217. }
  1218. S32 String::StrFormat::formatAppend( const char *format, va_list args )
  1219. {
  1220. // Format into the fixed buffer first.
  1221. S32 startLen = _len;
  1222. if (_dynamicBuffer == NULL)
  1223. {
  1224. _len += vsnprintf(_fixedBuffer + _len, sizeof(_fixedBuffer) - _len, format, args);
  1225. if (_len >= 0 && _len < sizeof(_fixedBuffer))
  1226. return _len;
  1227. // Start off the dynamic buffer at twice fixed buffer size
  1228. _len = startLen;
  1229. _dynamicSize = sizeof(_fixedBuffer) * 2;
  1230. _dynamicBuffer = (char*)dMalloc(_dynamicSize);
  1231. dMemcpy(_dynamicBuffer, _fixedBuffer, _len + 1);
  1232. }
  1233. // Format into the dynamic buffer, if the buffer is not large enough, then
  1234. // keep doubling it's size until it is. The buffer is not reallocated
  1235. // using reallocate() to avoid unnecessary buffer copying.
  1236. _len += vsnprintf(_dynamicBuffer + _len, _dynamicSize - _len, format, *(va_list*)args);
  1237. while (_len < 0 || _len >= _dynamicSize)
  1238. {
  1239. _len = startLen;
  1240. _dynamicBuffer = (char*)dRealloc(_dynamicBuffer, _dynamicSize *= 2);
  1241. _len += vsnprintf(_dynamicBuffer + _len, _dynamicSize - _len, format, *(va_list*)args);
  1242. }
  1243. return _len;
  1244. }
  1245. S32 String::StrFormat::append(const char * str, S32 len)
  1246. {
  1247. if (_dynamicBuffer == NULL)
  1248. {
  1249. if (_len+len >= 0 && _len+len < sizeof(_fixedBuffer))
  1250. {
  1251. dMemcpy(_fixedBuffer + _len, str, len);
  1252. _len += len;
  1253. _fixedBuffer[_len] = '\0';
  1254. return _len;
  1255. }
  1256. _dynamicSize = sizeof(_fixedBuffer) * 2;
  1257. _dynamicBuffer = (char*)dMalloc(_dynamicSize);
  1258. dMemcpy(_dynamicBuffer, _fixedBuffer, _len + 1);
  1259. }
  1260. S32 newSize = _dynamicSize;
  1261. while (newSize < _len+len)
  1262. newSize *= 2;
  1263. if (newSize != _dynamicSize)
  1264. _dynamicBuffer = (char*) dRealloc(_dynamicBuffer, newSize);
  1265. _dynamicSize = newSize;
  1266. dMemcpy(_dynamicBuffer + _len, str, len);
  1267. _len += len;
  1268. _dynamicBuffer[_len] = '\0';
  1269. return _len;
  1270. }
  1271. S32 String::StrFormat::append(const char * str)
  1272. {
  1273. return append(str, dStrlen(str));
  1274. }
  1275. char* String::StrFormat::copy( char *buffer ) const
  1276. {
  1277. dMemcpy(buffer, _dynamicBuffer? _dynamicBuffer: _fixedBuffer, _len+1);
  1278. return buffer;
  1279. }
  1280. //-----------------------------------------------------------------------------
  1281. String String::ToString( bool value )
  1282. {
  1283. static String sTrue = "true";
  1284. static String sFalse = "false";
  1285. if( value )
  1286. return sTrue;
  1287. return sFalse;
  1288. }
  1289. String String::ToString(const char *str, ...)
  1290. {
  1291. AssertFatal(str,"String:: Invalid null ptr argument");
  1292. // Use the format object
  1293. va_list args;
  1294. va_start(args, str);
  1295. String ret = VToString(str, args);
  1296. va_end(args);
  1297. return ret;
  1298. }
  1299. String String::VToString(const char* str, va_list args)
  1300. {
  1301. StrFormat format(str,args);
  1302. // Copy it into a string
  1303. U32 len = format.length();
  1304. StringData* sub;
  1305. if( !len )
  1306. sub = StringData::Empty();
  1307. else
  1308. {
  1309. //sub = new ( len ) StringData( NULL );
  1310. sub = StringData::Create(NULL, len);
  1311. format.copy( sub->utf8() );
  1312. sub->utf8()[ len ] = 0;
  1313. }
  1314. return sub;
  1315. }
  1316. String String::SpanToString(const char *start, const char *end)
  1317. {
  1318. if ( end == start )
  1319. return String();
  1320. AssertFatal( end > start, "Invalid arguments to String::SpanToString - end is before start" );
  1321. U32 len = U32(end - start);
  1322. //StringData* sub = new ( len ) StringData( start );
  1323. String::StringData* sub = StringData::Create(start, len);
  1324. return sub;
  1325. }
  1326. String String::ToLower(const String &string)
  1327. {
  1328. if ( string.isEmpty() )
  1329. return String();
  1330. //StringData* sub = new ( string.length() ) StringData( string );
  1331. String::StringData* sub = StringData::Create(string, string.length());
  1332. dStrlwr( sub->utf8() );
  1333. return sub;
  1334. }
  1335. String String::ToUpper(const String &string)
  1336. {
  1337. if ( string.isEmpty() )
  1338. return String();
  1339. // StringData* sub = new ( string.length() ) StringData( string );
  1340. String::StringData* sub = StringData::Create(string, string.length());
  1341. dStrupr( sub->utf8() );
  1342. return sub;
  1343. }
  1344. String String::GetTrailingNumber(const char* str, S32& number)
  1345. {
  1346. // Check for trivial strings
  1347. if (!str || !str[0])
  1348. return String::EmptyString;
  1349. // Find the number at the end of the string
  1350. String base(str);
  1351. const char* p = base.c_str() + base.length() - 1;
  1352. // Ignore trailing whitespace
  1353. while ((p != base.c_str()) && dIsspace(*p))
  1354. p--;
  1355. // Need at least one digit!
  1356. if (!isdigit(*p))
  1357. return base;
  1358. // Back up to the first non-digit character
  1359. while ((p != base.c_str()) && isdigit(*p))
  1360. p--;
  1361. // Convert number => allow negative numbers, treat '_' as '-' for Maya
  1362. if ((*p == '-') || (*p == '_'))
  1363. number = -dAtoi(p + 1);
  1364. else
  1365. number = (isdigit(*p) ? dAtoi(p) : dAtoi(++p));
  1366. // Remove space between the name and the number
  1367. while ((p > base.c_str()) && dIsspace(*(p-1)))
  1368. p--;
  1369. return base.substr(0, p - base.c_str());
  1370. }
  1371. String String::GetFirstNumber(const char* str, U32& startPos, U32& endPos)
  1372. {
  1373. // Check for trivial strings
  1374. if (!str || !str[0])
  1375. return String::EmptyString;
  1376. // Find the number at the end of the string
  1377. String base(str);
  1378. const char* p = base.c_str();
  1379. const char* end = base.c_str() + base.length() - 1;
  1380. bool dec = false;
  1381. startPos = 0;
  1382. //Check if we are just a digit
  1383. if(p == end && isdigit(*p))
  1384. return base;
  1385. //Look for the first digit
  1386. while ((p != end) && (dIsspace(*p) || !isdigit(*p)))
  1387. {
  1388. p++;
  1389. startPos++;
  1390. }
  1391. //Handle if we are at the end and found nothing
  1392. if(p == end && !isdigit(*p))
  1393. return "";
  1394. //update our end position at least to the start of our number
  1395. endPos = startPos;
  1396. //Backup our ptr
  1397. const char* backup = p;
  1398. //Check for any negative or decimal values
  1399. if(startPos > 0)
  1400. {
  1401. p--;
  1402. startPos--;
  1403. if(*p == '.')
  1404. {
  1405. dec = true;
  1406. //ignore any duplicate periods
  1407. while ((p != base.c_str()) && (*p == '.'))
  1408. {
  1409. p--;
  1410. startPos--;
  1411. }
  1412. //Found a decimal lets still check for negative sign
  1413. if(startPos > 0)
  1414. {
  1415. p--;
  1416. startPos--;
  1417. if((*p != '-') && (*p != '_'))
  1418. {
  1419. startPos++;
  1420. p++;
  1421. }
  1422. }
  1423. }
  1424. else if((*p != '-') && (*p != '_'))
  1425. {
  1426. //go back to where we where cause no decimal or negative sign found
  1427. startPos++;
  1428. p++;
  1429. }
  1430. }
  1431. //Restore where we were
  1432. p = backup;
  1433. //look for the end of the digits
  1434. bool justFoundDec = false;
  1435. while (p != end)
  1436. {
  1437. if(*p == '.')
  1438. {
  1439. if(dec && !justFoundDec)
  1440. break;
  1441. else
  1442. {
  1443. dec = true;
  1444. justFoundDec = true;
  1445. }
  1446. }
  1447. else if(!isdigit(*p))
  1448. break;
  1449. else if(justFoundDec)
  1450. justFoundDec = false;
  1451. p++;
  1452. endPos++;
  1453. }
  1454. U32 len = (!isdigit(*p)) ? endPos - startPos : (endPos + 1) - startPos;
  1455. return base.substr(startPos, len);
  1456. }