str.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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. if( data )
  242. {
  243. dMemcpy( mData, data, sizeof( StringChar ) * mLength );
  244. mData[ mLength ] = '\0';
  245. }
  246. #ifdef TORQUE_DEBUG
  247. mString = &mData[0];
  248. #endif
  249. if( mIsInterned )
  250. mRefCount = U32_MAX;
  251. }
  252. ~StringData()
  253. {
  254. AssertFatal( mRefCount == 0, "StringData::~StringData invalid refcount" );
  255. if( mUTF16 )
  256. delete [] mUTF16;
  257. }
  258. static StringData* Create(const StringChar* data, U32 len, bool interned = false)
  259. {
  260. void* memory = dMalloc(sizeof(StringData) + sizeof(StringChar) * len);
  261. StringData* result = new(memory) StringData(data, len, interned);
  262. return result;
  263. }
  264. static StringData* Create(const StringChar* data, U32 len, DataChunker& chunker, bool interned = false)
  265. {
  266. void* memory = chunker.alloc( sizeof(StringData) + len * sizeof(StringChar));
  267. StringData* result = new(memory) StringData(data, len, interned);
  268. return result;
  269. }
  270. bool isShared() const
  271. {
  272. return ( mRefCount > 1 );
  273. }
  274. void addRef()
  275. {
  276. if( mRefCount != U32_MAX )
  277. mRefCount ++;
  278. }
  279. void release()
  280. {
  281. if( mRefCount != U32_MAX )
  282. {
  283. -- mRefCount;
  284. if( !mRefCount )
  285. delete this;
  286. }
  287. }
  288. U32 getLength() const
  289. {
  290. return mLength;
  291. }
  292. U32 getDataSize() const
  293. {
  294. return ( mLength + 1 );
  295. }
  296. U32 getDataSizeUTF16() const
  297. {
  298. return ( mLength * sizeof( UTF16 ) );
  299. }
  300. UTF8 operator []( U32 index ) const
  301. {
  302. AssertFatal( index < mLength, "String::StringData::operator []() - index out of range" );
  303. return mData[ index ];
  304. }
  305. UTF8* utf8()
  306. {
  307. return mData;
  308. }
  309. const UTF8* utf8() const
  310. {
  311. return mData;
  312. }
  313. UTF16* utf16() const
  314. {
  315. if( !mUTF16 )
  316. {
  317. // Do this atomically to protect interned strings.
  318. UTF16* utf16 = createUTF16string( mData );
  319. if( !dCompareAndSwap( mUTF16,( UTF16* ) NULL, utf16 ) )
  320. delete [] utf16;
  321. }
  322. return mUTF16;
  323. }
  324. U32 getHashCase() const
  325. {
  326. return mHashCase;
  327. }
  328. U32 getOrCreateHashCase() const
  329. {
  330. if( mHashCase == U32_MAX )
  331. {
  332. PROFILE_SCOPE(StringData_getOrCreateHashCase);
  333. mHashCase = Torque::hash((const U8 *)(mData), mLength, 0);
  334. }
  335. return mHashCase;
  336. }
  337. U32 getHashNoCase() const
  338. {
  339. return mHashNoCase;
  340. }
  341. U32 getOrCreateHashNoCase() const
  342. {
  343. if( mHashNoCase == U32_MAX)
  344. {
  345. PROFILE_SCOPE(StringData_getOrCreateHashNoCase);
  346. UTF8 *lower = new UTF8[ mLength + 1 ];
  347. dStrncpy( lower, utf8(), mLength );
  348. lower[ mLength ] = 0;
  349. dStrlwr( lower );
  350. mHashNoCase = Torque::hash( (const U8*)lower, mLength, 0 );
  351. delete [] lower;
  352. }
  353. return mHashNoCase;
  354. }
  355. U32 getNumChars() const
  356. {
  357. if( mNumChars == U32_MAX )
  358. mNumChars = dStrlen( utf16() );
  359. return mNumChars;
  360. }
  361. bool isInterned() const
  362. {
  363. return mIsInterned;
  364. }
  365. static StringData* Empty()
  366. {
  367. static UTF16 emptyUTF16[ 1 ] = { 0 };
  368. static StringDataImpl empty =
  369. {
  370. #ifdef TORQUE_DEBUG
  371. "", // mString
  372. #endif
  373. U32_MAX, // mRefCount
  374. 0, // mLength
  375. 0, // mNumChars
  376. 0, // mHashCase
  377. 0, // mHashNoCase
  378. emptyUTF16, // mUTF16
  379. true, // mIsInterned
  380. { 0 } // mData
  381. };
  382. return ( StringData* ) &empty;
  383. }
  384. };
  385. //-----------------------------------------------------------------------------
  386. namespace DictHash
  387. {
  388. inline U32 hash( String::StringData* data )
  389. {
  390. return data->getOrCreateHashCase();
  391. }
  392. }
  393. namespace KeyCmp
  394. {
  395. template<>
  396. inline bool equals<>( String::StringData* const& d1, String::StringData* const& d2 )
  397. {
  398. return ( String::compare( d1->utf8(), d2->utf8() ) == 0 );
  399. }
  400. }
  401. /// Type for the intern string table. We don't want String instances directly
  402. /// on the table so that destructors don't run when the table is destroyed. This
  403. /// is because we really shouldn't depend on dtor ordering within this file and thus
  404. /// we can't tell whether the intern string memory is freed before or after the
  405. /// table is destroyed.
  406. struct StringInternTable : public HashTable< String::StringData*, String::StringData* >
  407. {
  408. Mutex mMutex;
  409. DataChunker mChunker;
  410. };
  411. static StringInternTable* sInternTable;
  412. struct KillInternTable
  413. {
  414. ~KillInternTable()
  415. {
  416. if( sInternTable )
  417. delete sInternTable;
  418. }
  419. };
  420. static KillInternTable sKillInternTable;
  421. //-----------------------------------------------------------------------------
  422. #ifdef TORQUE_DEBUG
  423. /// Tracks the number of bytes allocated for strings.
  424. /// @bug This currently does not include UTF16 allocations.
  425. static U32 sgStringMemBytes;
  426. /// Tracks the number of Strings which are currently instantiated.
  427. static U32 sgStringInstances;
  428. #endif
  429. DefineEngineFunction( dumpStringMemStats, void, (), , "()"
  430. "@brief Dumps information about String memory usage\n\n"
  431. "@ingroup Debugging\n"
  432. "@ingroup Strings\n")
  433. {
  434. #ifdef TORQUE_DEBUG
  435. Con::printf( "String Data: %i instances, %i bytes", sgStringInstances, sgStringMemBytes );
  436. #endif
  437. }
  438. //-----------------------------------------------------------------------------
  439. String::String()
  440. {
  441. PROFILE_SCOPE(String_default_constructor);
  442. _string = StringData::Empty();
  443. }
  444. String::String(const String &str)
  445. {
  446. PROFILE_SCOPE(String_String_constructor);
  447. _string = str._string;
  448. _string->addRef();
  449. }
  450. String::String(const StringChar *str)
  451. {
  452. PROFILE_SCOPE(String_char_constructor);
  453. if( str && *str )
  454. {
  455. U32 len = dStrlen(str);
  456. _string = StringData::Create(str, len);
  457. }
  458. else
  459. _string = StringData::Empty();
  460. }
  461. String::String(const StringChar *str, SizeType len)
  462. {
  463. PROFILE_SCOPE(String_char_len_constructor);
  464. if (str && *str && len!=0)
  465. {
  466. _string = StringData::Create(str, len);
  467. }
  468. else
  469. _string = StringData::Empty();
  470. }
  471. String::String(const UTF16 *str)
  472. {
  473. PROFILE_SCOPE(String_UTF16_constructor);
  474. if( str && str[ 0 ] )
  475. {
  476. UTF8* utf8 = createUTF8string( str );
  477. U32 len = dStrlen( utf8 );
  478. _string = StringData::Create(utf8, len);
  479. delete [] utf8;
  480. }
  481. else
  482. _string = StringData::Empty();
  483. }
  484. String::~String()
  485. {
  486. if (_string && _string != StringData::Empty())
  487. _string->release();
  488. }
  489. //-----------------------------------------------------------------------------
  490. String String::intern() const
  491. {
  492. if( isInterned() )
  493. return *this;
  494. // Create the intern table, if we haven't already.
  495. if( !sInternTable )
  496. sInternTable = new StringInternTable;
  497. // Lock the string table.
  498. MutexHandle mutex;
  499. mutex.lock( &sInternTable->mMutex );
  500. // Lookup.
  501. StringInternTable::Iterator iter = sInternTable->find( _string );
  502. if( iter != sInternTable->end() )
  503. return ( *iter ).value;
  504. // Create new.
  505. StringData* data = StringData::Create(c_str(), length(), sInternTable->mChunker, true);
  506. iter = sInternTable->insertUnique( data, data );
  507. return ( *iter ).value;
  508. }
  509. //-----------------------------------------------------------------------------
  510. const StringChar* String::c_str() const
  511. {
  512. return _string->utf8();
  513. }
  514. const UTF16 *String::utf16() const
  515. {
  516. return _string->utf16();
  517. }
  518. String::SizeType String::length() const
  519. {
  520. return _string->getLength();
  521. }
  522. String::SizeType String::size() const
  523. {
  524. return _string->getDataSize();
  525. }
  526. String::SizeType String::numChars() const
  527. {
  528. return _string->getNumChars();
  529. }
  530. bool String::isEmpty() const
  531. {
  532. return ( _string == StringData::Empty() );
  533. }
  534. bool String::isEmpty(const char* str)
  535. {
  536. return str == 0 || str[0] == '\0';
  537. }
  538. bool String::isShared() const
  539. {
  540. return _string->isShared();
  541. }
  542. bool String::isSame( const String& str ) const
  543. {
  544. return ( _string == str._string );
  545. }
  546. bool String::isInterned() const
  547. {
  548. return ( _string->isInterned() );
  549. }
  550. U32 String::getHashCaseSensitive() const
  551. {
  552. return _string->getOrCreateHashCase();
  553. }
  554. U32 String::getHashCaseInsensitive() const
  555. {
  556. return _string->getOrCreateHashNoCase();
  557. }
  558. //-----------------------------------------------------------------------------
  559. String::SizeType String::find(const String &str, SizeType pos, U32 mode) const
  560. {
  561. return find(str._string->utf8(), pos, mode);
  562. }
  563. String& String::insert(SizeType pos, const String &str)
  564. {
  565. return insert(pos, str._string->utf8());
  566. }
  567. String& String::replace(SizeType pos, SizeType len, const String &str)
  568. {
  569. return replace(pos, len, str._string->utf8());
  570. }
  571. //-----------------------------------------------------------------------------
  572. String& String::operator=(StringChar c)
  573. {
  574. _string->release();
  575. _string = StringData::Create(NULL, 2);
  576. _string->utf8()[ 0 ] = c;
  577. _string->utf8()[ 1 ] = '\0';
  578. return *this;
  579. }
  580. String& String::operator+=(StringChar c)
  581. {
  582. // Append the given string into a new string
  583. U32 len = _string->getLength();
  584. StringData* sub = StringData::Create(NULL, len + 1);
  585. copy( sub->utf8(), _string->utf8(), len );
  586. sub->utf8()[len] = c;
  587. sub->utf8()[len+1] = 0;
  588. _string->release();
  589. _string = sub;
  590. return *this;
  591. }
  592. //-----------------------------------------------------------------------------
  593. String& String::operator=(const StringChar *str)
  594. {
  595. // Protect against self assignment which is not only a
  596. // waste of time, but can also lead to the string being
  597. // freed before it can be reassigned.
  598. if ( _string->utf8() == str )
  599. return *this;
  600. _string->release();
  601. if (str && *str)
  602. {
  603. U32 len = dStrlen(str);
  604. _string = StringData::Create(str, len);
  605. }
  606. else
  607. _string = StringData::Empty();
  608. return *this;
  609. }
  610. String& String::operator=(const String &src)
  611. {
  612. // Inc src first to avoid assignment to self problems.
  613. src._string->addRef();
  614. _string->release();
  615. _string = src._string;
  616. return *this;
  617. }
  618. String& String::operator+=(const StringChar *src)
  619. {
  620. if( src == NULL || !*src )
  621. return *this;
  622. // Append the given string into a new string
  623. U32 lena = _string->getLength();
  624. U32 lenb = dStrlen(src);
  625. U32 newlen = lena + lenb;
  626. StringData* sub;
  627. if( !newlen )
  628. sub = StringData::Empty();
  629. else
  630. {
  631. sub = StringData::Create(NULL, newlen);
  632. copy(sub->utf8(),_string->utf8(),lena);
  633. copy(sub->utf8() + lena,src,lenb + 1);
  634. }
  635. _string->release();
  636. _string = sub;
  637. return *this;
  638. }
  639. String& String::operator+=(const String &src)
  640. {
  641. if( src.isEmpty() )
  642. return *this;
  643. // Append the given string into a new string
  644. U32 lena = _string->getLength();
  645. U32 lenb = src._string->getLength();
  646. U32 newlen = lena + lenb;
  647. StringData* sub;
  648. if( !newlen )
  649. sub = StringData::Empty();
  650. else
  651. {
  652. sub = StringData::Create(NULL, newlen);
  653. copy(sub->utf8(),_string->utf8(),lena);
  654. copy(sub->utf8() + lena,src._string->utf8(),lenb + 1);
  655. }
  656. _string->release();
  657. _string = sub;
  658. return *this;
  659. }
  660. //-----------------------------------------------------------------------------
  661. String operator+(const String &a, const String &b)
  662. {
  663. PROFILE_SCOPE( String_String_plus_String );
  664. if( a.isEmpty() )
  665. return b;
  666. else if( b.isEmpty() )
  667. return a;
  668. U32 lena = a.length();
  669. U32 lenb = b.length();
  670. String::StringData* sub = String::StringData::Create(NULL, lena + lenb);
  671. String::copy(sub->utf8(),a._string->utf8(),lena);
  672. String::copy(sub->utf8() + lena,b._string->utf8(),lenb + 1);
  673. return String(sub);
  674. }
  675. String operator+(const String &a, StringChar c)
  676. {
  677. //PROFILE_SCOPE( String_String_plus_Char );
  678. U32 lena = a.length();
  679. String::StringData* sub = String::StringData::Create(NULL, lena + 1);
  680. String::copy(sub->utf8(),a._string->utf8(),lena);
  681. sub->utf8()[lena] = c;
  682. sub->utf8()[lena+1] = 0;
  683. return String(sub);
  684. }
  685. String operator+(StringChar c, const String &a)
  686. {
  687. //PROFILE_SCOPE( String_Char_plus_String );
  688. U32 lena = a.length();
  689. String::StringData* sub = String::StringData::Create(NULL, lena + 1);
  690. String::copy(sub->utf8() + 1,a._string->utf8(),lena + 1);
  691. sub->utf8()[0] = c;
  692. return String(sub);
  693. }
  694. String operator+(const String &a, const StringChar *b)
  695. {
  696. //PROFILE_SCOPE( String_String_plus_CString );
  697. AssertFatal(b,"String:: Invalid null ptr argument");
  698. if( a.isEmpty() )
  699. return String( b );
  700. U32 lena = a.length();
  701. U32 lenb = dStrlen(b);
  702. if( !lenb )
  703. return a;
  704. String::StringData* sub = String::StringData::Create(NULL, lena + lenb);
  705. String::copy(sub->utf8(),a._string->utf8(),lena);
  706. String::copy(sub->utf8() + lena,b,lenb + 1);
  707. return String(sub);
  708. }
  709. String operator+(const StringChar *a, const String &b)
  710. {
  711. //PROFILE_SCOPE( String_CString_plus_String );
  712. AssertFatal(a,"String:: Invalid null ptr argument");
  713. if( b.isEmpty() )
  714. return String( a );
  715. U32 lena = dStrlen(a);
  716. if( !lena )
  717. return b;
  718. U32 lenb = b.length();
  719. String::StringData* sub = String::StringData::Create(NULL, lena + lenb);
  720. String::copy(sub->utf8(),a,lena);
  721. String::copy(sub->utf8() + lena,b._string->utf8(),lenb + 1);
  722. return String(sub);
  723. }
  724. bool String::operator==(const String &str) const
  725. {
  726. //PROFILE_SCOPE( String_op_equal );
  727. if( str._string == _string )
  728. return true;
  729. else if( str._string->isInterned() && _string->isInterned() )
  730. return false;
  731. else if( str.length() != length() )
  732. return false;
  733. else if( str._string->getHashCase() != U32_MAX
  734. && _string->getHashCase() != U32_MAX
  735. && str._string->getHashCase() != _string->getHashCase() )
  736. return false;
  737. else
  738. return ( dMemcmp( str._string->utf8(), _string->utf8(), _string->getLength() ) == 0 );
  739. }
  740. bool String::operator==( StringChar c ) const
  741. {
  742. if( !_string || _string->getLength() != 1 )
  743. return false;
  744. else
  745. return ( _string->utf8()[ 0 ] == c );
  746. }
  747. bool String::operator<(const String &str) const
  748. {
  749. return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) < 0 );
  750. }
  751. bool String::operator>(const String &str) const
  752. {
  753. return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) > 0 );
  754. }
  755. bool String::operator<=(const String &str) const
  756. {
  757. return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) <= 0 );
  758. }
  759. bool String::operator>=(const String &str) const
  760. {
  761. return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) >= 0 );
  762. }
  763. //-----------------------------------------------------------------------------
  764. // Base functions for string comparison
  765. S32 String::compare(const StringChar *str, SizeType len, U32 mode) const
  766. {
  767. PROFILE_SCOPE( String_compare );
  768. AssertFatal(str,"String:: Invalid null ptr argument");
  769. const StringChar *p1 = _string->utf8();
  770. const StringChar *p2 = str;
  771. if (p1 == p2)
  772. return 0;
  773. if( mode & String::Right )
  774. {
  775. U32 n = len;
  776. if( n > length() )
  777. n = length();
  778. p1 += length() - n;
  779. p2 += dStrlen( str ) - n;
  780. }
  781. if (mode & String::NoCase)
  782. {
  783. if (len)
  784. {
  785. for (;--len; p1++,p2++)
  786. {
  787. if (dTolower(*p1) != dTolower(*p2) || !*p1)
  788. break;
  789. }
  790. }
  791. else
  792. {
  793. while (dTolower(*p1) == dTolower(*p2) && *p1)
  794. {
  795. p1++;
  796. p2++;
  797. }
  798. }
  799. return dTolower(*p1) - dTolower(*p2);
  800. }
  801. if (len)
  802. return dMemcmp(p1,p2,len);
  803. while (*p1 == *p2 && *p1)
  804. {
  805. p1++;
  806. p2++;
  807. }
  808. return *p1 - *p2;
  809. }
  810. S32 String::compare(const String &str, SizeType len, U32 mode) const
  811. {
  812. if ( str._string == _string )
  813. return 0;
  814. return compare( str.c_str(), len, mode );
  815. }
  816. S32 String::compare(const char *str1, const char *str2)
  817. {
  818. return strcmp(str1, str2);
  819. }
  820. S32 String::compare(const UTF16 *str1, const UTF16 *str2)
  821. {
  822. #if defined(TORQUE_OS_WIN) || defined(TORQUE_OS_XBOX) || defined(TORQUE_OS_XENON)
  823. return wcscmp(reinterpret_cast<const wchar_t *>(str1), reinterpret_cast<const wchar_t *>(str2));
  824. #else
  825. S32 ret;
  826. const UTF16 *a, *b;
  827. a = str1;
  828. b = str2;
  829. while (((ret = *a - *b) == 0) && *a && *b)
  830. a++, b++;
  831. return ret;
  832. #endif
  833. }
  834. bool String::equal(const String &str, U32 mode) const
  835. {
  836. if( !mode )
  837. return ( *this == str );
  838. else
  839. {
  840. if( _string == str._string )
  841. return true;
  842. else if( _string->isInterned() && str._string->isInterned() )
  843. return false;
  844. else if( length() != str.length() )
  845. return false;
  846. else if( _string->getHashNoCase() != U32_MAX
  847. && str._string->getHashNoCase() != U32_MAX
  848. && _string->getHashNoCase() != str._string->getHashNoCase() )
  849. return false;
  850. else
  851. return ( compare( str.c_str(), length(), mode ) == 0 );
  852. }
  853. }
  854. //-----------------------------------------------------------------------------
  855. String::SizeType String::find(StringChar c, SizeType pos, U32 mode) const
  856. {
  857. const StringChar* ptr = StrFind(_string->utf8(),c,pos,mode);
  858. return ptr? SizeType(ptr - _string->utf8()): NPos;
  859. }
  860. String::SizeType String::find(const StringChar *str, SizeType pos, U32 mode) const
  861. {
  862. AssertFatal(str,"String:: Invalid null ptr argument");
  863. const StringChar* ptr = StrFind(_string->utf8(),str,pos,mode);
  864. return ptr? SizeType(ptr - _string->utf8()): NPos;
  865. }
  866. //-----------------------------------------------------------------------------
  867. String& String::insert(SizeType pos, const StringChar *str)
  868. {
  869. AssertFatal(str,"String:: Invalid null ptr argument");
  870. return insert(pos,str,dStrlen(str));
  871. }
  872. ///@todo review for error checking
  873. String& String::insert(SizeType pos, const StringChar *str, SizeType len)
  874. {
  875. if( !len )
  876. return *this;
  877. AssertFatal( str, "String:: Invalid null ptr argument" );
  878. SizeType lena = length();
  879. AssertFatal((pos <= lena),"Calling String::insert with position greater than length");
  880. U32 newlen = lena + len;
  881. StringData *sub;
  882. if( !newlen )
  883. sub = StringData::Empty();
  884. else
  885. {
  886. sub = StringData::Create(NULL, newlen);
  887. String::copy(sub->utf8(),_string->utf8(),pos);
  888. String::copy(sub->utf8() + pos,str,len);
  889. String::copy(sub->utf8() + pos + len,_string->utf8() + pos,lena - pos + 1);
  890. }
  891. _string->release();
  892. _string = sub;
  893. return *this;
  894. }
  895. String& String::erase(SizeType pos, SizeType len)
  896. {
  897. AssertFatal( len != 0, "String::erase() - Calling String::erase with 0 length" );
  898. AssertFatal( ( pos + len ) <= length(), "String::erase() - Invalid string region" );
  899. if( !len )
  900. return *this;
  901. SizeType slen = length();
  902. U32 newlen = slen - len;
  903. StringData *sub;
  904. if( !newlen )
  905. sub = StringData::Empty();
  906. else
  907. {
  908. sub = StringData::Create(NULL, newlen);
  909. if (pos > 0)
  910. String::copy(sub->utf8(),_string->utf8(),pos);
  911. String::copy(sub->utf8() + pos, _string->utf8() + pos + len, slen - (pos + len) + 1);
  912. }
  913. _string->release();
  914. _string = sub;
  915. return *this;
  916. }
  917. ///@todo review for error checking
  918. String& String::replace(SizeType pos, SizeType len, const StringChar *str)
  919. {
  920. AssertFatal( str, "String::replace() - Invalid null ptr argument" );
  921. AssertFatal( len != 0, "String::replace() - Zero length" );
  922. AssertFatal( ( pos + len ) <= length(), "String::replace() - Invalid string region" );
  923. SizeType slen = length();
  924. SizeType rlen = dStrlen(str);
  925. U32 newlen = slen - len + rlen;
  926. StringData *sub;
  927. if( !newlen )
  928. sub = StringData::Empty();
  929. else
  930. {
  931. sub = StringData::Create(NULL, newlen);
  932. String::copy(sub->utf8(),_string->utf8(), pos);
  933. String::copy(sub->utf8() + pos,str,rlen);
  934. String::copy(sub->utf8() + pos + rlen,_string->utf8() + pos + len,slen - pos - len + 1);
  935. }
  936. _string->release();
  937. _string = sub;
  938. return *this;
  939. }
  940. String& String::replace( StringChar c1, StringChar c2 )
  941. {
  942. if( isEmpty() )
  943. return *this;
  944. // Create the new string lazily so that we don't needlessly
  945. // dup strings when there is nothing to replace.
  946. StringData* sub = NULL;
  947. bool foundReplacement = false;
  948. StringChar* c = _string->utf8();
  949. while( *c )
  950. {
  951. if( *c == c1 )
  952. {
  953. if( !foundReplacement )
  954. {
  955. sub = StringData::Create(_string->utf8(), length());
  956. c = &sub->utf8()[ c - _string->utf8() ];
  957. foundReplacement = true;
  958. }
  959. *c = c2;
  960. }
  961. c++;
  962. }
  963. if( foundReplacement )
  964. {
  965. _string->release();
  966. _string = sub;
  967. }
  968. return *this;
  969. }
  970. String &String::replace(const String &s1, const String &s2)
  971. {
  972. // Find number of occurrences of s1 and
  973. // Calculate length of the new string...
  974. const U32 &s1len = s1.length();
  975. const U32 &s2len = s2.length();
  976. U32 pos = 0;
  977. Vector<U32> indices;
  978. StringChar *walk = _string->utf8();
  979. while ( walk )
  980. {
  981. // Casting away the const... was there a better way?
  982. walk = (StringChar*)StrFind( _string->utf8(), s1.c_str(), pos, Case|Left );
  983. if ( walk )
  984. {
  985. pos = SizeType(walk - _string->utf8());
  986. indices.push_back( pos );
  987. pos += s1len;
  988. }
  989. }
  990. // Early-out, no StringDatas found.
  991. if ( indices.size() == 0 )
  992. return *this;
  993. U32 newSize = size() - ( indices.size() * s1len ) + ( indices.size() * s2len );
  994. StringData *sub;
  995. if( newSize == 1 )
  996. sub = StringData::Empty();
  997. else
  998. {
  999. sub = StringData::Create(NULL, newSize - 1);
  1000. // Now assemble the new string from the pieces of the old...
  1001. // Index into the old string
  1002. pos = 0;
  1003. // Index into the new string
  1004. U32 newPos = 0;
  1005. // Used to store a character count to be memcpy'd
  1006. U32 copyCharCount = 0;
  1007. for ( U32 i = 0; i < indices.size(); i++ )
  1008. {
  1009. const U32 &index = indices[i];
  1010. // Number of chars (if any) before the next indexed StringData
  1011. copyCharCount = index - pos;
  1012. // Copy chars before the StringData if we have any.
  1013. if ( copyCharCount > 0 )
  1014. {
  1015. dMemcpy( sub->utf8() + newPos, _string->utf8() + pos, copyCharCount * sizeof(StringChar) );
  1016. newPos += copyCharCount;
  1017. }
  1018. // Copy over the replacement string.
  1019. if ( s2len > 0 )
  1020. dMemcpy( sub->utf8() + newPos, s2._string->utf8(), s2len * sizeof(StringChar) );
  1021. newPos += s2len;
  1022. pos = index + s1len;
  1023. }
  1024. // There could be characters left in the original string after the last
  1025. // StringData occurrence, which we need to copy now - outside the loop.
  1026. copyCharCount = length() - indices.last() - s1len;
  1027. if ( copyCharCount != 0 )
  1028. dMemcpy( sub->utf8() + newPos, _string->utf8() + pos, copyCharCount * sizeof(StringChar) );
  1029. // Null terminate it!
  1030. sub->utf8()[newSize-1] = 0;
  1031. }
  1032. _string->release();
  1033. _string = sub;
  1034. return *this;
  1035. }
  1036. //-----------------------------------------------------------------------------
  1037. String String::substr(SizeType pos, SizeType len) const
  1038. {
  1039. //PROFILE_SCOPE( String_substr );
  1040. AssertFatal( pos <= length(), "String::substr - Invalid position!" );
  1041. if ( len == -1 )
  1042. len = length() - pos;
  1043. AssertFatal( len + pos <= length(), "String::substr - Invalid length!" );
  1044. StringData* sub;
  1045. if( !len )
  1046. sub = StringData::Empty();
  1047. else
  1048. sub = StringData::Create(_string->utf8() + pos, len);
  1049. return sub;
  1050. }
  1051. //-----------------------------------------------------------------------------
  1052. String String::trim() const
  1053. {
  1054. if( isEmpty() )
  1055. return *this;
  1056. const StringChar* start = _string->utf8();
  1057. while( *start && dIsspace( *start ) )
  1058. start ++;
  1059. const StringChar* end = _string->utf8() + length() - 1;
  1060. while( end > start && dIsspace( *end ) )
  1061. end --;
  1062. end ++;
  1063. const U32 len = end - start;
  1064. if( len == length() )
  1065. return *this;
  1066. StringData* sub;
  1067. if( !len )
  1068. sub = StringData::Empty();
  1069. else
  1070. sub = StringData::Create(start, len);
  1071. return sub;
  1072. }
  1073. //-----------------------------------------------------------------------------
  1074. String String::expandEscapes() const
  1075. {
  1076. char* tmp = ( char* ) dMalloc( length() * 2 + 1 ); // worst-case situation.
  1077. expandEscape( tmp, c_str() );
  1078. String str( tmp );
  1079. dFree( tmp );
  1080. return str;
  1081. }
  1082. //-----------------------------------------------------------------------------
  1083. String String::collapseEscapes() const
  1084. {
  1085. char* tmp = dStrdup( c_str() );
  1086. collapseEscape( tmp );
  1087. String str( tmp );
  1088. dFree( tmp );
  1089. return str;
  1090. }
  1091. //-----------------------------------------------------------------------------
  1092. void String::split( const char* delimiter, Vector< String >& outElements ) const
  1093. {
  1094. const char* ptr = _string->utf8();
  1095. const char* start = ptr;
  1096. while( *ptr )
  1097. {
  1098. // Search for start of delimiter.
  1099. if( *ptr != delimiter[ 0 ] )
  1100. ptr ++;
  1101. else
  1102. {
  1103. // Skip delimiter.
  1104. const char* end = ptr;
  1105. const char* del = delimiter;
  1106. while( *del && *del == *ptr )
  1107. {
  1108. ptr ++;
  1109. del ++;
  1110. }
  1111. // If we didn't match all of delimiter,
  1112. // continue with search.
  1113. if( *del != '\0' )
  1114. continue;
  1115. // Extract component.
  1116. outElements.push_back( String( start, end - start ) );
  1117. start = ptr;
  1118. }
  1119. }
  1120. // Add rest of string if there is any.
  1121. if( start != ptr )
  1122. outElements.push_back( start );
  1123. }
  1124. //-----------------------------------------------------------------------------
  1125. bool String::startsWith( const char* text ) const
  1126. {
  1127. return dStrStartsWith( _string->utf8(), text );
  1128. }
  1129. //-----------------------------------------------------------------------------
  1130. bool String::endsWith( const char* text ) const
  1131. {
  1132. return dStrEndsWith( _string->utf8(), text );
  1133. }
  1134. //-----------------------------------------------------------------------------
  1135. void String::copy(StringChar* dst, const StringChar *src, U32 len)
  1136. {
  1137. dMemcpy(dst, src, len * sizeof(StringChar));
  1138. }
  1139. //-----------------------------------------------------------------------------
  1140. #if defined(TORQUE_OS_WIN)
  1141. // This standard function is not defined when compiling with VC7...
  1142. #define vsnprintf _vsnprintf
  1143. #endif
  1144. String::StrFormat::~StrFormat()
  1145. {
  1146. if( _dynamicBuffer )
  1147. dFree( _dynamicBuffer );
  1148. }
  1149. S32 String::StrFormat::format( const char *format, va_list args )
  1150. {
  1151. _len=0;
  1152. return formatAppend(format,args);
  1153. }
  1154. S32 String::StrFormat::formatAppend( const char *format, va_list args )
  1155. {
  1156. // Format into the fixed buffer first.
  1157. S32 startLen = _len;
  1158. if (_dynamicBuffer == NULL)
  1159. {
  1160. _len += vsnprintf(_fixedBuffer + _len, sizeof(_fixedBuffer) - _len, format, args);
  1161. if (_len >= 0 && _len < sizeof(_fixedBuffer))
  1162. return _len;
  1163. // Start off the dynamic buffer at twice fixed buffer size
  1164. _len = startLen;
  1165. _dynamicSize = sizeof(_fixedBuffer) * 2;
  1166. _dynamicBuffer = (char*)dMalloc(_dynamicSize);
  1167. dMemcpy(_dynamicBuffer, _fixedBuffer, _len + 1);
  1168. }
  1169. // Format into the dynamic buffer, if the buffer is not large enough, then
  1170. // keep doubling it's size until it is. The buffer is not reallocated
  1171. // using reallocate() to avoid unnecessary buffer copying.
  1172. _len += vsnprintf(_dynamicBuffer + _len, _dynamicSize - _len, format, *(va_list*)args);
  1173. while (_len < 0 || _len >= _dynamicSize)
  1174. {
  1175. _len = startLen;
  1176. _dynamicBuffer = (char*)dRealloc(_dynamicBuffer, _dynamicSize *= 2);
  1177. _len += vsnprintf(_dynamicBuffer + _len, _dynamicSize - _len, format, *(va_list*)args);
  1178. }
  1179. return _len;
  1180. }
  1181. S32 String::StrFormat::append(const char * str, S32 len)
  1182. {
  1183. if (_dynamicBuffer == NULL)
  1184. {
  1185. if (_len+len >= 0 && _len+len < sizeof(_fixedBuffer))
  1186. {
  1187. dMemcpy(_fixedBuffer + _len, str, len);
  1188. _len += len;
  1189. _fixedBuffer[_len] = '\0';
  1190. return _len;
  1191. }
  1192. _dynamicSize = sizeof(_fixedBuffer) * 2;
  1193. _dynamicBuffer = (char*)dMalloc(_dynamicSize);
  1194. dMemcpy(_dynamicBuffer, _fixedBuffer, _len + 1);
  1195. }
  1196. S32 newSize = _dynamicSize;
  1197. while (newSize < _len+len)
  1198. newSize *= 2;
  1199. if (newSize != _dynamicSize)
  1200. _dynamicBuffer = (char*) dRealloc(_dynamicBuffer, newSize);
  1201. _dynamicSize = newSize;
  1202. dMemcpy(_dynamicBuffer + _len, str, len);
  1203. _len += len;
  1204. _dynamicBuffer[_len] = '\0';
  1205. return _len;
  1206. }
  1207. S32 String::StrFormat::append(const char * str)
  1208. {
  1209. return append(str, dStrlen(str));
  1210. }
  1211. char* String::StrFormat::copy( char *buffer ) const
  1212. {
  1213. dMemcpy(buffer, _dynamicBuffer? _dynamicBuffer: _fixedBuffer, _len+1);
  1214. return buffer;
  1215. }
  1216. //-----------------------------------------------------------------------------
  1217. String String::ToString( bool value )
  1218. {
  1219. static String sTrue = "true";
  1220. static String sFalse = "false";
  1221. if( value )
  1222. return sTrue;
  1223. return sFalse;
  1224. }
  1225. String String::ToString(const char *str, ...)
  1226. {
  1227. AssertFatal(str,"String:: Invalid null ptr argument");
  1228. // Use the format object
  1229. va_list args;
  1230. va_start(args, str);
  1231. String ret = VToString(str, args);
  1232. va_end(args);
  1233. return ret;
  1234. }
  1235. String String::VToString(const char* str, va_list args)
  1236. {
  1237. StrFormat format(str,args);
  1238. // Copy it into a string
  1239. U32 len = format.length();
  1240. StringData* sub;
  1241. if( !len )
  1242. sub = StringData::Empty();
  1243. else
  1244. {
  1245. sub = StringData::Create(NULL, len);
  1246. format.copy( sub->utf8() );
  1247. sub->utf8()[ len ] = 0;
  1248. }
  1249. return sub;
  1250. }
  1251. String String::SpanToString(const char *start, const char *end)
  1252. {
  1253. if ( end == start )
  1254. return String();
  1255. AssertFatal( end > start, "Invalid arguments to String::SpanToString - end is before start" );
  1256. U32 len = U32(end - start);
  1257. String::StringData* sub = StringData::Create(start, len);
  1258. return sub;
  1259. }
  1260. String String::ToLower(const String &string)
  1261. {
  1262. if ( string.isEmpty() )
  1263. return String();
  1264. String::StringData* sub = StringData::Create(string, string.length());
  1265. dStrlwr( sub->utf8() );
  1266. return sub;
  1267. }
  1268. String String::ToUpper(const String &string)
  1269. {
  1270. if ( string.isEmpty() )
  1271. return String();
  1272. String::StringData* sub = StringData::Create(string, string.length());
  1273. dStrupr( sub->utf8() );
  1274. return sub;
  1275. }
  1276. String String::GetTrailingNumber(const char* str, S32& number)
  1277. {
  1278. // Check for trivial strings
  1279. if (!str || !str[0])
  1280. return String::EmptyString;
  1281. // Find the number at the end of the string
  1282. String base(str);
  1283. const char* p = base.c_str() + base.length() - 1;
  1284. // Ignore trailing whitespace
  1285. while ((p != base.c_str()) && dIsspace(*p))
  1286. p--;
  1287. // Need at least one digit!
  1288. if (!isdigit(*p))
  1289. return base;
  1290. // Back up to the first non-digit character
  1291. while ((p != base.c_str()) && isdigit(*p))
  1292. p--;
  1293. // Convert number => allow negative numbers, treat '_' as '-' for Maya
  1294. if ((*p == '-') || (*p == '_'))
  1295. number = -dAtoi(p + 1);
  1296. else
  1297. number = (isdigit(*p) ? dAtoi(p) : dAtoi(++p));
  1298. // Remove space between the name and the number
  1299. while ((p > base.c_str()) && dIsspace(*(p-1)))
  1300. p--;
  1301. return base.substr(0, p - base.c_str());
  1302. }
  1303. String String::GetFirstNumber(const char* str, U32& startPos, U32& endPos)
  1304. {
  1305. // Check for trivial strings
  1306. if (!str || !str[0])
  1307. return String::EmptyString;
  1308. // Find the number at the end of the string
  1309. String base(str);
  1310. const char* p = base.c_str();
  1311. const char* end = base.c_str() + base.length() - 1;
  1312. bool dec = false;
  1313. startPos = 0;
  1314. //Check if we are just a digit
  1315. if(p == end && isdigit(*p))
  1316. return base;
  1317. //Look for the first digit
  1318. while ((p != end) && (dIsspace(*p) || !isdigit(*p)))
  1319. {
  1320. p++;
  1321. startPos++;
  1322. }
  1323. //Handle if we are at the end and found nothing
  1324. if(p == end && !isdigit(*p))
  1325. return "";
  1326. //update our end position at least to the start of our number
  1327. endPos = startPos;
  1328. //Backup our ptr
  1329. const char* backup = p;
  1330. //Check for any negative or decimal values
  1331. if(startPos > 0)
  1332. {
  1333. p--;
  1334. startPos--;
  1335. if(*p == '.')
  1336. {
  1337. dec = true;
  1338. //ignore any duplicate periods
  1339. while ((p != base.c_str()) && (*p == '.'))
  1340. {
  1341. p--;
  1342. startPos--;
  1343. }
  1344. //Found a decimal lets still check for negative sign
  1345. if(startPos > 0)
  1346. {
  1347. p--;
  1348. startPos--;
  1349. if((*p != '-') && (*p != '_'))
  1350. {
  1351. startPos++;
  1352. p++;
  1353. }
  1354. }
  1355. }
  1356. else if((*p != '-') && (*p != '_'))
  1357. {
  1358. //go back to where we where cause no decimal or negative sign found
  1359. startPos++;
  1360. p++;
  1361. }
  1362. }
  1363. //Restore where we were
  1364. p = backup;
  1365. //look for the end of the digits
  1366. bool justFoundDec = false;
  1367. while (p != end)
  1368. {
  1369. if(*p == '.')
  1370. {
  1371. if(dec && !justFoundDec)
  1372. break;
  1373. else
  1374. {
  1375. dec = true;
  1376. justFoundDec = true;
  1377. }
  1378. }
  1379. else if(!isdigit(*p))
  1380. break;
  1381. else if(justFoundDec)
  1382. justFoundDec = false;
  1383. p++;
  1384. endPos++;
  1385. }
  1386. U32 len = (!isdigit(*p)) ? endPos - startPos : (endPos + 1) - startPos;
  1387. return base.substr(startPos, len);
  1388. }