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.

1397 lines
65KB

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