juce_String.cpp 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #if JUCE_MSVC
  22. #pragma warning (push)
  23. #pragma warning (disable: 4514 4996)
  24. #endif
  25. NewLine newLine;
  26. #if defined (JUCE_STRINGS_ARE_UNICODE) && ! JUCE_STRINGS_ARE_UNICODE
  27. #error "JUCE_STRINGS_ARE_UNICODE is deprecated! All strings are now unicode by default."
  28. #endif
  29. #if JUCE_NATIVE_WCHAR_IS_UTF8
  30. typedef CharPointer_UTF8 CharPointer_wchar_t;
  31. #elif JUCE_NATIVE_WCHAR_IS_UTF16
  32. typedef CharPointer_UTF16 CharPointer_wchar_t;
  33. #else
  34. typedef CharPointer_UTF32 CharPointer_wchar_t;
  35. #endif
  36. static inline CharPointer_wchar_t castToCharPointer_wchar_t (const void* t) noexcept
  37. {
  38. return CharPointer_wchar_t (static_cast<const CharPointer_wchar_t::CharType*> (t));
  39. }
  40. //==============================================================================
  41. // (Mirrors the structure of StringHolder, but without the atomic member, so can be statically constructed)
  42. struct EmptyString
  43. {
  44. int refCount;
  45. size_t allocatedBytes;
  46. String::CharPointerType::CharType text;
  47. };
  48. static const EmptyString emptyString = { 0x3fffffff, sizeof (String::CharPointerType::CharType), 0 };
  49. //==============================================================================
  50. class StringHolder
  51. {
  52. public:
  53. StringHolder() JUCE_DELETED_FUNCTION;
  54. typedef String::CharPointerType CharPointerType;
  55. typedef String::CharPointerType::CharType CharType;
  56. //==============================================================================
  57. static CharPointerType createUninitialisedBytes (size_t numBytes)
  58. {
  59. numBytes = (numBytes + 3) & ~(size_t) 3;
  60. StringHolder* const s = reinterpret_cast<StringHolder*> (new char [sizeof (StringHolder) - sizeof (CharType) + numBytes]);
  61. s->refCount.value = 0;
  62. s->allocatedNumBytes = numBytes;
  63. return CharPointerType (s->text);
  64. }
  65. template <class CharPointer>
  66. static CharPointerType createFromCharPointer (const CharPointer text)
  67. {
  68. if (text.getAddress() == nullptr || text.isEmpty())
  69. return CharPointerType (&(emptyString.text));
  70. const size_t bytesNeeded = sizeof (CharType) + CharPointerType::getBytesRequiredFor (text);
  71. const CharPointerType dest (createUninitialisedBytes (bytesNeeded));
  72. CharPointerType (dest).writeAll (text);
  73. return dest;
  74. }
  75. template <class CharPointer>
  76. static CharPointerType createFromCharPointer (const CharPointer text, size_t maxChars)
  77. {
  78. if (text.getAddress() == nullptr || text.isEmpty() || maxChars == 0)
  79. return CharPointerType (&(emptyString.text));
  80. CharPointer end (text);
  81. size_t numChars = 0;
  82. size_t bytesNeeded = sizeof (CharType);
  83. while (numChars < maxChars && ! end.isEmpty())
  84. {
  85. bytesNeeded += CharPointerType::getBytesRequiredFor (end.getAndAdvance());
  86. ++numChars;
  87. }
  88. const CharPointerType dest (createUninitialisedBytes (bytesNeeded));
  89. CharPointerType (dest).writeWithCharLimit (text, (int) numChars + 1);
  90. return dest;
  91. }
  92. template <class CharPointer>
  93. static CharPointerType createFromCharPointer (const CharPointer start, const CharPointer end)
  94. {
  95. if (start.getAddress() == nullptr || start.isEmpty())
  96. return CharPointerType (&(emptyString.text));
  97. CharPointer e (start);
  98. int numChars = 0;
  99. size_t bytesNeeded = sizeof (CharType);
  100. while (e < end && ! e.isEmpty())
  101. {
  102. bytesNeeded += CharPointerType::getBytesRequiredFor (e.getAndAdvance());
  103. ++numChars;
  104. }
  105. const CharPointerType dest (createUninitialisedBytes (bytesNeeded));
  106. CharPointerType (dest).writeWithCharLimit (start, numChars + 1);
  107. return dest;
  108. }
  109. static CharPointerType createFromCharPointer (const CharPointerType start, const CharPointerType end)
  110. {
  111. if (start.getAddress() == nullptr || start.isEmpty())
  112. return CharPointerType (&(emptyString.text));
  113. const size_t numBytes = (size_t) (reinterpret_cast<const char*> (end.getAddress())
  114. - reinterpret_cast<const char*> (start.getAddress()));
  115. const CharPointerType dest (createUninitialisedBytes (numBytes + sizeof (CharType)));
  116. memcpy (dest.getAddress(), start, numBytes);
  117. dest.getAddress()[numBytes / sizeof (CharType)] = 0;
  118. return dest;
  119. }
  120. static CharPointerType createFromFixedLength (const char* const src, const size_t numChars)
  121. {
  122. const CharPointerType dest (createUninitialisedBytes (numChars * sizeof (CharType) + sizeof (CharType)));
  123. CharPointerType (dest).writeWithCharLimit (CharPointer_UTF8 (src), (int) (numChars + 1));
  124. return dest;
  125. }
  126. //==============================================================================
  127. static void retain (const CharPointerType text) noexcept
  128. {
  129. StringHolder* const b = bufferFromText (text);
  130. if (b != (StringHolder*) &emptyString)
  131. ++(b->refCount);
  132. }
  133. static inline void release (StringHolder* const b) noexcept
  134. {
  135. if (b != (StringHolder*) &emptyString)
  136. if (--(b->refCount) == -1)
  137. delete[] reinterpret_cast<char*> (b);
  138. }
  139. static void release (const CharPointerType text) noexcept
  140. {
  141. release (bufferFromText (text));
  142. }
  143. static inline int getReferenceCount (const CharPointerType text) noexcept
  144. {
  145. return bufferFromText (text)->refCount.get() + 1;
  146. }
  147. //==============================================================================
  148. static CharPointerType makeUniqueWithByteSize (const CharPointerType text, size_t numBytes)
  149. {
  150. StringHolder* const b = bufferFromText (text);
  151. if (b == (StringHolder*) &emptyString)
  152. {
  153. CharPointerType newText (createUninitialisedBytes (numBytes));
  154. newText.writeNull();
  155. return newText;
  156. }
  157. if (b->allocatedNumBytes >= numBytes && b->refCount.get() <= 0)
  158. return text;
  159. CharPointerType newText (createUninitialisedBytes (jmax (b->allocatedNumBytes, numBytes)));
  160. memcpy (newText.getAddress(), text.getAddress(), b->allocatedNumBytes);
  161. release (b);
  162. return newText;
  163. }
  164. static size_t getAllocatedNumBytes (const CharPointerType text) noexcept
  165. {
  166. return bufferFromText (text)->allocatedNumBytes;
  167. }
  168. //==============================================================================
  169. Atomic<int> refCount;
  170. size_t allocatedNumBytes;
  171. CharType text[1];
  172. private:
  173. static inline StringHolder* bufferFromText (const CharPointerType text) noexcept
  174. {
  175. // (Can't use offsetof() here because of warnings about this not being a POD)
  176. return reinterpret_cast<StringHolder*> (reinterpret_cast<char*> (text.getAddress())
  177. - (reinterpret_cast<size_t> (reinterpret_cast<StringHolder*> (1)->text) - 1));
  178. }
  179. void compileTimeChecks()
  180. {
  181. // Let me know if any of these assertions fail on your system!
  182. #if JUCE_NATIVE_WCHAR_IS_UTF8
  183. static_jassert (sizeof (wchar_t) == 1);
  184. #elif JUCE_NATIVE_WCHAR_IS_UTF16
  185. static_jassert (sizeof (wchar_t) == 2);
  186. #elif JUCE_NATIVE_WCHAR_IS_UTF32
  187. static_jassert (sizeof (wchar_t) == 4);
  188. #else
  189. #error "native wchar_t size is unknown"
  190. #endif
  191. static_jassert (sizeof (EmptyString) == sizeof (StringHolder));
  192. }
  193. };
  194. const String String::empty;
  195. //==============================================================================
  196. String::String() noexcept : text (&(emptyString.text))
  197. {
  198. }
  199. String::~String() noexcept
  200. {
  201. StringHolder::release (text);
  202. }
  203. String::String (const String& other) noexcept : text (other.text)
  204. {
  205. StringHolder::retain (text);
  206. }
  207. void String::swapWith (String& other) noexcept
  208. {
  209. std::swap (text, other.text);
  210. }
  211. void String::clear() noexcept
  212. {
  213. StringHolder::release (text);
  214. text = &(emptyString.text);
  215. }
  216. String& String::operator= (const String& other) noexcept
  217. {
  218. StringHolder::retain (other.text);
  219. StringHolder::release (text.atomicSwap (other.text));
  220. return *this;
  221. }
  222. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  223. String::String (String&& other) noexcept : text (other.text)
  224. {
  225. other.text = &(emptyString.text);
  226. }
  227. String& String::operator= (String&& other) noexcept
  228. {
  229. std::swap (text, other.text);
  230. return *this;
  231. }
  232. #endif
  233. inline String::PreallocationBytes::PreallocationBytes (const size_t num) noexcept : numBytes (num) {}
  234. String::String (const PreallocationBytes& preallocationSize)
  235. : text (StringHolder::createUninitialisedBytes (preallocationSize.numBytes + sizeof (CharPointerType::CharType)))
  236. {
  237. }
  238. void String::preallocateBytes (const size_t numBytesNeeded)
  239. {
  240. text = StringHolder::makeUniqueWithByteSize (text, numBytesNeeded + sizeof (CharPointerType::CharType));
  241. }
  242. int String::getReferenceCount() const noexcept
  243. {
  244. return StringHolder::getReferenceCount (text);
  245. }
  246. //==============================================================================
  247. String::String (const char* const t)
  248. : text (StringHolder::createFromCharPointer (CharPointer_ASCII (t)))
  249. {
  250. /* If you get an assertion here, then you're trying to create a string from 8-bit data
  251. that contains values greater than 127. These can NOT be correctly converted to unicode
  252. because there's no way for the String class to know what encoding was used to
  253. create them. The source data could be UTF-8, ASCII or one of many local code-pages.
  254. To get around this problem, you must be more explicit when you pass an ambiguous 8-bit
  255. string to the String class - so for example if your source data is actually UTF-8,
  256. you'd call String (CharPointer_UTF8 ("my utf8 string..")), and it would be able to
  257. correctly convert the multi-byte characters to unicode. It's *highly* recommended that
  258. you use UTF-8 with escape characters in your source code to represent extended characters,
  259. because there's no other way to represent these strings in a way that isn't dependent on
  260. the compiler, source code editor and platform.
  261. Note that the Projucer has a handy string literal generator utility that will convert
  262. any unicode string to a valid C++ string literal, creating ascii escape sequences that will
  263. work in any compiler.
  264. */
  265. jassert (t == nullptr || CharPointer_ASCII::isValidString (t, std::numeric_limits<int>::max()));
  266. }
  267. String::String (const char* const t, const size_t maxChars)
  268. : text (StringHolder::createFromCharPointer (CharPointer_ASCII (t), maxChars))
  269. {
  270. /* If you get an assertion here, then you're trying to create a string from 8-bit data
  271. that contains values greater than 127. These can NOT be correctly converted to unicode
  272. because there's no way for the String class to know what encoding was used to
  273. create them. The source data could be UTF-8, ASCII or one of many local code-pages.
  274. To get around this problem, you must be more explicit when you pass an ambiguous 8-bit
  275. string to the String class - so for example if your source data is actually UTF-8,
  276. you'd call String (CharPointer_UTF8 ("my utf8 string..")), and it would be able to
  277. correctly convert the multi-byte characters to unicode. It's *highly* recommended that
  278. you use UTF-8 with escape characters in your source code to represent extended characters,
  279. because there's no other way to represent these strings in a way that isn't dependent on
  280. the compiler, source code editor and platform.
  281. Note that the Projucer has a handy string literal generator utility that will convert
  282. any unicode string to a valid C++ string literal, creating ascii escape sequences that will
  283. work in any compiler.
  284. */
  285. jassert (t == nullptr || CharPointer_ASCII::isValidString (t, (int) maxChars));
  286. }
  287. String::String (const wchar_t* const t) : text (StringHolder::createFromCharPointer (castToCharPointer_wchar_t (t))) {}
  288. String::String (const CharPointer_UTF8 t) : text (StringHolder::createFromCharPointer (t)) {}
  289. String::String (const CharPointer_UTF16 t) : text (StringHolder::createFromCharPointer (t)) {}
  290. String::String (const CharPointer_UTF32 t) : text (StringHolder::createFromCharPointer (t)) {}
  291. String::String (const CharPointer_ASCII t) : text (StringHolder::createFromCharPointer (t)) {}
  292. String::String (const CharPointer_UTF8 t, const size_t maxChars) : text (StringHolder::createFromCharPointer (t, maxChars)) {}
  293. String::String (const CharPointer_UTF16 t, const size_t maxChars) : text (StringHolder::createFromCharPointer (t, maxChars)) {}
  294. String::String (const CharPointer_UTF32 t, const size_t maxChars) : text (StringHolder::createFromCharPointer (t, maxChars)) {}
  295. String::String (const wchar_t* const t, size_t maxChars) : text (StringHolder::createFromCharPointer (castToCharPointer_wchar_t (t), maxChars)) {}
  296. String::String (const CharPointer_UTF8 start, const CharPointer_UTF8 end) : text (StringHolder::createFromCharPointer (start, end)) {}
  297. String::String (const CharPointer_UTF16 start, const CharPointer_UTF16 end) : text (StringHolder::createFromCharPointer (start, end)) {}
  298. String::String (const CharPointer_UTF32 start, const CharPointer_UTF32 end) : text (StringHolder::createFromCharPointer (start, end)) {}
  299. String::String (const std::string& s) : text (StringHolder::createFromFixedLength (s.data(), s.size())) {}
  300. String::String (StringRef s) : text (StringHolder::createFromCharPointer (s.text)) {}
  301. String String::charToString (const juce_wchar character)
  302. {
  303. String result (PreallocationBytes (CharPointerType::getBytesRequiredFor (character)));
  304. CharPointerType t (result.text);
  305. t.write (character);
  306. t.writeNull();
  307. return result;
  308. }
  309. //==============================================================================
  310. namespace NumberToStringConverters
  311. {
  312. enum
  313. {
  314. charsNeededForInt = 32,
  315. charsNeededForDouble = 48
  316. };
  317. template <typename Type>
  318. static char* printDigits (char* t, Type v) noexcept
  319. {
  320. *--t = 0;
  321. do
  322. {
  323. *--t = '0' + (char) (v % 10);
  324. v /= 10;
  325. } while (v > 0);
  326. return t;
  327. }
  328. // pass in a pointer to the END of a buffer..
  329. static char* numberToString (char* t, const int64 n) noexcept
  330. {
  331. if (n >= 0)
  332. return printDigits (t, static_cast<uint64> (n));
  333. // NB: this needs to be careful not to call -std::numeric_limits<int64>::min(),
  334. // which has undefined behaviour
  335. t = printDigits (t, static_cast<uint64> (-(n + 1)) + 1);
  336. *--t = '-';
  337. return t;
  338. }
  339. static char* numberToString (char* t, uint64 v) noexcept
  340. {
  341. return printDigits (t, v);
  342. }
  343. static char* numberToString (char* t, const int n) noexcept
  344. {
  345. if (n >= 0)
  346. return printDigits (t, static_cast<unsigned int> (n));
  347. // NB: this needs to be careful not to call -std::numeric_limits<int>::min(),
  348. // which has undefined behaviour
  349. t = printDigits (t, static_cast<unsigned int> (-(n + 1)) + 1);
  350. *--t = '-';
  351. return t;
  352. }
  353. static char* numberToString (char* t, const unsigned int v) noexcept
  354. {
  355. return printDigits (t, v);
  356. }
  357. static char* numberToString (char* t, const long n) noexcept
  358. {
  359. if (n >= 0)
  360. return printDigits (t, static_cast<unsigned long> (n));
  361. t = printDigits (t, static_cast<unsigned long> (-(n + 1)) + 1);
  362. *--t = '-';
  363. return t;
  364. }
  365. static char* numberToString (char* t, const unsigned long v) noexcept
  366. {
  367. return printDigits (t, v);
  368. }
  369. struct StackArrayStream : public std::basic_streambuf<char, std::char_traits<char> >
  370. {
  371. explicit StackArrayStream (char* d)
  372. {
  373. static const std::locale classicLocale (std::locale::classic());
  374. imbue (classicLocale);
  375. setp (d, d + charsNeededForDouble);
  376. }
  377. size_t writeDouble (double n, int numDecPlaces)
  378. {
  379. {
  380. std::ostream o (this);
  381. if (numDecPlaces > 0)
  382. o.precision ((std::streamsize) numDecPlaces);
  383. o << n;
  384. }
  385. return (size_t) (pptr() - pbase());
  386. }
  387. };
  388. static char* doubleToString (char* buffer, const int numChars, double n, int numDecPlaces, size_t& len) noexcept
  389. {
  390. if (numDecPlaces > 0 && numDecPlaces < 7 && n > -1.0e20 && n < 1.0e20)
  391. {
  392. char* const end = buffer + numChars;
  393. char* t = end;
  394. int64 v = (int64) (pow (10.0, numDecPlaces) * std::abs (n) + 0.5);
  395. *--t = (char) 0;
  396. while (numDecPlaces >= 0 || v > 0)
  397. {
  398. if (numDecPlaces == 0)
  399. *--t = '.';
  400. *--t = (char) ('0' + (v % 10));
  401. v /= 10;
  402. --numDecPlaces;
  403. }
  404. if (n < 0)
  405. *--t = '-';
  406. len = (size_t) (end - t - 1);
  407. return t;
  408. }
  409. StackArrayStream strm (buffer);
  410. len = strm.writeDouble (n, numDecPlaces);
  411. jassert (len <= charsNeededForDouble);
  412. return buffer;
  413. }
  414. template <typename IntegerType>
  415. static String::CharPointerType createFromInteger (const IntegerType number)
  416. {
  417. char buffer [charsNeededForInt];
  418. char* const end = buffer + numElementsInArray (buffer);
  419. char* const start = numberToString (end, number);
  420. return StringHolder::createFromFixedLength (start, (size_t) (end - start - 1));
  421. }
  422. static String::CharPointerType createFromDouble (const double number, const int numberOfDecimalPlaces)
  423. {
  424. char buffer [charsNeededForDouble];
  425. size_t len;
  426. char* const start = doubleToString (buffer, numElementsInArray (buffer), (double) number, numberOfDecimalPlaces, len);
  427. return StringHolder::createFromFixedLength (start, len);
  428. }
  429. }
  430. //==============================================================================
  431. String::String (const int number) : text (NumberToStringConverters::createFromInteger (number)) {}
  432. String::String (const unsigned int number) : text (NumberToStringConverters::createFromInteger (number)) {}
  433. String::String (const short number) : text (NumberToStringConverters::createFromInteger ((int) number)) {}
  434. String::String (const unsigned short number) : text (NumberToStringConverters::createFromInteger ((unsigned int) number)) {}
  435. String::String (const int64 number) : text (NumberToStringConverters::createFromInteger (number)) {}
  436. String::String (const uint64 number) : text (NumberToStringConverters::createFromInteger (number)) {}
  437. String::String (const long number) : text (NumberToStringConverters::createFromInteger (number)) {}
  438. String::String (const unsigned long number) : text (NumberToStringConverters::createFromInteger (number)) {}
  439. String::String (const float number) : text (NumberToStringConverters::createFromDouble ((double) number, 0)) {}
  440. String::String (const double number) : text (NumberToStringConverters::createFromDouble (number, 0)) {}
  441. String::String (const float number, const int numberOfDecimalPlaces) : text (NumberToStringConverters::createFromDouble ((double) number, numberOfDecimalPlaces)) {}
  442. String::String (const double number, const int numberOfDecimalPlaces) : text (NumberToStringConverters::createFromDouble (number, numberOfDecimalPlaces)) {}
  443. //==============================================================================
  444. int String::length() const noexcept
  445. {
  446. return (int) text.length();
  447. }
  448. static size_t findByteOffsetOfEnd (String::CharPointerType text) noexcept
  449. {
  450. return (size_t) (((char*) text.findTerminatingNull().getAddress()) - (char*) text.getAddress());
  451. }
  452. size_t String::getByteOffsetOfEnd() const noexcept
  453. {
  454. return findByteOffsetOfEnd (text);
  455. }
  456. juce_wchar String::operator[] (int index) const noexcept
  457. {
  458. jassert (index == 0 || (index > 0 && index <= (int) text.lengthUpTo ((size_t) index + 1)));
  459. return text [index];
  460. }
  461. template <typename Type>
  462. struct HashGenerator
  463. {
  464. template <typename CharPointer>
  465. static Type calculate (CharPointer t) noexcept
  466. {
  467. Type result = Type();
  468. while (! t.isEmpty())
  469. result = ((Type) multiplier) * result + (Type) t.getAndAdvance();
  470. return result;
  471. }
  472. enum { multiplier = sizeof (Type) > 4 ? 101 : 31 };
  473. };
  474. int String::hashCode() const noexcept { return HashGenerator<int> ::calculate (text); }
  475. int64 String::hashCode64() const noexcept { return HashGenerator<int64> ::calculate (text); }
  476. size_t String::hash() const noexcept { return HashGenerator<size_t> ::calculate (text); }
  477. //==============================================================================
  478. JUCE_API bool JUCE_CALLTYPE operator== (const String& s1, const String& s2) noexcept { return s1.compare (s2) == 0; }
  479. JUCE_API bool JUCE_CALLTYPE operator!= (const String& s1, const String& s2) noexcept { return s1.compare (s2) != 0; }
  480. JUCE_API bool JUCE_CALLTYPE operator== (const String& s1, const char* s2) noexcept { return s1.compare (s2) == 0; }
  481. JUCE_API bool JUCE_CALLTYPE operator!= (const String& s1, const char* s2) noexcept { return s1.compare (s2) != 0; }
  482. JUCE_API bool JUCE_CALLTYPE operator== (const String& s1, const wchar_t* s2) noexcept { return s1.compare (s2) == 0; }
  483. JUCE_API bool JUCE_CALLTYPE operator!= (const String& s1, const wchar_t* s2) noexcept { return s1.compare (s2) != 0; }
  484. JUCE_API bool JUCE_CALLTYPE operator== (const String& s1, StringRef s2) noexcept { return s1.getCharPointer().compare (s2.text) == 0; }
  485. JUCE_API bool JUCE_CALLTYPE operator!= (const String& s1, StringRef s2) noexcept { return s1.getCharPointer().compare (s2.text) != 0; }
  486. JUCE_API bool JUCE_CALLTYPE operator== (const String& s1, const CharPointer_UTF8 s2) noexcept { return s1.getCharPointer().compare (s2) == 0; }
  487. JUCE_API bool JUCE_CALLTYPE operator!= (const String& s1, const CharPointer_UTF8 s2) noexcept { return s1.getCharPointer().compare (s2) != 0; }
  488. JUCE_API bool JUCE_CALLTYPE operator== (const String& s1, const CharPointer_UTF16 s2) noexcept { return s1.getCharPointer().compare (s2) == 0; }
  489. JUCE_API bool JUCE_CALLTYPE operator!= (const String& s1, const CharPointer_UTF16 s2) noexcept { return s1.getCharPointer().compare (s2) != 0; }
  490. JUCE_API bool JUCE_CALLTYPE operator== (const String& s1, const CharPointer_UTF32 s2) noexcept { return s1.getCharPointer().compare (s2) == 0; }
  491. JUCE_API bool JUCE_CALLTYPE operator!= (const String& s1, const CharPointer_UTF32 s2) noexcept { return s1.getCharPointer().compare (s2) != 0; }
  492. JUCE_API bool JUCE_CALLTYPE operator> (const String& s1, const String& s2) noexcept { return s1.compare (s2) > 0; }
  493. JUCE_API bool JUCE_CALLTYPE operator< (const String& s1, const String& s2) noexcept { return s1.compare (s2) < 0; }
  494. JUCE_API bool JUCE_CALLTYPE operator>= (const String& s1, const String& s2) noexcept { return s1.compare (s2) >= 0; }
  495. JUCE_API bool JUCE_CALLTYPE operator<= (const String& s1, const String& s2) noexcept { return s1.compare (s2) <= 0; }
  496. bool String::equalsIgnoreCase (const wchar_t* const t) const noexcept
  497. {
  498. return t != nullptr ? text.compareIgnoreCase (castToCharPointer_wchar_t (t)) == 0
  499. : isEmpty();
  500. }
  501. bool String::equalsIgnoreCase (const char* const t) const noexcept
  502. {
  503. return t != nullptr ? text.compareIgnoreCase (CharPointer_UTF8 (t)) == 0
  504. : isEmpty();
  505. }
  506. bool String::equalsIgnoreCase (StringRef t) const noexcept
  507. {
  508. return text.compareIgnoreCase (t.text) == 0;
  509. }
  510. bool String::equalsIgnoreCase (const String& other) const noexcept
  511. {
  512. return text == other.text
  513. || text.compareIgnoreCase (other.text) == 0;
  514. }
  515. int String::compare (const String& other) const noexcept { return (text == other.text) ? 0 : text.compare (other.text); }
  516. int String::compare (const char* const other) const noexcept { return text.compare (CharPointer_UTF8 (other)); }
  517. int String::compare (const wchar_t* const other) const noexcept { return text.compare (castToCharPointer_wchar_t (other)); }
  518. int String::compareIgnoreCase (const String& other) const noexcept { return (text == other.text) ? 0 : text.compareIgnoreCase (other.text); }
  519. static int stringCompareRight (String::CharPointerType s1, String::CharPointerType s2) noexcept
  520. {
  521. for (int bias = 0;;)
  522. {
  523. const juce_wchar c1 = s1.getAndAdvance();
  524. const bool isDigit1 = CharacterFunctions::isDigit (c1);
  525. const juce_wchar c2 = s2.getAndAdvance();
  526. const bool isDigit2 = CharacterFunctions::isDigit (c2);
  527. if (! (isDigit1 || isDigit2)) return bias;
  528. if (! isDigit1) return -1;
  529. if (! isDigit2) return 1;
  530. if (c1 != c2 && bias == 0)
  531. bias = c1 < c2 ? -1 : 1;
  532. jassert (c1 != 0 && c2 != 0);
  533. }
  534. }
  535. static int stringCompareLeft (String::CharPointerType s1, String::CharPointerType s2) noexcept
  536. {
  537. for (;;)
  538. {
  539. const juce_wchar c1 = s1.getAndAdvance();
  540. const bool isDigit1 = CharacterFunctions::isDigit (c1);
  541. const juce_wchar c2 = s2.getAndAdvance();
  542. const bool isDigit2 = CharacterFunctions::isDigit (c2);
  543. if (! (isDigit1 || isDigit2)) return 0;
  544. if (! isDigit1) return -1;
  545. if (! isDigit2) return 1;
  546. if (c1 < c2) return -1;
  547. if (c1 > c2) return 1;
  548. }
  549. }
  550. static int naturalStringCompare (String::CharPointerType s1, String::CharPointerType s2) noexcept
  551. {
  552. bool firstLoop = true;
  553. for (;;)
  554. {
  555. const bool hasSpace1 = s1.isWhitespace();
  556. const bool hasSpace2 = s2.isWhitespace();
  557. if ((! firstLoop) && (hasSpace1 ^ hasSpace2))
  558. return hasSpace2 ? 1 : -1;
  559. firstLoop = false;
  560. if (hasSpace1) s1 = s1.findEndOfWhitespace();
  561. if (hasSpace2) s2 = s2.findEndOfWhitespace();
  562. if (s1.isDigit() && s2.isDigit())
  563. {
  564. const int result = (*s1 == '0' || *s2 == '0') ? stringCompareLeft (s1, s2)
  565. : stringCompareRight (s1, s2);
  566. if (result != 0)
  567. return result;
  568. }
  569. juce_wchar c1 = s1.getAndAdvance();
  570. juce_wchar c2 = s2.getAndAdvance();
  571. if (c1 != c2)
  572. {
  573. c1 = CharacterFunctions::toUpperCase (c1);
  574. c2 = CharacterFunctions::toUpperCase (c2);
  575. }
  576. if (c1 == c2)
  577. {
  578. if (c1 == 0)
  579. return 0;
  580. }
  581. else
  582. {
  583. const bool isAlphaNum1 = CharacterFunctions::isLetterOrDigit (c1);
  584. const bool isAlphaNum2 = CharacterFunctions::isLetterOrDigit (c2);
  585. if (isAlphaNum2 && ! isAlphaNum1) return -1;
  586. if (isAlphaNum1 && ! isAlphaNum2) return 1;
  587. return c1 < c2 ? -1 : 1;
  588. }
  589. jassert (c1 != 0 && c2 != 0);
  590. }
  591. }
  592. int String::compareNatural (StringRef other) const noexcept
  593. {
  594. return naturalStringCompare (getCharPointer(), other.text);
  595. }
  596. //==============================================================================
  597. void String::append (const String& textToAppend, size_t maxCharsToTake)
  598. {
  599. appendCharPointer (this == &textToAppend ? String (textToAppend).text
  600. : textToAppend.text, maxCharsToTake);
  601. }
  602. void String::appendCharPointer (const CharPointerType textToAppend)
  603. {
  604. appendCharPointer (textToAppend, textToAppend.findTerminatingNull());
  605. }
  606. void String::appendCharPointer (const CharPointerType startOfTextToAppend,
  607. const CharPointerType endOfTextToAppend)
  608. {
  609. jassert (startOfTextToAppend.getAddress() != nullptr && endOfTextToAppend.getAddress() != nullptr);
  610. const int extraBytesNeeded = getAddressDifference (endOfTextToAppend.getAddress(),
  611. startOfTextToAppend.getAddress());
  612. jassert (extraBytesNeeded >= 0);
  613. if (extraBytesNeeded > 0)
  614. {
  615. const size_t byteOffsetOfNull = getByteOffsetOfEnd();
  616. preallocateBytes (byteOffsetOfNull + (size_t) extraBytesNeeded);
  617. CharPointerType::CharType* const newStringStart = addBytesToPointer (text.getAddress(), (int) byteOffsetOfNull);
  618. memcpy (newStringStart, startOfTextToAppend.getAddress(), (size_t) extraBytesNeeded);
  619. CharPointerType (addBytesToPointer (newStringStart, extraBytesNeeded)).writeNull();
  620. }
  621. }
  622. String& String::operator+= (const wchar_t* const t)
  623. {
  624. appendCharPointer (castToCharPointer_wchar_t (t));
  625. return *this;
  626. }
  627. String& String::operator+= (const char* const t)
  628. {
  629. appendCharPointer (CharPointer_UTF8 (t)); // (using UTF8 here triggers a faster code-path than ascii)
  630. return *this;
  631. }
  632. String& String::operator+= (const String& other)
  633. {
  634. if (isEmpty())
  635. return operator= (other);
  636. if (this == &other)
  637. return operator+= (String (*this));
  638. appendCharPointer (other.text);
  639. return *this;
  640. }
  641. String& String::operator+= (StringRef other)
  642. {
  643. return operator+= (String (other));
  644. }
  645. String& String::operator+= (const char ch)
  646. {
  647. const char asString[] = { ch, 0 };
  648. return operator+= (asString);
  649. }
  650. String& String::operator+= (const wchar_t ch)
  651. {
  652. const wchar_t asString[] = { ch, 0 };
  653. return operator+= (asString);
  654. }
  655. #if ! JUCE_NATIVE_WCHAR_IS_UTF32
  656. String& String::operator+= (const juce_wchar ch)
  657. {
  658. const juce_wchar asString[] = { ch, 0 };
  659. appendCharPointer (CharPointer_UTF32 (asString));
  660. return *this;
  661. }
  662. #endif
  663. namespace StringHelpers
  664. {
  665. template <typename T>
  666. inline String& operationAddAssign (String& str, const T number)
  667. {
  668. char buffer [(sizeof(T) * 8) / 2];
  669. char* end = buffer + numElementsInArray (buffer);
  670. char* start = NumberToStringConverters::numberToString (end, number);
  671. #if JUCE_STRING_UTF_TYPE == 8
  672. str.appendCharPointer (String::CharPointerType (start), String::CharPointerType (end));
  673. #else
  674. str.appendCharPointer (CharPointer_ASCII (start), CharPointer_ASCII (end));
  675. #endif
  676. return str;
  677. }
  678. }
  679. String& String::operator+= (const int number) { return StringHelpers::operationAddAssign<int> (*this, number); }
  680. String& String::operator+= (const int64 number) { return StringHelpers::operationAddAssign<int64> (*this, number); }
  681. String& String::operator+= (const uint64 number) { return StringHelpers::operationAddAssign<uint64> (*this, number); }
  682. //==============================================================================
  683. JUCE_API String JUCE_CALLTYPE operator+ (const char* const s1, const String& s2) { String s (s1); return s += s2; }
  684. JUCE_API String JUCE_CALLTYPE operator+ (const wchar_t* const s1, const String& s2) { String s (s1); return s += s2; }
  685. JUCE_API String JUCE_CALLTYPE operator+ (const char s1, const String& s2) { return String::charToString ((juce_wchar) (uint8) s1) + s2; }
  686. JUCE_API String JUCE_CALLTYPE operator+ (const wchar_t s1, const String& s2) { return String::charToString (s1) + s2; }
  687. JUCE_API String JUCE_CALLTYPE operator+ (String s1, const String& s2) { return s1 += s2; }
  688. JUCE_API String JUCE_CALLTYPE operator+ (String s1, const char* const s2) { return s1 += s2; }
  689. JUCE_API String JUCE_CALLTYPE operator+ (String s1, const wchar_t* s2) { return s1 += s2; }
  690. JUCE_API String JUCE_CALLTYPE operator+ (String s1, const char s2) { return s1 += s2; }
  691. JUCE_API String JUCE_CALLTYPE operator+ (String s1, const wchar_t s2) { return s1 += s2; }
  692. #if ! JUCE_NATIVE_WCHAR_IS_UTF32
  693. JUCE_API String JUCE_CALLTYPE operator+ (const juce_wchar s1, const String& s2) { return String::charToString (s1) + s2; }
  694. JUCE_API String JUCE_CALLTYPE operator+ (String s1, const juce_wchar s2) { return s1 += s2; }
  695. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const juce_wchar s2) { return s1 += s2; }
  696. #endif
  697. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const char s2) { return s1 += s2; }
  698. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const wchar_t s2) { return s1 += s2; }
  699. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const char* const s2) { return s1 += s2; }
  700. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const wchar_t* const s2) { return s1 += s2; }
  701. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const String& s2) { return s1 += s2; }
  702. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, StringRef s2) { return s1 += s2; }
  703. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const int number) { return s1 += number; }
  704. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const short number) { return s1 += (int) number; }
  705. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const unsigned short number) { return s1 += (uint64) number; }
  706. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const long number) { return s1 += String (number); }
  707. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const unsigned long number) { return s1 += String (number); }
  708. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const int64 number) { return s1 += String (number); }
  709. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const uint64 number) { return s1 += String (number); }
  710. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const float number) { return s1 += String (number); }
  711. JUCE_API String& JUCE_CALLTYPE operator<< (String& s1, const double number) { return s1 += String (number); }
  712. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const String& text)
  713. {
  714. return operator<< (stream, StringRef (text));
  715. }
  716. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, StringRef text)
  717. {
  718. const size_t numBytes = CharPointer_UTF8::getBytesRequiredFor (text.text);
  719. #if (JUCE_STRING_UTF_TYPE == 8)
  720. stream.write (text.text.getAddress(), numBytes);
  721. #else
  722. // (This avoids using toUTF8() to prevent the memory bloat that it would leave behind
  723. // if lots of large, persistent strings were to be written to streams).
  724. HeapBlock<char> temp (numBytes + 1);
  725. CharPointer_UTF8 (temp).writeAll (text.text);
  726. stream.write (temp, numBytes);
  727. #endif
  728. return stream;
  729. }
  730. //==============================================================================
  731. int String::indexOfChar (const juce_wchar character) const noexcept
  732. {
  733. return text.indexOf (character);
  734. }
  735. int String::indexOfChar (const int startIndex, const juce_wchar character) const noexcept
  736. {
  737. CharPointerType t (text);
  738. for (int i = 0; ! t.isEmpty(); ++i)
  739. {
  740. if (i >= startIndex)
  741. {
  742. if (t.getAndAdvance() == character)
  743. return i;
  744. }
  745. else
  746. {
  747. ++t;
  748. }
  749. }
  750. return -1;
  751. }
  752. int String::lastIndexOfChar (const juce_wchar character) const noexcept
  753. {
  754. CharPointerType t (text);
  755. int last = -1;
  756. for (int i = 0; ! t.isEmpty(); ++i)
  757. if (t.getAndAdvance() == character)
  758. last = i;
  759. return last;
  760. }
  761. int String::indexOfAnyOf (StringRef charactersToLookFor, const int startIndex, const bool ignoreCase) const noexcept
  762. {
  763. CharPointerType t (text);
  764. for (int i = 0; ! t.isEmpty(); ++i)
  765. {
  766. if (i >= startIndex)
  767. {
  768. if (charactersToLookFor.text.indexOf (t.getAndAdvance(), ignoreCase) >= 0)
  769. return i;
  770. }
  771. else
  772. {
  773. ++t;
  774. }
  775. }
  776. return -1;
  777. }
  778. int String::indexOf (StringRef other) const noexcept
  779. {
  780. return other.isEmpty() ? 0 : text.indexOf (other.text);
  781. }
  782. int String::indexOfIgnoreCase (StringRef other) const noexcept
  783. {
  784. return other.isEmpty() ? 0 : CharacterFunctions::indexOfIgnoreCase (text, other.text);
  785. }
  786. int String::indexOf (const int startIndex, StringRef other) const noexcept
  787. {
  788. if (other.isEmpty())
  789. return -1;
  790. CharPointerType t (text);
  791. for (int i = startIndex; --i >= 0;)
  792. {
  793. if (t.isEmpty())
  794. return -1;
  795. ++t;
  796. }
  797. int found = t.indexOf (other.text);
  798. if (found >= 0)
  799. found += startIndex;
  800. return found;
  801. }
  802. int String::indexOfIgnoreCase (const int startIndex, StringRef other) const noexcept
  803. {
  804. if (other.isEmpty())
  805. return -1;
  806. CharPointerType t (text);
  807. for (int i = startIndex; --i >= 0;)
  808. {
  809. if (t.isEmpty())
  810. return -1;
  811. ++t;
  812. }
  813. int found = CharacterFunctions::indexOfIgnoreCase (t, other.text);
  814. if (found >= 0)
  815. found += startIndex;
  816. return found;
  817. }
  818. int String::lastIndexOf (StringRef other) const noexcept
  819. {
  820. if (other.isNotEmpty())
  821. {
  822. const int len = other.length();
  823. int i = length() - len;
  824. if (i >= 0)
  825. {
  826. for (CharPointerType n (text + i); i >= 0; --i)
  827. {
  828. if (n.compareUpTo (other.text, len) == 0)
  829. return i;
  830. --n;
  831. }
  832. }
  833. }
  834. return -1;
  835. }
  836. int String::lastIndexOfIgnoreCase (StringRef other) const noexcept
  837. {
  838. if (other.isNotEmpty())
  839. {
  840. const int len = other.length();
  841. int i = length() - len;
  842. if (i >= 0)
  843. {
  844. for (CharPointerType n (text + i); i >= 0; --i)
  845. {
  846. if (n.compareIgnoreCaseUpTo (other.text, len) == 0)
  847. return i;
  848. --n;
  849. }
  850. }
  851. }
  852. return -1;
  853. }
  854. int String::lastIndexOfAnyOf (StringRef charactersToLookFor, const bool ignoreCase) const noexcept
  855. {
  856. CharPointerType t (text);
  857. int last = -1;
  858. for (int i = 0; ! t.isEmpty(); ++i)
  859. if (charactersToLookFor.text.indexOf (t.getAndAdvance(), ignoreCase) >= 0)
  860. last = i;
  861. return last;
  862. }
  863. bool String::contains (StringRef other) const noexcept
  864. {
  865. return indexOf (other) >= 0;
  866. }
  867. bool String::containsChar (const juce_wchar character) const noexcept
  868. {
  869. return text.indexOf (character) >= 0;
  870. }
  871. bool String::containsIgnoreCase (StringRef t) const noexcept
  872. {
  873. return indexOfIgnoreCase (t) >= 0;
  874. }
  875. int String::indexOfWholeWord (StringRef word) const noexcept
  876. {
  877. if (word.isNotEmpty())
  878. {
  879. CharPointerType t (text);
  880. const int wordLen = word.length();
  881. const int end = (int) t.length() - wordLen;
  882. for (int i = 0; i <= end; ++i)
  883. {
  884. if (t.compareUpTo (word.text, wordLen) == 0
  885. && (i == 0 || ! (t - 1).isLetterOrDigit())
  886. && ! (t + wordLen).isLetterOrDigit())
  887. return i;
  888. ++t;
  889. }
  890. }
  891. return -1;
  892. }
  893. int String::indexOfWholeWordIgnoreCase (StringRef word) const noexcept
  894. {
  895. if (word.isNotEmpty())
  896. {
  897. CharPointerType t (text);
  898. const int wordLen = word.length();
  899. const int end = (int) t.length() - wordLen;
  900. for (int i = 0; i <= end; ++i)
  901. {
  902. if (t.compareIgnoreCaseUpTo (word.text, wordLen) == 0
  903. && (i == 0 || ! (t - 1).isLetterOrDigit())
  904. && ! (t + wordLen).isLetterOrDigit())
  905. return i;
  906. ++t;
  907. }
  908. }
  909. return -1;
  910. }
  911. bool String::containsWholeWord (StringRef wordToLookFor) const noexcept
  912. {
  913. return indexOfWholeWord (wordToLookFor) >= 0;
  914. }
  915. bool String::containsWholeWordIgnoreCase (StringRef wordToLookFor) const noexcept
  916. {
  917. return indexOfWholeWordIgnoreCase (wordToLookFor) >= 0;
  918. }
  919. //==============================================================================
  920. template <typename CharPointer>
  921. struct WildCardMatcher
  922. {
  923. static bool matches (CharPointer wildcard, CharPointer test, const bool ignoreCase) noexcept
  924. {
  925. for (;;)
  926. {
  927. const juce_wchar wc = wildcard.getAndAdvance();
  928. if (wc == '*')
  929. return wildcard.isEmpty() || matchesAnywhere (wildcard, test, ignoreCase);
  930. if (! characterMatches (wc, test.getAndAdvance(), ignoreCase))
  931. return false;
  932. if (wc == 0)
  933. return true;
  934. }
  935. }
  936. static bool characterMatches (const juce_wchar wc, const juce_wchar tc, const bool ignoreCase) noexcept
  937. {
  938. return (wc == tc) || (wc == '?' && tc != 0)
  939. || (ignoreCase && CharacterFunctions::toLowerCase (wc) == CharacterFunctions::toLowerCase (tc));
  940. }
  941. static bool matchesAnywhere (const CharPointer wildcard, CharPointer test, const bool ignoreCase) noexcept
  942. {
  943. for (; ! test.isEmpty(); ++test)
  944. if (matches (wildcard, test, ignoreCase))
  945. return true;
  946. return false;
  947. }
  948. };
  949. bool String::matchesWildcard (StringRef wildcard, const bool ignoreCase) const noexcept
  950. {
  951. return WildCardMatcher<CharPointerType>::matches (wildcard.text, text, ignoreCase);
  952. }
  953. //==============================================================================
  954. String String::repeatedString (StringRef stringToRepeat, int numberOfTimesToRepeat)
  955. {
  956. if (numberOfTimesToRepeat <= 0)
  957. return String();
  958. String result (PreallocationBytes (findByteOffsetOfEnd (stringToRepeat) * (size_t) numberOfTimesToRepeat));
  959. CharPointerType n (result.text);
  960. while (--numberOfTimesToRepeat >= 0)
  961. n.writeAll (stringToRepeat.text);
  962. return result;
  963. }
  964. String String::paddedLeft (const juce_wchar padCharacter, int minimumLength) const
  965. {
  966. jassert (padCharacter != 0);
  967. int extraChars = minimumLength;
  968. CharPointerType end (text);
  969. while (! end.isEmpty())
  970. {
  971. --extraChars;
  972. ++end;
  973. }
  974. if (extraChars <= 0 || padCharacter == 0)
  975. return *this;
  976. const size_t currentByteSize = (size_t) (((char*) end.getAddress()) - (char*) text.getAddress());
  977. String result (PreallocationBytes (currentByteSize + (size_t) extraChars * CharPointerType::getBytesRequiredFor (padCharacter)));
  978. CharPointerType n (result.text);
  979. while (--extraChars >= 0)
  980. n.write (padCharacter);
  981. n.writeAll (text);
  982. return result;
  983. }
  984. String String::paddedRight (const juce_wchar padCharacter, int minimumLength) const
  985. {
  986. jassert (padCharacter != 0);
  987. int extraChars = minimumLength;
  988. CharPointerType end (text);
  989. while (! end.isEmpty())
  990. {
  991. --extraChars;
  992. ++end;
  993. }
  994. if (extraChars <= 0 || padCharacter == 0)
  995. return *this;
  996. const size_t currentByteSize = (size_t) (((char*) end.getAddress()) - (char*) text.getAddress());
  997. String result (PreallocationBytes (currentByteSize + (size_t) extraChars * CharPointerType::getBytesRequiredFor (padCharacter)));
  998. CharPointerType n (result.text);
  999. n.writeAll (text);
  1000. while (--extraChars >= 0)
  1001. n.write (padCharacter);
  1002. n.writeNull();
  1003. return result;
  1004. }
  1005. //==============================================================================
  1006. String String::replaceSection (int index, int numCharsToReplace, StringRef stringToInsert) const
  1007. {
  1008. if (index < 0)
  1009. {
  1010. // a negative index to replace from?
  1011. jassertfalse;
  1012. index = 0;
  1013. }
  1014. if (numCharsToReplace < 0)
  1015. {
  1016. // replacing a negative number of characters?
  1017. numCharsToReplace = 0;
  1018. jassertfalse;
  1019. }
  1020. CharPointerType insertPoint (text);
  1021. for (int i = 0; i < index; ++i)
  1022. {
  1023. if (insertPoint.isEmpty())
  1024. {
  1025. // replacing beyond the end of the string?
  1026. jassertfalse;
  1027. return *this + stringToInsert;
  1028. }
  1029. ++insertPoint;
  1030. }
  1031. CharPointerType startOfRemainder (insertPoint);
  1032. for (int i = 0; i < numCharsToReplace && ! startOfRemainder.isEmpty(); ++i)
  1033. ++startOfRemainder;
  1034. if (insertPoint == text && startOfRemainder.isEmpty())
  1035. return stringToInsert.text;
  1036. const size_t initialBytes = (size_t) (((char*) insertPoint.getAddress()) - (char*) text.getAddress());
  1037. const size_t newStringBytes = findByteOffsetOfEnd (stringToInsert);
  1038. const size_t remainderBytes = (size_t) (((char*) startOfRemainder.findTerminatingNull().getAddress()) - (char*) startOfRemainder.getAddress());
  1039. const size_t newTotalBytes = initialBytes + newStringBytes + remainderBytes;
  1040. if (newTotalBytes <= 0)
  1041. return String();
  1042. String result (PreallocationBytes ((size_t) newTotalBytes));
  1043. char* dest = (char*) result.text.getAddress();
  1044. memcpy (dest, text.getAddress(), initialBytes);
  1045. dest += initialBytes;
  1046. memcpy (dest, stringToInsert.text.getAddress(), newStringBytes);
  1047. dest += newStringBytes;
  1048. memcpy (dest, startOfRemainder.getAddress(), remainderBytes);
  1049. dest += remainderBytes;
  1050. CharPointerType ((CharPointerType::CharType*) dest).writeNull();
  1051. return result;
  1052. }
  1053. String String::replace (StringRef stringToReplace, StringRef stringToInsert, const bool ignoreCase) const
  1054. {
  1055. const int stringToReplaceLen = stringToReplace.length();
  1056. const int stringToInsertLen = stringToInsert.length();
  1057. int i = 0;
  1058. String result (*this);
  1059. while ((i = (ignoreCase ? result.indexOfIgnoreCase (i, stringToReplace)
  1060. : result.indexOf (i, stringToReplace))) >= 0)
  1061. {
  1062. result = result.replaceSection (i, stringToReplaceLen, stringToInsert);
  1063. i += stringToInsertLen;
  1064. }
  1065. return result;
  1066. }
  1067. class StringCreationHelper
  1068. {
  1069. public:
  1070. StringCreationHelper (const size_t initialBytes)
  1071. : source (nullptr), dest (nullptr), allocatedBytes (initialBytes), bytesWritten (0)
  1072. {
  1073. result.preallocateBytes (allocatedBytes);
  1074. dest = result.getCharPointer();
  1075. }
  1076. StringCreationHelper (const String::CharPointerType s)
  1077. : source (s), dest (nullptr), allocatedBytes (StringHolder::getAllocatedNumBytes (s)), bytesWritten (0)
  1078. {
  1079. result.preallocateBytes (allocatedBytes);
  1080. dest = result.getCharPointer();
  1081. }
  1082. void write (juce_wchar c)
  1083. {
  1084. bytesWritten += String::CharPointerType::getBytesRequiredFor (c);
  1085. if (bytesWritten > allocatedBytes)
  1086. {
  1087. allocatedBytes += jmax ((size_t) 8, allocatedBytes / 16);
  1088. const size_t destOffset = (size_t) (((char*) dest.getAddress()) - (char*) result.getCharPointer().getAddress());
  1089. result.preallocateBytes (allocatedBytes);
  1090. dest = addBytesToPointer (result.getCharPointer().getAddress(), (int) destOffset);
  1091. }
  1092. dest.write (c);
  1093. }
  1094. String result;
  1095. String::CharPointerType source;
  1096. private:
  1097. String::CharPointerType dest;
  1098. size_t allocatedBytes, bytesWritten;
  1099. };
  1100. String String::replaceCharacter (const juce_wchar charToReplace, const juce_wchar charToInsert) const
  1101. {
  1102. if (! containsChar (charToReplace))
  1103. return *this;
  1104. StringCreationHelper builder (text);
  1105. for (;;)
  1106. {
  1107. juce_wchar c = builder.source.getAndAdvance();
  1108. if (c == charToReplace)
  1109. c = charToInsert;
  1110. builder.write (c);
  1111. if (c == 0)
  1112. break;
  1113. }
  1114. return builder.result;
  1115. }
  1116. String String::replaceCharacters (StringRef charactersToReplace, StringRef charactersToInsertInstead) const
  1117. {
  1118. // Each character in the first string must have a matching one in the
  1119. // second, so the two strings must be the same length.
  1120. jassert (charactersToReplace.length() == charactersToInsertInstead.length());
  1121. StringCreationHelper builder (text);
  1122. for (;;)
  1123. {
  1124. juce_wchar c = builder.source.getAndAdvance();
  1125. const int index = charactersToReplace.text.indexOf (c);
  1126. if (index >= 0)
  1127. c = charactersToInsertInstead [index];
  1128. builder.write (c);
  1129. if (c == 0)
  1130. break;
  1131. }
  1132. return builder.result;
  1133. }
  1134. //==============================================================================
  1135. bool String::startsWith (StringRef other) const noexcept
  1136. {
  1137. return text.compareUpTo (other.text, other.length()) == 0;
  1138. }
  1139. bool String::startsWithIgnoreCase (StringRef other) const noexcept
  1140. {
  1141. return text.compareIgnoreCaseUpTo (other.text, other.length()) == 0;
  1142. }
  1143. bool String::startsWithChar (const juce_wchar character) const noexcept
  1144. {
  1145. jassert (character != 0); // strings can't contain a null character!
  1146. return *text == character;
  1147. }
  1148. bool String::endsWithChar (const juce_wchar character) const noexcept
  1149. {
  1150. jassert (character != 0); // strings can't contain a null character!
  1151. if (text.isEmpty())
  1152. return false;
  1153. CharPointerType t (text.findTerminatingNull());
  1154. return *--t == character;
  1155. }
  1156. bool String::endsWith (StringRef other) const noexcept
  1157. {
  1158. CharPointerType end (text.findTerminatingNull());
  1159. CharPointerType otherEnd (other.text.findTerminatingNull());
  1160. while (end > text && otherEnd > other.text)
  1161. {
  1162. --end;
  1163. --otherEnd;
  1164. if (*end != *otherEnd)
  1165. return false;
  1166. }
  1167. return otherEnd == other.text;
  1168. }
  1169. bool String::endsWithIgnoreCase (StringRef other) const noexcept
  1170. {
  1171. CharPointerType end (text.findTerminatingNull());
  1172. CharPointerType otherEnd (other.text.findTerminatingNull());
  1173. while (end > text && otherEnd > other.text)
  1174. {
  1175. --end;
  1176. --otherEnd;
  1177. if (end.toLowerCase() != otherEnd.toLowerCase())
  1178. return false;
  1179. }
  1180. return otherEnd == other.text;
  1181. }
  1182. //==============================================================================
  1183. String String::toUpperCase() const
  1184. {
  1185. StringCreationHelper builder (text);
  1186. for (;;)
  1187. {
  1188. const juce_wchar c = builder.source.toUpperCase();
  1189. builder.write (c);
  1190. if (c == 0)
  1191. break;
  1192. ++(builder.source);
  1193. }
  1194. return builder.result;
  1195. }
  1196. String String::toLowerCase() const
  1197. {
  1198. StringCreationHelper builder (text);
  1199. for (;;)
  1200. {
  1201. const juce_wchar c = builder.source.toLowerCase();
  1202. builder.write (c);
  1203. if (c == 0)
  1204. break;
  1205. ++(builder.source);
  1206. }
  1207. return builder.result;
  1208. }
  1209. //==============================================================================
  1210. juce_wchar String::getLastCharacter() const noexcept
  1211. {
  1212. return isEmpty() ? juce_wchar() : text [length() - 1];
  1213. }
  1214. String String::substring (int start, const int end) const
  1215. {
  1216. if (start < 0)
  1217. start = 0;
  1218. if (end <= start)
  1219. return String();
  1220. int i = 0;
  1221. CharPointerType t1 (text);
  1222. while (i < start)
  1223. {
  1224. if (t1.isEmpty())
  1225. return String();
  1226. ++i;
  1227. ++t1;
  1228. }
  1229. CharPointerType t2 (t1);
  1230. while (i < end)
  1231. {
  1232. if (t2.isEmpty())
  1233. {
  1234. if (start == 0)
  1235. return *this;
  1236. break;
  1237. }
  1238. ++i;
  1239. ++t2;
  1240. }
  1241. return String (t1, t2);
  1242. }
  1243. String String::substring (int start) const
  1244. {
  1245. if (start <= 0)
  1246. return *this;
  1247. CharPointerType t (text);
  1248. while (--start >= 0)
  1249. {
  1250. if (t.isEmpty())
  1251. return String();
  1252. ++t;
  1253. }
  1254. return String (t);
  1255. }
  1256. String String::dropLastCharacters (const int numberToDrop) const
  1257. {
  1258. return String (text, (size_t) jmax (0, length() - numberToDrop));
  1259. }
  1260. String String::getLastCharacters (const int numCharacters) const
  1261. {
  1262. return String (text + jmax (0, length() - jmax (0, numCharacters)));
  1263. }
  1264. String String::fromFirstOccurrenceOf (StringRef sub,
  1265. const bool includeSubString,
  1266. const bool ignoreCase) const
  1267. {
  1268. const int i = ignoreCase ? indexOfIgnoreCase (sub)
  1269. : indexOf (sub);
  1270. if (i < 0)
  1271. return String();
  1272. return substring (includeSubString ? i : i + sub.length());
  1273. }
  1274. String String::fromLastOccurrenceOf (StringRef sub,
  1275. const bool includeSubString,
  1276. const bool ignoreCase) const
  1277. {
  1278. const int i = ignoreCase ? lastIndexOfIgnoreCase (sub)
  1279. : lastIndexOf (sub);
  1280. if (i < 0)
  1281. return *this;
  1282. return substring (includeSubString ? i : i + sub.length());
  1283. }
  1284. String String::upToFirstOccurrenceOf (StringRef sub,
  1285. const bool includeSubString,
  1286. const bool ignoreCase) const
  1287. {
  1288. const int i = ignoreCase ? indexOfIgnoreCase (sub)
  1289. : indexOf (sub);
  1290. if (i < 0)
  1291. return *this;
  1292. return substring (0, includeSubString ? i + sub.length() : i);
  1293. }
  1294. String String::upToLastOccurrenceOf (StringRef sub,
  1295. const bool includeSubString,
  1296. const bool ignoreCase) const
  1297. {
  1298. const int i = ignoreCase ? lastIndexOfIgnoreCase (sub)
  1299. : lastIndexOf (sub);
  1300. if (i < 0)
  1301. return *this;
  1302. return substring (0, includeSubString ? i + sub.length() : i);
  1303. }
  1304. bool String::isQuotedString() const
  1305. {
  1306. const juce_wchar trimmedStart = trimStart()[0];
  1307. return trimmedStart == '"'
  1308. || trimmedStart == '\'';
  1309. }
  1310. String String::unquoted() const
  1311. {
  1312. const int len = length();
  1313. if (len == 0)
  1314. return String();
  1315. const juce_wchar lastChar = text [len - 1];
  1316. const int dropAtStart = (*text == '"' || *text == '\'') ? 1 : 0;
  1317. const int dropAtEnd = (lastChar == '"' || lastChar == '\'') ? 1 : 0;
  1318. return substring (dropAtStart, len - dropAtEnd);
  1319. }
  1320. String String::quoted (const juce_wchar quoteCharacter) const
  1321. {
  1322. if (isEmpty())
  1323. return charToString (quoteCharacter) + quoteCharacter;
  1324. String t (*this);
  1325. if (! t.startsWithChar (quoteCharacter))
  1326. t = charToString (quoteCharacter) + t;
  1327. if (! t.endsWithChar (quoteCharacter))
  1328. t += quoteCharacter;
  1329. return t;
  1330. }
  1331. //==============================================================================
  1332. static String::CharPointerType findTrimmedEnd (const String::CharPointerType start,
  1333. String::CharPointerType end)
  1334. {
  1335. while (end > start)
  1336. {
  1337. if (! (--end).isWhitespace())
  1338. {
  1339. ++end;
  1340. break;
  1341. }
  1342. }
  1343. return end;
  1344. }
  1345. String String::trim() const
  1346. {
  1347. if (isNotEmpty())
  1348. {
  1349. CharPointerType start (text.findEndOfWhitespace());
  1350. const CharPointerType end (start.findTerminatingNull());
  1351. CharPointerType trimmedEnd (findTrimmedEnd (start, end));
  1352. if (trimmedEnd <= start)
  1353. return String();
  1354. if (text < start || trimmedEnd < end)
  1355. return String (start, trimmedEnd);
  1356. }
  1357. return *this;
  1358. }
  1359. String String::trimStart() const
  1360. {
  1361. if (isNotEmpty())
  1362. {
  1363. const CharPointerType t (text.findEndOfWhitespace());
  1364. if (t != text)
  1365. return String (t);
  1366. }
  1367. return *this;
  1368. }
  1369. String String::trimEnd() const
  1370. {
  1371. if (isNotEmpty())
  1372. {
  1373. const CharPointerType end (text.findTerminatingNull());
  1374. CharPointerType trimmedEnd (findTrimmedEnd (text, end));
  1375. if (trimmedEnd < end)
  1376. return String (text, trimmedEnd);
  1377. }
  1378. return *this;
  1379. }
  1380. String String::trimCharactersAtStart (StringRef charactersToTrim) const
  1381. {
  1382. CharPointerType t (text);
  1383. while (charactersToTrim.text.indexOf (*t) >= 0)
  1384. ++t;
  1385. return t == text ? *this : String (t);
  1386. }
  1387. String String::trimCharactersAtEnd (StringRef charactersToTrim) const
  1388. {
  1389. if (isNotEmpty())
  1390. {
  1391. const CharPointerType end (text.findTerminatingNull());
  1392. CharPointerType trimmedEnd (end);
  1393. while (trimmedEnd > text)
  1394. {
  1395. if (charactersToTrim.text.indexOf (*--trimmedEnd) < 0)
  1396. {
  1397. ++trimmedEnd;
  1398. break;
  1399. }
  1400. }
  1401. if (trimmedEnd < end)
  1402. return String (text, trimmedEnd);
  1403. }
  1404. return *this;
  1405. }
  1406. //==============================================================================
  1407. String String::retainCharacters (StringRef charactersToRetain) const
  1408. {
  1409. if (isEmpty())
  1410. return String();
  1411. StringCreationHelper builder (text);
  1412. for (;;)
  1413. {
  1414. juce_wchar c = builder.source.getAndAdvance();
  1415. if (charactersToRetain.text.indexOf (c) >= 0)
  1416. builder.write (c);
  1417. if (c == 0)
  1418. break;
  1419. }
  1420. builder.write (0);
  1421. return builder.result;
  1422. }
  1423. String String::removeCharacters (StringRef charactersToRemove) const
  1424. {
  1425. if (isEmpty())
  1426. return String();
  1427. StringCreationHelper builder (text);
  1428. for (;;)
  1429. {
  1430. juce_wchar c = builder.source.getAndAdvance();
  1431. if (charactersToRemove.text.indexOf (c) < 0)
  1432. builder.write (c);
  1433. if (c == 0)
  1434. break;
  1435. }
  1436. return builder.result;
  1437. }
  1438. String String::initialSectionContainingOnly (StringRef permittedCharacters) const
  1439. {
  1440. for (CharPointerType t (text); ! t.isEmpty(); ++t)
  1441. if (permittedCharacters.text.indexOf (*t) < 0)
  1442. return String (text, t);
  1443. return *this;
  1444. }
  1445. String String::initialSectionNotContaining (StringRef charactersToStopAt) const
  1446. {
  1447. for (CharPointerType t (text); ! t.isEmpty(); ++t)
  1448. if (charactersToStopAt.text.indexOf (*t) >= 0)
  1449. return String (text, t);
  1450. return *this;
  1451. }
  1452. bool String::containsOnly (StringRef chars) const noexcept
  1453. {
  1454. for (CharPointerType t (text); ! t.isEmpty();)
  1455. if (chars.text.indexOf (t.getAndAdvance()) < 0)
  1456. return false;
  1457. return true;
  1458. }
  1459. bool String::containsAnyOf (StringRef chars) const noexcept
  1460. {
  1461. for (CharPointerType t (text); ! t.isEmpty();)
  1462. if (chars.text.indexOf (t.getAndAdvance()) >= 0)
  1463. return true;
  1464. return false;
  1465. }
  1466. bool String::containsNonWhitespaceChars() const noexcept
  1467. {
  1468. for (CharPointerType t (text); ! t.isEmpty(); ++t)
  1469. if (! t.isWhitespace())
  1470. return true;
  1471. return false;
  1472. }
  1473. // Note! The format parameter here MUST NOT be a reference, otherwise MS's va_start macro fails to work (but still compiles).
  1474. String String::formatted (const String pf, ... )
  1475. {
  1476. size_t bufferSize = 256;
  1477. for (;;)
  1478. {
  1479. va_list args;
  1480. va_start (args, pf);
  1481. #if JUCE_WINDOWS
  1482. HeapBlock<wchar_t> temp (bufferSize);
  1483. const int num = (int) _vsnwprintf (temp.getData(), bufferSize - 1, pf.toWideCharPointer(), args);
  1484. #elif JUCE_ANDROID
  1485. HeapBlock<char> temp (bufferSize);
  1486. const int num = (int) vsnprintf (temp.getData(), bufferSize - 1, pf.toUTF8(), args);
  1487. #else
  1488. HeapBlock<wchar_t> temp (bufferSize);
  1489. const int num = (int) vswprintf (temp.getData(), bufferSize - 1, pf.toWideCharPointer(), args);
  1490. #endif
  1491. va_end (args);
  1492. if (num > 0)
  1493. return String (temp);
  1494. bufferSize += 256;
  1495. if (num == 0 || bufferSize > 65536) // the upper limit is a sanity check to avoid situations where vprintf repeatedly
  1496. break; // returns -1 because of an error rather than because it needs more space.
  1497. }
  1498. return String();
  1499. }
  1500. //==============================================================================
  1501. int String::getIntValue() const noexcept { return text.getIntValue32(); }
  1502. int64 String::getLargeIntValue() const noexcept { return text.getIntValue64(); }
  1503. float String::getFloatValue() const noexcept { return (float) getDoubleValue(); }
  1504. double String::getDoubleValue() const noexcept { return text.getDoubleValue(); }
  1505. int String::getTrailingIntValue() const noexcept
  1506. {
  1507. int n = 0;
  1508. int mult = 1;
  1509. CharPointerType t (text.findTerminatingNull());
  1510. while (--t >= text)
  1511. {
  1512. if (! t.isDigit())
  1513. {
  1514. if (*t == '-')
  1515. n = -n;
  1516. break;
  1517. }
  1518. n += mult * (*t - '0');
  1519. mult *= 10;
  1520. }
  1521. return n;
  1522. }
  1523. static const char hexDigits[] = "0123456789abcdef";
  1524. template <typename Type>
  1525. static String hexToString (Type v)
  1526. {
  1527. String::CharPointerType::CharType buffer[32];
  1528. String::CharPointerType::CharType* const end = buffer + numElementsInArray (buffer) - 1;
  1529. String::CharPointerType::CharType* t = end;
  1530. *t = 0;
  1531. do
  1532. {
  1533. *--t = hexDigits [(int) (v & 15)];
  1534. v >>= 4;
  1535. } while (v != 0);
  1536. return String (String::CharPointerType (t),
  1537. String::CharPointerType (end));
  1538. }
  1539. String String::toHexString (int number) { return hexToString ((unsigned int) number); }
  1540. String String::toHexString (int64 number) { return hexToString ((uint64) number); }
  1541. String String::toHexString (short number) { return toHexString ((int) (unsigned short) number); }
  1542. String String::toHexString (const void* const d, const int size, const int groupSize)
  1543. {
  1544. if (size <= 0)
  1545. return String();
  1546. int numChars = (size * 2) + 2;
  1547. if (groupSize > 0)
  1548. numChars += size / groupSize;
  1549. String s (PreallocationBytes (sizeof (CharPointerType::CharType) * (size_t) numChars));
  1550. const unsigned char* data = static_cast<const unsigned char*> (d);
  1551. CharPointerType dest (s.text);
  1552. for (int i = 0; i < size; ++i)
  1553. {
  1554. const unsigned char nextByte = *data++;
  1555. dest.write ((juce_wchar) hexDigits [nextByte >> 4]);
  1556. dest.write ((juce_wchar) hexDigits [nextByte & 0xf]);
  1557. if (groupSize > 0 && (i % groupSize) == (groupSize - 1) && i < (size - 1))
  1558. dest.write ((juce_wchar) ' ');
  1559. }
  1560. dest.writeNull();
  1561. return s;
  1562. }
  1563. int String::getHexValue32() const noexcept { return CharacterFunctions::HexParser<int> ::parse (text); }
  1564. int64 String::getHexValue64() const noexcept { return CharacterFunctions::HexParser<int64>::parse (text); }
  1565. //==============================================================================
  1566. static String getStringFromWindows1252Codepage (const char* data, size_t num)
  1567. {
  1568. HeapBlock<juce_wchar> unicode (num + 1);
  1569. for (size_t i = 0; i < num; ++i)
  1570. unicode[i] = CharacterFunctions::getUnicodeCharFromWindows1252Codepage ((uint8) data[i]);
  1571. unicode[num] = 0;
  1572. return CharPointer_UTF32 (unicode);
  1573. }
  1574. String String::createStringFromData (const void* const unknownData, int size)
  1575. {
  1576. const uint8* const data = static_cast<const uint8*> (unknownData);
  1577. if (size <= 0 || data == nullptr)
  1578. return String();
  1579. if (size == 1)
  1580. return charToString ((juce_wchar) data[0]);
  1581. if (CharPointer_UTF16::isByteOrderMarkBigEndian (data)
  1582. || CharPointer_UTF16::isByteOrderMarkLittleEndian (data))
  1583. {
  1584. const int numChars = size / 2 - 1;
  1585. StringCreationHelper builder ((size_t) numChars);
  1586. const uint16* const src = (const uint16*) (data + 2);
  1587. if (CharPointer_UTF16::isByteOrderMarkBigEndian (data))
  1588. {
  1589. for (int i = 0; i < numChars; ++i)
  1590. builder.write ((juce_wchar) ByteOrder::swapIfLittleEndian (src[i]));
  1591. }
  1592. else
  1593. {
  1594. for (int i = 0; i < numChars; ++i)
  1595. builder.write ((juce_wchar) ByteOrder::swapIfBigEndian (src[i]));
  1596. }
  1597. builder.write (0);
  1598. return builder.result;
  1599. }
  1600. const char* start = (const char*) data;
  1601. if (size >= 3 && CharPointer_UTF8::isByteOrderMark (data))
  1602. {
  1603. start += 3;
  1604. size -= 3;
  1605. }
  1606. if (CharPointer_UTF8::isValidString (start, size))
  1607. return String (CharPointer_UTF8 (start),
  1608. CharPointer_UTF8 (start + size));
  1609. return getStringFromWindows1252Codepage (start, (size_t) size);
  1610. }
  1611. //==============================================================================
  1612. static const juce_wchar emptyChar = 0;
  1613. template <class CharPointerType_Src, class CharPointerType_Dest>
  1614. struct StringEncodingConverter
  1615. {
  1616. static CharPointerType_Dest convert (const String& s)
  1617. {
  1618. String& source = const_cast<String&> (s);
  1619. typedef typename CharPointerType_Dest::CharType DestChar;
  1620. if (source.isEmpty())
  1621. return CharPointerType_Dest (reinterpret_cast<const DestChar*> (&emptyChar));
  1622. CharPointerType_Src text (source.getCharPointer());
  1623. const size_t extraBytesNeeded = CharPointerType_Dest::getBytesRequiredFor (text) + sizeof (typename CharPointerType_Dest::CharType);
  1624. const size_t endOffset = (text.sizeInBytes() + 3) & ~3u; // the new string must be word-aligned or many Windows
  1625. // functions will fail to read it correctly!
  1626. source.preallocateBytes (endOffset + extraBytesNeeded);
  1627. text = source.getCharPointer();
  1628. void* const newSpace = addBytesToPointer (text.getAddress(), (int) endOffset);
  1629. const CharPointerType_Dest extraSpace (static_cast<DestChar*> (newSpace));
  1630. #if JUCE_DEBUG // (This just avoids spurious warnings from valgrind about the uninitialised bytes at the end of the buffer..)
  1631. const size_t bytesToClear = (size_t) jmin ((int) extraBytesNeeded, 4);
  1632. zeromem (addBytesToPointer (newSpace, extraBytesNeeded - bytesToClear), bytesToClear);
  1633. #endif
  1634. CharPointerType_Dest (extraSpace).writeAll (text);
  1635. return extraSpace;
  1636. }
  1637. };
  1638. template <>
  1639. struct StringEncodingConverter<CharPointer_UTF8, CharPointer_UTF8>
  1640. {
  1641. static CharPointer_UTF8 convert (const String& source) noexcept { return CharPointer_UTF8 ((CharPointer_UTF8::CharType*) source.getCharPointer().getAddress()); }
  1642. };
  1643. template <>
  1644. struct StringEncodingConverter<CharPointer_UTF16, CharPointer_UTF16>
  1645. {
  1646. static CharPointer_UTF16 convert (const String& source) noexcept { return CharPointer_UTF16 ((CharPointer_UTF16::CharType*) source.getCharPointer().getAddress()); }
  1647. };
  1648. template <>
  1649. struct StringEncodingConverter<CharPointer_UTF32, CharPointer_UTF32>
  1650. {
  1651. static CharPointer_UTF32 convert (const String& source) noexcept { return CharPointer_UTF32 ((CharPointer_UTF32::CharType*) source.getCharPointer().getAddress()); }
  1652. };
  1653. CharPointer_UTF8 String::toUTF8() const { return StringEncodingConverter<CharPointerType, CharPointer_UTF8 >::convert (*this); }
  1654. CharPointer_UTF16 String::toUTF16() const { return StringEncodingConverter<CharPointerType, CharPointer_UTF16>::convert (*this); }
  1655. CharPointer_UTF32 String::toUTF32() const { return StringEncodingConverter<CharPointerType, CharPointer_UTF32>::convert (*this); }
  1656. const char* String::toRawUTF8() const
  1657. {
  1658. return toUTF8().getAddress();
  1659. }
  1660. const wchar_t* String::toWideCharPointer() const
  1661. {
  1662. return StringEncodingConverter<CharPointerType, CharPointer_wchar_t>::convert (*this).getAddress();
  1663. }
  1664. std::string String::toStdString() const
  1665. {
  1666. return std::string (toRawUTF8());
  1667. }
  1668. //==============================================================================
  1669. template <class CharPointerType_Src, class CharPointerType_Dest>
  1670. struct StringCopier
  1671. {
  1672. static size_t copyToBuffer (const CharPointerType_Src source, typename CharPointerType_Dest::CharType* const buffer, const size_t maxBufferSizeBytes)
  1673. {
  1674. jassert (((ssize_t) maxBufferSizeBytes) >= 0); // keep this value positive!
  1675. if (buffer == nullptr)
  1676. return CharPointerType_Dest::getBytesRequiredFor (source) + sizeof (typename CharPointerType_Dest::CharType);
  1677. return CharPointerType_Dest (buffer).writeWithDestByteLimit (source, maxBufferSizeBytes);
  1678. }
  1679. };
  1680. size_t String::copyToUTF8 (CharPointer_UTF8::CharType* const buffer, size_t maxBufferSizeBytes) const noexcept
  1681. {
  1682. return StringCopier<CharPointerType, CharPointer_UTF8>::copyToBuffer (text, buffer, maxBufferSizeBytes);
  1683. }
  1684. size_t String::copyToUTF16 (CharPointer_UTF16::CharType* const buffer, size_t maxBufferSizeBytes) const noexcept
  1685. {
  1686. return StringCopier<CharPointerType, CharPointer_UTF16>::copyToBuffer (text, buffer, maxBufferSizeBytes);
  1687. }
  1688. size_t String::copyToUTF32 (CharPointer_UTF32::CharType* const buffer, size_t maxBufferSizeBytes) const noexcept
  1689. {
  1690. return StringCopier<CharPointerType, CharPointer_UTF32>::copyToBuffer (text, buffer, maxBufferSizeBytes);
  1691. }
  1692. //==============================================================================
  1693. size_t String::getNumBytesAsUTF8() const noexcept
  1694. {
  1695. return CharPointer_UTF8::getBytesRequiredFor (text);
  1696. }
  1697. String String::fromUTF8 (const char* const buffer, int bufferSizeBytes)
  1698. {
  1699. if (buffer != nullptr)
  1700. {
  1701. if (bufferSizeBytes < 0)
  1702. return String (CharPointer_UTF8 (buffer));
  1703. if (bufferSizeBytes > 0)
  1704. {
  1705. jassert (CharPointer_UTF8::isValidString (buffer, bufferSizeBytes));
  1706. return String (CharPointer_UTF8 (buffer), CharPointer_UTF8 (buffer + bufferSizeBytes));
  1707. }
  1708. }
  1709. return String();
  1710. }
  1711. #if JUCE_MSVC
  1712. #pragma warning (pop)
  1713. #endif
  1714. //==============================================================================
  1715. StringRef::StringRef() noexcept : text ((const String::CharPointerType::CharType*) "\0\0\0")
  1716. {
  1717. }
  1718. StringRef::StringRef (const char* stringLiteral) noexcept
  1719. #if JUCE_STRING_UTF_TYPE != 8
  1720. : text (nullptr), stringCopy (stringLiteral)
  1721. #else
  1722. : text (stringLiteral)
  1723. #endif
  1724. {
  1725. #if JUCE_STRING_UTF_TYPE != 8
  1726. text = stringCopy.getCharPointer();
  1727. #endif
  1728. jassert (stringLiteral != nullptr); // This must be a valid string literal, not a null pointer!!
  1729. #if JUCE_NATIVE_WCHAR_IS_UTF8
  1730. /* If you get an assertion here, then you're trying to create a string from 8-bit data
  1731. that contains values greater than 127. These can NOT be correctly converted to unicode
  1732. because there's no way for the String class to know what encoding was used to
  1733. create them. The source data could be UTF-8, ASCII or one of many local code-pages.
  1734. To get around this problem, you must be more explicit when you pass an ambiguous 8-bit
  1735. string to the StringRef class - so for example if your source data is actually UTF-8,
  1736. you'd call StringRef (CharPointer_UTF8 ("my utf8 string..")), and it would be able to
  1737. correctly convert the multi-byte characters to unicode. It's *highly* recommended that
  1738. you use UTF-8 with escape characters in your source code to represent extended characters,
  1739. because there's no other way to represent these strings in a way that isn't dependent on
  1740. the compiler, source code editor and platform.
  1741. */
  1742. jassert (CharPointer_ASCII::isValidString (stringLiteral, std::numeric_limits<int>::max()));
  1743. #endif
  1744. }
  1745. StringRef::StringRef (String::CharPointerType stringLiteral) noexcept : text (stringLiteral)
  1746. {
  1747. jassert (stringLiteral.getAddress() != nullptr); // This must be a valid string literal, not a null pointer!!
  1748. }
  1749. StringRef::StringRef (const String& string) noexcept : text (string.getCharPointer()) {}
  1750. //==============================================================================
  1751. //==============================================================================
  1752. #if JUCE_UNIT_TESTS
  1753. #define STRINGIFY2(X) #X
  1754. #define STRINGIFY(X) STRINGIFY2(X)
  1755. class StringTests : public UnitTest
  1756. {
  1757. public:
  1758. StringTests() : UnitTest ("String class") {}
  1759. template <class CharPointerType>
  1760. struct TestUTFConversion
  1761. {
  1762. static void test (UnitTest& test, Random& r)
  1763. {
  1764. String s (createRandomWideCharString (r));
  1765. typename CharPointerType::CharType buffer [300];
  1766. memset (buffer, 0xff, sizeof (buffer));
  1767. CharPointerType (buffer).writeAll (s.toUTF32());
  1768. test.expectEquals (String (CharPointerType (buffer)), s);
  1769. memset (buffer, 0xff, sizeof (buffer));
  1770. CharPointerType (buffer).writeAll (s.toUTF16());
  1771. test.expectEquals (String (CharPointerType (buffer)), s);
  1772. memset (buffer, 0xff, sizeof (buffer));
  1773. CharPointerType (buffer).writeAll (s.toUTF8());
  1774. test.expectEquals (String (CharPointerType (buffer)), s);
  1775. test.expect (CharPointerType::isValidString (buffer, (int) strlen ((const char*) buffer)));
  1776. }
  1777. };
  1778. static String createRandomWideCharString (Random& r)
  1779. {
  1780. juce_wchar buffer[50] = { 0 };
  1781. for (int i = 0; i < numElementsInArray (buffer) - 1; ++i)
  1782. {
  1783. if (r.nextBool())
  1784. {
  1785. do
  1786. {
  1787. buffer[i] = (juce_wchar) (1 + r.nextInt (0x10ffff - 1));
  1788. }
  1789. while (! CharPointer_UTF16::canRepresent (buffer[i]));
  1790. }
  1791. else
  1792. buffer[i] = (juce_wchar) (1 + r.nextInt (0xff));
  1793. }
  1794. return CharPointer_UTF32 (buffer);
  1795. }
  1796. void runTest() override
  1797. {
  1798. Random r = getRandom();
  1799. {
  1800. beginTest ("Basics");
  1801. expect (String().length() == 0);
  1802. expect (String() == String::empty);
  1803. String s1, s2 ("abcd");
  1804. expect (s1.isEmpty() && ! s1.isNotEmpty());
  1805. expect (s2.isNotEmpty() && ! s2.isEmpty());
  1806. expect (s2.length() == 4);
  1807. s1 = "abcd";
  1808. expect (s2 == s1 && s1 == s2);
  1809. expect (s1 == "abcd" && s1 == L"abcd");
  1810. expect (String ("abcd") == String (L"abcd"));
  1811. expect (String ("abcdefg", 4) == L"abcd");
  1812. expect (String ("abcdefg", 4) == String (L"abcdefg", 4));
  1813. expect (String::charToString ('x') == "x");
  1814. expect (String::charToString (0) == String::empty);
  1815. expect (s2 + "e" == "abcde" && s2 + 'e' == "abcde");
  1816. expect (s2 + L'e' == "abcde" && s2 + L"e" == "abcde");
  1817. expect (s1.equalsIgnoreCase ("abcD") && s1 < "abce" && s1 > "abbb");
  1818. expect (s1.startsWith ("ab") && s1.startsWith ("abcd") && ! s1.startsWith ("abcde"));
  1819. expect (s1.startsWithIgnoreCase ("aB") && s1.endsWithIgnoreCase ("CD"));
  1820. expect (s1.endsWith ("bcd") && ! s1.endsWith ("aabcd"));
  1821. expectEquals (s1.indexOf (String::empty), 0);
  1822. expectEquals (s1.indexOfIgnoreCase (String::empty), 0);
  1823. expect (s1.startsWith (String::empty) && s1.endsWith (String::empty) && s1.contains (String::empty));
  1824. expect (s1.contains ("cd") && s1.contains ("ab") && s1.contains ("abcd"));
  1825. expect (s1.containsChar ('a'));
  1826. expect (! s1.containsChar ('x'));
  1827. expect (! s1.containsChar (0));
  1828. expect (String ("abc foo bar").containsWholeWord ("abc") && String ("abc foo bar").containsWholeWord ("abc"));
  1829. }
  1830. {
  1831. beginTest ("Operations");
  1832. String s ("012345678");
  1833. expect (s.hashCode() != 0);
  1834. expect (s.hashCode64() != 0);
  1835. expect (s.hashCode() != (s + s).hashCode());
  1836. expect (s.hashCode64() != (s + s).hashCode64());
  1837. expect (s.compare (String ("012345678")) == 0);
  1838. expect (s.compare (String ("012345679")) < 0);
  1839. expect (s.compare (String ("012345676")) > 0);
  1840. expect (String("a").compareNatural ("A") == 0);
  1841. expect (String("A").compareNatural ("B") < 0);
  1842. expect (String("a").compareNatural ("B") < 0);
  1843. expect (String("10").compareNatural ("2") > 0);
  1844. expect (String("Abc 10").compareNatural ("aBC 2") > 0);
  1845. expect (String("Abc 1").compareNatural ("aBC 2") < 0);
  1846. expect (s.substring (2, 3) == String::charToString (s[2]));
  1847. expect (s.substring (0, 1) == String::charToString (s[0]));
  1848. expect (s.getLastCharacter() == s [s.length() - 1]);
  1849. expect (String::charToString (s.getLastCharacter()) == s.getLastCharacters (1));
  1850. expect (s.substring (0, 3) == L"012");
  1851. expect (s.substring (0, 100) == s);
  1852. expect (s.substring (-1, 100) == s);
  1853. expect (s.substring (3) == "345678");
  1854. expect (s.indexOf (String (L"45")) == 4);
  1855. expect (String ("444445").indexOf ("45") == 4);
  1856. expect (String ("444445").lastIndexOfChar ('4') == 4);
  1857. expect (String ("45454545x").lastIndexOf (String (L"45")) == 6);
  1858. expect (String ("45454545x").lastIndexOfAnyOf ("456") == 7);
  1859. expect (String ("45454545x").lastIndexOfAnyOf (String (L"456x")) == 8);
  1860. expect (String ("abABaBaBa").lastIndexOfIgnoreCase ("aB") == 6);
  1861. expect (s.indexOfChar (L'4') == 4);
  1862. expect (s + s == "012345678012345678");
  1863. expect (s.startsWith (s));
  1864. expect (s.startsWith (s.substring (0, 4)));
  1865. expect (s.startsWith (s.dropLastCharacters (4)));
  1866. expect (s.endsWith (s.substring (5)));
  1867. expect (s.endsWith (s));
  1868. expect (s.contains (s.substring (3, 6)));
  1869. expect (s.contains (s.substring (3)));
  1870. expect (s.startsWithChar (s[0]));
  1871. expect (s.endsWithChar (s.getLastCharacter()));
  1872. expect (s [s.length()] == 0);
  1873. expect (String ("abcdEFGH").toLowerCase() == String ("abcdefgh"));
  1874. expect (String ("abcdEFGH").toUpperCase() == String ("ABCDEFGH"));
  1875. expect (String (StringRef ("abc")) == "abc");
  1876. expect (String (StringRef ("abc")) == StringRef ("abc"));
  1877. expect (String ("abc") + StringRef ("def") == "abcdef");
  1878. String s2 ("123");
  1879. s2 << ((int) 4) << ((short) 5) << "678" << L"9" << '0';
  1880. s2 += "xyz";
  1881. expect (s2 == "1234567890xyz");
  1882. s2 += (int) 123;
  1883. expect (s2 == "1234567890xyz123");
  1884. s2 += (int64) 123;
  1885. expect (s2 == "1234567890xyz123123");
  1886. s2 << StringRef ("def");
  1887. expect (s2 == "1234567890xyz123123def");
  1888. // int16
  1889. {
  1890. String numStr (std::numeric_limits<int16>::max());
  1891. expect (numStr == "32767");
  1892. }
  1893. {
  1894. String numStr (std::numeric_limits<int16>::min());
  1895. expect (numStr == "-32768");
  1896. }
  1897. {
  1898. String numStr;
  1899. numStr << std::numeric_limits<int16>::max();
  1900. expect (numStr == "32767");
  1901. }
  1902. {
  1903. String numStr;
  1904. numStr << std::numeric_limits<int16>::min();
  1905. expect (numStr == "-32768");
  1906. }
  1907. // uint16
  1908. {
  1909. String numStr (std::numeric_limits<uint16>::max());
  1910. expect (numStr == "65535");
  1911. }
  1912. {
  1913. String numStr (std::numeric_limits<uint16>::min());
  1914. expect (numStr == "0");
  1915. }
  1916. {
  1917. String numStr;
  1918. numStr << std::numeric_limits<uint16>::max();
  1919. expect (numStr == "65535");
  1920. }
  1921. {
  1922. String numStr;
  1923. numStr << std::numeric_limits<uint16>::min();
  1924. expect (numStr == "0");
  1925. }
  1926. // int32
  1927. {
  1928. String numStr (std::numeric_limits<int32>::max());
  1929. expect (numStr == "2147483647");
  1930. }
  1931. {
  1932. String numStr (std::numeric_limits<int32>::min());
  1933. expect (numStr == "-2147483648");
  1934. }
  1935. {
  1936. String numStr;
  1937. numStr << std::numeric_limits<int32>::max();
  1938. expect (numStr == "2147483647");
  1939. }
  1940. {
  1941. String numStr;
  1942. numStr << std::numeric_limits<int32>::min();
  1943. expect (numStr == "-2147483648");
  1944. }
  1945. // uint32
  1946. {
  1947. String numStr (std::numeric_limits<uint32>::max());
  1948. expect (numStr == "4294967295");
  1949. }
  1950. {
  1951. String numStr (std::numeric_limits<uint32>::min());
  1952. expect (numStr == "0");
  1953. }
  1954. // int64
  1955. {
  1956. String numStr (std::numeric_limits<int64>::max());
  1957. expect (numStr == "9223372036854775807");
  1958. }
  1959. {
  1960. String numStr (std::numeric_limits<int64>::min());
  1961. expect (numStr == "-9223372036854775808");
  1962. }
  1963. {
  1964. String numStr;
  1965. numStr << std::numeric_limits<int64>::max();
  1966. expect (numStr == "9223372036854775807");
  1967. }
  1968. {
  1969. String numStr;
  1970. numStr << std::numeric_limits<int64>::min();
  1971. expect (numStr == "-9223372036854775808");
  1972. }
  1973. // uint64
  1974. {
  1975. String numStr (std::numeric_limits<uint64>::max());
  1976. expect (numStr == "18446744073709551615");
  1977. }
  1978. {
  1979. String numStr (std::numeric_limits<uint64>::min());
  1980. expect (numStr == "0");
  1981. }
  1982. {
  1983. String numStr;
  1984. numStr << std::numeric_limits<uint64>::max();
  1985. expect (numStr == "18446744073709551615");
  1986. }
  1987. {
  1988. String numStr;
  1989. numStr << std::numeric_limits<uint64>::min();
  1990. expect (numStr == "0");
  1991. }
  1992. // size_t
  1993. {
  1994. String numStr (std::numeric_limits<size_t>::min());
  1995. expect (numStr == "0");
  1996. }
  1997. beginTest ("Numeric conversions");
  1998. expect (String::empty.getIntValue() == 0);
  1999. expect (String::empty.getDoubleValue() == 0.0);
  2000. expect (String::empty.getFloatValue() == 0.0f);
  2001. expect (s.getIntValue() == 12345678);
  2002. expect (s.getLargeIntValue() == (int64) 12345678);
  2003. expect (s.getDoubleValue() == 12345678.0);
  2004. expect (s.getFloatValue() == 12345678.0f);
  2005. expect (String (-1234).getIntValue() == -1234);
  2006. expect (String ((int64) -1234).getLargeIntValue() == -1234);
  2007. expect (String (-1234.56).getDoubleValue() == -1234.56);
  2008. expect (String (-1234.56f).getFloatValue() == -1234.56f);
  2009. expect (String (std::numeric_limits<int>::max()).getIntValue() == std::numeric_limits<int>::max());
  2010. expect (String (std::numeric_limits<int>::min()).getIntValue() == std::numeric_limits<int>::min());
  2011. expect (String (std::numeric_limits<int64>::max()).getLargeIntValue() == std::numeric_limits<int64>::max());
  2012. expect (String (std::numeric_limits<int64>::min()).getLargeIntValue() == std::numeric_limits<int64>::min());
  2013. expect (("xyz" + s).getTrailingIntValue() == s.getIntValue());
  2014. expect (s.getHexValue32() == 0x12345678);
  2015. expect (s.getHexValue64() == (int64) 0x12345678);
  2016. expect (String::toHexString (0x1234abcd).equalsIgnoreCase ("1234abcd"));
  2017. expect (String::toHexString ((int64) 0x1234abcd).equalsIgnoreCase ("1234abcd"));
  2018. expect (String::toHexString ((short) 0x12ab).equalsIgnoreCase ("12ab"));
  2019. unsigned char data[] = { 1, 2, 3, 4, 0xa, 0xb, 0xc, 0xd };
  2020. expect (String::toHexString (data, 8, 0).equalsIgnoreCase ("010203040a0b0c0d"));
  2021. expect (String::toHexString (data, 8, 1).equalsIgnoreCase ("01 02 03 04 0a 0b 0c 0d"));
  2022. expect (String::toHexString (data, 8, 2).equalsIgnoreCase ("0102 0304 0a0b 0c0d"));
  2023. beginTest ("Subsections");
  2024. String s3;
  2025. s3 = "abcdeFGHIJ";
  2026. expect (s3.equalsIgnoreCase ("ABCdeFGhiJ"));
  2027. expect (s3.compareIgnoreCase (L"ABCdeFGhiJ") == 0);
  2028. expect (s3.containsIgnoreCase (s3.substring (3)));
  2029. expect (s3.indexOfAnyOf ("xyzf", 2, true) == 5);
  2030. expect (s3.indexOfAnyOf (String (L"xyzf"), 2, false) == -1);
  2031. expect (s3.indexOfAnyOf ("xyzF", 2, false) == 5);
  2032. expect (s3.containsAnyOf (String (L"zzzFs")));
  2033. expect (s3.startsWith ("abcd"));
  2034. expect (s3.startsWithIgnoreCase (String (L"abCD")));
  2035. expect (s3.startsWith (String::empty));
  2036. expect (s3.startsWithChar ('a'));
  2037. expect (s3.endsWith (String ("HIJ")));
  2038. expect (s3.endsWithIgnoreCase (String (L"Hij")));
  2039. expect (s3.endsWith (String::empty));
  2040. expect (s3.endsWithChar (L'J'));
  2041. expect (s3.indexOf ("HIJ") == 7);
  2042. expect (s3.indexOf (String (L"HIJK")) == -1);
  2043. expect (s3.indexOfIgnoreCase ("hij") == 7);
  2044. expect (s3.indexOfIgnoreCase (String (L"hijk")) == -1);
  2045. expect (s3.toStdString() == s3.toRawUTF8());
  2046. String s4 (s3);
  2047. s4.append (String ("xyz123"), 3);
  2048. expect (s4 == s3 + "xyz");
  2049. expect (String (1234) < String (1235));
  2050. expect (String (1235) > String (1234));
  2051. expect (String (1234) >= String (1234));
  2052. expect (String (1234) <= String (1234));
  2053. expect (String (1235) >= String (1234));
  2054. expect (String (1234) <= String (1235));
  2055. String s5 ("word word2 word3");
  2056. expect (s5.containsWholeWord (String ("word2")));
  2057. expect (s5.indexOfWholeWord ("word2") == 5);
  2058. expect (s5.containsWholeWord (String (L"word")));
  2059. expect (s5.containsWholeWord ("word3"));
  2060. expect (s5.containsWholeWord (s5));
  2061. expect (s5.containsWholeWordIgnoreCase (String (L"Word2")));
  2062. expect (s5.indexOfWholeWordIgnoreCase ("Word2") == 5);
  2063. expect (s5.containsWholeWordIgnoreCase (String (L"Word")));
  2064. expect (s5.containsWholeWordIgnoreCase ("Word3"));
  2065. expect (! s5.containsWholeWordIgnoreCase (String (L"Wordx")));
  2066. expect (! s5.containsWholeWordIgnoreCase ("xWord2"));
  2067. expect (s5.containsNonWhitespaceChars());
  2068. expect (s5.containsOnly ("ordw23 "));
  2069. expect (! String (" \n\r\t").containsNonWhitespaceChars());
  2070. expect (s5.matchesWildcard (String (L"wor*"), false));
  2071. expect (s5.matchesWildcard ("wOr*", true));
  2072. expect (s5.matchesWildcard (String (L"*word3"), true));
  2073. expect (s5.matchesWildcard ("*word?", true));
  2074. expect (s5.matchesWildcard (String (L"Word*3"), true));
  2075. expect (! s5.matchesWildcard (String (L"*34"), true));
  2076. expect (String ("xx**y").matchesWildcard ("*y", true));
  2077. expect (String ("xx**y").matchesWildcard ("x*y", true));
  2078. expect (String ("xx**y").matchesWildcard ("xx*y", true));
  2079. expect (String ("xx**y").matchesWildcard ("xx*", true));
  2080. expect (String ("xx?y").matchesWildcard ("x??y", true));
  2081. expect (String ("xx?y").matchesWildcard ("xx?y", true));
  2082. expect (! String ("xx?y").matchesWildcard ("xx?y?", true));
  2083. expect (String ("xx?y").matchesWildcard ("xx??", true));
  2084. expectEquals (s5.fromFirstOccurrenceOf (String::empty, true, false), s5);
  2085. expectEquals (s5.fromFirstOccurrenceOf ("xword2", true, false), s5.substring (100));
  2086. expectEquals (s5.fromFirstOccurrenceOf (String (L"word2"), true, false), s5.substring (5));
  2087. expectEquals (s5.fromFirstOccurrenceOf ("Word2", true, true), s5.substring (5));
  2088. expectEquals (s5.fromFirstOccurrenceOf ("word2", false, false), s5.getLastCharacters (6));
  2089. expectEquals (s5.fromFirstOccurrenceOf ("Word2", false, true), s5.getLastCharacters (6));
  2090. expectEquals (s5.fromLastOccurrenceOf (String::empty, true, false), s5);
  2091. expectEquals (s5.fromLastOccurrenceOf ("wordx", true, false), s5);
  2092. expectEquals (s5.fromLastOccurrenceOf ("word", true, false), s5.getLastCharacters (5));
  2093. expectEquals (s5.fromLastOccurrenceOf ("worD", true, true), s5.getLastCharacters (5));
  2094. expectEquals (s5.fromLastOccurrenceOf ("word", false, false), s5.getLastCharacters (1));
  2095. expectEquals (s5.fromLastOccurrenceOf ("worD", false, true), s5.getLastCharacters (1));
  2096. expect (s5.upToFirstOccurrenceOf (String::empty, true, false).isEmpty());
  2097. expectEquals (s5.upToFirstOccurrenceOf ("word4", true, false), s5);
  2098. expectEquals (s5.upToFirstOccurrenceOf ("word2", true, false), s5.substring (0, 10));
  2099. expectEquals (s5.upToFirstOccurrenceOf ("Word2", true, true), s5.substring (0, 10));
  2100. expectEquals (s5.upToFirstOccurrenceOf ("word2", false, false), s5.substring (0, 5));
  2101. expectEquals (s5.upToFirstOccurrenceOf ("Word2", false, true), s5.substring (0, 5));
  2102. expectEquals (s5.upToLastOccurrenceOf (String::empty, true, false), s5);
  2103. expectEquals (s5.upToLastOccurrenceOf ("zword", true, false), s5);
  2104. expectEquals (s5.upToLastOccurrenceOf ("word", true, false), s5.dropLastCharacters (1));
  2105. expectEquals (s5.dropLastCharacters(1).upToLastOccurrenceOf ("word", true, false), s5.dropLastCharacters (1));
  2106. expectEquals (s5.upToLastOccurrenceOf ("Word", true, true), s5.dropLastCharacters (1));
  2107. expectEquals (s5.upToLastOccurrenceOf ("word", false, false), s5.dropLastCharacters (5));
  2108. expectEquals (s5.upToLastOccurrenceOf ("Word", false, true), s5.dropLastCharacters (5));
  2109. expectEquals (s5.replace ("word", "xyz", false), String ("xyz xyz2 xyz3"));
  2110. expect (s5.replace ("Word", "xyz", true) == "xyz xyz2 xyz3");
  2111. expect (s5.dropLastCharacters (1).replace ("Word", String ("xyz"), true) == L"xyz xyz2 xyz");
  2112. expect (s5.replace ("Word", "", true) == " 2 3");
  2113. expectEquals (s5.replace ("Word2", "xyz", true), String ("word xyz word3"));
  2114. expect (s5.replaceCharacter (L'w', 'x') != s5);
  2115. expectEquals (s5.replaceCharacter ('w', L'x').replaceCharacter ('x', 'w'), s5);
  2116. expect (s5.replaceCharacters ("wo", "xy") != s5);
  2117. expectEquals (s5.replaceCharacters ("wo", "xy").replaceCharacters ("xy", "wo"), s5);
  2118. expectEquals (s5.retainCharacters ("1wordxya"), String ("wordwordword"));
  2119. expect (s5.retainCharacters (String::empty).isEmpty());
  2120. expect (s5.removeCharacters ("1wordxya") == " 2 3");
  2121. expectEquals (s5.removeCharacters (String::empty), s5);
  2122. expect (s5.initialSectionContainingOnly ("word") == L"word");
  2123. expect (String ("word").initialSectionContainingOnly ("word") == L"word");
  2124. expectEquals (s5.initialSectionNotContaining (String ("xyz ")), String ("word"));
  2125. expectEquals (s5.initialSectionNotContaining (String (";[:'/")), s5);
  2126. expect (! s5.isQuotedString());
  2127. expect (s5.quoted().isQuotedString());
  2128. expect (! s5.quoted().unquoted().isQuotedString());
  2129. expect (! String ("x'").isQuotedString());
  2130. expect (String ("'x").isQuotedString());
  2131. String s6 (" \t xyz \t\r\n");
  2132. expectEquals (s6.trim(), String ("xyz"));
  2133. expect (s6.trim().trim() == "xyz");
  2134. expectEquals (s5.trim(), s5);
  2135. expectEquals (s6.trimStart().trimEnd(), s6.trim());
  2136. expectEquals (s6.trimStart().trimEnd(), s6.trimEnd().trimStart());
  2137. expectEquals (s6.trimStart().trimStart().trimEnd().trimEnd(), s6.trimEnd().trimStart());
  2138. expect (s6.trimStart() != s6.trimEnd());
  2139. expectEquals (("\t\r\n " + s6 + "\t\n \r").trim(), s6.trim());
  2140. expect (String::repeatedString ("xyz", 3) == L"xyzxyzxyz");
  2141. }
  2142. {
  2143. beginTest ("UTF conversions");
  2144. TestUTFConversion <CharPointer_UTF32>::test (*this, r);
  2145. TestUTFConversion <CharPointer_UTF8>::test (*this, r);
  2146. TestUTFConversion <CharPointer_UTF16>::test (*this, r);
  2147. }
  2148. {
  2149. beginTest ("StringArray");
  2150. StringArray s;
  2151. s.addTokens ("4,3,2,1,0", ";,", "x");
  2152. expectEquals (s.size(), 5);
  2153. expectEquals (s.joinIntoString ("-"), String ("4-3-2-1-0"));
  2154. s.remove (2);
  2155. expectEquals (s.joinIntoString ("--"), String ("4--3--1--0"));
  2156. expectEquals (s.joinIntoString (String::empty), String ("4310"));
  2157. s.clear();
  2158. expectEquals (s.joinIntoString ("x"), String::empty);
  2159. StringArray toks;
  2160. toks.addTokens ("x,,", ";,", "");
  2161. expectEquals (toks.size(), 3);
  2162. expectEquals (toks.joinIntoString ("-"), String ("x--"));
  2163. toks.clear();
  2164. toks.addTokens (",x,", ";,", "");
  2165. expectEquals (toks.size(), 3);
  2166. expectEquals (toks.joinIntoString ("-"), String ("-x-"));
  2167. toks.clear();
  2168. toks.addTokens ("x,'y,z',", ";,", "'");
  2169. expectEquals (toks.size(), 3);
  2170. expectEquals (toks.joinIntoString ("-"), String ("x-'y,z'-"));
  2171. }
  2172. {
  2173. beginTest ("var");
  2174. var v1 = 0;
  2175. var v2 = 0.16;
  2176. var v3 = "0.16";
  2177. var v4 = (int64) 0;
  2178. var v5 = 0.0;
  2179. expect (! v2.equals (v1));
  2180. expect (! v1.equals (v2));
  2181. expect (v2.equals (v3));
  2182. expect (! v3.equals (v1));
  2183. expect (! v1.equals (v3));
  2184. expect (v1.equals (v4));
  2185. expect (v4.equals (v1));
  2186. expect (v5.equals (v4));
  2187. expect (v4.equals (v5));
  2188. expect (! v2.equals (v4));
  2189. expect (! v4.equals (v2));
  2190. }
  2191. }
  2192. };
  2193. static StringTests stringUnitTests;
  2194. #endif