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.

2065 lines
65KB

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