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.

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