str.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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, bool interned = false )
  233. {
  234. mRefCount = 1;
  235. mNumChars = U32_MAX;
  236. mHashCase = U32_MAX;
  237. mHashNoCase = U32_MAX;
  238. mUTF16 = NULL;
  239. mIsInterned = interned;
  240. // mLength is initialized by operator new()
  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. if( mUTF16 )
  255. delete [] mUTF16;
  256. }
  257. void* operator new(size_t size, U32 len);
  258. void* operator new( size_t size, U32 len, DataChunker& chunker );
  259. void operator delete(void *);
  260. bool isShared() const
  261. {
  262. return ( mRefCount > 1 );
  263. }
  264. void addRef()
  265. {
  266. if( mRefCount != U32_MAX )
  267. mRefCount ++;
  268. }
  269. void release()
  270. {
  271. if( mRefCount != U32_MAX )
  272. {
  273. -- mRefCount;
  274. if( !mRefCount )
  275. delete this;
  276. }
  277. }
  278. U32 getLength() const
  279. {
  280. return mLength;
  281. }
  282. U32 getDataSize() const
  283. {
  284. return ( mLength + 1 );
  285. }
  286. U32 getDataSizeUTF16() const
  287. {
  288. return ( mLength * sizeof( UTF16 ) );
  289. }
  290. UTF8 operator []( U32 index ) const
  291. {
  292. AssertFatal( index < mLength, "String::StringData::operator []() - index out of range" );
  293. return mData[ index ];
  294. }
  295. UTF8* utf8()
  296. {
  297. return mData;
  298. }
  299. const UTF8* utf8() const
  300. {
  301. return mData;
  302. }
  303. UTF16* utf16() const
  304. {
  305. if( !mUTF16 )
  306. {
  307. // Do this atomically to protect interned strings.
  308. UTF16* utf16 = createUTF16string( mData );
  309. if( !dCompareAndSwap( mUTF16,( UTF16* ) NULL, utf16 ) )
  310. delete [] utf16;
  311. }
  312. return mUTF16;
  313. }
  314. U32 getHashCase() const
  315. {
  316. return mHashCase;
  317. }
  318. U32 getOrCreateHashCase() const
  319. {
  320. if( mHashCase == U32_MAX )
  321. {
  322. PROFILE_SCOPE(StringData_getOrCreateHashCase);
  323. mHashCase = Torque::hash((const U8 *)(mData), mLength, 0);
  324. }
  325. return mHashCase;
  326. }
  327. U32 getHashNoCase() const
  328. {
  329. return mHashNoCase;
  330. }
  331. U32 getOrCreateHashNoCase() const
  332. {
  333. if( mHashNoCase == U32_MAX)
  334. {
  335. PROFILE_SCOPE(StringData_getOrCreateHashNoCase);
  336. UTF8 *lower = new UTF8[ mLength + 1 ];
  337. dStrncpy( lower, utf8(), mLength );
  338. lower[ mLength ] = 0;
  339. dStrlwr( lower );
  340. mHashNoCase = Torque::hash( (const U8*)lower, mLength, 0 );
  341. delete [] lower;
  342. }
  343. return mHashNoCase;
  344. }
  345. U32 getNumChars() const
  346. {
  347. if( mNumChars == U32_MAX )
  348. mNumChars = dStrlen( utf16() );
  349. return mNumChars;
  350. }
  351. bool isInterned() const
  352. {
  353. return mIsInterned;
  354. }
  355. static StringData* Empty()
  356. {
  357. static UTF16 emptyUTF16[ 1 ] = { 0 };
  358. static StringDataImpl empty =
  359. {
  360. #ifdef TORQUE_DEBUG
  361. "", // mString
  362. #endif
  363. U32_MAX, // mRefCount
  364. 0, // mLength
  365. 0, // mNumChars
  366. 0, // mHashCase
  367. 0, // mHashNoCase
  368. emptyUTF16, // mUTF16
  369. true, // mIsInterned
  370. { 0 } // mData
  371. };
  372. return ( StringData* ) &empty;
  373. }
  374. };
  375. //-----------------------------------------------------------------------------
  376. namespace DictHash
  377. {
  378. inline U32 hash( String::StringData* data )
  379. {
  380. return data->getOrCreateHashCase();
  381. }
  382. }
  383. namespace KeyCmp
  384. {
  385. template<>
  386. inline bool equals<>( String::StringData* const& d1, String::StringData* const& d2 )
  387. {
  388. return ( dStrcmp( d1->utf8(), d2->utf8() ) == 0 );
  389. }
  390. }
  391. /// Type for the intern string table. We don't want String instances directly
  392. /// on the table so that destructors don't run when the table is destroyed. This
  393. /// is because we really shouldn't depend on dtor ordering within this file and thus
  394. /// we can't tell whether the intern string memory is freed before or after the
  395. /// table is destroyed.
  396. struct StringInternTable : public HashTable< String::StringData*, String::StringData* >
  397. {
  398. Mutex mMutex;
  399. DataChunker mChunker;
  400. };
  401. static StringInternTable* sInternTable;
  402. struct KillInternTable
  403. {
  404. ~KillInternTable()
  405. {
  406. if( sInternTable )
  407. delete sInternTable;
  408. }
  409. };
  410. static KillInternTable sKillInternTable;
  411. //-----------------------------------------------------------------------------
  412. #ifdef TORQUE_DEBUG
  413. /// Tracks the number of bytes allocated for strings.
  414. /// @bug This currently does not include UTF16 allocations.
  415. static U32 sgStringMemBytes;
  416. /// Tracks the number of Strings which are currently instantiated.
  417. static U32 sgStringInstances;
  418. #endif
  419. DefineConsoleFunction( dumpStringMemStats, void, (), , "()"
  420. "@brief Dumps information about String memory usage\n\n"
  421. "@ingroup Debugging\n"
  422. "@ingroup Strings\n")
  423. {
  424. #ifdef TORQUE_DEBUG
  425. Con::printf( "String Data: %i instances, %i bytes", sgStringInstances, sgStringMemBytes );
  426. #endif
  427. }
  428. //-----------------------------------------------------------------------------
  429. void* String::StringData::operator new( size_t size, U32 len )
  430. {
  431. AssertFatal( len != 0, "String::StringData::operator new() - string must not be empty" );
  432. StringData *str = static_cast<StringData*>( dMalloc( size + len * sizeof(StringChar) ) );
  433. str->mLength = len;
  434. #ifdef TORQUE_DEBUG
  435. dFetchAndAdd( sgStringMemBytes, size + len * sizeof(StringChar) );
  436. dFetchAndAdd( sgStringInstances, 1 );
  437. #endif
  438. return str;
  439. }
  440. void String::StringData::operator delete(void *ptr)
  441. {
  442. StringData* sub = static_cast<StringData *>(ptr);
  443. AssertFatal( sub->mRefCount == 0, "StringData::delete() - invalid refcount" );
  444. #ifdef TORQUE_DEBUG
  445. dFetchAndAdd( sgStringMemBytes, U32( -( S32( sizeof( StringData ) + sub->mLength * sizeof(StringChar) ) ) ) );
  446. dFetchAndAdd( sgStringInstances, U32( -1 ) );
  447. #endif
  448. dFree( ptr );
  449. }
  450. void* String::StringData::operator new( size_t size, U32 len, DataChunker& chunker )
  451. {
  452. AssertFatal( len != 0, "String::StringData::operator new() - string must not be empty" );
  453. StringData *str = static_cast<StringData*>( chunker.alloc( size + len * sizeof(StringChar) ) );
  454. str->mLength = len;
  455. #ifdef TORQUE_DEBUG
  456. dFetchAndAdd( sgStringMemBytes, size + len * sizeof(StringChar) );
  457. dFetchAndAdd( sgStringInstances, 1 );
  458. #endif
  459. return str;
  460. }
  461. //-----------------------------------------------------------------------------
  462. String::String()
  463. {
  464. PROFILE_SCOPE(String_default_constructor);
  465. _string = StringData::Empty();
  466. }
  467. String::String(const String &str)
  468. {
  469. PROFILE_SCOPE(String_String_constructor);
  470. _string = str._string;
  471. _string->addRef();
  472. }
  473. String::String(const StringChar *str)
  474. {
  475. PROFILE_SCOPE(String_char_constructor);
  476. if( str && *str )
  477. {
  478. U32 len = dStrlen(str);
  479. _string = new ( len ) StringData( str );
  480. }
  481. else
  482. _string = StringData::Empty();
  483. }
  484. String::String(const StringChar *str, SizeType len)
  485. {
  486. PROFILE_SCOPE(String_char_len_constructor);
  487. if (str && *str && len!=0)
  488. {
  489. _string = new ( len ) StringData( str );
  490. }
  491. else
  492. _string = StringData::Empty();
  493. }
  494. String::String(const UTF16 *str)
  495. {
  496. PROFILE_SCOPE(String_UTF16_constructor);
  497. if( str && str[ 0 ] )
  498. {
  499. UTF8* utf8 = createUTF8string( str );
  500. U32 len = dStrlen( utf8 );
  501. _string = new ( len ) StringData( utf8 );
  502. delete [] utf8;
  503. }
  504. else
  505. _string = StringData::Empty();
  506. }
  507. String::~String()
  508. {
  509. _string->release();
  510. }
  511. //-----------------------------------------------------------------------------
  512. String String::intern() const
  513. {
  514. if( isInterned() )
  515. return *this;
  516. // Create the intern table, if we haven't already.
  517. if( !sInternTable )
  518. sInternTable = new StringInternTable;
  519. // Lock the string table.
  520. MutexHandle mutex;
  521. mutex.lock( &sInternTable->mMutex );
  522. // Lookup.
  523. StringInternTable::Iterator iter = sInternTable->find( _string );
  524. if( iter != sInternTable->end() )
  525. return ( *iter ).value;
  526. // Create new.
  527. StringData* data = new ( length(), sInternTable->mChunker ) StringData( c_str(), true );
  528. iter = sInternTable->insertUnique( data, data );
  529. return ( *iter ).value;
  530. }
  531. //-----------------------------------------------------------------------------
  532. const StringChar* String::c_str() const
  533. {
  534. return _string->utf8();
  535. }
  536. const UTF16 *String::utf16() const
  537. {
  538. return _string->utf16();
  539. }
  540. String::SizeType String::length() const
  541. {
  542. return _string->getLength();
  543. }
  544. String::SizeType String::size() const
  545. {
  546. return _string->getDataSize();
  547. }
  548. String::SizeType String::numChars() const
  549. {
  550. return _string->getNumChars();
  551. }
  552. bool String::isEmpty() const
  553. {
  554. return ( _string == StringData::Empty() );
  555. }
  556. bool String::isShared() const
  557. {
  558. return _string->isShared();
  559. }
  560. bool String::isSame( const String& str ) const
  561. {
  562. return ( _string == str._string );
  563. }
  564. bool String::isInterned() const
  565. {
  566. return ( _string->isInterned() );
  567. }
  568. U32 String::getHashCaseSensitive() const
  569. {
  570. return _string->getOrCreateHashCase();
  571. }
  572. U32 String::getHashCaseInsensitive() const
  573. {
  574. return _string->getOrCreateHashNoCase();
  575. }
  576. //-----------------------------------------------------------------------------
  577. String::SizeType String::find(const String &str, SizeType pos, U32 mode) const
  578. {
  579. return find(str._string->utf8(), pos, mode);
  580. }
  581. String& String::insert(SizeType pos, const String &str)
  582. {
  583. return insert(pos, str._string->utf8());
  584. }
  585. String& String::replace(SizeType pos, SizeType len, const String &str)
  586. {
  587. return replace(pos, len, str._string->utf8());
  588. }
  589. //-----------------------------------------------------------------------------
  590. String& String::operator=(StringChar c)
  591. {
  592. _string->release();
  593. _string = new ( 2 ) StringData( 0 );
  594. _string->utf8()[ 0 ] = c;
  595. _string->utf8()[ 1 ] = '\0';
  596. return *this;
  597. }
  598. String& String::operator+=(StringChar c)
  599. {
  600. // Append the given string into a new string
  601. U32 len = _string->getLength();
  602. StringData* sub = new ( len + 1 ) StringData( NULL );
  603. copy( sub->utf8(), _string->utf8(), len );
  604. sub->utf8()[len] = c;
  605. sub->utf8()[len+1] = 0;
  606. _string->release();
  607. _string = sub;
  608. return *this;
  609. }
  610. //-----------------------------------------------------------------------------
  611. String& String::operator=(const StringChar *str)
  612. {
  613. // Protect against self assignment which is not only a
  614. // waste of time, but can also lead to the string being
  615. // freed before it can be reassigned.
  616. if ( _string->utf8() == str )
  617. return *this;
  618. _string->release();
  619. if (str && *str)
  620. {
  621. U32 len = dStrlen(str);
  622. _string = new ( len ) StringData( str );
  623. }
  624. else
  625. _string = StringData::Empty();
  626. return *this;
  627. }
  628. String& String::operator=(const String &src)
  629. {
  630. // Inc src first to avoid assignment to self problems.
  631. src._string->addRef();
  632. _string->release();
  633. _string = src._string;
  634. return *this;
  635. }
  636. String& String::operator+=(const StringChar *src)
  637. {
  638. if( src == NULL || !*src )
  639. return *this;
  640. // Append the given string into a new string
  641. U32 lena = _string->getLength();
  642. U32 lenb = dStrlen(src);
  643. U32 newlen = lena + lenb;
  644. StringData* sub;
  645. if( !newlen )
  646. sub = StringData::Empty();
  647. else
  648. {
  649. sub = new ( newlen ) StringData( NULL );
  650. copy(sub->utf8(),_string->utf8(),lena);
  651. copy(sub->utf8() + lena,src,lenb + 1);
  652. }
  653. _string->release();
  654. _string = sub;
  655. return *this;
  656. }
  657. String& String::operator+=(const String &src)
  658. {
  659. if( src.isEmpty() )
  660. return *this;
  661. // Append the given string into a new string
  662. U32 lena = _string->getLength();
  663. U32 lenb = src._string->getLength();
  664. U32 newlen = lena + lenb;
  665. StringData* sub;
  666. if( !newlen )
  667. sub = StringData::Empty();
  668. else
  669. {
  670. sub = new ( newlen ) StringData( NULL );
  671. copy(sub->utf8(),_string->utf8(),lena);
  672. copy(sub->utf8() + lena,src._string->utf8(),lenb + 1);
  673. }
  674. _string->release();
  675. _string = sub;
  676. return *this;
  677. }
  678. //-----------------------------------------------------------------------------
  679. String operator+(const String &a, const String &b)
  680. {
  681. PROFILE_SCOPE( String_String_plus_String );
  682. if( a.isEmpty() )
  683. return b;
  684. else if( b.isEmpty() )
  685. return a;
  686. U32 lena = a.length();
  687. U32 lenb = b.length();
  688. String::StringData *sub = new ( lena + lenb ) String::StringData( NULL );
  689. String::copy(sub->utf8(),a._string->utf8(),lena);
  690. String::copy(sub->utf8() + lena,b._string->utf8(),lenb + 1);
  691. return String(sub);
  692. }
  693. String operator+(const String &a, StringChar c)
  694. {
  695. //PROFILE_SCOPE( String_String_plus_Char );
  696. U32 lena = a.length();
  697. String::StringData *sub = new ( lena + 1 ) String::StringData( NULL );
  698. String::copy(sub->utf8(),a._string->utf8(),lena);
  699. sub->utf8()[lena] = c;
  700. sub->utf8()[lena+1] = 0;
  701. return String(sub);
  702. }
  703. String operator+(StringChar c, const String &a)
  704. {
  705. //PROFILE_SCOPE( String_Char_plus_String );
  706. U32 lena = a.length();
  707. String::StringData *sub = new ( lena + 1 ) String::StringData( NULL );
  708. String::copy(sub->utf8() + 1,a._string->utf8(),lena + 1);
  709. sub->utf8()[0] = c;
  710. return String(sub);
  711. }
  712. String operator+(const String &a, const StringChar *b)
  713. {
  714. //PROFILE_SCOPE( String_String_plus_CString );
  715. AssertFatal(b,"String:: Invalid null ptr argument");
  716. if( a.isEmpty() )
  717. return String( b );
  718. U32 lena = a.length();
  719. U32 lenb = dStrlen(b);
  720. if( !lenb )
  721. return a;
  722. String::StringData *sub = new ( lena + lenb ) String::StringData( NULL );
  723. String::copy(sub->utf8(),a._string->utf8(),lena);
  724. String::copy(sub->utf8() + lena,b,lenb + 1);
  725. return String(sub);
  726. }
  727. String operator+(const StringChar *a, const String &b)
  728. {
  729. //PROFILE_SCOPE( String_CString_plus_String );
  730. AssertFatal(a,"String:: Invalid null ptr argument");
  731. if( b.isEmpty() )
  732. return String( a );
  733. U32 lena = dStrlen(a);
  734. if( !lena )
  735. return b;
  736. U32 lenb = b.length();
  737. String::StringData* sub = new ( lena + lenb ) String::StringData( NULL );
  738. String::copy(sub->utf8(),a,lena);
  739. String::copy(sub->utf8() + lena,b._string->utf8(),lenb + 1);
  740. return String(sub);
  741. }
  742. bool String::operator==(const String &str) const
  743. {
  744. //PROFILE_SCOPE( String_op_equal );
  745. if( str._string == _string )
  746. return true;
  747. else if( str._string->isInterned() && _string->isInterned() )
  748. return false;
  749. else if( str.length() != length() )
  750. return false;
  751. else if( str._string->getHashCase() != U32_MAX
  752. && _string->getHashCase() != U32_MAX
  753. && str._string->getHashCase() != _string->getHashCase() )
  754. return false;
  755. else
  756. return ( dMemcmp( str._string->utf8(), _string->utf8(), _string->getLength() ) == 0 );
  757. }
  758. bool String::operator==( StringChar c ) const
  759. {
  760. if( !_string || _string->getLength() != 1 )
  761. return false;
  762. else
  763. return ( _string->utf8()[ 0 ] == c );
  764. }
  765. bool String::operator<(const String &str) const
  766. {
  767. return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) < 0 );
  768. }
  769. bool String::operator>(const String &str) const
  770. {
  771. return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) > 0 );
  772. }
  773. bool String::operator<=(const String &str) const
  774. {
  775. return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) <= 0 );
  776. }
  777. bool String::operator>=(const String &str) const
  778. {
  779. return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) >= 0 );
  780. }
  781. //-----------------------------------------------------------------------------
  782. // Base functions for string comparison
  783. S32 String::compare(const StringChar *str, SizeType len, U32 mode) const
  784. {
  785. PROFILE_SCOPE( String_compare );
  786. AssertFatal(str,"String:: Invalid null ptr argument");
  787. const StringChar *p1 = _string->utf8();
  788. const StringChar *p2 = str;
  789. if (p1 == p2)
  790. return 0;
  791. if( mode & String::Right )
  792. {
  793. U32 n = len;
  794. if( n > length() )
  795. n = length();
  796. p1 += length() - n;
  797. p2 += dStrlen( str ) - n;
  798. }
  799. if (mode & String::NoCase)
  800. {
  801. if (len)
  802. {
  803. for (;--len; p1++,p2++)
  804. {
  805. if (dTolower(*p1) != dTolower(*p2) || !*p1)
  806. break;
  807. }
  808. }
  809. else
  810. {
  811. while (dTolower(*p1) == dTolower(*p2) && *p1)
  812. {
  813. p1++;
  814. p2++;
  815. }
  816. }
  817. return dTolower(*p1) - dTolower(*p2);
  818. }
  819. if (len)
  820. return dMemcmp(p1,p2,len);
  821. while (*p1 == *p2 && *p1)
  822. {
  823. p1++;
  824. p2++;
  825. }
  826. return *p1 - *p2;
  827. }
  828. S32 String::compare(const String &str, SizeType len, U32 mode) const
  829. {
  830. if ( str._string == _string )
  831. return 0;
  832. return compare( str.c_str(), len, mode );
  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 = new ( newlen ) StringData( NULL );
  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 = new ( newlen ) StringData( NULL );
  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 = new ( newlen ) StringData( NULL );
  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 = new ( length() ) StringData( _string->utf8() );
  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 = new (newSize - 1 ) StringData( NULL );
  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 = new ( len ) StringData( _string->utf8() + pos );
  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 = new ( len ) StringData( start );
  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) || defined(TORQUE_OS_XBOX) || defined(TORQUE_OS_XENON)
  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 = new ( len ) StringData( NULL );
  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. StringData* sub = new ( len ) StringData( start );
  1258. return sub;
  1259. }
  1260. String String::ToLower(const String &string)
  1261. {
  1262. if ( string.isEmpty() )
  1263. return String();
  1264. StringData* sub = new ( string.length() ) StringData( string );
  1265. dStrlwr( sub->utf8() );
  1266. return sub;
  1267. }
  1268. String String::ToUpper(const String &string)
  1269. {
  1270. if ( string.isEmpty() )
  1271. return String();
  1272. StringData* sub = new ( string.length() ) StringData( string );
  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 = ((p == base.c_str()) ? 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. }