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
61KB

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