The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

2968 lines
106KB

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