str.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  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::isEmpty(const char* str)
  557. {
  558. return str == 0 || str[0] == '\0';
  559. }
  560. bool String::isShared() const
  561. {
  562. return _string->isShared();
  563. }
  564. bool String::isSame( const String& str ) const
  565. {
  566. return ( _string == str._string );
  567. }
  568. bool String::isInterned() const
  569. {
  570. return ( _string->isInterned() );
  571. }
  572. U32 String::getHashCaseSensitive() const
  573. {
  574. return _string->getOrCreateHashCase();
  575. }
  576. U32 String::getHashCaseInsensitive() const
  577. {
  578. return _string->getOrCreateHashNoCase();
  579. }
  580. //-----------------------------------------------------------------------------
  581. String::SizeType String::find(const String &str, SizeType pos, U32 mode) const
  582. {
  583. return find(str._string->utf8(), pos, mode);
  584. }
  585. String& String::insert(SizeType pos, const String &str)
  586. {
  587. return insert(pos, str._string->utf8());
  588. }
  589. String& String::replace(SizeType pos, SizeType len, const String &str)
  590. {
  591. return replace(pos, len, str._string->utf8());
  592. }
  593. //-----------------------------------------------------------------------------
  594. String& String::operator=(StringChar c)
  595. {
  596. _string->release();
  597. _string = new ( 2 ) StringData( 0 );
  598. _string->utf8()[ 0 ] = c;
  599. _string->utf8()[ 1 ] = '\0';
  600. return *this;
  601. }
  602. String& String::operator+=(StringChar c)
  603. {
  604. // Append the given string into a new string
  605. U32 len = _string->getLength();
  606. StringData* sub = new ( len + 1 ) StringData( NULL );
  607. copy( sub->utf8(), _string->utf8(), len );
  608. sub->utf8()[len] = c;
  609. sub->utf8()[len+1] = 0;
  610. _string->release();
  611. _string = sub;
  612. return *this;
  613. }
  614. //-----------------------------------------------------------------------------
  615. String& String::operator=(const StringChar *str)
  616. {
  617. // Protect against self assignment which is not only a
  618. // waste of time, but can also lead to the string being
  619. // freed before it can be reassigned.
  620. if ( _string->utf8() == str )
  621. return *this;
  622. _string->release();
  623. if (str && *str)
  624. {
  625. U32 len = dStrlen(str);
  626. _string = new ( len ) StringData( str );
  627. }
  628. else
  629. _string = StringData::Empty();
  630. return *this;
  631. }
  632. String& String::operator=(const String &src)
  633. {
  634. // Inc src first to avoid assignment to self problems.
  635. src._string->addRef();
  636. _string->release();
  637. _string = src._string;
  638. return *this;
  639. }
  640. String& String::operator+=(const StringChar *src)
  641. {
  642. if( src == NULL || !*src )
  643. return *this;
  644. // Append the given string into a new string
  645. U32 lena = _string->getLength();
  646. U32 lenb = dStrlen(src);
  647. U32 newlen = lena + lenb;
  648. StringData* sub;
  649. if( !newlen )
  650. sub = StringData::Empty();
  651. else
  652. {
  653. sub = new ( newlen ) StringData( NULL );
  654. copy(sub->utf8(),_string->utf8(),lena);
  655. copy(sub->utf8() + lena,src,lenb + 1);
  656. }
  657. _string->release();
  658. _string = sub;
  659. return *this;
  660. }
  661. String& String::operator+=(const String &src)
  662. {
  663. if( src.isEmpty() )
  664. return *this;
  665. // Append the given string into a new string
  666. U32 lena = _string->getLength();
  667. U32 lenb = src._string->getLength();
  668. U32 newlen = lena + lenb;
  669. StringData* sub;
  670. if( !newlen )
  671. sub = StringData::Empty();
  672. else
  673. {
  674. sub = new ( newlen ) StringData( NULL );
  675. copy(sub->utf8(),_string->utf8(),lena);
  676. copy(sub->utf8() + lena,src._string->utf8(),lenb + 1);
  677. }
  678. _string->release();
  679. _string = sub;
  680. return *this;
  681. }
  682. //-----------------------------------------------------------------------------
  683. String operator+(const String &a, const String &b)
  684. {
  685. PROFILE_SCOPE( String_String_plus_String );
  686. if( a.isEmpty() )
  687. return b;
  688. else if( b.isEmpty() )
  689. return a;
  690. U32 lena = a.length();
  691. U32 lenb = b.length();
  692. String::StringData *sub = new ( lena + lenb ) String::StringData( NULL );
  693. String::copy(sub->utf8(),a._string->utf8(),lena);
  694. String::copy(sub->utf8() + lena,b._string->utf8(),lenb + 1);
  695. return String(sub);
  696. }
  697. String operator+(const String &a, StringChar c)
  698. {
  699. //PROFILE_SCOPE( String_String_plus_Char );
  700. U32 lena = a.length();
  701. String::StringData *sub = new ( lena + 1 ) String::StringData( NULL );
  702. String::copy(sub->utf8(),a._string->utf8(),lena);
  703. sub->utf8()[lena] = c;
  704. sub->utf8()[lena+1] = 0;
  705. return String(sub);
  706. }
  707. String operator+(StringChar c, const String &a)
  708. {
  709. //PROFILE_SCOPE( String_Char_plus_String );
  710. U32 lena = a.length();
  711. String::StringData *sub = new ( lena + 1 ) String::StringData( NULL );
  712. String::copy(sub->utf8() + 1,a._string->utf8(),lena + 1);
  713. sub->utf8()[0] = c;
  714. return String(sub);
  715. }
  716. String operator+(const String &a, const StringChar *b)
  717. {
  718. //PROFILE_SCOPE( String_String_plus_CString );
  719. AssertFatal(b,"String:: Invalid null ptr argument");
  720. if( a.isEmpty() )
  721. return String( b );
  722. U32 lena = a.length();
  723. U32 lenb = dStrlen(b);
  724. if( !lenb )
  725. return a;
  726. String::StringData *sub = new ( lena + lenb ) String::StringData( NULL );
  727. String::copy(sub->utf8(),a._string->utf8(),lena);
  728. String::copy(sub->utf8() + lena,b,lenb + 1);
  729. return String(sub);
  730. }
  731. String operator+(const StringChar *a, const String &b)
  732. {
  733. //PROFILE_SCOPE( String_CString_plus_String );
  734. AssertFatal(a,"String:: Invalid null ptr argument");
  735. if( b.isEmpty() )
  736. return String( a );
  737. U32 lena = dStrlen(a);
  738. if( !lena )
  739. return b;
  740. U32 lenb = b.length();
  741. String::StringData* sub = new ( lena + lenb ) String::StringData( NULL );
  742. String::copy(sub->utf8(),a,lena);
  743. String::copy(sub->utf8() + lena,b._string->utf8(),lenb + 1);
  744. return String(sub);
  745. }
  746. bool String::operator==(const String &str) const
  747. {
  748. //PROFILE_SCOPE( String_op_equal );
  749. if( str._string == _string )
  750. return true;
  751. else if( str._string->isInterned() && _string->isInterned() )
  752. return false;
  753. else if( str.length() != length() )
  754. return false;
  755. else if( str._string->getHashCase() != U32_MAX
  756. && _string->getHashCase() != U32_MAX
  757. && str._string->getHashCase() != _string->getHashCase() )
  758. return false;
  759. else
  760. return ( dMemcmp( str._string->utf8(), _string->utf8(), _string->getLength() ) == 0 );
  761. }
  762. bool String::operator==( StringChar c ) const
  763. {
  764. if( !_string || _string->getLength() != 1 )
  765. return false;
  766. else
  767. return ( _string->utf8()[ 0 ] == c );
  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. bool String::operator>=(const String &str) const
  782. {
  783. return ( dStrnatcmp( _string->utf8(), str._string->utf8() ) >= 0 );
  784. }
  785. //-----------------------------------------------------------------------------
  786. // Base functions for string comparison
  787. S32 String::compare(const StringChar *str, SizeType len, U32 mode) const
  788. {
  789. PROFILE_SCOPE( String_compare );
  790. AssertFatal(str,"String:: Invalid null ptr argument");
  791. const StringChar *p1 = _string->utf8();
  792. const StringChar *p2 = str;
  793. if (p1 == p2)
  794. return 0;
  795. if( mode & String::Right )
  796. {
  797. U32 n = len;
  798. if( n > length() )
  799. n = length();
  800. p1 += length() - n;
  801. p2 += dStrlen( str ) - n;
  802. }
  803. if (mode & String::NoCase)
  804. {
  805. if (len)
  806. {
  807. for (;--len; p1++,p2++)
  808. {
  809. if (dTolower(*p1) != dTolower(*p2) || !*p1)
  810. break;
  811. }
  812. }
  813. else
  814. {
  815. while (dTolower(*p1) == dTolower(*p2) && *p1)
  816. {
  817. p1++;
  818. p2++;
  819. }
  820. }
  821. return dTolower(*p1) - dTolower(*p2);
  822. }
  823. if (len)
  824. return dMemcmp(p1,p2,len);
  825. while (*p1 == *p2 && *p1)
  826. {
  827. p1++;
  828. p2++;
  829. }
  830. return *p1 - *p2;
  831. }
  832. S32 String::compare(const String &str, SizeType len, U32 mode) const
  833. {
  834. if ( str._string == _string )
  835. return 0;
  836. return compare( str.c_str(), len, mode );
  837. }
  838. bool String::equal(const String &str, U32 mode) const
  839. {
  840. if( !mode )
  841. return ( *this == str );
  842. else
  843. {
  844. if( _string == str._string )
  845. return true;
  846. else if( _string->isInterned() && str._string->isInterned() )
  847. return false;
  848. else if( length() != str.length() )
  849. return false;
  850. else if( _string->getHashNoCase() != U32_MAX
  851. && str._string->getHashNoCase() != U32_MAX
  852. && _string->getHashNoCase() != str._string->getHashNoCase() )
  853. return false;
  854. else
  855. return ( compare( str.c_str(), length(), mode ) == 0 );
  856. }
  857. }
  858. //-----------------------------------------------------------------------------
  859. String::SizeType String::find(StringChar c, SizeType pos, U32 mode) const
  860. {
  861. const StringChar* ptr = StrFind(_string->utf8(),c,pos,mode);
  862. return ptr? SizeType(ptr - _string->utf8()): NPos;
  863. }
  864. String::SizeType String::find(const StringChar *str, SizeType pos, U32 mode) const
  865. {
  866. AssertFatal(str,"String:: Invalid null ptr argument");
  867. const StringChar* ptr = StrFind(_string->utf8(),str,pos,mode);
  868. return ptr? SizeType(ptr - _string->utf8()): NPos;
  869. }
  870. //-----------------------------------------------------------------------------
  871. String& String::insert(SizeType pos, const StringChar *str)
  872. {
  873. AssertFatal(str,"String:: Invalid null ptr argument");
  874. return insert(pos,str,dStrlen(str));
  875. }
  876. ///@todo review for error checking
  877. String& String::insert(SizeType pos, const StringChar *str, SizeType len)
  878. {
  879. if( !len )
  880. return *this;
  881. AssertFatal( str, "String:: Invalid null ptr argument" );
  882. SizeType lena = length();
  883. AssertFatal((pos <= lena),"Calling String::insert with position greater than length");
  884. U32 newlen = lena + len;
  885. StringData *sub;
  886. if( !newlen )
  887. sub = StringData::Empty();
  888. else
  889. {
  890. sub = new ( newlen ) StringData( NULL );
  891. String::copy(sub->utf8(),_string->utf8(),pos);
  892. String::copy(sub->utf8() + pos,str,len);
  893. String::copy(sub->utf8() + pos + len,_string->utf8() + pos,lena - pos + 1);
  894. }
  895. _string->release();
  896. _string = sub;
  897. return *this;
  898. }
  899. String& String::erase(SizeType pos, SizeType len)
  900. {
  901. AssertFatal( len != 0, "String::erase() - Calling String::erase with 0 length" );
  902. AssertFatal( ( pos + len ) <= length(), "String::erase() - Invalid string region" );
  903. if( !len )
  904. return *this;
  905. SizeType slen = length();
  906. U32 newlen = slen - len;
  907. StringData *sub;
  908. if( !newlen )
  909. sub = StringData::Empty();
  910. else
  911. {
  912. sub = new ( newlen ) StringData( NULL );
  913. if (pos > 0)
  914. String::copy(sub->utf8(),_string->utf8(),pos);
  915. String::copy(sub->utf8() + pos, _string->utf8() + pos + len, slen - (pos + len) + 1);
  916. }
  917. _string->release();
  918. _string = sub;
  919. return *this;
  920. }
  921. ///@todo review for error checking
  922. String& String::replace(SizeType pos, SizeType len, const StringChar *str)
  923. {
  924. AssertFatal( str, "String::replace() - Invalid null ptr argument" );
  925. AssertFatal( len != 0, "String::replace() - Zero length" );
  926. AssertFatal( ( pos + len ) <= length(), "String::replace() - Invalid string region" );
  927. SizeType slen = length();
  928. SizeType rlen = dStrlen(str);
  929. U32 newlen = slen - len + rlen;
  930. StringData *sub;
  931. if( !newlen )
  932. sub = StringData::Empty();
  933. else
  934. {
  935. sub = new ( newlen ) StringData( NULL );
  936. String::copy(sub->utf8(),_string->utf8(), pos);
  937. String::copy(sub->utf8() + pos,str,rlen);
  938. String::copy(sub->utf8() + pos + rlen,_string->utf8() + pos + len,slen - pos - len + 1);
  939. }
  940. _string->release();
  941. _string = sub;
  942. return *this;
  943. }
  944. String& String::replace( StringChar c1, StringChar c2 )
  945. {
  946. if( isEmpty() )
  947. return *this;
  948. // Create the new string lazily so that we don't needlessly
  949. // dup strings when there is nothing to replace.
  950. StringData* sub = NULL;
  951. bool foundReplacement = false;
  952. StringChar* c = _string->utf8();
  953. while( *c )
  954. {
  955. if( *c == c1 )
  956. {
  957. if( !foundReplacement )
  958. {
  959. sub = new ( length() ) StringData( _string->utf8() );
  960. c = &sub->utf8()[ c - _string->utf8() ];
  961. foundReplacement = true;
  962. }
  963. *c = c2;
  964. }
  965. c++;
  966. }
  967. if( foundReplacement )
  968. {
  969. _string->release();
  970. _string = sub;
  971. }
  972. return *this;
  973. }
  974. String &String::replace(const String &s1, const String &s2)
  975. {
  976. // Find number of occurrences of s1 and
  977. // Calculate length of the new string...
  978. const U32 &s1len = s1.length();
  979. const U32 &s2len = s2.length();
  980. U32 pos = 0;
  981. Vector<U32> indices;
  982. StringChar *walk = _string->utf8();
  983. while ( walk )
  984. {
  985. // Casting away the const... was there a better way?
  986. walk = (StringChar*)StrFind( _string->utf8(), s1.c_str(), pos, Case|Left );
  987. if ( walk )
  988. {
  989. pos = SizeType(walk - _string->utf8());
  990. indices.push_back( pos );
  991. pos += s1len;
  992. }
  993. }
  994. // Early-out, no StringDatas found.
  995. if ( indices.size() == 0 )
  996. return *this;
  997. U32 newSize = size() - ( indices.size() * s1len ) + ( indices.size() * s2len );
  998. StringData *sub;
  999. if( newSize == 1 )
  1000. sub = StringData::Empty();
  1001. else
  1002. {
  1003. sub = new (newSize - 1 ) StringData( NULL );
  1004. // Now assemble the new string from the pieces of the old...
  1005. // Index into the old string
  1006. pos = 0;
  1007. // Index into the new string
  1008. U32 newPos = 0;
  1009. // Used to store a character count to be memcpy'd
  1010. U32 copyCharCount = 0;
  1011. for ( U32 i = 0; i < indices.size(); i++ )
  1012. {
  1013. const U32 &index = indices[i];
  1014. // Number of chars (if any) before the next indexed StringData
  1015. copyCharCount = index - pos;
  1016. // Copy chars before the StringData if we have any.
  1017. if ( copyCharCount > 0 )
  1018. {
  1019. dMemcpy( sub->utf8() + newPos, _string->utf8() + pos, copyCharCount * sizeof(StringChar) );
  1020. newPos += copyCharCount;
  1021. }
  1022. // Copy over the replacement string.
  1023. if ( s2len > 0 )
  1024. dMemcpy( sub->utf8() + newPos, s2._string->utf8(), s2len * sizeof(StringChar) );
  1025. newPos += s2len;
  1026. pos = index + s1len;
  1027. }
  1028. // There could be characters left in the original string after the last
  1029. // StringData occurrence, which we need to copy now - outside the loop.
  1030. copyCharCount = length() - indices.last() - s1len;
  1031. if ( copyCharCount != 0 )
  1032. dMemcpy( sub->utf8() + newPos, _string->utf8() + pos, copyCharCount * sizeof(StringChar) );
  1033. // Null terminate it!
  1034. sub->utf8()[newSize-1] = 0;
  1035. }
  1036. _string->release();
  1037. _string = sub;
  1038. return *this;
  1039. }
  1040. //-----------------------------------------------------------------------------
  1041. String String::substr(SizeType pos, SizeType len) const
  1042. {
  1043. //PROFILE_SCOPE( String_substr );
  1044. AssertFatal( pos <= length(), "String::substr - Invalid position!" );
  1045. if ( len == -1 )
  1046. len = length() - pos;
  1047. AssertFatal( len + pos <= length(), "String::substr - Invalid length!" );
  1048. StringData* sub;
  1049. if( !len )
  1050. sub = StringData::Empty();
  1051. else
  1052. sub = new ( len ) StringData( _string->utf8() + pos );
  1053. return sub;
  1054. }
  1055. //-----------------------------------------------------------------------------
  1056. String String::trim() const
  1057. {
  1058. if( isEmpty() )
  1059. return *this;
  1060. const StringChar* start = _string->utf8();
  1061. while( *start && dIsspace( *start ) )
  1062. start ++;
  1063. const StringChar* end = _string->utf8() + length() - 1;
  1064. while( end > start && dIsspace( *end ) )
  1065. end --;
  1066. end ++;
  1067. const U32 len = end - start;
  1068. if( len == length() )
  1069. return *this;
  1070. StringData* sub;
  1071. if( !len )
  1072. sub = StringData::Empty();
  1073. else
  1074. sub = new ( len ) StringData( start );
  1075. return sub;
  1076. }
  1077. //-----------------------------------------------------------------------------
  1078. String String::expandEscapes() const
  1079. {
  1080. char* tmp = ( char* ) dMalloc( length() * 2 + 1 ); // worst-case situation.
  1081. expandEscape( tmp, c_str() );
  1082. String str( tmp );
  1083. dFree( tmp );
  1084. return str;
  1085. }
  1086. //-----------------------------------------------------------------------------
  1087. String String::collapseEscapes() const
  1088. {
  1089. char* tmp = dStrdup( c_str() );
  1090. collapseEscape( tmp );
  1091. String str( tmp );
  1092. dFree( tmp );
  1093. return str;
  1094. }
  1095. //-----------------------------------------------------------------------------
  1096. void String::split( const char* delimiter, Vector< String >& outElements ) const
  1097. {
  1098. const char* ptr = _string->utf8();
  1099. const char* start = ptr;
  1100. while( *ptr )
  1101. {
  1102. // Search for start of delimiter.
  1103. if( *ptr != delimiter[ 0 ] )
  1104. ptr ++;
  1105. else
  1106. {
  1107. // Skip delimiter.
  1108. const char* end = ptr;
  1109. const char* del = delimiter;
  1110. while( *del && *del == *ptr )
  1111. {
  1112. ptr ++;
  1113. del ++;
  1114. }
  1115. // If we didn't match all of delimiter,
  1116. // continue with search.
  1117. if( *del != '\0' )
  1118. continue;
  1119. // Extract component.
  1120. outElements.push_back( String( start, end - start ) );
  1121. start = ptr;
  1122. }
  1123. }
  1124. // Add rest of string if there is any.
  1125. if( start != ptr )
  1126. outElements.push_back( start );
  1127. }
  1128. //-----------------------------------------------------------------------------
  1129. bool String::startsWith( const char* text ) const
  1130. {
  1131. return dStrStartsWith( _string->utf8(), text );
  1132. }
  1133. //-----------------------------------------------------------------------------
  1134. bool String::endsWith( const char* text ) const
  1135. {
  1136. return dStrEndsWith( _string->utf8(), text );
  1137. }
  1138. //-----------------------------------------------------------------------------
  1139. void String::copy(StringChar* dst, const StringChar *src, U32 len)
  1140. {
  1141. dMemcpy(dst, src, len * sizeof(StringChar));
  1142. }
  1143. //-----------------------------------------------------------------------------
  1144. #if defined(TORQUE_OS_WIN) || defined(TORQUE_OS_XBOX) || defined(TORQUE_OS_XENON)
  1145. // This standard function is not defined when compiling with VC7...
  1146. #define vsnprintf _vsnprintf
  1147. #endif
  1148. String::StrFormat::~StrFormat()
  1149. {
  1150. if( _dynamicBuffer )
  1151. dFree( _dynamicBuffer );
  1152. }
  1153. S32 String::StrFormat::format( const char *format, va_list args )
  1154. {
  1155. _len=0;
  1156. return formatAppend(format,args);
  1157. }
  1158. S32 String::StrFormat::formatAppend( const char *format, va_list args )
  1159. {
  1160. // Format into the fixed buffer first.
  1161. S32 startLen = _len;
  1162. if (_dynamicBuffer == NULL)
  1163. {
  1164. _len += vsnprintf(_fixedBuffer + _len, sizeof(_fixedBuffer) - _len, format, args);
  1165. if (_len >= 0 && _len < sizeof(_fixedBuffer))
  1166. return _len;
  1167. // Start off the dynamic buffer at twice fixed buffer size
  1168. _len = startLen;
  1169. _dynamicSize = sizeof(_fixedBuffer) * 2;
  1170. _dynamicBuffer = (char*)dMalloc(_dynamicSize);
  1171. dMemcpy(_dynamicBuffer, _fixedBuffer, _len + 1);
  1172. }
  1173. // Format into the dynamic buffer, if the buffer is not large enough, then
  1174. // keep doubling it's size until it is. The buffer is not reallocated
  1175. // using reallocate() to avoid unnecessary buffer copying.
  1176. _len += vsnprintf(_dynamicBuffer + _len, _dynamicSize - _len, format, *(va_list*)args);
  1177. while (_len < 0 || _len >= _dynamicSize)
  1178. {
  1179. _len = startLen;
  1180. _dynamicBuffer = (char*)dRealloc(_dynamicBuffer, _dynamicSize *= 2);
  1181. _len += vsnprintf(_dynamicBuffer + _len, _dynamicSize - _len, format, *(va_list*)args);
  1182. }
  1183. return _len;
  1184. }
  1185. S32 String::StrFormat::append(const char * str, S32 len)
  1186. {
  1187. if (_dynamicBuffer == NULL)
  1188. {
  1189. if (_len+len >= 0 && _len+len < sizeof(_fixedBuffer))
  1190. {
  1191. dMemcpy(_fixedBuffer + _len, str, len);
  1192. _len += len;
  1193. _fixedBuffer[_len] = '\0';
  1194. return _len;
  1195. }
  1196. _dynamicSize = sizeof(_fixedBuffer) * 2;
  1197. _dynamicBuffer = (char*)dMalloc(_dynamicSize);
  1198. dMemcpy(_dynamicBuffer, _fixedBuffer, _len + 1);
  1199. }
  1200. S32 newSize = _dynamicSize;
  1201. while (newSize < _len+len)
  1202. newSize *= 2;
  1203. if (newSize != _dynamicSize)
  1204. _dynamicBuffer = (char*) dRealloc(_dynamicBuffer, newSize);
  1205. _dynamicSize = newSize;
  1206. dMemcpy(_dynamicBuffer + _len, str, len);
  1207. _len += len;
  1208. _dynamicBuffer[_len] = '\0';
  1209. return _len;
  1210. }
  1211. S32 String::StrFormat::append(const char * str)
  1212. {
  1213. return append(str, dStrlen(str));
  1214. }
  1215. char* String::StrFormat::copy( char *buffer ) const
  1216. {
  1217. dMemcpy(buffer, _dynamicBuffer? _dynamicBuffer: _fixedBuffer, _len+1);
  1218. return buffer;
  1219. }
  1220. //-----------------------------------------------------------------------------
  1221. String String::ToString( bool value )
  1222. {
  1223. static String sTrue = "true";
  1224. static String sFalse = "false";
  1225. if( value )
  1226. return sTrue;
  1227. return sFalse;
  1228. }
  1229. String String::ToString(const char *str, ...)
  1230. {
  1231. AssertFatal(str,"String:: Invalid null ptr argument");
  1232. // Use the format object
  1233. va_list args;
  1234. va_start(args, str);
  1235. String ret = VToString(str, args);
  1236. va_end(args);
  1237. return ret;
  1238. }
  1239. String String::VToString(const char* str, va_list args)
  1240. {
  1241. StrFormat format(str,args);
  1242. // Copy it into a string
  1243. U32 len = format.length();
  1244. StringData* sub;
  1245. if( !len )
  1246. sub = StringData::Empty();
  1247. else
  1248. {
  1249. sub = new ( len ) StringData( NULL );
  1250. format.copy( sub->utf8() );
  1251. sub->utf8()[ len ] = 0;
  1252. }
  1253. return sub;
  1254. }
  1255. String String::SpanToString(const char *start, const char *end)
  1256. {
  1257. if ( end == start )
  1258. return String();
  1259. AssertFatal( end > start, "Invalid arguments to String::SpanToString - end is before start" );
  1260. U32 len = U32(end - start);
  1261. StringData* sub = new ( len ) StringData( start );
  1262. return sub;
  1263. }
  1264. String String::ToLower(const String &string)
  1265. {
  1266. if ( string.isEmpty() )
  1267. return String();
  1268. StringData* sub = new ( string.length() ) StringData( string );
  1269. dStrlwr( sub->utf8() );
  1270. return sub;
  1271. }
  1272. String String::ToUpper(const String &string)
  1273. {
  1274. if ( string.isEmpty() )
  1275. return String();
  1276. StringData* sub = new ( string.length() ) StringData( string );
  1277. dStrupr( sub->utf8() );
  1278. return sub;
  1279. }
  1280. String String::GetTrailingNumber(const char* str, S32& number)
  1281. {
  1282. // Check for trivial strings
  1283. if (!str || !str[0])
  1284. return String::EmptyString;
  1285. // Find the number at the end of the string
  1286. String base(str);
  1287. const char* p = base.c_str() + base.length() - 1;
  1288. // Ignore trailing whitespace
  1289. while ((p != base.c_str()) && dIsspace(*p))
  1290. p--;
  1291. // Need at least one digit!
  1292. if (!isdigit(*p))
  1293. return base;
  1294. // Back up to the first non-digit character
  1295. while ((p != base.c_str()) && isdigit(*p))
  1296. p--;
  1297. // Convert number => allow negative numbers, treat '_' as '-' for Maya
  1298. if ((*p == '-') || (*p == '_'))
  1299. number = -dAtoi(p + 1);
  1300. else
  1301. number = ((p == base.c_str()) ? dAtoi(p) : dAtoi(++p));
  1302. // Remove space between the name and the number
  1303. while ((p > base.c_str()) && dIsspace(*(p-1)))
  1304. p--;
  1305. return base.substr(0, p - base.c_str());
  1306. }
  1307. String String::GetFirstNumber(const char* str, U32& startPos, U32& endPos)
  1308. {
  1309. // Check for trivial strings
  1310. if (!str || !str[0])
  1311. return String::EmptyString;
  1312. // Find the number at the end of the string
  1313. String base(str);
  1314. const char* p = base.c_str();
  1315. const char* end = base.c_str() + base.length() - 1;
  1316. bool dec = false;
  1317. startPos = 0;
  1318. //Check if we are just a digit
  1319. if(p == end && isdigit(*p))
  1320. return base;
  1321. //Look for the first digit
  1322. while ((p != end) && (dIsspace(*p) || !isdigit(*p)))
  1323. {
  1324. p++;
  1325. startPos++;
  1326. }
  1327. //Handle if we are at the end and found nothing
  1328. if(p == end && !isdigit(*p))
  1329. return "";
  1330. //update our end position at least to the start of our number
  1331. endPos = startPos;
  1332. //Backup our ptr
  1333. const char* backup = p;
  1334. //Check for any negative or decimal values
  1335. if(startPos > 0)
  1336. {
  1337. p--;
  1338. startPos--;
  1339. if(*p == '.')
  1340. {
  1341. dec = true;
  1342. //ignore any duplicate periods
  1343. while ((p != base.c_str()) && (*p == '.'))
  1344. {
  1345. p--;
  1346. startPos--;
  1347. }
  1348. //Found a decimal lets still check for negative sign
  1349. if(startPos > 0)
  1350. {
  1351. p--;
  1352. startPos--;
  1353. if((*p != '-') && (*p != '_'))
  1354. {
  1355. startPos++;
  1356. p++;
  1357. }
  1358. }
  1359. }
  1360. else if((*p != '-') && (*p != '_'))
  1361. {
  1362. //go back to where we where cause no decimal or negative sign found
  1363. startPos++;
  1364. p++;
  1365. }
  1366. }
  1367. //Restore where we were
  1368. p = backup;
  1369. //look for the end of the digits
  1370. bool justFoundDec = false;
  1371. while (p != end)
  1372. {
  1373. if(*p == '.')
  1374. {
  1375. if(dec && !justFoundDec)
  1376. break;
  1377. else
  1378. {
  1379. dec = true;
  1380. justFoundDec = true;
  1381. }
  1382. }
  1383. else if(!isdigit(*p))
  1384. break;
  1385. else if(justFoundDec)
  1386. justFoundDec = false;
  1387. p++;
  1388. endPos++;
  1389. }
  1390. U32 len = (!isdigit(*p)) ? endPos - startPos : (endPos + 1) - startPos;
  1391. return base.substr(startPos, len);
  1392. }