Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

String.cpp 65KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #include "String.h"
  21. #include "NewLine.h"
  22. #include "../maths/MathsFunctions.h"
  23. #include "../memory/HeapBlock.h"
  24. #include "../streams/OutputStream.h"
  25. #include "CarlaJuceUtils.hpp"
  26. #include <locale>
  27. #include <iostream>
  28. #ifdef CARLA_OS_MAC
  29. // FIXME
  30. // # import <CoreData/CoreData.h>
  31. #endif
  32. // #import <Carbon/UnicodeConverter.h>
  33. namespace water {
  34. //==============================================================================
  35. // (Mirrors the structure of StringHolder, but without the atomic member, so can be statically constructed)
  36. struct EmptyString
  37. {
  38. int refCount;
  39. size_t allocatedBytes;
  40. CharPointer_UTF8::CharType text;
  41. };
  42. static const EmptyString emptyString = { 0x3fffffff, sizeof (CharPointer_UTF8::CharType), '\0' };
  43. //==============================================================================
  44. class StringHolder
  45. {
  46. public:
  47. StringHolder() WATER_DELETED_FUNCTION;
  48. typedef CharPointer_UTF8::CharType CharType;
  49. //==============================================================================
  50. static CharPointer_UTF8 createUninitialisedBytes (size_t numBytes)
  51. {
  52. numBytes = (numBytes + 3) & ~(size_t) 3;
  53. StringHolder* const s = reinterpret_cast<StringHolder*> (new char [sizeof (StringHolder) - sizeof (CharType) + numBytes]);
  54. s->refCount.value = 0;
  55. s->allocatedNumBytes = numBytes;
  56. return CharPointer_UTF8 (s->text);
  57. }
  58. template <class CharPointer>
  59. static CharPointer_UTF8 createFromCharPointer (const CharPointer text)
  60. {
  61. if (text.getAddress() == nullptr || text.isEmpty())
  62. return CharPointer_UTF8 (&(emptyString.text));
  63. const size_t bytesNeeded = sizeof (CharType) + CharPointer_UTF8::getBytesRequiredFor (text);
  64. const CharPointer_UTF8 dest (createUninitialisedBytes (bytesNeeded));
  65. CharPointer_UTF8 (dest).writeAll (text);
  66. return dest;
  67. }
  68. template <class CharPointer>
  69. static CharPointer_UTF8 createFromCharPointer (const CharPointer text, size_t maxChars)
  70. {
  71. if (text.getAddress() == nullptr || text.isEmpty() || maxChars == 0)
  72. return CharPointer_UTF8 (&(emptyString.text));
  73. CharPointer end (text);
  74. size_t numChars = 0;
  75. size_t bytesNeeded = sizeof (CharType);
  76. while (numChars < maxChars && ! end.isEmpty())
  77. {
  78. bytesNeeded += CharPointer_UTF8::getBytesRequiredFor (end.getAndAdvance());
  79. ++numChars;
  80. }
  81. const CharPointer_UTF8 dest (createUninitialisedBytes (bytesNeeded));
  82. CharPointer_UTF8 (dest).writeWithCharLimit (text, (int) numChars + 1);
  83. return dest;
  84. }
  85. template <class CharPointer>
  86. static CharPointer_UTF8 createFromCharPointer (const CharPointer start, const CharPointer end)
  87. {
  88. if (start.getAddress() == nullptr || start.isEmpty())
  89. return CharPointer_UTF8 (&(emptyString.text));
  90. CharPointer e (start);
  91. int numChars = 0;
  92. size_t bytesNeeded = sizeof (CharType);
  93. while (e < end && ! e.isEmpty())
  94. {
  95. bytesNeeded += CharPointer_UTF8::getBytesRequiredFor (e.getAndAdvance());
  96. ++numChars;
  97. }
  98. const CharPointer_UTF8 dest (createUninitialisedBytes (bytesNeeded));
  99. CharPointer_UTF8 (dest).writeWithCharLimit (start, numChars + 1);
  100. return dest;
  101. }
  102. static CharPointer_UTF8 createFromCharPointer (const CharPointer_UTF8 start, const CharPointer_UTF8 end)
  103. {
  104. if (start.getAddress() == nullptr || start.isEmpty())
  105. return CharPointer_UTF8 (&(emptyString.text));
  106. const size_t numBytes = (size_t) (reinterpret_cast<const char*> (end.getAddress())
  107. - reinterpret_cast<const char*> (start.getAddress()));
  108. const CharPointer_UTF8 dest (createUninitialisedBytes (numBytes + sizeof (CharType)));
  109. memcpy (dest.getAddress(), start, numBytes);
  110. dest.getAddress()[numBytes / sizeof (CharType)] = 0;
  111. return dest;
  112. }
  113. static CharPointer_UTF8 createFromFixedLength (const char* const src, const size_t numChars)
  114. {
  115. const CharPointer_UTF8 dest (createUninitialisedBytes (numChars * sizeof (CharType) + sizeof (CharType)));
  116. CharPointer_UTF8 (dest).writeWithCharLimit (CharPointer_UTF8 (src), (int) (numChars + 1));
  117. return dest;
  118. }
  119. //==============================================================================
  120. static void retain (const CharPointer_UTF8 text) noexcept
  121. {
  122. StringHolder* const b = bufferFromText (text);
  123. if (b != (StringHolder*) &emptyString)
  124. ++(b->refCount);
  125. }
  126. static inline void release (StringHolder* const b) noexcept
  127. {
  128. if (b != (StringHolder*) &emptyString)
  129. if (--(b->refCount) == -1)
  130. delete[] reinterpret_cast<char*> (b);
  131. }
  132. static void release (const CharPointer_UTF8 text) noexcept
  133. {
  134. release (bufferFromText (text));
  135. }
  136. static inline int getReferenceCount (const CharPointer_UTF8 text) noexcept
  137. {
  138. return bufferFromText (text)->refCount.get() + 1;
  139. }
  140. //==============================================================================
  141. static CharPointer_UTF8 makeUniqueWithByteSize (const CharPointer_UTF8 text, size_t numBytes)
  142. {
  143. StringHolder* const b = bufferFromText (text);
  144. if (b == (StringHolder*) &emptyString)
  145. {
  146. CharPointer_UTF8 newText (createUninitialisedBytes (numBytes));
  147. newText.writeNull();
  148. return newText;
  149. }
  150. if (b->allocatedNumBytes >= numBytes && b->refCount.get() <= 0)
  151. return text;
  152. CharPointer_UTF8 newText (createUninitialisedBytes (jmax (b->allocatedNumBytes, numBytes)));
  153. memcpy (newText.getAddress(), text.getAddress(), b->allocatedNumBytes);
  154. release (b);
  155. return newText;
  156. }
  157. static size_t getAllocatedNumBytes (const CharPointer_UTF8 text) noexcept
  158. {
  159. return bufferFromText (text)->allocatedNumBytes;
  160. }
  161. //==============================================================================
  162. Atomic<int> refCount;
  163. size_t allocatedNumBytes;
  164. CharType text[1];
  165. private:
  166. static inline StringHolder* bufferFromText (const CharPointer_UTF8 text) noexcept
  167. {
  168. // (Can't use offsetof() here because of warnings about this not being a POD)
  169. return reinterpret_cast<StringHolder*> (reinterpret_cast<char*> (text.getAddress())
  170. - (reinterpret_cast<size_t> (reinterpret_cast<StringHolder*> (1)->text) - 1));
  171. }
  172. };
  173. //==============================================================================
  174. String::String() noexcept : text (&(emptyString.text))
  175. {
  176. }
  177. String::~String() noexcept
  178. {
  179. StringHolder::release (text);
  180. }
  181. String::String (const String& other) noexcept : text (other.text)
  182. {
  183. StringHolder::retain (text);
  184. }
  185. void String::swapWith (String& other) noexcept
  186. {
  187. std::swap (text, other.text);
  188. }
  189. void String::clear() noexcept
  190. {
  191. StringHolder::release (text);
  192. text = &(emptyString.text);
  193. }
  194. String& String::operator= (const String& other) noexcept
  195. {
  196. swapWith(const_cast<String&>(other));
  197. return *this;
  198. }
  199. inline String::PreallocationBytes::PreallocationBytes (const size_t num) noexcept : numBytes (num) {}
  200. String::String (const PreallocationBytes& preallocationSize)
  201. : text (StringHolder::createUninitialisedBytes (preallocationSize.numBytes + sizeof (CharPointer_UTF8::CharType)))
  202. {
  203. }
  204. void String::preallocateBytes (const size_t numBytesNeeded)
  205. {
  206. text = StringHolder::makeUniqueWithByteSize (text, numBytesNeeded + sizeof (CharPointer_UTF8::CharType));
  207. }
  208. int String::getReferenceCount() const noexcept
  209. {
  210. return StringHolder::getReferenceCount (text);
  211. }
  212. //==============================================================================
  213. String::String (const char* const t)
  214. : text (StringHolder::createFromCharPointer (CharPointer_UTF8 (t)))
  215. {
  216. /* If you get an assertion here, then you're trying to create a string from 8-bit data
  217. that contains values greater than 127. These can NOT be correctly converted to unicode
  218. because there's no way for the String class to know what encoding was used to
  219. create them. The source data could be UTF-8, ASCII or one of many local code-pages.
  220. To get around this problem, you must be more explicit when you pass an ambiguous 8-bit
  221. string to the String class - so for example if your source data is actually UTF-8,
  222. you'd call String (CharPointer_UTF8 ("my utf8 string..")), and it would be able to
  223. correctly convert the multi-byte characters to unicode. It's *highly* recommended that
  224. you use UTF-8 with escape characters in your source code to represent extended characters,
  225. because there's no other way to represent these strings in a way that isn't dependent on
  226. the compiler, source code editor and platform.
  227. */
  228. CARLA_SAFE_ASSERT (t == nullptr || CharPointer_UTF8::isValidString (t, std::numeric_limits<int>::max()));
  229. }
  230. String::String (const char* const t, const size_t maxChars)
  231. : text (StringHolder::createFromCharPointer (CharPointer_UTF8 (t), maxChars))
  232. {
  233. /* If you get an assertion here, then you're trying to create a string from 8-bit data
  234. that contains values greater than 127. These can NOT be correctly converted to unicode
  235. because there's no way for the String class to know what encoding was used to
  236. create them. The source data could be UTF-8, ASCII or one of many local code-pages.
  237. To get around this problem, you must be more explicit when you pass an ambiguous 8-bit
  238. string to the String class - so for example if your source data is actually UTF-8,
  239. you'd call String (CharPointer_UTF8 ("my utf8 string..")), and it would be able to
  240. correctly convert the multi-byte characters to unicode. It's *highly* recommended that
  241. you use UTF-8 with escape characters in your source code to represent extended characters,
  242. because there's no other way to represent these strings in a way that isn't dependent on
  243. the compiler, source code editor and platform.
  244. */
  245. CARLA_SAFE_ASSERT (t == nullptr || CharPointer_UTF8::isValidString (t, (int) maxChars));
  246. }
  247. String::String (const CharPointer_UTF8 t) : text (StringHolder::createFromCharPointer (t)) {}
  248. String::String (const CharPointer_UTF8 t, const size_t maxChars) : text (StringHolder::createFromCharPointer (t, maxChars)) {}
  249. String::String (const CharPointer_UTF8 start, const CharPointer_UTF8 end) : text (StringHolder::createFromCharPointer (start, end)) {}
  250. String::String (const std::string& s) : text (StringHolder::createFromFixedLength (s.data(), s.size())) {}
  251. String::String (StringRef s) : text (StringHolder::createFromCharPointer (s.text)) {}
  252. String String::charToString (const water_uchar character)
  253. {
  254. String result (PreallocationBytes (CharPointer_UTF8::getBytesRequiredFor (character)));
  255. CharPointer_UTF8 t (result.text);
  256. t.write (character);
  257. t.writeNull();
  258. return result;
  259. }
  260. //==============================================================================
  261. namespace NumberToStringConverters
  262. {
  263. static char* numberToString (char* t, uint64 v) noexcept
  264. {
  265. return printDigits (t, v);
  266. }
  267. static char* numberToString (char* t, const int n) noexcept
  268. {
  269. if (n >= 0)
  270. return printDigits (t, static_cast<unsigned int> (n));
  271. // NB: this needs to be careful not to call -std::numeric_limits<int>::min(),
  272. // which has undefined behaviour
  273. t = printDigits (t, static_cast<unsigned int> (-(n + 1)) + 1);
  274. *--t = '-';
  275. return t;
  276. }
  277. static char* numberToString (char* t, const unsigned int v) noexcept
  278. {
  279. return printDigits (t, v);
  280. }
  281. static char* numberToString (char* t, const long n) noexcept
  282. {
  283. if (n >= 0)
  284. return printDigits (t, static_cast<unsigned long> (n));
  285. t = printDigits (t, static_cast<unsigned long> (-(n + 1)) + 1);
  286. *--t = '-';
  287. return t;
  288. }
  289. static char* numberToString (char* t, const unsigned long v) noexcept
  290. {
  291. return printDigits (t, v);
  292. }
  293. struct StackArrayStream : public std::basic_streambuf<char, std::char_traits<char> >
  294. {
  295. explicit StackArrayStream (char* d)
  296. {
  297. static const std::locale classicLocale (std::locale::classic());
  298. imbue (classicLocale);
  299. setp (d, d + charsNeededForDouble);
  300. }
  301. size_t writeDouble (double n, int numDecPlaces)
  302. {
  303. {
  304. std::ostream o (this);
  305. if (numDecPlaces > 0)
  306. o.precision ((std::streamsize) numDecPlaces);
  307. o << n;
  308. }
  309. return (size_t) (pptr() - pbase());
  310. }
  311. };
  312. static char* doubleToString (char* buffer, const int numChars, double n, int numDecPlaces, size_t& len) noexcept
  313. {
  314. if (numDecPlaces > 0 && numDecPlaces < 7 && n > -1.0e20 && n < 1.0e20)
  315. {
  316. char* const end = buffer + numChars;
  317. char* t = end;
  318. int64 v = (int64) (pow (10.0, numDecPlaces) * std::abs (n) + 0.5);
  319. *--t = (char) 0;
  320. while (numDecPlaces >= 0 || v > 0)
  321. {
  322. if (numDecPlaces == 0)
  323. *--t = '.';
  324. *--t = (char) ('0' + (v % 10));
  325. v /= 10;
  326. --numDecPlaces;
  327. }
  328. if (n < 0)
  329. *--t = '-';
  330. len = (size_t) (end - t - 1);
  331. return t;
  332. }
  333. StackArrayStream strm (buffer);
  334. len = strm.writeDouble (n, numDecPlaces);
  335. wassert (len <= charsNeededForDouble);
  336. return buffer;
  337. }
  338. template <typename IntegerType>
  339. static CharPointer_UTF8 createFromInteger (const IntegerType number)
  340. {
  341. char buffer [charsNeededForInt];
  342. char* const end = buffer + numElementsInArray (buffer);
  343. char* const start = numberToString (end, number);
  344. return StringHolder::createFromFixedLength (start, (size_t) (end - start - 1));
  345. }
  346. static CharPointer_UTF8 createFromDouble (const double number, const int numberOfDecimalPlaces)
  347. {
  348. char buffer [charsNeededForDouble];
  349. size_t len;
  350. char* const start = doubleToString (buffer, numElementsInArray (buffer), (double) number, numberOfDecimalPlaces, len);
  351. return StringHolder::createFromFixedLength (start, len);
  352. }
  353. }
  354. //==============================================================================
  355. String::String (const int number) : text (NumberToStringConverters::createFromInteger (number)) {}
  356. String::String (const unsigned int number) : text (NumberToStringConverters::createFromInteger (number)) {}
  357. String::String (const short number) : text (NumberToStringConverters::createFromInteger ((int) number)) {}
  358. String::String (const unsigned short number) : text (NumberToStringConverters::createFromInteger ((unsigned int) number)) {}
  359. String::String (const int64 number) : text (NumberToStringConverters::createFromInteger (number)) {}
  360. String::String (const uint64 number) : text (NumberToStringConverters::createFromInteger (number)) {}
  361. String::String (const long number) : text (NumberToStringConverters::createFromInteger (number)) {}
  362. String::String (const unsigned long number) : text (NumberToStringConverters::createFromInteger (number)) {}
  363. String::String (const float number) : text (NumberToStringConverters::createFromDouble ((double) number, 0)) {}
  364. String::String (const double number) : text (NumberToStringConverters::createFromDouble (number, 0)) {}
  365. String::String (const float number, const int numberOfDecimalPlaces) : text (NumberToStringConverters::createFromDouble ((double) number, numberOfDecimalPlaces)) {}
  366. String::String (const double number, const int numberOfDecimalPlaces) : text (NumberToStringConverters::createFromDouble (number, numberOfDecimalPlaces)) {}
  367. //==============================================================================
  368. int String::length() const noexcept
  369. {
  370. return (int) text.length();
  371. }
  372. static size_t findByteOffsetOfEnd (CharPointer_UTF8 text) noexcept
  373. {
  374. return (size_t) (((char*) text.findTerminatingNull().getAddress()) - (char*) text.getAddress());
  375. }
  376. size_t String::getByteOffsetOfEnd() const noexcept
  377. {
  378. return findByteOffsetOfEnd (text);
  379. }
  380. water_uchar String::operator[] (int index) const noexcept
  381. {
  382. wassert (index == 0 || (index > 0 && index <= (int) text.lengthUpTo ((size_t) index + 1)));
  383. return text [index];
  384. }
  385. template <typename Type>
  386. struct HashGenerator
  387. {
  388. template <typename CharPointer>
  389. static Type calculate (CharPointer t) noexcept
  390. {
  391. Type result = Type();
  392. while (! t.isEmpty())
  393. result = ((Type) multiplier) * result + (Type) t.getAndAdvance();
  394. return result;
  395. }
  396. enum { multiplier = sizeof (Type) > 4 ? 101 : 31 };
  397. };
  398. int String::hashCode() const noexcept { return HashGenerator<int> ::calculate (text); }
  399. int64 String::hashCode64() const noexcept { return HashGenerator<int64> ::calculate (text); }
  400. size_t String::hash() const noexcept { return HashGenerator<size_t> ::calculate (text); }
  401. //==============================================================================
  402. bool operator== (const String& s1, const String& s2) noexcept { return s1.compare (s2) == 0; }
  403. bool operator!= (const String& s1, const String& s2) noexcept { return s1.compare (s2) != 0; }
  404. bool operator== (const String& s1, const char* s2) noexcept { return s1.compare (s2) == 0; }
  405. bool operator!= (const String& s1, const char* s2) noexcept { return s1.compare (s2) != 0; }
  406. bool operator== (const String& s1, StringRef s2) noexcept { return s1.getCharPointer().compare (s2.text) == 0; }
  407. bool operator!= (const String& s1, StringRef s2) noexcept { return s1.getCharPointer().compare (s2.text) != 0; }
  408. bool operator== (const String& s1, const CharPointer_UTF8 s2) noexcept { return s1.getCharPointer().compare (s2) == 0; }
  409. bool operator!= (const String& s1, const CharPointer_UTF8 s2) noexcept { return s1.getCharPointer().compare (s2) != 0; }
  410. bool operator> (const String& s1, const String& s2) noexcept { return s1.compare (s2) > 0; }
  411. bool operator< (const String& s1, const String& s2) noexcept { return s1.compare (s2) < 0; }
  412. bool operator>= (const String& s1, const String& s2) noexcept { return s1.compare (s2) >= 0; }
  413. bool operator<= (const String& s1, const String& s2) noexcept { return s1.compare (s2) <= 0; }
  414. bool String::equalsIgnoreCase (const char* const t) const noexcept
  415. {
  416. return t != nullptr ? text.compareIgnoreCase (CharPointer_UTF8 (t)) == 0
  417. : isEmpty();
  418. }
  419. bool String::equalsIgnoreCase (StringRef t) const noexcept
  420. {
  421. return text.compareIgnoreCase (t.text) == 0;
  422. }
  423. bool String::equalsIgnoreCase (const String& other) const noexcept
  424. {
  425. return text == other.text
  426. || text.compareIgnoreCase (other.text) == 0;
  427. }
  428. int String::compare (const String& other) const noexcept { return (text == other.text) ? 0 : text.compare (other.text); }
  429. int String::compare (const char* const other) const noexcept { return text.compare (CharPointer_UTF8 (other)); }
  430. int String::compareIgnoreCase (const String& other) const noexcept { return (text == other.text) ? 0 : text.compareIgnoreCase (other.text); }
  431. static int stringCompareRight (CharPointer_UTF8 s1, CharPointer_UTF8 s2) noexcept
  432. {
  433. for (int bias = 0;;)
  434. {
  435. const water_uchar c1 = s1.getAndAdvance();
  436. const bool isDigit1 = CharacterFunctions::isDigit (c1);
  437. const water_uchar c2 = s2.getAndAdvance();
  438. const bool isDigit2 = CharacterFunctions::isDigit (c2);
  439. if (! (isDigit1 || isDigit2)) return bias;
  440. if (! isDigit1) return -1;
  441. if (! isDigit2) return 1;
  442. if (c1 != c2 && bias == 0)
  443. bias = c1 < c2 ? -1 : 1;
  444. wassert (c1 != 0 && c2 != 0);
  445. }
  446. }
  447. static int stringCompareLeft (CharPointer_UTF8 s1, CharPointer_UTF8 s2) noexcept
  448. {
  449. for (;;)
  450. {
  451. const water_uchar c1 = s1.getAndAdvance();
  452. const bool isDigit1 = CharacterFunctions::isDigit (c1);
  453. const water_uchar c2 = s2.getAndAdvance();
  454. const bool isDigit2 = CharacterFunctions::isDigit (c2);
  455. if (! (isDigit1 || isDigit2)) return 0;
  456. if (! isDigit1) return -1;
  457. if (! isDigit2) return 1;
  458. if (c1 < c2) return -1;
  459. if (c1 > c2) return 1;
  460. }
  461. }
  462. static int naturalStringCompare (CharPointer_UTF8 s1, CharPointer_UTF8 s2, bool isCaseSensitive) noexcept
  463. {
  464. bool firstLoop = true;
  465. for (;;)
  466. {
  467. const bool hasSpace1 = s1.isWhitespace();
  468. const bool hasSpace2 = s2.isWhitespace();
  469. if ((! firstLoop) && (hasSpace1 ^ hasSpace2))
  470. return hasSpace2 ? 1 : -1;
  471. firstLoop = false;
  472. if (hasSpace1) s1 = s1.findEndOfWhitespace();
  473. if (hasSpace2) s2 = s2.findEndOfWhitespace();
  474. if (s1.isDigit() && s2.isDigit())
  475. {
  476. const int result = (*s1 == '0' || *s2 == '0') ? stringCompareLeft (s1, s2)
  477. : stringCompareRight (s1, s2);
  478. if (result != 0)
  479. return result;
  480. }
  481. water_uchar c1 = s1.getAndAdvance();
  482. water_uchar c2 = s2.getAndAdvance();
  483. if (c1 != c2 && ! isCaseSensitive)
  484. {
  485. c1 = CharacterFunctions::toUpperCase (c1);
  486. c2 = CharacterFunctions::toUpperCase (c2);
  487. }
  488. if (c1 == c2)
  489. {
  490. if (c1 == 0)
  491. return 0;
  492. }
  493. else
  494. {
  495. const bool isAlphaNum1 = CharacterFunctions::isLetterOrDigit (c1);
  496. const bool isAlphaNum2 = CharacterFunctions::isLetterOrDigit (c2);
  497. if (isAlphaNum2 && ! isAlphaNum1) return -1;
  498. if (isAlphaNum1 && ! isAlphaNum2) return 1;
  499. return c1 < c2 ? -1 : 1;
  500. }
  501. wassert (c1 != 0 && c2 != 0);
  502. }
  503. }
  504. int String::compareNatural (StringRef other, bool isCaseSensitive) const noexcept
  505. {
  506. return naturalStringCompare (getCharPointer(), other.text, isCaseSensitive);
  507. }
  508. //==============================================================================
  509. void String::append (const String& textToAppend, size_t maxCharsToTake)
  510. {
  511. appendCharPointer (this == &textToAppend ? String (textToAppend).text
  512. : textToAppend.text, maxCharsToTake);
  513. }
  514. void String::appendCharPointer (const CharPointer_UTF8 textToAppend)
  515. {
  516. appendCharPointer (textToAppend, textToAppend.findTerminatingNull());
  517. }
  518. void String::appendCharPointer (const CharPointer_UTF8 startOfTextToAppend,
  519. const CharPointer_UTF8 endOfTextToAppend)
  520. {
  521. wassert (startOfTextToAppend.getAddress() != nullptr && endOfTextToAppend.getAddress() != nullptr);
  522. const int extraBytesNeeded = getAddressDifference (endOfTextToAppend.getAddress(),
  523. startOfTextToAppend.getAddress());
  524. wassert (extraBytesNeeded >= 0);
  525. if (extraBytesNeeded > 0)
  526. {
  527. const size_t byteOffsetOfNull = getByteOffsetOfEnd();
  528. preallocateBytes (byteOffsetOfNull + (size_t) extraBytesNeeded);
  529. CharPointer_UTF8::CharType* const newStringStart = addBytesToPointer (text.getAddress(), (int) byteOffsetOfNull);
  530. memcpy (newStringStart, startOfTextToAppend.getAddress(), (size_t) extraBytesNeeded);
  531. CharPointer_UTF8 (addBytesToPointer (newStringStart, extraBytesNeeded)).writeNull();
  532. }
  533. }
  534. String& String::operator+= (const char* const t)
  535. {
  536. appendCharPointer (CharPointer_UTF8 (t)); // (using UTF8 here triggers a faster code-path than ascii)
  537. return *this;
  538. }
  539. String& String::operator+= (const String& other)
  540. {
  541. if (isEmpty())
  542. return operator= (other);
  543. if (this == &other)
  544. return operator+= (String (*this));
  545. appendCharPointer (other.text);
  546. return *this;
  547. }
  548. String& String::operator+= (StringRef other)
  549. {
  550. return operator+= (String (other));
  551. }
  552. String& String::operator+= (const char ch)
  553. {
  554. const char asString[] = { ch, 0 };
  555. return operator+= (asString);
  556. }
  557. String& String::operator+= (const water_uchar ch)
  558. {
  559. return operator+= (charToString(ch));
  560. }
  561. namespace StringHelpers
  562. {
  563. template <typename T>
  564. inline String& operationAddAssign (String& str, const T number)
  565. {
  566. char buffer [(sizeof(T) * 8) / 2];
  567. char* end = buffer + numElementsInArray (buffer);
  568. char* start = NumberToStringConverters::numberToString (end, number);
  569. str.appendCharPointer (CharPointer_UTF8 (start), CharPointer_UTF8 (end));
  570. return str;
  571. }
  572. }
  573. String& String::operator+= (const int number) { return StringHelpers::operationAddAssign<int> (*this, number); }
  574. String& String::operator+= (const int64 number) { return StringHelpers::operationAddAssign<int64> (*this, number); }
  575. String& String::operator+= (const uint64 number) { return StringHelpers::operationAddAssign<uint64> (*this, number); }
  576. //==============================================================================
  577. String operator+ (const char* const s1, const String& s2) { String s (s1); return s += s2; }
  578. String operator+ (const char s1, const String& s2) { return String::charToString ((water_uchar) (uint8) s1) + s2; }
  579. String operator+ (String s1, const String& s2) { return s1 += s2; }
  580. String operator+ (String s1, const char* const s2) { return s1 += s2; }
  581. String operator+ (String s1, const char s2) { return s1 += s2; }
  582. String operator+ (const water_uchar s1, const String& s2) { return String::charToString (s1) + s2; }
  583. String operator+ (String s1, const water_uchar s2) { return s1 += s2; }
  584. String& operator<< (String& s1, const water_uchar s2) { return s1 += s2; }
  585. String& operator<< (String& s1, const char s2) { return s1 += s2; }
  586. String& operator<< (String& s1, const char* const s2) { return s1 += s2; }
  587. String& operator<< (String& s1, const String& s2) { return s1 += s2; }
  588. String& operator<< (String& s1, StringRef s2) { return s1 += s2; }
  589. String& operator<< (String& s1, const int number) { return s1 += number; }
  590. String& operator<< (String& s1, const short number) { return s1 += (int) number; }
  591. String& operator<< (String& s1, const unsigned short number) { return s1 += (uint64) number; }
  592. String& operator<< (String& s1, const long number) { return s1 += String (number); }
  593. String& operator<< (String& s1, const unsigned long number) { return s1 += String (number); }
  594. String& operator<< (String& s1, const int64 number) { return s1 += String (number); }
  595. String& operator<< (String& s1, const uint64 number) { return s1 += String (number); }
  596. String& operator<< (String& s1, const float number) { return s1 += String (number); }
  597. String& operator<< (String& s1, const double number) { return s1 += String (number); }
  598. OutputStream& operator<< (OutputStream& stream, const String& text)
  599. {
  600. return operator<< (stream, StringRef (text));
  601. }
  602. OutputStream& operator<< (OutputStream& stream, StringRef text)
  603. {
  604. const size_t numBytes = CharPointer_UTF8::getBytesRequiredFor (text.text);
  605. stream.write (text.text.getAddress(), numBytes);
  606. return stream;
  607. }
  608. //==============================================================================
  609. int String::indexOfChar (const water_uchar character) const noexcept
  610. {
  611. return text.indexOf (character);
  612. }
  613. int String::indexOfChar (const int startIndex, const water_uchar character) const noexcept
  614. {
  615. CharPointer_UTF8 t (text);
  616. for (int i = 0; ! t.isEmpty(); ++i)
  617. {
  618. if (i >= startIndex)
  619. {
  620. if (t.getAndAdvance() == character)
  621. return i;
  622. }
  623. else
  624. {
  625. ++t;
  626. }
  627. }
  628. return -1;
  629. }
  630. int String::lastIndexOfChar (const water_uchar character) const noexcept
  631. {
  632. CharPointer_UTF8 t (text);
  633. int last = -1;
  634. for (int i = 0; ! t.isEmpty(); ++i)
  635. if (t.getAndAdvance() == character)
  636. last = i;
  637. return last;
  638. }
  639. int String::indexOfAnyOf (StringRef charactersToLookFor, const int startIndex, const bool ignoreCase) const noexcept
  640. {
  641. CharPointer_UTF8 t (text);
  642. for (int i = 0; ! t.isEmpty(); ++i)
  643. {
  644. if (i >= startIndex)
  645. {
  646. if (charactersToLookFor.text.indexOf (t.getAndAdvance(), ignoreCase) >= 0)
  647. return i;
  648. }
  649. else
  650. {
  651. ++t;
  652. }
  653. }
  654. return -1;
  655. }
  656. int String::indexOf (StringRef other) const noexcept
  657. {
  658. return other.isEmpty() ? 0 : text.indexOf (other.text);
  659. }
  660. int String::indexOfIgnoreCase (StringRef other) const noexcept
  661. {
  662. return other.isEmpty() ? 0 : CharacterFunctions::indexOfIgnoreCase (text, other.text);
  663. }
  664. int String::indexOf (const int startIndex, StringRef other) const noexcept
  665. {
  666. if (other.isEmpty())
  667. return -1;
  668. CharPointer_UTF8 t (text);
  669. for (int i = startIndex; --i >= 0;)
  670. {
  671. if (t.isEmpty())
  672. return -1;
  673. ++t;
  674. }
  675. int found = t.indexOf (other.text);
  676. if (found >= 0)
  677. found += startIndex;
  678. return found;
  679. }
  680. int String::indexOfIgnoreCase (const int startIndex, StringRef other) const noexcept
  681. {
  682. if (other.isEmpty())
  683. return -1;
  684. CharPointer_UTF8 t (text);
  685. for (int i = startIndex; --i >= 0;)
  686. {
  687. if (t.isEmpty())
  688. return -1;
  689. ++t;
  690. }
  691. int found = CharacterFunctions::indexOfIgnoreCase (t, other.text);
  692. if (found >= 0)
  693. found += startIndex;
  694. return found;
  695. }
  696. int String::lastIndexOf (StringRef other) const noexcept
  697. {
  698. if (other.isNotEmpty())
  699. {
  700. const int len = other.length();
  701. int i = length() - len;
  702. if (i >= 0)
  703. {
  704. for (CharPointer_UTF8 n (text + i); i >= 0; --i)
  705. {
  706. if (n.compareUpTo (other.text, len) == 0)
  707. return i;
  708. --n;
  709. }
  710. }
  711. }
  712. return -1;
  713. }
  714. int String::lastIndexOfIgnoreCase (StringRef other) const noexcept
  715. {
  716. if (other.isNotEmpty())
  717. {
  718. const int len = other.length();
  719. int i = length() - len;
  720. if (i >= 0)
  721. {
  722. for (CharPointer_UTF8 n (text + i); i >= 0; --i)
  723. {
  724. if (n.compareIgnoreCaseUpTo (other.text, len) == 0)
  725. return i;
  726. --n;
  727. }
  728. }
  729. }
  730. return -1;
  731. }
  732. int String::lastIndexOfAnyOf (StringRef charactersToLookFor, const bool ignoreCase) const noexcept
  733. {
  734. CharPointer_UTF8 t (text);
  735. int last = -1;
  736. for (int i = 0; ! t.isEmpty(); ++i)
  737. if (charactersToLookFor.text.indexOf (t.getAndAdvance(), ignoreCase) >= 0)
  738. last = i;
  739. return last;
  740. }
  741. bool String::contains (StringRef other) const noexcept
  742. {
  743. return indexOf (other) >= 0;
  744. }
  745. bool String::containsChar (const water_uchar character) const noexcept
  746. {
  747. return text.indexOf (character) >= 0;
  748. }
  749. bool String::containsIgnoreCase (StringRef t) const noexcept
  750. {
  751. return indexOfIgnoreCase (t) >= 0;
  752. }
  753. int String::indexOfWholeWord (StringRef word) const noexcept
  754. {
  755. if (word.isNotEmpty())
  756. {
  757. CharPointer_UTF8 t (text);
  758. const int wordLen = word.length();
  759. const int end = (int) t.length() - wordLen;
  760. for (int i = 0; i <= end; ++i)
  761. {
  762. if (t.compareUpTo (word.text, wordLen) == 0
  763. && (i == 0 || ! (t - 1).isLetterOrDigit())
  764. && ! (t + wordLen).isLetterOrDigit())
  765. return i;
  766. ++t;
  767. }
  768. }
  769. return -1;
  770. }
  771. int String::indexOfWholeWordIgnoreCase (StringRef word) const noexcept
  772. {
  773. if (word.isNotEmpty())
  774. {
  775. CharPointer_UTF8 t (text);
  776. const int wordLen = word.length();
  777. const int end = (int) t.length() - wordLen;
  778. for (int i = 0; i <= end; ++i)
  779. {
  780. if (t.compareIgnoreCaseUpTo (word.text, wordLen) == 0
  781. && (i == 0 || ! (t - 1).isLetterOrDigit())
  782. && ! (t + wordLen).isLetterOrDigit())
  783. return i;
  784. ++t;
  785. }
  786. }
  787. return -1;
  788. }
  789. bool String::containsWholeWord (StringRef wordToLookFor) const noexcept
  790. {
  791. return indexOfWholeWord (wordToLookFor) >= 0;
  792. }
  793. bool String::containsWholeWordIgnoreCase (StringRef wordToLookFor) const noexcept
  794. {
  795. return indexOfWholeWordIgnoreCase (wordToLookFor) >= 0;
  796. }
  797. //==============================================================================
  798. template <typename CharPointer>
  799. struct WildCardMatcher
  800. {
  801. static bool matches (CharPointer wildcard, CharPointer test, const bool ignoreCase) noexcept
  802. {
  803. for (;;)
  804. {
  805. const water_uchar wc = wildcard.getAndAdvance();
  806. if (wc == '*')
  807. return wildcard.isEmpty() || matchesAnywhere (wildcard, test, ignoreCase);
  808. if (! characterMatches (wc, test.getAndAdvance(), ignoreCase))
  809. return false;
  810. if (wc == 0)
  811. return true;
  812. }
  813. }
  814. static bool characterMatches (const water_uchar wc, const water_uchar tc, const bool ignoreCase) noexcept
  815. {
  816. return (wc == tc) || (wc == '?' && tc != 0)
  817. || (ignoreCase && CharacterFunctions::toLowerCase (wc) == CharacterFunctions::toLowerCase (tc));
  818. }
  819. static bool matchesAnywhere (const CharPointer wildcard, CharPointer test, const bool ignoreCase) noexcept
  820. {
  821. for (; ! test.isEmpty(); ++test)
  822. if (matches (wildcard, test, ignoreCase))
  823. return true;
  824. return false;
  825. }
  826. };
  827. bool String::matchesWildcard (StringRef wildcard, const bool ignoreCase) const noexcept
  828. {
  829. return WildCardMatcher<CharPointer_UTF8>::matches (wildcard.text, text, ignoreCase);
  830. }
  831. //==============================================================================
  832. String String::repeatedString (StringRef stringToRepeat, int numberOfTimesToRepeat)
  833. {
  834. if (numberOfTimesToRepeat <= 0)
  835. return String();
  836. String result (PreallocationBytes (findByteOffsetOfEnd (stringToRepeat) * (size_t) numberOfTimesToRepeat));
  837. CharPointer_UTF8 n (result.text);
  838. while (--numberOfTimesToRepeat >= 0)
  839. n.writeAll (stringToRepeat.text);
  840. return result;
  841. }
  842. String String::paddedLeft (const water_uchar padCharacter, int minimumLength) const
  843. {
  844. wassert (padCharacter != 0);
  845. int extraChars = minimumLength;
  846. CharPointer_UTF8 end (text);
  847. while (! end.isEmpty())
  848. {
  849. --extraChars;
  850. ++end;
  851. }
  852. if (extraChars <= 0 || padCharacter == 0)
  853. return *this;
  854. const size_t currentByteSize = (size_t) (((char*) end.getAddress()) - (char*) text.getAddress());
  855. String result (PreallocationBytes (currentByteSize + (size_t) extraChars * CharPointer_UTF8::getBytesRequiredFor (padCharacter)));
  856. CharPointer_UTF8 n (result.text);
  857. while (--extraChars >= 0)
  858. n.write (padCharacter);
  859. n.writeAll (text);
  860. return result;
  861. }
  862. String String::paddedRight (const water_uchar padCharacter, int minimumLength) const
  863. {
  864. CARLA_SAFE_ASSERT_RETURN (padCharacter != 0, *this);
  865. int extraChars = minimumLength;
  866. CharPointer_UTF8 end (text);
  867. while (! end.isEmpty())
  868. {
  869. --extraChars;
  870. ++end;
  871. }
  872. if (extraChars <= 0 || padCharacter == 0)
  873. return *this;
  874. const size_t currentByteSize = (size_t) (((char*) end.getAddress()) - (char*) text.getAddress());
  875. String result (PreallocationBytes (currentByteSize + (size_t) extraChars * CharPointer_UTF8::getBytesRequiredFor (padCharacter)));
  876. CharPointer_UTF8 n (result.text);
  877. n.writeAll (text);
  878. while (--extraChars >= 0)
  879. n.write (padCharacter);
  880. n.writeNull();
  881. return result;
  882. }
  883. //==============================================================================
  884. String String::replaceSection (int index, int numCharsToReplace, StringRef stringToInsert) const
  885. {
  886. if (index < 0)
  887. {
  888. // a negative index to replace from?
  889. wassertfalse;
  890. index = 0;
  891. }
  892. if (numCharsToReplace < 0)
  893. {
  894. // replacing a negative number of characters?
  895. numCharsToReplace = 0;
  896. wassertfalse;
  897. }
  898. CharPointer_UTF8 insertPoint (text);
  899. for (int i = 0; i < index; ++i)
  900. {
  901. if (insertPoint.isEmpty())
  902. {
  903. // replacing beyond the end of the string?
  904. wassertfalse;
  905. return *this + stringToInsert;
  906. }
  907. ++insertPoint;
  908. }
  909. CharPointer_UTF8 startOfRemainder (insertPoint);
  910. for (int i = 0; i < numCharsToReplace && ! startOfRemainder.isEmpty(); ++i)
  911. ++startOfRemainder;
  912. if (insertPoint == text && startOfRemainder.isEmpty())
  913. return stringToInsert.text;
  914. const size_t initialBytes = (size_t) (((char*) insertPoint.getAddress()) - (char*) text.getAddress());
  915. const size_t newStringBytes = findByteOffsetOfEnd (stringToInsert);
  916. const size_t remainderBytes = (size_t) (((char*) startOfRemainder.findTerminatingNull().getAddress()) - (char*) startOfRemainder.getAddress());
  917. const size_t newTotalBytes = initialBytes + newStringBytes + remainderBytes;
  918. if (newTotalBytes <= 0)
  919. return String();
  920. String result (PreallocationBytes ((size_t) newTotalBytes));
  921. char* dest = (char*) result.text.getAddress();
  922. memcpy (dest, text.getAddress(), initialBytes);
  923. dest += initialBytes;
  924. memcpy (dest, stringToInsert.text.getAddress(), newStringBytes);
  925. dest += newStringBytes;
  926. memcpy (dest, startOfRemainder.getAddress(), remainderBytes);
  927. dest += remainderBytes;
  928. CharPointer_UTF8 ((CharPointer_UTF8::CharType*) dest).writeNull();
  929. return result;
  930. }
  931. String String::replace (StringRef stringToReplace, StringRef stringToInsert, const bool ignoreCase) const
  932. {
  933. const int stringToReplaceLen = stringToReplace.length();
  934. const int stringToInsertLen = stringToInsert.length();
  935. int i = 0;
  936. String result (*this);
  937. while ((i = (ignoreCase ? result.indexOfIgnoreCase (i, stringToReplace)
  938. : result.indexOf (i, stringToReplace))) >= 0)
  939. {
  940. result = result.replaceSection (i, stringToReplaceLen, stringToInsert);
  941. i += stringToInsertLen;
  942. }
  943. return result;
  944. }
  945. class StringCreationHelper
  946. {
  947. public:
  948. StringCreationHelper (const size_t initialBytes)
  949. : source (nullptr), dest (nullptr), allocatedBytes (initialBytes), bytesWritten (0)
  950. {
  951. result.preallocateBytes (allocatedBytes);
  952. dest = result.getCharPointer();
  953. }
  954. StringCreationHelper (const CharPointer_UTF8 s)
  955. : source (s), dest (nullptr), allocatedBytes (StringHolder::getAllocatedNumBytes (s)), bytesWritten (0)
  956. {
  957. result.preallocateBytes (allocatedBytes);
  958. dest = result.getCharPointer();
  959. }
  960. void write (water_uchar c)
  961. {
  962. bytesWritten += CharPointer_UTF8::getBytesRequiredFor (c);
  963. if (bytesWritten > allocatedBytes)
  964. {
  965. allocatedBytes += jmax ((size_t) 8, allocatedBytes / 16);
  966. const size_t destOffset = (size_t) (((char*) dest.getAddress()) - (char*) result.getCharPointer().getAddress());
  967. result.preallocateBytes (allocatedBytes);
  968. dest = addBytesToPointer (result.getCharPointer().getAddress(), (int) destOffset);
  969. }
  970. dest.write (c);
  971. }
  972. String result;
  973. CharPointer_UTF8 source;
  974. private:
  975. CharPointer_UTF8 dest;
  976. size_t allocatedBytes, bytesWritten;
  977. };
  978. String String::replaceCharacter (const water_uchar charToReplace, const water_uchar charToInsert) const
  979. {
  980. if (! containsChar (charToReplace))
  981. return *this;
  982. StringCreationHelper builder (text);
  983. for (;;)
  984. {
  985. water_uchar c = builder.source.getAndAdvance();
  986. if (c == charToReplace)
  987. c = charToInsert;
  988. builder.write (c);
  989. if (c == 0)
  990. break;
  991. }
  992. return builder.result;
  993. }
  994. String String::replaceCharacters (StringRef charactersToReplace, StringRef charactersToInsertInstead) const
  995. {
  996. // Each character in the first string must have a matching one in the
  997. // second, so the two strings must be the same length.
  998. wassert (charactersToReplace.length() == charactersToInsertInstead.length());
  999. StringCreationHelper builder (text);
  1000. for (;;)
  1001. {
  1002. water_uchar c = builder.source.getAndAdvance();
  1003. const int index = charactersToReplace.text.indexOf (c);
  1004. if (index >= 0)
  1005. c = charactersToInsertInstead [index];
  1006. builder.write (c);
  1007. if (c == 0)
  1008. break;
  1009. }
  1010. return builder.result;
  1011. }
  1012. //==============================================================================
  1013. bool String::startsWith (StringRef other) const noexcept
  1014. {
  1015. return text.compareUpTo (other.text, other.length()) == 0;
  1016. }
  1017. bool String::startsWithIgnoreCase (StringRef other) const noexcept
  1018. {
  1019. return text.compareIgnoreCaseUpTo (other.text, other.length()) == 0;
  1020. }
  1021. bool String::startsWithChar (const water_uchar character) const noexcept
  1022. {
  1023. // strings can't contain a null character!
  1024. CARLA_SAFE_ASSERT_RETURN (character != 0, false);
  1025. return *text == character;
  1026. }
  1027. bool String::endsWithChar (const water_uchar character) const noexcept
  1028. {
  1029. // strings can't contain a null character!
  1030. CARLA_SAFE_ASSERT_RETURN (character != 0, false);
  1031. if (text.isEmpty())
  1032. return false;
  1033. CharPointer_UTF8 t (text.findTerminatingNull());
  1034. return *--t == character;
  1035. }
  1036. bool String::endsWith (StringRef other) const noexcept
  1037. {
  1038. CharPointer_UTF8 end (text.findTerminatingNull());
  1039. CharPointer_UTF8 otherEnd (other.text.findTerminatingNull());
  1040. while (end > text && otherEnd > other.text)
  1041. {
  1042. --end;
  1043. --otherEnd;
  1044. if (*end != *otherEnd)
  1045. return false;
  1046. }
  1047. return otherEnd == other.text;
  1048. }
  1049. bool String::endsWithIgnoreCase (StringRef other) const noexcept
  1050. {
  1051. CharPointer_UTF8 end (text.findTerminatingNull());
  1052. CharPointer_UTF8 otherEnd (other.text.findTerminatingNull());
  1053. while (end > text && otherEnd > other.text)
  1054. {
  1055. --end;
  1056. --otherEnd;
  1057. if (end.toLowerCase() != otherEnd.toLowerCase())
  1058. return false;
  1059. }
  1060. return otherEnd == other.text;
  1061. }
  1062. //==============================================================================
  1063. String String::toUpperCase() const
  1064. {
  1065. StringCreationHelper builder (text);
  1066. for (;;)
  1067. {
  1068. const water_uchar c = builder.source.toUpperCase();
  1069. builder.write (c);
  1070. if (c == 0)
  1071. break;
  1072. ++(builder.source);
  1073. }
  1074. return builder.result;
  1075. }
  1076. String String::toLowerCase() const
  1077. {
  1078. StringCreationHelper builder (text);
  1079. for (;;)
  1080. {
  1081. const water_uchar c = builder.source.toLowerCase();
  1082. builder.write (c);
  1083. if (c == 0)
  1084. break;
  1085. ++(builder.source);
  1086. }
  1087. return builder.result;
  1088. }
  1089. //==============================================================================
  1090. water_uchar String::getLastCharacter() const noexcept
  1091. {
  1092. return isEmpty() ? water_uchar() : text [length() - 1];
  1093. }
  1094. String String::substring (int start, const int end) const
  1095. {
  1096. if (start < 0)
  1097. start = 0;
  1098. if (end <= start)
  1099. return String();
  1100. int i = 0;
  1101. CharPointer_UTF8 t1 (text);
  1102. while (i < start)
  1103. {
  1104. if (t1.isEmpty())
  1105. return String();
  1106. ++i;
  1107. ++t1;
  1108. }
  1109. CharPointer_UTF8 t2 (t1);
  1110. while (i < end)
  1111. {
  1112. if (t2.isEmpty())
  1113. {
  1114. if (start == 0)
  1115. return *this;
  1116. break;
  1117. }
  1118. ++i;
  1119. ++t2;
  1120. }
  1121. return String (t1, t2);
  1122. }
  1123. String String::substring (int start) const
  1124. {
  1125. if (start <= 0)
  1126. return *this;
  1127. CharPointer_UTF8 t (text);
  1128. while (--start >= 0)
  1129. {
  1130. if (t.isEmpty())
  1131. return String();
  1132. ++t;
  1133. }
  1134. return String (t);
  1135. }
  1136. String String::dropLastCharacters (const int numberToDrop) const
  1137. {
  1138. return String (text, (size_t) jmax (0, length() - numberToDrop));
  1139. }
  1140. String String::getLastCharacters (const int numCharacters) const
  1141. {
  1142. return String (text + jmax (0, length() - jmax (0, numCharacters)));
  1143. }
  1144. String String::fromFirstOccurrenceOf (StringRef sub,
  1145. const bool includeSubString,
  1146. const bool ignoreCase) const
  1147. {
  1148. const int i = ignoreCase ? indexOfIgnoreCase (sub)
  1149. : indexOf (sub);
  1150. if (i < 0)
  1151. return String();
  1152. return substring (includeSubString ? i : i + sub.length());
  1153. }
  1154. String String::fromLastOccurrenceOf (StringRef sub,
  1155. const bool includeSubString,
  1156. const bool ignoreCase) const
  1157. {
  1158. const int i = ignoreCase ? lastIndexOfIgnoreCase (sub)
  1159. : lastIndexOf (sub);
  1160. if (i < 0)
  1161. return *this;
  1162. return substring (includeSubString ? i : i + sub.length());
  1163. }
  1164. String String::upToFirstOccurrenceOf (StringRef sub,
  1165. const bool includeSubString,
  1166. const bool ignoreCase) const
  1167. {
  1168. const int i = ignoreCase ? indexOfIgnoreCase (sub)
  1169. : indexOf (sub);
  1170. if (i < 0)
  1171. return *this;
  1172. return substring (0, includeSubString ? i + sub.length() : i);
  1173. }
  1174. String String::upToLastOccurrenceOf (StringRef sub,
  1175. const bool includeSubString,
  1176. const bool ignoreCase) const
  1177. {
  1178. const int i = ignoreCase ? lastIndexOfIgnoreCase (sub)
  1179. : lastIndexOf (sub);
  1180. if (i < 0)
  1181. return *this;
  1182. return substring (0, includeSubString ? i + sub.length() : i);
  1183. }
  1184. bool String::isQuotedString() const
  1185. {
  1186. const water_uchar trimmedStart = trimStart()[0];
  1187. return trimmedStart == '"'
  1188. || trimmedStart == '\'';
  1189. }
  1190. String String::unquoted() const
  1191. {
  1192. const int len = length();
  1193. if (len == 0)
  1194. return String();
  1195. const water_uchar lastChar = text [len - 1];
  1196. const int dropAtStart = (*text == '"' || *text == '\'') ? 1 : 0;
  1197. const int dropAtEnd = (lastChar == '"' || lastChar == '\'') ? 1 : 0;
  1198. return substring (dropAtStart, len - dropAtEnd);
  1199. }
  1200. String String::quoted (water_uchar quoteCharacter) const
  1201. {
  1202. if (isEmpty())
  1203. return charToString (quoteCharacter) + quoteCharacter;
  1204. String t (*this);
  1205. if (! t.startsWithChar (quoteCharacter))
  1206. t = charToString (quoteCharacter) + t;
  1207. if (! t.endsWithChar (quoteCharacter))
  1208. t += quoteCharacter;
  1209. return t;
  1210. }
  1211. //==============================================================================
  1212. static CharPointer_UTF8 findTrimmedEnd (const CharPointer_UTF8 start, CharPointer_UTF8 end)
  1213. {
  1214. while (end > start)
  1215. {
  1216. if (! (--end).isWhitespace())
  1217. {
  1218. ++end;
  1219. break;
  1220. }
  1221. }
  1222. return end;
  1223. }
  1224. String String::trim() const
  1225. {
  1226. if (isNotEmpty())
  1227. {
  1228. CharPointer_UTF8 start (text.findEndOfWhitespace());
  1229. const CharPointer_UTF8 end (start.findTerminatingNull());
  1230. CharPointer_UTF8 trimmedEnd (findTrimmedEnd (start, end));
  1231. if (trimmedEnd <= start)
  1232. return String();
  1233. if (text < start || trimmedEnd < end)
  1234. return String (start, trimmedEnd);
  1235. }
  1236. return *this;
  1237. }
  1238. String String::trimStart() const
  1239. {
  1240. if (isNotEmpty())
  1241. {
  1242. const CharPointer_UTF8 t (text.findEndOfWhitespace());
  1243. if (t != text)
  1244. return String (t);
  1245. }
  1246. return *this;
  1247. }
  1248. String String::trimEnd() const
  1249. {
  1250. if (isNotEmpty())
  1251. {
  1252. const CharPointer_UTF8 end (text.findTerminatingNull());
  1253. CharPointer_UTF8 trimmedEnd (findTrimmedEnd (text, end));
  1254. if (trimmedEnd < end)
  1255. return String (text, trimmedEnd);
  1256. }
  1257. return *this;
  1258. }
  1259. String String::trimCharactersAtStart (StringRef charactersToTrim) const
  1260. {
  1261. CharPointer_UTF8 t (text);
  1262. while (charactersToTrim.text.indexOf (*t) >= 0)
  1263. ++t;
  1264. return t == text ? *this : String (t);
  1265. }
  1266. String String::trimCharactersAtEnd (StringRef charactersToTrim) const
  1267. {
  1268. if (isNotEmpty())
  1269. {
  1270. const CharPointer_UTF8 end (text.findTerminatingNull());
  1271. CharPointer_UTF8 trimmedEnd (end);
  1272. while (trimmedEnd > text)
  1273. {
  1274. if (charactersToTrim.text.indexOf (*--trimmedEnd) < 0)
  1275. {
  1276. ++trimmedEnd;
  1277. break;
  1278. }
  1279. }
  1280. if (trimmedEnd < end)
  1281. return String (text, trimmedEnd);
  1282. }
  1283. return *this;
  1284. }
  1285. //==============================================================================
  1286. String String::retainCharacters (StringRef charactersToRetain) const
  1287. {
  1288. if (isEmpty())
  1289. return String();
  1290. StringCreationHelper builder (text);
  1291. for (;;)
  1292. {
  1293. water_uchar c = builder.source.getAndAdvance();
  1294. if (charactersToRetain.text.indexOf (c) >= 0)
  1295. builder.write (c);
  1296. if (c == 0)
  1297. break;
  1298. }
  1299. builder.write (0);
  1300. return builder.result;
  1301. }
  1302. String String::removeCharacters (StringRef charactersToRemove) const
  1303. {
  1304. if (isEmpty())
  1305. return String();
  1306. StringCreationHelper builder (text);
  1307. for (;;)
  1308. {
  1309. water_uchar c = builder.source.getAndAdvance();
  1310. if (charactersToRemove.text.indexOf (c) < 0)
  1311. builder.write (c);
  1312. if (c == 0)
  1313. break;
  1314. }
  1315. return builder.result;
  1316. }
  1317. String String::initialSectionContainingOnly (StringRef permittedCharacters) const
  1318. {
  1319. for (CharPointer_UTF8 t (text); ! t.isEmpty(); ++t)
  1320. if (permittedCharacters.text.indexOf (*t) < 0)
  1321. return String (text, t);
  1322. return *this;
  1323. }
  1324. String String::initialSectionNotContaining (StringRef charactersToStopAt) const
  1325. {
  1326. for (CharPointer_UTF8 t (text); ! t.isEmpty(); ++t)
  1327. if (charactersToStopAt.text.indexOf (*t) >= 0)
  1328. return String (text, t);
  1329. return *this;
  1330. }
  1331. bool String::containsOnly (StringRef chars) const noexcept
  1332. {
  1333. for (CharPointer_UTF8 t (text); ! t.isEmpty();)
  1334. if (chars.text.indexOf (t.getAndAdvance()) < 0)
  1335. return false;
  1336. return true;
  1337. }
  1338. bool String::containsAnyOf (StringRef chars) const noexcept
  1339. {
  1340. for (CharPointer_UTF8 t (text); ! t.isEmpty();)
  1341. if (chars.text.indexOf (t.getAndAdvance()) >= 0)
  1342. return true;
  1343. return false;
  1344. }
  1345. bool String::containsNonWhitespaceChars() const noexcept
  1346. {
  1347. for (CharPointer_UTF8 t (text); ! t.isEmpty(); ++t)
  1348. if (! t.isWhitespace())
  1349. return true;
  1350. return false;
  1351. }
  1352. //=====================================================================================================================
  1353. static String getStringFromWindows1252Codepage (const char* data, size_t num)
  1354. {
  1355. HeapBlock<char> unicode;
  1356. CARLA_SAFE_ASSERT_RETURN(unicode.malloc(num + 1), String());
  1357. for (size_t i = 0; i < num; ++i)
  1358. unicode[i] = CharacterFunctions::getUnicodeCharFromWindows1252Codepage ((uint8) data[i]);
  1359. unicode[num] = 0;
  1360. return CharPointer_UTF8 (unicode);
  1361. }
  1362. String String::createStringFromData (const void* const unknownData, int size)
  1363. {
  1364. const uint8* const data = static_cast<const uint8*> (unknownData);
  1365. if (size <= 0 || data == nullptr)
  1366. return String();
  1367. if (size == 1)
  1368. return charToString ((water_uchar) data[0]);
  1369. const char* start = (const char*) data;
  1370. if (size >= 3 && CharPointer_UTF8::isByteOrderMark (data))
  1371. {
  1372. start += 3;
  1373. size -= 3;
  1374. }
  1375. if (CharPointer_UTF8::isValidString (start, size))
  1376. return String (CharPointer_UTF8 (start),
  1377. CharPointer_UTF8 (start + size));
  1378. return getStringFromWindows1252Codepage (start, (size_t) size);
  1379. }
  1380. // Note! The format parameter here MUST NOT be a reference, otherwise MS's va_start macro fails to work (but still compiles).
  1381. String String::formatted (const String pf, ... )
  1382. {
  1383. size_t bufferSize = 256;
  1384. HeapBlock<char> temp;
  1385. CARLA_SAFE_ASSERT_RETURN(temp.malloc(bufferSize), String());
  1386. for (;;)
  1387. {
  1388. va_list args;
  1389. va_start (args, pf);
  1390. // FIXME - needed?
  1391. temp.clear (bufferSize);
  1392. const int num = vsnprintf (temp.getData(), bufferSize - 1, pf.toRawUTF8(), args);
  1393. va_end (args);
  1394. if (num > 0)
  1395. return String (temp);
  1396. bufferSize += 256;
  1397. if (num == 0 || bufferSize > 65536) // the upper limit is a sanity check to avoid situations where vsnprintf repeatedly
  1398. break; // returns -1 because of an error rather than because it needs more space.
  1399. temp.realloc (bufferSize);
  1400. }
  1401. return String();
  1402. }
  1403. //==============================================================================
  1404. int String::getIntValue() const noexcept { return text.getIntValue32(); }
  1405. int64 String::getLargeIntValue() const noexcept { return text.getIntValue64(); }
  1406. float String::getFloatValue() const noexcept { return (float) getDoubleValue(); }
  1407. double String::getDoubleValue() const noexcept { return text.getDoubleValue(); }
  1408. int String::getTrailingIntValue() const noexcept
  1409. {
  1410. int n = 0;
  1411. int mult = 1;
  1412. CharPointer_UTF8 t (text.findTerminatingNull());
  1413. while (--t >= text)
  1414. {
  1415. if (! t.isDigit())
  1416. {
  1417. if (*t == '-')
  1418. n = -n;
  1419. break;
  1420. }
  1421. n += mult * (*t - '0');
  1422. mult *= 10;
  1423. }
  1424. return n;
  1425. }
  1426. static const char hexDigits[] = "0123456789abcdef";
  1427. template <typename Type>
  1428. static String hexToString (Type v)
  1429. {
  1430. CharPointer_UTF8::CharType buffer[32];
  1431. CharPointer_UTF8::CharType* const end = buffer + numElementsInArray (buffer) - 1;
  1432. CharPointer_UTF8::CharType* t = end;
  1433. *t = 0;
  1434. do
  1435. {
  1436. *--t = hexDigits [(int) (v & 15)];
  1437. v >>= 4;
  1438. } while (v != 0);
  1439. return String (CharPointer_UTF8 (t),
  1440. CharPointer_UTF8 (end));
  1441. }
  1442. String String::toHexString (int number) { return hexToString ((unsigned int) number); }
  1443. String String::toHexString (int64 number) { return hexToString ((uint64) number); }
  1444. String String::toHexString (short number) { return toHexString ((int) (unsigned short) number); }
  1445. String String::toHexString (const void* const d, const int size, const int groupSize)
  1446. {
  1447. if (size <= 0)
  1448. return String();
  1449. int numChars = (size * 2) + 2;
  1450. if (groupSize > 0)
  1451. numChars += size / groupSize;
  1452. String s (PreallocationBytes (sizeof (CharPointer_UTF8::CharType) * (size_t) numChars));
  1453. const unsigned char* data = static_cast<const unsigned char*> (d);
  1454. CharPointer_UTF8 dest (s.text);
  1455. for (int i = 0; i < size; ++i)
  1456. {
  1457. const unsigned char nextByte = *data++;
  1458. dest.write ((water_uchar) hexDigits [nextByte >> 4]);
  1459. dest.write ((water_uchar) hexDigits [nextByte & 0xf]);
  1460. if (groupSize > 0 && (i % groupSize) == (groupSize - 1) && i < (size - 1))
  1461. dest.write ((water_uchar) ' ');
  1462. }
  1463. dest.writeNull();
  1464. return s;
  1465. }
  1466. int String::getHexValue32() const noexcept { return CharacterFunctions::HexParser<int> ::parse (text); }
  1467. int64 String::getHexValue64() const noexcept { return CharacterFunctions::HexParser<int64>::parse (text); }
  1468. //==============================================================================
  1469. static const water_uchar emptyChar = 0;
  1470. template <class CharPointerType_Src, class CharPointerType_Dest>
  1471. struct StringEncodingConverter
  1472. {
  1473. static CharPointerType_Dest convert (const String& s)
  1474. {
  1475. String& source = const_cast<String&> (s);
  1476. typedef typename CharPointerType_Dest::CharType DestChar;
  1477. if (source.isEmpty())
  1478. return CharPointerType_Dest (reinterpret_cast<const DestChar*> (&emptyChar));
  1479. CharPointerType_Src text (source.getCharPointer());
  1480. const size_t extraBytesNeeded = CharPointerType_Dest::getBytesRequiredFor (text) + sizeof (typename CharPointerType_Dest::CharType);
  1481. const size_t endOffset = (text.sizeInBytes() + 3) & ~3u; // the new string must be word-aligned or many Windows
  1482. // functions will fail to read it correctly!
  1483. source.preallocateBytes (endOffset + extraBytesNeeded);
  1484. text = source.getCharPointer();
  1485. void* const newSpace = addBytesToPointer (text.getAddress(), (int) endOffset);
  1486. const CharPointerType_Dest extraSpace (static_cast<DestChar*> (newSpace));
  1487. #ifdef DEBUG // (This just avoids spurious warnings from valgrind about the uninitialised bytes at the end of the buffer..)
  1488. const size_t bytesToClear = (size_t) jmin ((int) extraBytesNeeded, 4);
  1489. zeromem (addBytesToPointer (newSpace, extraBytesNeeded - bytesToClear), bytesToClear);
  1490. #endif
  1491. CharPointerType_Dest (extraSpace).writeAll (text);
  1492. return extraSpace;
  1493. }
  1494. };
  1495. template <>
  1496. struct StringEncodingConverter<CharPointer_UTF8, CharPointer_UTF8>
  1497. {
  1498. static CharPointer_UTF8 convert (const String& source) noexcept { return CharPointer_UTF8 ((CharPointer_UTF8::CharType*) source.getCharPointer().getAddress()); }
  1499. };
  1500. CharPointer_UTF8 String::toUTF8() const { return text; }
  1501. #ifdef CARLA_OS_WIN
  1502. std::wstring String::toUTF16() const
  1503. {
  1504. if (isEmpty())
  1505. return L"";
  1506. const int len = MultiByteToWideChar (CP_UTF8, 0, toUTF8(), length() + 1, nullptr, 0);
  1507. CARLA_SAFE_ASSERT_RETURN(len > 0, L"");
  1508. std::wstring ret;
  1509. ret.resize(len);
  1510. MultiByteToWideChar (CP_UTF8, 0, toUTF8(), length(), &ret[0], len);
  1511. return ret;
  1512. }
  1513. #endif
  1514. const char* String::toRawUTF8() const
  1515. {
  1516. return toUTF8().getAddress();
  1517. }
  1518. std::string String::toStdString() const
  1519. {
  1520. return std::string (toRawUTF8());
  1521. }
  1522. //==============================================================================
  1523. template <class CharPointerType_Src, class CharPointerType_Dest>
  1524. struct StringCopier
  1525. {
  1526. static size_t copyToBuffer (const CharPointerType_Src source, typename CharPointerType_Dest::CharType* const buffer, const size_t maxBufferSizeBytes)
  1527. {
  1528. wassert (((ssize_t) maxBufferSizeBytes) >= 0); // keep this value positive!
  1529. if (buffer == nullptr)
  1530. return CharPointerType_Dest::getBytesRequiredFor (source) + sizeof (typename CharPointerType_Dest::CharType);
  1531. return CharPointerType_Dest (buffer).writeWithDestByteLimit (source, maxBufferSizeBytes);
  1532. }
  1533. };
  1534. size_t String::copyToUTF8 (CharPointer_UTF8::CharType* const buffer, size_t maxBufferSizeBytes) const noexcept
  1535. {
  1536. return StringCopier<CharPointer_UTF8, CharPointer_UTF8>::copyToBuffer (text, buffer, maxBufferSizeBytes);
  1537. }
  1538. //==============================================================================
  1539. size_t String::getNumBytesAsUTF8() const noexcept
  1540. {
  1541. return CharPointer_UTF8::getBytesRequiredFor (text);
  1542. }
  1543. String String::fromUTF8 (const char* const buffer, int bufferSizeBytes)
  1544. {
  1545. if (buffer != nullptr)
  1546. {
  1547. if (bufferSizeBytes < 0)
  1548. return String (CharPointer_UTF8 (buffer));
  1549. if (bufferSizeBytes > 0)
  1550. {
  1551. wassert (CharPointer_UTF8::isValidString (buffer, bufferSizeBytes));
  1552. return String (CharPointer_UTF8 (buffer), CharPointer_UTF8 (buffer + bufferSizeBytes));
  1553. }
  1554. }
  1555. return String();
  1556. }
  1557. #ifdef CARLA_OS_MAC
  1558. String String::convertToPrecomposedUnicode() const
  1559. {
  1560. #if 0
  1561. UnicodeMapping map;
  1562. map.unicodeEncoding = CreateTextEncoding (kTextEncodingUnicodeDefault,
  1563. kUnicodeNoSubset,
  1564. kTextEncodingDefaultFormat);
  1565. map.otherEncoding = CreateTextEncoding (kTextEncodingUnicodeDefault,
  1566. kUnicodeCanonicalCompVariant,
  1567. kTextEncodingDefaultFormat);
  1568. map.mappingVersion = kUnicodeUseLatestMapping;
  1569. UnicodeToTextInfo conversionInfo = 0;
  1570. String result;
  1571. if (CreateUnicodeToTextInfo (&map, &conversionInfo) == noErr)
  1572. {
  1573. const size_t bytesNeeded = CharPointer_UTF16::getBytesRequiredFor (getCharPointer());
  1574. HeapBlock<char> tempOut;
  1575. tempOut.calloc (bytesNeeded + 4);
  1576. ByteCount bytesRead = 0;
  1577. ByteCount outputBufferSize = 0;
  1578. if (ConvertFromUnicodeToText (conversionInfo,
  1579. bytesNeeded, (ConstUniCharArrayPtr) toUTF16().getAddress(),
  1580. kUnicodeDefaultDirectionMask,
  1581. 0, 0, 0, 0,
  1582. bytesNeeded, &bytesRead,
  1583. &outputBufferSize, tempOut) == noErr)
  1584. {
  1585. result = String (CharPointer_UTF16 ((CharPointer_UTF16::CharType*) tempOut.getData()));
  1586. }
  1587. DisposeUnicodeToTextInfo (&conversionInfo);
  1588. }
  1589. return result;
  1590. #else
  1591. return *this;
  1592. #endif
  1593. }
  1594. #endif
  1595. //==============================================================================
  1596. StringRef::StringRef() noexcept : text ((const CharPointer_UTF8::CharType*) "\0\0\0")
  1597. {
  1598. }
  1599. StringRef::StringRef (const char* stringLiteral) noexcept
  1600. : text (stringLiteral)
  1601. {
  1602. wassert (stringLiteral != nullptr); // This must be a valid string literal, not a null pointer!!
  1603. /* If you get an assertion here, then you're trying to create a string from 8-bit data
  1604. that contains values greater than 127. These can NOT be correctly converted to unicode
  1605. because there's no way for the String class to know what encoding was used to
  1606. create them. The source data could be UTF-8, ASCII or one of many local code-pages.
  1607. To get around this problem, you must be more explicit when you pass an ambiguous 8-bit
  1608. string to the StringRef class - so for example if your source data is actually UTF-8,
  1609. you'd call StringRef (CharPointer_UTF8 ("my utf8 string..")), and it would be able to
  1610. correctly convert the multi-byte characters to unicode. It's *highly* recommended that
  1611. you use UTF-8 with escape characters in your source code to represent extended characters,
  1612. because there's no other way to represent these strings in a way that isn't dependent on
  1613. the compiler, source code editor and platform.
  1614. */
  1615. wassert (CharPointer_UTF8::isValidString (stringLiteral, std::numeric_limits<int>::max()));
  1616. }
  1617. StringRef::StringRef (CharPointer_UTF8 stringLiteral) noexcept : text (stringLiteral)
  1618. {
  1619. wassert (stringLiteral.getAddress() != nullptr); // This must be a valid string literal, not a null pointer!!
  1620. }
  1621. StringRef::StringRef (const String& string) noexcept : text (string.getCharPointer()) {}
  1622. }
  1623. //==============================================================================