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.

1348 lines
62KB

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