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.

2391 lines
81KB

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