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.

1427 lines
66KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. //==============================================================================
  20. /**
  21. The JUCE String class!
  22. Using a reference-counted internal representation, these strings are fast
  23. and efficient, and there are methods to do just about any operation you'll ever
  24. dream of.
  25. @see StringArray, StringPairArray
  26. */
  27. class JUCE_API String final
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates an empty string.
  32. @see empty
  33. */
  34. String() noexcept;
  35. /** Creates a copy of another string. */
  36. String (const String&) noexcept;
  37. /** Move constructor */
  38. String (String&&) noexcept;
  39. /** Creates a string from a zero-terminated ascii text string.
  40. The string passed-in must not contain any characters with a value above 127, because
  41. these can't be converted to unicode without knowing the original encoding that was
  42. used to create the string. If you attempt to pass-in values above 127, you'll get an
  43. assertion.
  44. To create strings with extended characters from UTF-8, you should explicitly call
  45. String (CharPointer_UTF8 ("my utf8 string..")). It's *highly* recommended that you
  46. use UTF-8 with escape characters in your source code to represent extended characters,
  47. because there's no other way to represent unicode strings in a way that isn't dependent
  48. on the compiler, source code editor and platform.
  49. */
  50. String (const char* text);
  51. /** Creates a string from a string of 8-bit ascii characters.
  52. The string passed-in must not contain any characters with a value above 127, because
  53. these can't be converted to unicode without knowing the original encoding that was
  54. used to create the string. If you attempt to pass-in values above 127, you'll get an
  55. assertion.
  56. To create strings with extended characters from UTF-8, you should explicitly call
  57. String (CharPointer_UTF8 ("my utf8 string..")). It's *highly* recommended that you
  58. use UTF-8 with escape characters in your source code to represent extended characters,
  59. because there's no other way to represent unicode strings in a way that isn't dependent
  60. on the compiler, source code editor and platform.
  61. This will use up to the first maxChars characters of the string (or less if the string
  62. is actually shorter).
  63. */
  64. String (const char* text, size_t maxChars);
  65. /** Creates a string from a wchar_t character string.
  66. Depending on the platform, this may be treated as either UTF-32 or UTF-16.
  67. */
  68. String (const wchar_t* text);
  69. /** Creates a string from a wchar_t character string.
  70. Depending on the platform, this may be treated as either UTF-32 or UTF-16.
  71. */
  72. String (const wchar_t* text, size_t maxChars);
  73. //==============================================================================
  74. /** Creates a string from a UTF-8 character string */
  75. String (CharPointer_UTF8 text);
  76. /** Creates a string from a UTF-8 character string */
  77. String (CharPointer_UTF8 text, size_t maxChars);
  78. /** Creates a string from a UTF-8 character string */
  79. String (CharPointer_UTF8 start, CharPointer_UTF8 end);
  80. //==============================================================================
  81. /** Creates a string from a UTF-16 character string */
  82. String (CharPointer_UTF16 text);
  83. /** Creates a string from a UTF-16 character string */
  84. String (CharPointer_UTF16 text, size_t maxChars);
  85. /** Creates a string from a UTF-16 character string */
  86. String (CharPointer_UTF16 start, CharPointer_UTF16 end);
  87. //==============================================================================
  88. /** Creates a string from a UTF-32 character string */
  89. String (CharPointer_UTF32 text);
  90. /** Creates a string from a UTF-32 character string */
  91. String (CharPointer_UTF32 text, size_t maxChars);
  92. /** Creates a string from a UTF-32 character string */
  93. String (CharPointer_UTF32 start, CharPointer_UTF32 end);
  94. //==============================================================================
  95. /** Creates a string from an ASCII character string */
  96. String (CharPointer_ASCII text);
  97. /** Creates a string from a UTF-8 encoded std::string. */
  98. String (const std::string&);
  99. /** Creates a string from a StringRef */
  100. String (StringRef);
  101. //==============================================================================
  102. /** Creates a string from a single character. */
  103. static String charToString (juce_wchar character);
  104. /** Destructor. */
  105. ~String() noexcept;
  106. /** This is the character encoding type used internally to store the string.
  107. By setting the value of JUCE_STRING_UTF_TYPE to 8, 16, or 32, you can change the
  108. internal storage format of the String class. UTF-8 uses the least space (if your strings
  109. contain few extended characters), but call operator[] involves iterating the string to find
  110. the required index. UTF-32 provides instant random access to its characters, but uses 4 bytes
  111. per character to store them. UTF-16 uses more space than UTF-8 and is also slow to index,
  112. but is the native wchar_t format used in Windows.
  113. It doesn't matter too much which format you pick, because the toUTF8(), toUTF16() and
  114. toUTF32() methods let you access the string's content in any of the other formats.
  115. */
  116. #if (JUCE_STRING_UTF_TYPE == 32)
  117. typedef CharPointer_UTF32 CharPointerType;
  118. #elif (JUCE_STRING_UTF_TYPE == 16)
  119. typedef CharPointer_UTF16 CharPointerType;
  120. #elif (DOXYGEN || JUCE_STRING_UTF_TYPE == 8)
  121. typedef CharPointer_UTF8 CharPointerType;
  122. #else
  123. #error "You must set the value of JUCE_STRING_UTF_TYPE to be either 8, 16, or 32!"
  124. #endif
  125. //==============================================================================
  126. /** Generates a probably-unique 32-bit hashcode from this string. */
  127. int hashCode() const noexcept;
  128. /** Generates a probably-unique 64-bit hashcode from this string. */
  129. int64 hashCode64() const noexcept;
  130. /** Generates a probably-unique hashcode from this string. */
  131. size_t hash() const noexcept;
  132. /** Returns the number of characters in the string. */
  133. int length() const noexcept;
  134. //==============================================================================
  135. // Assignment and concatenation operators..
  136. /** Replaces this string's contents with another string. */
  137. String& operator= (const String& other) noexcept;
  138. /** Moves the contents of another string to the receiver */
  139. String& operator= (String&& other) noexcept;
  140. /** Appends another string at the end of this one. */
  141. String& operator+= (const String& stringToAppend);
  142. /** Appends another string at the end of this one. */
  143. String& operator+= (const char* textToAppend);
  144. /** Appends another string at the end of this one. */
  145. String& operator+= (const wchar_t* textToAppend);
  146. /** Appends another string at the end of this one. */
  147. String& operator+= (StringRef textToAppend);
  148. /** Appends a decimal number at the end of this string. */
  149. String& operator+= (int numberToAppend);
  150. /** Appends a decimal number at the end of this string. */
  151. String& operator+= (long numberToAppend);
  152. /** Appends a decimal number at the end of this string. */
  153. String& operator+= (int64 numberToAppend);
  154. /** Appends a decimal number at the end of this string. */
  155. String& operator+= (uint64 numberToAppend);
  156. /** Appends a character at the end of this string. */
  157. String& operator+= (char characterToAppend);
  158. /** Appends a character at the end of this string. */
  159. String& operator+= (wchar_t characterToAppend);
  160. #if ! JUCE_NATIVE_WCHAR_IS_UTF32
  161. /** Appends a character at the end of this string. */
  162. String& operator+= (juce_wchar characterToAppend);
  163. #endif
  164. /** Appends a string to the end of this one.
  165. @param textToAppend the string to add
  166. @param maxCharsToTake the maximum number of characters to take from the string passed in
  167. */
  168. void append (const String& textToAppend, size_t maxCharsToTake);
  169. /** Appends a string to the end of this one.
  170. @param startOfTextToAppend the start of the string to add. This must not be a nullptr
  171. @param endOfTextToAppend the end of the string to add. This must not be a nullptr
  172. */
  173. void appendCharPointer (CharPointerType startOfTextToAppend,
  174. CharPointerType endOfTextToAppend);
  175. /** Appends a string to the end of this one.
  176. @param startOfTextToAppend the start of the string to add. This must not be a nullptr
  177. @param endOfTextToAppend the end of the string to add. This must not be a nullptr
  178. */
  179. template <class CharPointer>
  180. void appendCharPointer (CharPointer startOfTextToAppend,
  181. CharPointer endOfTextToAppend)
  182. {
  183. jassert (startOfTextToAppend.getAddress() != nullptr && endOfTextToAppend.getAddress() != nullptr);
  184. size_t extraBytesNeeded = 0, numChars = 1;
  185. for (auto t = startOfTextToAppend; t != endOfTextToAppend && ! t.isEmpty(); ++numChars)
  186. extraBytesNeeded += CharPointerType::getBytesRequiredFor (t.getAndAdvance());
  187. if (extraBytesNeeded > 0)
  188. {
  189. auto byteOffsetOfNull = getByteOffsetOfEnd();
  190. preallocateBytes (byteOffsetOfNull + extraBytesNeeded);
  191. CharPointerType (addBytesToPointer (text.getAddress(), (int) byteOffsetOfNull))
  192. .writeWithCharLimit (startOfTextToAppend, (int) numChars);
  193. }
  194. }
  195. /** Appends a string to the end of this one. */
  196. void appendCharPointer (CharPointerType textToAppend);
  197. /** Appends a string to the end of this one.
  198. @param textToAppend the string to add
  199. @param maxCharsToTake the maximum number of characters to take from the string passed in
  200. */
  201. template <class CharPointer>
  202. void appendCharPointer (CharPointer textToAppend, size_t maxCharsToTake)
  203. {
  204. if (textToAppend.getAddress() != nullptr)
  205. {
  206. size_t extraBytesNeeded = 0, numChars = 1;
  207. for (auto t = textToAppend; numChars <= maxCharsToTake && ! t.isEmpty(); ++numChars)
  208. extraBytesNeeded += CharPointerType::getBytesRequiredFor (t.getAndAdvance());
  209. if (extraBytesNeeded > 0)
  210. {
  211. auto byteOffsetOfNull = getByteOffsetOfEnd();
  212. preallocateBytes (byteOffsetOfNull + extraBytesNeeded);
  213. CharPointerType (addBytesToPointer (text.getAddress(), (int) byteOffsetOfNull))
  214. .writeWithCharLimit (textToAppend, (int) numChars);
  215. }
  216. }
  217. }
  218. /** Appends a string to the end of this one. */
  219. template <class CharPointer>
  220. void appendCharPointer (CharPointer textToAppend)
  221. {
  222. appendCharPointer (textToAppend, std::numeric_limits<size_t>::max());
  223. }
  224. //==============================================================================
  225. // Comparison methods..
  226. /** Returns true if the string contains no characters.
  227. Note that there's also an isNotEmpty() method to help write readable code.
  228. @see containsNonWhitespaceChars()
  229. */
  230. inline bool isEmpty() const noexcept { return text.isEmpty(); }
  231. /** Returns true if the string contains at least one character.
  232. Note that there's also an isEmpty() method to help write readable code.
  233. @see containsNonWhitespaceChars()
  234. */
  235. inline bool isNotEmpty() const noexcept { return ! text.isEmpty(); }
  236. /** Resets this string to be empty. */
  237. void clear() noexcept;
  238. /** Case-insensitive comparison with another string. */
  239. bool equalsIgnoreCase (const String& other) const noexcept;
  240. /** Case-insensitive comparison with another string. */
  241. bool equalsIgnoreCase (StringRef other) const noexcept;
  242. /** Case-insensitive comparison with another string. */
  243. bool equalsIgnoreCase (const wchar_t* other) const noexcept;
  244. /** Case-insensitive comparison with another string. */
  245. bool equalsIgnoreCase (const char* other) const noexcept;
  246. /** Case-sensitive comparison with another string.
  247. @returns 0 if the two strings are identical; negative if this string comes before
  248. the other one alphabetically, or positive if it comes after it.
  249. */
  250. int compare (const String& other) const noexcept;
  251. /** Case-sensitive comparison with another string.
  252. @returns 0 if the two strings are identical; negative if this string comes before
  253. the other one alphabetically, or positive if it comes after it.
  254. */
  255. int compare (const char* other) const noexcept;
  256. /** Case-sensitive comparison with another string.
  257. @returns 0 if the two strings are identical; negative if this string comes before
  258. the other one alphabetically, or positive if it comes after it.
  259. */
  260. int compare (const wchar_t* other) const noexcept;
  261. /** Case-insensitive comparison with another string.
  262. @returns 0 if the two strings are identical; negative if this string comes before
  263. the other one alphabetically, or positive if it comes after it.
  264. */
  265. int compareIgnoreCase (const String& other) const noexcept;
  266. /** Compares two strings, taking into account textual characteristics like numbers and spaces.
  267. This comparison is case-insensitive and can detect words and embedded numbers in the
  268. strings, making it good for sorting human-readable lists of things like filenames.
  269. @returns 0 if the two strings are identical; negative if this string comes before
  270. the other one alphabetically, or positive if it comes after it.
  271. */
  272. int compareNatural (StringRef other, bool isCaseSensitive = false) const noexcept;
  273. /** Tests whether the string begins with another string.
  274. If the parameter is an empty string, this will always return true.
  275. Uses a case-sensitive comparison.
  276. */
  277. bool startsWith (StringRef text) const noexcept;
  278. /** Tests whether the string begins with a particular character.
  279. If the character is 0, this will always return false.
  280. Uses a case-sensitive comparison.
  281. */
  282. bool startsWithChar (juce_wchar character) const noexcept;
  283. /** Tests whether the string begins with another string.
  284. If the parameter is an empty string, this will always return true.
  285. Uses a case-insensitive comparison.
  286. */
  287. bool startsWithIgnoreCase (StringRef text) const noexcept;
  288. /** Tests whether the string ends with another string.
  289. If the parameter is an empty string, this will always return true.
  290. Uses a case-sensitive comparison.
  291. */
  292. bool endsWith (StringRef text) const noexcept;
  293. /** Tests whether the string ends with a particular character.
  294. If the character is 0, this will always return false.
  295. Uses a case-sensitive comparison.
  296. */
  297. bool endsWithChar (juce_wchar character) const noexcept;
  298. /** Tests whether the string ends with another string.
  299. If the parameter is an empty string, this will always return true.
  300. Uses a case-insensitive comparison.
  301. */
  302. bool endsWithIgnoreCase (StringRef text) const noexcept;
  303. /** Tests whether the string contains another substring.
  304. If the parameter is an empty string, this will always return true.
  305. Uses a case-sensitive comparison.
  306. */
  307. bool contains (StringRef text) const noexcept;
  308. /** Tests whether the string contains a particular character.
  309. Uses a case-sensitive comparison.
  310. */
  311. bool containsChar (juce_wchar character) const noexcept;
  312. /** Tests whether the string contains another substring.
  313. Uses a case-insensitive comparison.
  314. */
  315. bool containsIgnoreCase (StringRef text) const noexcept;
  316. /** Tests whether the string contains another substring as a distinct word.
  317. @returns true if the string contains this word, surrounded by
  318. non-alphanumeric characters
  319. @see indexOfWholeWord, containsWholeWordIgnoreCase
  320. */
  321. bool containsWholeWord (StringRef wordToLookFor) const noexcept;
  322. /** Tests whether the string contains another substring as a distinct word.
  323. @returns true if the string contains this word, surrounded by
  324. non-alphanumeric characters
  325. @see indexOfWholeWordIgnoreCase, containsWholeWord
  326. */
  327. bool containsWholeWordIgnoreCase (StringRef wordToLookFor) const noexcept;
  328. /** Finds an instance of another substring if it exists as a distinct word.
  329. @returns if the string contains this word, surrounded by non-alphanumeric characters,
  330. then this will return the index of the start of the substring. If it isn't
  331. found, then it will return -1
  332. @see indexOfWholeWordIgnoreCase, containsWholeWord
  333. */
  334. int indexOfWholeWord (StringRef wordToLookFor) const noexcept;
  335. /** Finds an instance of another substring if it exists as a distinct word.
  336. @returns if the string contains this word, surrounded by non-alphanumeric characters,
  337. then this will return the index of the start of the substring. If it isn't
  338. found, then it will return -1
  339. @see indexOfWholeWord, containsWholeWordIgnoreCase
  340. */
  341. int indexOfWholeWordIgnoreCase (StringRef wordToLookFor) const noexcept;
  342. /** Looks for any of a set of characters in the string.
  343. Uses a case-sensitive comparison.
  344. @returns true if the string contains any of the characters from
  345. the string that is passed in.
  346. */
  347. bool containsAnyOf (StringRef charactersItMightContain) const noexcept;
  348. /** Looks for a set of characters in the string.
  349. Uses a case-sensitive comparison.
  350. @returns Returns false if any of the characters in this string do not occur in
  351. the parameter string. If this string is empty, the return value will
  352. always be true.
  353. */
  354. bool containsOnly (StringRef charactersItMightContain) const noexcept;
  355. /** Returns true if this string contains any non-whitespace characters.
  356. This will return false if the string contains only whitespace characters, or
  357. if it's empty.
  358. It is equivalent to calling "myString.trim().isNotEmpty()".
  359. */
  360. bool containsNonWhitespaceChars() const noexcept;
  361. /** Returns true if the string matches this simple wildcard expression.
  362. So for example String ("abcdef").matchesWildcard ("*DEF", true) would return true.
  363. This isn't a full-blown regex though! The only wildcard characters supported
  364. are "*" and "?". It's mainly intended for filename pattern matching.
  365. */
  366. bool matchesWildcard (StringRef wildcard, bool ignoreCase) const noexcept;
  367. //==============================================================================
  368. // Substring location methods..
  369. /** Searches for a character inside this string.
  370. Uses a case-sensitive comparison.
  371. @returns the index of the first occurrence of the character in this
  372. string, or -1 if it's not found.
  373. */
  374. int indexOfChar (juce_wchar characterToLookFor) const noexcept;
  375. /** Searches for a character inside this string.
  376. Uses a case-sensitive comparison.
  377. @param startIndex the index from which the search should proceed
  378. @param characterToLookFor the character to look for
  379. @returns the index of the first occurrence of the character in this
  380. string, or -1 if it's not found.
  381. */
  382. int indexOfChar (int startIndex, juce_wchar characterToLookFor) const noexcept;
  383. /** Returns the index of the first character that matches one of the characters
  384. passed-in to this method.
  385. This scans the string, beginning from the startIndex supplied, and if it finds
  386. a character that appears in the string charactersToLookFor, it returns its index.
  387. If none of these characters are found, it returns -1.
  388. If ignoreCase is true, the comparison will be case-insensitive.
  389. @see indexOfChar, lastIndexOfAnyOf
  390. */
  391. int indexOfAnyOf (StringRef charactersToLookFor,
  392. int startIndex = 0,
  393. bool ignoreCase = false) const noexcept;
  394. /** Searches for a substring within this string.
  395. Uses a case-sensitive comparison.
  396. @returns the index of the first occurrence of this substring, or -1 if it's not found.
  397. If textToLookFor is an empty string, this will always return 0.
  398. */
  399. int indexOf (StringRef textToLookFor) const noexcept;
  400. /** Searches for a substring within this string.
  401. Uses a case-sensitive comparison.
  402. @param startIndex the index from which the search should proceed
  403. @param textToLookFor the string to search for
  404. @returns the index of the first occurrence of this substring, or -1 if it's not found.
  405. If textToLookFor is an empty string, this will always return -1.
  406. */
  407. int indexOf (int startIndex, StringRef textToLookFor) const noexcept;
  408. /** Searches for a substring within this string.
  409. Uses a case-insensitive comparison.
  410. @returns the index of the first occurrence of this substring, or -1 if it's not found.
  411. If textToLookFor is an empty string, this will always return 0.
  412. */
  413. int indexOfIgnoreCase (StringRef textToLookFor) const noexcept;
  414. /** Searches for a substring within this string.
  415. Uses a case-insensitive comparison.
  416. @param startIndex the index from which the search should proceed
  417. @param textToLookFor the string to search for
  418. @returns the index of the first occurrence of this substring, or -1 if it's not found.
  419. If textToLookFor is an empty string, this will always return -1.
  420. */
  421. int indexOfIgnoreCase (int startIndex, StringRef textToLookFor) const noexcept;
  422. /** Searches for a character inside this string (working backwards from the end of the string).
  423. Uses a case-sensitive comparison.
  424. @returns the index of the last occurrence of the character in this string, or -1 if it's not found.
  425. */
  426. int lastIndexOfChar (juce_wchar character) const noexcept;
  427. /** Searches for a substring inside this string (working backwards from the end of the string).
  428. Uses a case-sensitive comparison.
  429. @returns the index of the start of the last occurrence of the substring within this string,
  430. or -1 if it's not found. If textToLookFor is an empty string, this will always return -1.
  431. */
  432. int lastIndexOf (StringRef textToLookFor) const noexcept;
  433. /** Searches for a substring inside this string (working backwards from the end of the string).
  434. Uses a case-insensitive comparison.
  435. @returns the index of the start of the last occurrence of the substring within this string, or -1
  436. if it's not found. If textToLookFor is an empty string, this will always return -1.
  437. */
  438. int lastIndexOfIgnoreCase (StringRef textToLookFor) const noexcept;
  439. /** Returns the index of the last character in this string that matches one of the
  440. characters passed-in to this method.
  441. This scans the string backwards, starting from its end, and if it finds
  442. a character that appears in the string charactersToLookFor, it returns its index.
  443. If none of these characters are found, it returns -1.
  444. If ignoreCase is true, the comparison will be case-insensitive.
  445. @see lastIndexOf, indexOfAnyOf
  446. */
  447. int lastIndexOfAnyOf (StringRef charactersToLookFor,
  448. bool ignoreCase = false) const noexcept;
  449. //==============================================================================
  450. // Substring extraction and manipulation methods..
  451. /** Returns the character at this index in the string.
  452. In a release build, no checks are made to see if the index is within a valid range, so be
  453. careful! In a debug build, the index is checked and an assertion fires if it's out-of-range.
  454. Also beware that depending on the encoding format that the string is using internally, this
  455. method may execute in either O(1) or O(n) time, so be careful when using it in your algorithms.
  456. If you're scanning through a string to inspect its characters, you should never use this operator
  457. for random access, it's far more efficient to call getCharPointer() to return a pointer, and
  458. then to use that to iterate the string.
  459. @see getCharPointer
  460. */
  461. juce_wchar operator[] (int index) const noexcept;
  462. /** Returns the final character of the string.
  463. If the string is empty this will return 0.
  464. */
  465. juce_wchar getLastCharacter() const noexcept;
  466. //==============================================================================
  467. /** Returns a subsection of the string.
  468. If the range specified is beyond the limits of the string, as much as
  469. possible is returned.
  470. @param startIndex the index of the start of the substring needed
  471. @param endIndex all characters from startIndex up to (but not including)
  472. this index are returned
  473. @see fromFirstOccurrenceOf, dropLastCharacters, getLastCharacters, upToFirstOccurrenceOf
  474. */
  475. String substring (int startIndex, int endIndex) const;
  476. /** Returns a section of the string, starting from a given position.
  477. @param startIndex the first character to include. If this is beyond the end
  478. of the string, an empty string is returned. If it is zero or
  479. less, the whole string is returned.
  480. @returns the substring from startIndex up to the end of the string
  481. @see dropLastCharacters, getLastCharacters, fromFirstOccurrenceOf, upToFirstOccurrenceOf, fromLastOccurrenceOf
  482. */
  483. String substring (int startIndex) const;
  484. /** Returns a version of this string with a number of characters removed
  485. from the end.
  486. @param numberToDrop the number of characters to drop from the end of the
  487. string. If this is greater than the length of the string,
  488. an empty string will be returned. If zero or less, the
  489. original string will be returned.
  490. @see substring, fromFirstOccurrenceOf, upToFirstOccurrenceOf, fromLastOccurrenceOf, getLastCharacter
  491. */
  492. String dropLastCharacters (int numberToDrop) const;
  493. /** Returns a number of characters from the end of the string.
  494. This returns the last numCharacters characters from the end of the string. If the
  495. string is shorter than numCharacters, the whole string is returned.
  496. @see substring, dropLastCharacters, getLastCharacter
  497. */
  498. String getLastCharacters (int numCharacters) const;
  499. //==============================================================================
  500. /** Returns a section of the string starting from a given substring.
  501. This will search for the first occurrence of the given substring, and
  502. return the section of the string starting from the point where this is
  503. found (optionally not including the substring itself).
  504. e.g. for the string "123456", fromFirstOccurrenceOf ("34", true) would return "3456", and
  505. fromFirstOccurrenceOf ("34", false) would return "56".
  506. If the substring isn't found, the method will return an empty string.
  507. If ignoreCase is true, the comparison will be case-insensitive.
  508. @see upToFirstOccurrenceOf, fromLastOccurrenceOf
  509. */
  510. String fromFirstOccurrenceOf (StringRef substringToStartFrom,
  511. bool includeSubStringInResult,
  512. bool ignoreCase) const;
  513. /** Returns a section of the string starting from the last occurrence of a given substring.
  514. Similar to fromFirstOccurrenceOf(), but using the last occurrence of the substring, and
  515. unlike fromFirstOccurrenceOf(), if the substring isn't found, this method will
  516. return the whole of the original string.
  517. @see fromFirstOccurrenceOf, upToLastOccurrenceOf
  518. */
  519. String fromLastOccurrenceOf (StringRef substringToFind,
  520. bool includeSubStringInResult,
  521. bool ignoreCase) const;
  522. /** Returns the start of this string, up to the first occurrence of a substring.
  523. This will search for the first occurrence of a given substring, and then
  524. return a copy of the string, up to the position of this substring,
  525. optionally including or excluding the substring itself in the result.
  526. e.g. for the string "123456", upTo ("34", false) would return "12", and
  527. upTo ("34", true) would return "1234".
  528. If the substring isn't found, this will return the whole of the original string.
  529. @see upToLastOccurrenceOf, fromFirstOccurrenceOf
  530. */
  531. String upToFirstOccurrenceOf (StringRef substringToEndWith,
  532. bool includeSubStringInResult,
  533. bool ignoreCase) const;
  534. /** Returns the start of this string, up to the last occurrence of a substring.
  535. Similar to upToFirstOccurrenceOf(), but this finds the last occurrence rather than the first.
  536. If the substring isn't found, this will return the whole of the original string.
  537. @see upToFirstOccurrenceOf, fromFirstOccurrenceOf
  538. */
  539. String upToLastOccurrenceOf (StringRef substringToFind,
  540. bool includeSubStringInResult,
  541. bool ignoreCase) const;
  542. //==============================================================================
  543. /** Returns a copy of this string with any whitespace characters removed from the start and end. */
  544. String trim() const;
  545. /** Returns a copy of this string with any whitespace characters removed from the start. */
  546. String trimStart() const;
  547. /** Returns a copy of this string with any whitespace characters removed from the end. */
  548. String trimEnd() const;
  549. /** Returns a copy of this string, having removed a specified set of characters from its start.
  550. Characters are removed from the start of the string until it finds one that is not in the
  551. specified set, and then it stops.
  552. @param charactersToTrim the set of characters to remove.
  553. @see trim, trimStart, trimCharactersAtEnd
  554. */
  555. String trimCharactersAtStart (StringRef charactersToTrim) const;
  556. /** Returns a copy of this string, having removed a specified set of characters from its end.
  557. Characters are removed from the end of the string until it finds one that is not in the
  558. specified set, and then it stops.
  559. @param charactersToTrim the set of characters to remove.
  560. @see trim, trimEnd, trimCharactersAtStart
  561. */
  562. String trimCharactersAtEnd (StringRef charactersToTrim) const;
  563. //==============================================================================
  564. /** Returns an upper-case version of this string. */
  565. String toUpperCase() const;
  566. /** Returns an lower-case version of this string. */
  567. String toLowerCase() const;
  568. //==============================================================================
  569. /** Replaces a sub-section of the string with another string.
  570. This will return a copy of this string, with a set of characters
  571. from startIndex to startIndex + numCharsToReplace removed, and with
  572. a new string inserted in their place.
  573. Note that this is a const method, and won't alter the string itself.
  574. @param startIndex the first character to remove. If this is beyond the bounds of the string,
  575. it will be constrained to a valid range.
  576. @param numCharactersToReplace the number of characters to remove. If zero or less, no
  577. characters will be taken out.
  578. @param stringToInsert the new string to insert at startIndex after the characters have been
  579. removed.
  580. */
  581. String replaceSection (int startIndex,
  582. int numCharactersToReplace,
  583. StringRef stringToInsert) const;
  584. /** Replaces all occurrences of a substring with another string.
  585. Returns a copy of this string, with any occurrences of stringToReplace
  586. swapped for stringToInsertInstead.
  587. Note that this is a const method, and won't alter the string itself.
  588. */
  589. String replace (StringRef stringToReplace,
  590. StringRef stringToInsertInstead,
  591. bool ignoreCase = false) const;
  592. /** Replaces the first occurrence of a substring with another string.
  593. Returns a copy of this string, with the first occurrence of stringToReplace
  594. swapped for stringToInsertInstead.
  595. Note that this is a const method, and won't alter the string itself.
  596. */
  597. String replaceFirstOccurrenceOf (StringRef stringToReplace,
  598. StringRef stringToInsertInstead,
  599. bool ignoreCase = false) const;
  600. /** Returns a string with all occurrences of a character replaced with a different one. */
  601. String replaceCharacter (juce_wchar characterToReplace,
  602. juce_wchar characterToInsertInstead) const;
  603. /** Replaces a set of characters with another set.
  604. Returns a string in which each character from charactersToReplace has been replaced
  605. by the character at the equivalent position in newCharacters (so the two strings
  606. passed in must be the same length).
  607. e.g. replaceCharacters ("abc", "def") replaces 'a' with 'd', 'b' with 'e', etc.
  608. Note that this is a const method, and won't affect the string itself.
  609. */
  610. String replaceCharacters (StringRef charactersToReplace,
  611. StringRef charactersToInsertInstead) const;
  612. /** Returns a version of this string that only retains a fixed set of characters.
  613. This will return a copy of this string, omitting any characters which are not
  614. found in the string passed-in.
  615. e.g. for "1122334455", retainCharacters ("432") would return "223344"
  616. Note that this is a const method, and won't alter the string itself.
  617. */
  618. String retainCharacters (StringRef charactersToRetain) const;
  619. /** Returns a version of this string with a set of characters removed.
  620. This will return a copy of this string, omitting any characters which are
  621. found in the string passed-in.
  622. e.g. for "1122334455", removeCharacters ("432") would return "1155"
  623. Note that this is a const method, and won't alter the string itself.
  624. */
  625. String removeCharacters (StringRef charactersToRemove) const;
  626. /** Returns a section from the start of the string that only contains a certain set of characters.
  627. This returns the leftmost section of the string, up to (and not including) the
  628. first character that doesn't appear in the string passed in.
  629. */
  630. String initialSectionContainingOnly (StringRef permittedCharacters) const;
  631. /** Returns a section from the start of the string that only contains a certain set of characters.
  632. This returns the leftmost section of the string, up to (and not including) the
  633. first character that occurs in the string passed in. (If none of the specified
  634. characters are found in the string, the return value will just be the original string).
  635. */
  636. String initialSectionNotContaining (StringRef charactersToStopAt) const;
  637. //==============================================================================
  638. /** Checks whether the string might be in quotation marks.
  639. @returns true if the string begins with a quote character (either a double or single quote).
  640. It is also true if there is whitespace before the quote, but it doesn't check the end of the string.
  641. @see unquoted, quoted
  642. */
  643. bool isQuotedString() const;
  644. /** Removes quotation marks from around the string, (if there are any).
  645. Returns a copy of this string with any quotes removed from its ends. Quotes that aren't
  646. at the ends of the string are not affected. If there aren't any quotes, the original string
  647. is returned.
  648. Note that this is a const method, and won't alter the string itself.
  649. @see isQuotedString, quoted
  650. */
  651. String unquoted() const;
  652. /** Adds quotation marks around a string.
  653. This will return a copy of the string with a quote at the start and end, (but won't
  654. add the quote if there's already one there, so it's safe to call this on strings that
  655. may already have quotes around them).
  656. Note that this is a const method, and won't alter the string itself.
  657. @param quoteCharacter the character to add at the start and end
  658. @see isQuotedString, unquoted
  659. */
  660. String quoted (juce_wchar quoteCharacter = '"') const;
  661. //==============================================================================
  662. /** Creates a string which is a version of a string repeated and joined together.
  663. @param stringToRepeat the string to repeat
  664. @param numberOfTimesToRepeat how many times to repeat it
  665. */
  666. static String repeatedString (StringRef stringToRepeat,
  667. int numberOfTimesToRepeat);
  668. /** Returns a copy of this string with the specified character repeatedly added to its
  669. beginning until the total length is at least the minimum length specified.
  670. */
  671. String paddedLeft (juce_wchar padCharacter, int minimumLength) const;
  672. /** Returns a copy of this string with the specified character repeatedly added to its
  673. end until the total length is at least the minimum length specified.
  674. */
  675. String paddedRight (juce_wchar padCharacter, int minimumLength) const;
  676. /** Creates a string from data in an unknown format.
  677. This looks at some binary data and tries to guess whether it's Unicode
  678. or 8-bit characters, then returns a string that represents it correctly.
  679. Should be able to handle Unicode endianness correctly, by looking at
  680. the first two bytes.
  681. */
  682. static String createStringFromData (const void* data, int size);
  683. /** Creates a String from a printf-style parameter list.
  684. I don't like this method. I don't use it myself, and I recommend avoiding it and
  685. using the operator<< methods or pretty much anything else instead. It's only provided
  686. here because of the popular unrest that was stirred-up when I tried to remove it...
  687. If you're really determined to use it, at least make sure that you never, ever,
  688. pass any String objects to it as parameters. And bear in mind that internally, depending
  689. on the platform, it may be using wchar_t or char character types, so that even string
  690. literals can't be safely used as parameters if you're writing portable code.
  691. */
  692. template <typename... Args>
  693. static String formatted (const String& formatStr, Args... args) { return formattedRaw (formatStr.toRawUTF8(), args...); }
  694. //==============================================================================
  695. // Numeric conversions..
  696. /** Creates a string containing this signed 32-bit integer as a decimal number.
  697. @see getIntValue, getFloatValue, getDoubleValue, toHexString
  698. */
  699. explicit String (int decimalInteger);
  700. /** Creates a string containing this unsigned 32-bit integer as a decimal number.
  701. @see getIntValue, getFloatValue, getDoubleValue, toHexString
  702. */
  703. explicit String (unsigned int decimalInteger);
  704. /** Creates a string containing this signed 16-bit integer as a decimal number.
  705. @see getIntValue, getFloatValue, getDoubleValue, toHexString
  706. */
  707. explicit String (short decimalInteger);
  708. /** Creates a string containing this unsigned 16-bit integer as a decimal number.
  709. @see getIntValue, getFloatValue, getDoubleValue, toHexString
  710. */
  711. explicit String (unsigned short decimalInteger);
  712. /** Creates a string containing this signed 64-bit integer as a decimal number.
  713. @see getLargeIntValue, getFloatValue, getDoubleValue, toHexString
  714. */
  715. explicit String (int64 largeIntegerValue);
  716. /** Creates a string containing this unsigned 64-bit integer as a decimal number.
  717. @see getLargeIntValue, getFloatValue, getDoubleValue, toHexString
  718. */
  719. explicit String (uint64 largeIntegerValue);
  720. /** Creates a string containing this signed long integer as a decimal number.
  721. @see getIntValue, getFloatValue, getDoubleValue, toHexString
  722. */
  723. explicit String (long decimalInteger);
  724. /** Creates a string containing this unsigned long integer as a decimal number.
  725. @see getIntValue, getFloatValue, getDoubleValue, toHexString
  726. */
  727. explicit String (unsigned long decimalInteger);
  728. /** Creates a string representing this floating-point number.
  729. @param floatValue the value to convert to a string
  730. @see getDoubleValue, getIntValue
  731. */
  732. explicit String (float floatValue);
  733. /** Creates a string representing this floating-point number.
  734. @param doubleValue the value to convert to a string
  735. @see getFloatValue, getIntValue
  736. */
  737. explicit String (double doubleValue);
  738. /** Creates a string representing this floating-point number.
  739. @param floatValue the value to convert to a string
  740. @param numberOfDecimalPlaces if this is > 0, it will format the number using that many
  741. decimal places, and will not use exponent notation. If 0 or
  742. less, it will use exponent notation if necessary.
  743. @see getDoubleValue, getIntValue
  744. */
  745. String (float floatValue, int numberOfDecimalPlaces);
  746. /** Creates a string representing this floating-point number.
  747. @param doubleValue the value to convert to a string
  748. @param numberOfDecimalPlaces if this is > 0, it will format the number using that many
  749. decimal places, and will not use exponent notation. If 0 or
  750. less, it will use exponent notation if necessary.
  751. @see getFloatValue, getIntValue
  752. */
  753. String (double doubleValue, int numberOfDecimalPlaces);
  754. // Automatically creating a String from a bool opens up lots of nasty type conversion edge cases.
  755. // If you want a String representation of a bool you can cast the bool to an int first.
  756. explicit String (bool) = delete;
  757. /** Reads the value of the string as a decimal number (up to 32 bits in size).
  758. @returns the value of the string as a 32 bit signed base-10 integer.
  759. @see getTrailingIntValue, getHexValue32, getHexValue64
  760. */
  761. int getIntValue() const noexcept;
  762. /** Reads the value of the string as a decimal number (up to 64 bits in size).
  763. @returns the value of the string as a 64 bit signed base-10 integer.
  764. */
  765. int64 getLargeIntValue() const noexcept;
  766. /** Parses a decimal number from the end of the string.
  767. This will look for a value at the end of the string.
  768. e.g. for "321 xyz654" it will return 654; for "2 3 4" it'll return 4.
  769. Negative numbers are not handled, so "xyz-5" returns 5.
  770. @see getIntValue
  771. */
  772. int getTrailingIntValue() const noexcept;
  773. /** Parses this string as a floating point number.
  774. @returns the value of the string as a 32-bit floating point value.
  775. @see getDoubleValue
  776. */
  777. float getFloatValue() const noexcept;
  778. /** Parses this string as a floating point number.
  779. @returns the value of the string as a 64-bit floating point value.
  780. @see getFloatValue
  781. */
  782. double getDoubleValue() const noexcept;
  783. /** Parses the string as a hexadecimal number.
  784. Non-hexadecimal characters in the string are ignored.
  785. If the string contains too many characters, then the lowest significant
  786. digits are returned, e.g. "ffff12345678" would produce 0x12345678.
  787. @returns a 32-bit number which is the value of the string in hex.
  788. */
  789. int getHexValue32() const noexcept;
  790. /** Parses the string as a hexadecimal number.
  791. Non-hexadecimal characters in the string are ignored.
  792. If the string contains too many characters, then the lowest significant
  793. digits are returned, e.g. "ffff1234567812345678" would produce 0x1234567812345678.
  794. @returns a 64-bit number which is the value of the string in hex.
  795. */
  796. int64 getHexValue64() const noexcept;
  797. /** Returns a string representing this numeric value in hexadecimal. */
  798. template <typename IntegerType>
  799. static String toHexString (IntegerType number) { return createHex (number); }
  800. /** Returns a string containing a hex dump of a block of binary data.
  801. @param data the binary data to use as input
  802. @param size how many bytes of data to use
  803. @param groupSize how many bytes are grouped together before inserting a
  804. space into the output. e.g. group size 0 has no spaces,
  805. group size 1 looks like: "be a1 c2 ff", group size 2 looks
  806. like "bea1 c2ff".
  807. */
  808. static String toHexString (const void* data, int size, int groupSize = 1);
  809. //==============================================================================
  810. /** Returns the character pointer currently being used to store this string.
  811. Because it returns a reference to the string's internal data, the pointer
  812. that is returned must not be stored anywhere, as it can be deleted whenever the
  813. string changes.
  814. */
  815. inline CharPointerType getCharPointer() const noexcept { return text; }
  816. /** Returns a pointer to a UTF-8 version of this string.
  817. Because it returns a reference to the string's internal data, the pointer
  818. that is returned must not be stored anywhere, as it can be deleted whenever the
  819. string changes.
  820. To find out how many bytes you need to store this string as UTF-8, you can call
  821. CharPointer_UTF8::getBytesRequiredFor (myString.getCharPointer())
  822. @see toRawUTF8, getCharPointer, toUTF16, toUTF32
  823. */
  824. CharPointer_UTF8 toUTF8() const;
  825. /** Returns a pointer to a UTF-8 version of this string.
  826. Because it returns a reference to the string's internal data, the pointer
  827. that is returned must not be stored anywhere, as it can be deleted whenever the
  828. string changes.
  829. To find out how many bytes you need to store this string as UTF-8, you can call
  830. CharPointer_UTF8::getBytesRequiredFor (myString.getCharPointer())
  831. @see getCharPointer, toUTF8, toUTF16, toUTF32
  832. */
  833. const char* toRawUTF8() const;
  834. /** Returns a pointer to a UTF-16 version of this string.
  835. Because it returns a reference to the string's internal data, the pointer
  836. that is returned must not be stored anywhere, as it can be deleted whenever the
  837. string changes.
  838. To find out how many bytes you need to store this string as UTF-16, you can call
  839. CharPointer_UTF16::getBytesRequiredFor (myString.getCharPointer())
  840. @see getCharPointer, toUTF8, toUTF32
  841. */
  842. CharPointer_UTF16 toUTF16() const;
  843. /** Returns a pointer to a UTF-32 version of this string.
  844. Because it returns a reference to the string's internal data, the pointer
  845. that is returned must not be stored anywhere, as it can be deleted whenever the
  846. string changes.
  847. @see getCharPointer, toUTF8, toUTF16
  848. */
  849. CharPointer_UTF32 toUTF32() const;
  850. /** Returns a pointer to a wchar_t version of this string.
  851. Because it returns a reference to the string's internal data, the pointer
  852. that is returned must not be stored anywhere, as it can be deleted whenever the
  853. string changes.
  854. Bear in mind that the wchar_t type is different on different platforms, so on
  855. Windows, this will be equivalent to calling toUTF16(), on unix it'll be the same
  856. as calling toUTF32(), etc.
  857. @see getCharPointer, toUTF8, toUTF16, toUTF32
  858. */
  859. const wchar_t* toWideCharPointer() const;
  860. /** */
  861. std::string toStdString() const;
  862. //==============================================================================
  863. /** Creates a String from a UTF-8 encoded buffer.
  864. If the size is < 0, it'll keep reading until it hits a zero.
  865. */
  866. static String fromUTF8 (const char* utf8buffer, int bufferSizeBytes = -1);
  867. /** Returns the number of bytes required to represent this string as UTF8.
  868. The number returned does NOT include the trailing zero.
  869. @see toUTF8, copyToUTF8
  870. */
  871. size_t getNumBytesAsUTF8() const noexcept;
  872. //==============================================================================
  873. /** Copies the string to a buffer as UTF-8 characters.
  874. Returns the number of bytes copied to the buffer, including the terminating null
  875. character.
  876. To find out how many bytes you need to store this string as UTF-8, you can call
  877. CharPointer_UTF8::getBytesRequiredFor (myString.getCharPointer())
  878. @param destBuffer the place to copy it to; if this is a null pointer, the method just
  879. returns the number of bytes required (including the terminating null character).
  880. @param maxBufferSizeBytes the size of the destination buffer, in bytes. If the string won't fit, it'll
  881. put in as many as it can while still allowing for a terminating null char at the
  882. end, and will return the number of bytes that were actually used.
  883. @see CharPointer_UTF8::writeWithDestByteLimit
  884. */
  885. size_t copyToUTF8 (CharPointer_UTF8::CharType* destBuffer, size_t maxBufferSizeBytes) const noexcept;
  886. /** Copies the string to a buffer as UTF-16 characters.
  887. Returns the number of bytes copied to the buffer, including the terminating null
  888. character.
  889. To find out how many bytes you need to store this string as UTF-16, you can call
  890. CharPointer_UTF16::getBytesRequiredFor (myString.getCharPointer())
  891. @param destBuffer the place to copy it to; if this is a null pointer, the method just
  892. returns the number of bytes required (including the terminating null character).
  893. @param maxBufferSizeBytes the size of the destination buffer, in bytes. If the string won't fit, it'll
  894. put in as many as it can while still allowing for a terminating null char at the
  895. end, and will return the number of bytes that were actually used.
  896. @see CharPointer_UTF16::writeWithDestByteLimit
  897. */
  898. size_t copyToUTF16 (CharPointer_UTF16::CharType* destBuffer, size_t maxBufferSizeBytes) const noexcept;
  899. /** Copies the string to a buffer as UTF-32 characters.
  900. Returns the number of bytes copied to the buffer, including the terminating null
  901. character.
  902. To find out how many bytes you need to store this string as UTF-32, you can call
  903. CharPointer_UTF32::getBytesRequiredFor (myString.getCharPointer())
  904. @param destBuffer the place to copy it to; if this is a null pointer, the method just
  905. returns the number of bytes required (including the terminating null character).
  906. @param maxBufferSizeBytes the size of the destination buffer, in bytes. If the string won't fit, it'll
  907. put in as many as it can while still allowing for a terminating null char at the
  908. end, and will return the number of bytes that were actually used.
  909. @see CharPointer_UTF32::writeWithDestByteLimit
  910. */
  911. size_t copyToUTF32 (CharPointer_UTF32::CharType* destBuffer, size_t maxBufferSizeBytes) const noexcept;
  912. //==============================================================================
  913. /** Increases the string's internally allocated storage.
  914. Although the string's contents won't be affected by this call, it will
  915. increase the amount of memory allocated internally for the string to grow into.
  916. If you're about to make a large number of calls to methods such
  917. as += or <<, it's more efficient to preallocate enough extra space
  918. beforehand, so that these methods won't have to keep resizing the string
  919. to append the extra characters.
  920. @param numBytesNeeded the number of bytes to allocate storage for. If this
  921. value is less than the currently allocated size, it will
  922. have no effect.
  923. */
  924. void preallocateBytes (size_t numBytesNeeded);
  925. /** Swaps the contents of this string with another one.
  926. This is a very fast operation, as no allocation or copying needs to be done.
  927. */
  928. void swapWith (String& other) noexcept;
  929. //==============================================================================
  930. #if JUCE_MAC || JUCE_IOS || DOXYGEN
  931. /** OSX ONLY - Creates a String from an OSX CFString. */
  932. static String fromCFString (CFStringRef cfString);
  933. /** OSX ONLY - Converts this string to a CFString.
  934. Remember that you must use CFRelease() to free the returned string when you're
  935. finished with it.
  936. */
  937. CFStringRef toCFString() const;
  938. /** OSX ONLY - Returns a copy of this string in which any decomposed unicode characters have
  939. been converted to their precomposed equivalents. */
  940. String convertToPrecomposedUnicode() const;
  941. #endif
  942. /** Returns the number of String objects which are currently sharing the same internal
  943. data as this one.
  944. */
  945. int getReferenceCount() const noexcept;
  946. //==============================================================================
  947. #if JUCE_ALLOW_STATIC_NULL_VARIABLES
  948. /** This was a static empty string object, but is now deprecated as it's too easy to accidentally
  949. use it indirectly during a static constructor, leading to hard-to-find order-of-initialisation
  950. problems.
  951. @deprecated If you need an empty String object, just use String() or {}.
  952. The only time you might miss having String::empty available might be if you need to return an
  953. empty string from a function by reference, but if you need to do that, it's easy enough to use
  954. a function-local static String object and return that, avoiding any order-of-initialisation issues.
  955. */
  956. static const String empty;
  957. #endif
  958. private:
  959. //==============================================================================
  960. CharPointerType text;
  961. //==============================================================================
  962. struct PreallocationBytes
  963. {
  964. explicit PreallocationBytes (size_t) noexcept;
  965. size_t numBytes;
  966. };
  967. explicit String (const PreallocationBytes&); // This constructor preallocates a certain amount of memory
  968. size_t getByteOffsetOfEnd() const noexcept;
  969. JUCE_DEPRECATED (String (const String&, size_t));
  970. // This private cast operator should prevent strings being accidentally cast
  971. // to bools (this is possible because the compiler can add an implicit cast
  972. // via a const char*)
  973. operator bool() const noexcept { return false; }
  974. //==============================================================================
  975. static String formattedRaw (const char*, ...);
  976. static String createHex (uint8);
  977. static String createHex (uint16);
  978. static String createHex (uint32);
  979. static String createHex (uint64);
  980. template <typename Type>
  981. static String createHex (Type n) { return createHex (static_cast<typename TypeHelpers::UnsignedTypeWithSize<sizeof (n)>::type> (n)); }
  982. };
  983. //==============================================================================
  984. /** Concatenates two strings. */
  985. JUCE_API String JUCE_CALLTYPE operator+ (const char* string1, const String& string2);
  986. /** Concatenates two strings. */
  987. JUCE_API String JUCE_CALLTYPE operator+ (const wchar_t* string1, const String& string2);
  988. /** Concatenates two strings. */
  989. JUCE_API String JUCE_CALLTYPE operator+ (char string1, const String& string2);
  990. /** Concatenates two strings. */
  991. JUCE_API String JUCE_CALLTYPE operator+ (wchar_t string1, const String& string2);
  992. #if ! JUCE_NATIVE_WCHAR_IS_UTF32
  993. /** Concatenates two strings. */
  994. JUCE_API String JUCE_CALLTYPE operator+ (juce_wchar string1, const String& string2);
  995. #endif
  996. /** Concatenates two strings. */
  997. JUCE_API String JUCE_CALLTYPE operator+ (String string1, const String& string2);
  998. /** Concatenates two strings. */
  999. JUCE_API String JUCE_CALLTYPE operator+ (String string1, const char* string2);
  1000. /** Concatenates two strings. */
  1001. JUCE_API String JUCE_CALLTYPE operator+ (String string1, const wchar_t* string2);
  1002. /** Concatenates two strings. */
  1003. JUCE_API String JUCE_CALLTYPE operator+ (String string1, char characterToAppend);
  1004. /** Concatenates two strings. */
  1005. JUCE_API String JUCE_CALLTYPE operator+ (String string1, wchar_t characterToAppend);
  1006. #if ! JUCE_NATIVE_WCHAR_IS_UTF32
  1007. /** Concatenates two strings. */
  1008. JUCE_API String JUCE_CALLTYPE operator+ (String string1, juce_wchar characterToAppend);
  1009. #endif
  1010. //==============================================================================
  1011. /** Appends a character at the end of a string. */
  1012. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, char characterToAppend);
  1013. /** Appends a character at the end of a string. */
  1014. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, wchar_t characterToAppend);
  1015. #if ! JUCE_NATIVE_WCHAR_IS_UTF32
  1016. /** Appends a character at the end of a string. */
  1017. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, juce_wchar characterToAppend);
  1018. #endif
  1019. /** Appends a string to the end of the first one. */
  1020. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, const char* string2);
  1021. /** Appends a string to the end of the first one. */
  1022. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, const wchar_t* string2);
  1023. /** Appends a string to the end of the first one. */
  1024. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, const String& string2);
  1025. /** Appends a string to the end of the first one. */
  1026. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, StringRef string2);
  1027. /** Appends a decimal number to the end of a string. */
  1028. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, uint8 number);
  1029. /** Appends a decimal number to the end of a string. */
  1030. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, short number);
  1031. /** Appends a decimal number to the end of a string. */
  1032. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, int number);
  1033. /** Appends a decimal number to the end of a string. */
  1034. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, long number);
  1035. /** Appends a decimal number to the end of a string. */
  1036. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, unsigned long number);
  1037. /** Appends a decimal number to the end of a string. */
  1038. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, int64 number);
  1039. /** Appends a decimal number to the end of a string. */
  1040. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, uint64 number);
  1041. /** Appends a decimal number to the end of a string. */
  1042. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, float number);
  1043. /** Appends a decimal number to the end of a string. */
  1044. JUCE_API String& JUCE_CALLTYPE operator<< (String& string1, double number);
  1045. // Automatically creating a String from a bool opens up lots of nasty type conversion edge cases.
  1046. // If you want a String representation of a bool you can cast the bool to an int first.
  1047. String& JUCE_CALLTYPE operator<< (String&, bool) = delete;
  1048. //==============================================================================
  1049. /** Case-sensitive comparison of two strings. */
  1050. JUCE_API bool JUCE_CALLTYPE operator== (const String& string1, const String& string2) noexcept;
  1051. /** Case-sensitive comparison of two strings. */
  1052. JUCE_API bool JUCE_CALLTYPE operator== (const String& string1, const char* string2) noexcept;
  1053. /** Case-sensitive comparison of two strings. */
  1054. JUCE_API bool JUCE_CALLTYPE operator== (const String& string1, const wchar_t* string2) noexcept;
  1055. /** Case-sensitive comparison of two strings. */
  1056. JUCE_API bool JUCE_CALLTYPE operator== (const String& string1, CharPointer_UTF8 string2) noexcept;
  1057. /** Case-sensitive comparison of two strings. */
  1058. JUCE_API bool JUCE_CALLTYPE operator== (const String& string1, CharPointer_UTF16 string2) noexcept;
  1059. /** Case-sensitive comparison of two strings. */
  1060. JUCE_API bool JUCE_CALLTYPE operator== (const String& string1, CharPointer_UTF32 string2) noexcept;
  1061. /** Case-sensitive comparison of two strings. */
  1062. JUCE_API bool JUCE_CALLTYPE operator!= (const String& string1, const String& string2) noexcept;
  1063. /** Case-sensitive comparison of two strings. */
  1064. JUCE_API bool JUCE_CALLTYPE operator!= (const String& string1, const char* string2) noexcept;
  1065. /** Case-sensitive comparison of two strings. */
  1066. JUCE_API bool JUCE_CALLTYPE operator!= (const String& string1, const wchar_t* string2) noexcept;
  1067. /** Case-sensitive comparison of two strings. */
  1068. JUCE_API bool JUCE_CALLTYPE operator!= (const String& string1, CharPointer_UTF8 string2) noexcept;
  1069. /** Case-sensitive comparison of two strings. */
  1070. JUCE_API bool JUCE_CALLTYPE operator!= (const String& string1, CharPointer_UTF16 string2) noexcept;
  1071. /** Case-sensitive comparison of two strings. */
  1072. JUCE_API bool JUCE_CALLTYPE operator!= (const String& string1, CharPointer_UTF32 string2) noexcept;
  1073. /** Case-sensitive comparison of two strings. */
  1074. JUCE_API bool JUCE_CALLTYPE operator> (const String& string1, const String& string2) noexcept;
  1075. /** Case-sensitive comparison of two strings. */
  1076. JUCE_API bool JUCE_CALLTYPE operator< (const String& string1, const String& string2) noexcept;
  1077. /** Case-sensitive comparison of two strings. */
  1078. JUCE_API bool JUCE_CALLTYPE operator>= (const String& string1, const String& string2) noexcept;
  1079. /** Case-sensitive comparison of two strings. */
  1080. JUCE_API bool JUCE_CALLTYPE operator<= (const String& string1, const String& string2) noexcept;
  1081. //==============================================================================
  1082. /** This operator allows you to write a juce String directly to std output streams.
  1083. This is handy for writing strings to std::cout, std::cerr, etc.
  1084. */
  1085. template <class traits>
  1086. std::basic_ostream <char, traits>& JUCE_CALLTYPE operator<< (std::basic_ostream <char, traits>& stream, const String& stringToWrite)
  1087. {
  1088. return stream << stringToWrite.toRawUTF8();
  1089. }
  1090. /** This operator allows you to write a juce String directly to std output streams.
  1091. This is handy for writing strings to std::wcout, std::wcerr, etc.
  1092. */
  1093. template <class traits>
  1094. std::basic_ostream <wchar_t, traits>& JUCE_CALLTYPE operator<< (std::basic_ostream <wchar_t, traits>& stream, const String& stringToWrite)
  1095. {
  1096. return stream << stringToWrite.toWideCharPointer();
  1097. }
  1098. /** Writes a string to an OutputStream as UTF8. */
  1099. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const String& stringToWrite);
  1100. /** Writes a string to an OutputStream as UTF8. */
  1101. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, StringRef stringToWrite);
  1102. } // namespace juce
  1103. #if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS // just used to avoid compiling this under compilers that lack libc++
  1104. namespace std
  1105. {
  1106. template <> struct hash<juce::String>
  1107. {
  1108. size_t operator() (const juce::String& s) const noexcept { return s.hash(); }
  1109. };
  1110. }
  1111. #endif