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.

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