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.

1131 lines
50KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_STRING_JUCEHEADER__
  19. #define __JUCE_STRING_JUCEHEADER__
  20. #include "juce_CharacterFunctions.h"
  21. class OutputStream;
  22. //==============================================================================
  23. /**
  24. The JUCE String class!
  25. Using a reference-counted internal representation, these strings are fast
  26. and efficient, and there are methods to do just about any operation you'll ever
  27. dream of.
  28. @see StringArray, StringPairArray
  29. */
  30. class JUCE_API String
  31. {
  32. public:
  33. //==============================================================================
  34. /** Creates an empty string.
  35. @see empty
  36. */
  37. String() throw();
  38. /** Creates a copy of another string. */
  39. String (const String& other) throw();
  40. /** Creates a string from a zero-terminated text string.
  41. The string is assumed to be stored in the default system encoding.
  42. */
  43. String (const char* text);
  44. /** Creates a string from an string of characters.
  45. This will use up the the first maxChars characters of the string (or
  46. less if the string is actually shorter)
  47. */
  48. String (const char* text, size_t maxChars);
  49. /** Creates a string from a zero-terminated unicode text string. */
  50. String (const juce_wchar* unicodeText);
  51. /** Creates a string from a unicode text string.
  52. This will use up the the first maxChars characters of the string (or
  53. less if the string is actually shorter)
  54. */
  55. String (const juce_wchar* unicodeText, size_t maxChars);
  56. /** Creates a string from a single character. */
  57. static const String charToString (juce_wchar character);
  58. /** Destructor. */
  59. ~String() throw();
  60. //========================juce_wchar======================================================
  61. /** This is an empty string that can be used whenever one is needed.
  62. It's better to use this than String() because it explains what's going on
  63. and is more efficient.
  64. */
  65. static const String empty;
  66. //==============================================================================
  67. /** Generates a probably-unique 32-bit hashcode from this string. */
  68. int hashCode() const throw();
  69. /** Generates a probably-unique 64-bit hashcode from this string. */
  70. int64 hashCode64() const throw();
  71. /** Returns the number of characters in the string. */
  72. int length() const throw();
  73. //==============================================================================
  74. // Assignment and concatenation operators..
  75. /** Replaces this string's contents with another string. */
  76. String& operator= (const String& other) throw();
  77. /** Appends another string at the end of this one. */
  78. String& operator+= (const juce_wchar* textToAppend);
  79. /** Appends another string at the end of this one. */
  80. String& operator+= (const String& stringToAppend);
  81. /** Appends a character at the end of this string. */
  82. String& operator+= (char characterToAppend);
  83. /** Appends a character at the end of this string. */
  84. String& operator+= (juce_wchar characterToAppend);
  85. /** Appends a decimal number at the end of this string. */
  86. String& operator+= (int numberToAppend);
  87. /** Appends a decimal number at the end of this string. */
  88. String& operator+= (unsigned int numberToAppend);
  89. /** Appends a string at the end of this one.
  90. @param textToAppend the string to add
  91. @param maxCharsToTake the maximum number of characters to take from the string passed in
  92. */
  93. void append (const juce_wchar* textToAppend, int maxCharsToTake);
  94. //==============================================================================
  95. // Comparison methods..
  96. /** Returns true if the string contains no characters.
  97. Note that there's also an isNotEmpty() method to help write readable code.
  98. @see containsNonWhitespaceChars()
  99. */
  100. inline bool isEmpty() const throw() { return text[0] == 0; }
  101. /** Returns true if the string contains at least one character.
  102. Note that there's also an isEmpty() method to help write readable code.
  103. @see containsNonWhitespaceChars()
  104. */
  105. inline bool isNotEmpty() const throw() { return text[0] != 0; }
  106. /** Case-insensitive comparison with another string. */
  107. bool equalsIgnoreCase (const String& other) const throw();
  108. /** Case-insensitive comparison with another string. */
  109. bool equalsIgnoreCase (const juce_wchar* other) const throw();
  110. /** Case-sensitive comparison with another string.
  111. @returns 0 if the two strings are identical; negative if this string
  112. comes before the other one alphabetically, or positive if it
  113. comes after it.
  114. */
  115. int compare (const String& other) const throw();
  116. /** Case-sensitive comparison with another string.
  117. @returns 0 if the two strings are identical; negative if this string
  118. comes before the other one alphabetically, or positive if it
  119. comes after it.
  120. */
  121. int compare (const char* other) const throw();
  122. /** Case-sensitive comparison with another string.
  123. @returns 0 if the two strings are identical; negative if this string
  124. comes before the other one alphabetically, or positive if it
  125. comes after it.
  126. */
  127. int compare (const juce_wchar* other) const throw();
  128. /** Case-insensitive comparison with another string.
  129. @returns 0 if the two strings are identical; negative if this string
  130. comes before the other one alphabetically, or positive if it
  131. comes after it.
  132. */
  133. int compareIgnoreCase (const String& other) const throw();
  134. /** Lexicographic comparison with another string.
  135. The comparison used here is case-insensitive and ignores leading non-alphanumeric
  136. characters, making it good for sorting human-readable strings.
  137. @returns 0 if the two strings are identical; negative if this string
  138. comes before the other one alphabetically, or positive if it
  139. comes after it.
  140. */
  141. int compareLexicographically (const String& other) const throw();
  142. /** Tests whether the string begins with another string.
  143. Uses a case-sensitive comparison.
  144. */
  145. bool startsWith (const juce_wchar* text) const throw();
  146. /** Tests whether the string begins with a particular character.
  147. Uses a case-sensitive comparison.
  148. */
  149. bool startsWithChar (juce_wchar character) const throw();
  150. /** Tests whether the string begins with another string.
  151. Uses a case-insensitive comparison.
  152. */
  153. bool startsWithIgnoreCase (const juce_wchar* text) const throw();
  154. /** Tests whether the string ends with another string.
  155. Uses a case-sensitive comparison.
  156. */
  157. bool endsWith (const juce_wchar* text) const throw();
  158. /** Tests whether the string ends with a particular character.
  159. Uses a case-sensitive comparison.
  160. */
  161. bool endsWithChar (juce_wchar character) const throw();
  162. /** Tests whether the string ends with another string.
  163. Uses a case-insensitive comparison.
  164. */
  165. bool endsWithIgnoreCase (const juce_wchar* text) const throw();
  166. /** Tests whether the string contains another substring.
  167. Uses a case-sensitive comparison.
  168. */
  169. bool contains (const juce_wchar* text) const throw();
  170. /** Tests whether the string contains a particular character.
  171. Uses a case-sensitive comparison.
  172. */
  173. bool containsChar (juce_wchar character) const throw();
  174. /** Tests whether the string contains another substring.
  175. Uses a case-insensitive comparison.
  176. */
  177. bool containsIgnoreCase (const juce_wchar* text) const throw();
  178. /** Tests whether the string contains another substring as a distict word.
  179. @returns true if the string contains this word, surrounded by
  180. non-alphanumeric characters
  181. @see indexOfWholeWord, containsWholeWordIgnoreCase
  182. */
  183. bool containsWholeWord (const juce_wchar* wordToLookFor) const throw();
  184. /** Tests whether the string contains another substring as a distict word.
  185. @returns true if the string contains this word, surrounded by
  186. non-alphanumeric characters
  187. @see indexOfWholeWordIgnoreCase, containsWholeWord
  188. */
  189. bool containsWholeWordIgnoreCase (const juce_wchar* wordToLookFor) const throw();
  190. /** Finds an instance of another substring if it exists as a distict word.
  191. @returns if the string contains this word, surrounded by non-alphanumeric characters,
  192. then this will return the index of the start of the substring. If it isn't
  193. found, then it will return -1
  194. @see indexOfWholeWordIgnoreCase, containsWholeWord
  195. */
  196. int indexOfWholeWord (const juce_wchar* wordToLookFor) const throw();
  197. /** Finds an instance of another substring if it exists as a distict word.
  198. @returns if the string contains this word, surrounded by non-alphanumeric characters,
  199. then this will return the index of the start of the substring. If it isn't
  200. found, then it will return -1
  201. @see indexOfWholeWord, containsWholeWordIgnoreCase
  202. */
  203. int indexOfWholeWordIgnoreCase (const juce_wchar* wordToLookFor) const throw();
  204. /** Looks for any of a set of characters in the string.
  205. Uses a case-sensitive comparison.
  206. @returns true if the string contains any of the characters from
  207. the string that is passed in.
  208. */
  209. bool containsAnyOf (const juce_wchar* charactersItMightContain) const throw();
  210. /** Looks for a set of characters in the string.
  211. Uses a case-sensitive comparison.
  212. @returns true if the all the characters in the string are also found in the
  213. string that is passed in.
  214. */
  215. bool containsOnly (const juce_wchar* charactersItMightContain) const throw();
  216. /** Returns true if this string contains any non-whitespace characters.
  217. This will return false if the string contains only whitespace characters, or
  218. if it's empty.
  219. It is equivalent to calling "myString.trim().isNotEmpty()".
  220. */
  221. bool containsNonWhitespaceChars() const throw();
  222. /** Returns true if the string matches this simple wildcard expression.
  223. So for example String ("abcdef").matchesWildcard ("*DEF", true) would return true.
  224. This isn't a full-blown regex though! The only wildcard characters supported
  225. are "*" and "?". It's mainly intended for filename pattern matching.
  226. */
  227. bool matchesWildcard (const juce_wchar* wildcard, bool ignoreCase) const throw();
  228. //==============================================================================
  229. // Substring location methods..
  230. /** Searches for a character inside this string.
  231. Uses a case-sensitive comparison.
  232. @returns the index of the first occurrence of the character in this
  233. string, or -1 if it's not found.
  234. */
  235. int indexOfChar (juce_wchar characterToLookFor) const throw();
  236. /** Searches for a character inside this string.
  237. Uses a case-sensitive comparison.
  238. @param startIndex the index from which the search should proceed
  239. @param characterToLookFor the character to look for
  240. @returns the index of the first occurrence of the character in this
  241. string, or -1 if it's not found.
  242. */
  243. int indexOfChar (int startIndex, juce_wchar characterToLookFor) const throw();
  244. /** Returns the index of the first character that matches one of the characters
  245. passed-in to this method.
  246. This scans the string, beginning from the startIndex supplied, and if it finds
  247. a character that appears in the string charactersToLookFor, it returns its index.
  248. If none of these characters are found, it returns -1.
  249. If ignoreCase is true, the comparison will be case-insensitive.
  250. @see indexOfChar, lastIndexOfAnyOf
  251. */
  252. int indexOfAnyOf (const juce_wchar* charactersToLookFor,
  253. int startIndex = 0,
  254. bool ignoreCase = false) const throw();
  255. /** Searches for a substring within this string.
  256. Uses a case-sensitive comparison.
  257. @returns the index of the first occurrence of this substring, or -1 if it's not found.
  258. */
  259. int indexOf (const juce_wchar* text) const throw();
  260. /** Searches for a substring within this string.
  261. Uses a case-sensitive comparison.
  262. @param startIndex the index from which the search should proceed
  263. @param textToLookFor the string to search for
  264. @returns the index of the first occurrence of this substring, or -1 if it's not found.
  265. */
  266. int indexOf (int startIndex,
  267. const juce_wchar* textToLookFor) const throw();
  268. /** Searches for a substring within this string.
  269. Uses a case-insensitive comparison.
  270. @returns the index of the first occurrence of this substring, or -1 if it's not found.
  271. */
  272. int indexOfIgnoreCase (const juce_wchar* textToLookFor) const throw();
  273. /** Searches for a substring within this string.
  274. Uses a case-insensitive comparison.
  275. @param startIndex the index from which the search should proceed
  276. @param textToLookFor the string to search for
  277. @returns the index of the first occurrence of this substring, or -1 if it's not found.
  278. */
  279. int indexOfIgnoreCase (int startIndex,
  280. const juce_wchar* textToLookFor) const throw();
  281. /** Searches for a character inside this string (working backwards from the end of the string).
  282. Uses a case-sensitive comparison.
  283. @returns the index of the last occurrence of the character in this
  284. string, or -1 if it's not found.
  285. */
  286. int lastIndexOfChar (juce_wchar character) const throw();
  287. /** Searches for a substring inside this string (working backwards from the end of the string).
  288. Uses a case-sensitive comparison.
  289. @returns the index of the start of the last occurrence of the
  290. substring within this string, or -1 if it's not found.
  291. */
  292. int lastIndexOf (const juce_wchar* textToLookFor) const throw();
  293. /** Searches for a substring inside this string (working backwards from the end of the string).
  294. Uses a case-insensitive comparison.
  295. @returns the index of the start of the last occurrence of the
  296. substring within this string, or -1 if it's not found.
  297. */
  298. int lastIndexOfIgnoreCase (const juce_wchar* textToLookFor) const throw();
  299. /** Returns the index of the last character in this string that matches one of the
  300. characters passed-in to this method.
  301. This scans the string backwards, starting from its end, and if it finds
  302. a character that appears in the string charactersToLookFor, it returns its index.
  303. If none of these characters are found, it returns -1.
  304. If ignoreCase is true, the comparison will be case-insensitive.
  305. @see lastIndexOf, indexOfAnyOf
  306. */
  307. int lastIndexOfAnyOf (const juce_wchar* charactersToLookFor,
  308. bool ignoreCase = false) const throw();
  309. //==============================================================================
  310. // Substring extraction and manipulation methods..
  311. /** Returns the character at this index in the string.
  312. No checks are made to see if the index is within a valid range, so be careful!
  313. */
  314. inline const juce_wchar& operator[] (int index) const throw() { jassert (((unsigned int) index) <= (unsigned int) length()); return text [index]; }
  315. /** Returns a character from the string such that it can also be altered.
  316. This can be used as a way of easily changing characters in the string.
  317. Note that the index passed-in is not checked to see whether it's in-range, so
  318. be careful when using this.
  319. */
  320. juce_wchar& operator[] (int index);
  321. /** Returns the final character of the string.
  322. If the string is empty this will return 0.
  323. */
  324. juce_wchar getLastCharacter() const throw();
  325. //==============================================================================
  326. /** Returns a subsection of the string.
  327. If the range specified is beyond the limits of the string, as much as
  328. possible is returned.
  329. @param startIndex the index of the start of the substring needed
  330. @param endIndex all characters from startIndex up to (but not including)
  331. this index are returned
  332. @see fromFirstOccurrenceOf, dropLastCharacters, getLastCharacters, upToFirstOccurrenceOf
  333. */
  334. const String substring (int startIndex, int endIndex) const;
  335. /** Returns a section of the string, starting from a given position.
  336. @param startIndex the first character to include. If this is beyond the end
  337. of the string, an empty string is returned. If it is zero or
  338. less, the whole string is returned.
  339. @returns the substring from startIndex up to the end of the string
  340. @see dropLastCharacters, getLastCharacters, fromFirstOccurrenceOf, upToFirstOccurrenceOf, fromLastOccurrenceOf
  341. */
  342. const String substring (int startIndex) const;
  343. /** Returns a version of this string with a number of characters removed
  344. from the end.
  345. @param numberToDrop the number of characters to drop from the end of the
  346. string. If this is greater than the length of the string,
  347. an empty string will be returned. If zero or less, the
  348. original string will be returned.
  349. @see substring, fromFirstOccurrenceOf, upToFirstOccurrenceOf, fromLastOccurrenceOf, getLastCharacter
  350. */
  351. const String dropLastCharacters (int numberToDrop) const;
  352. /** Returns a number of characters from the end of the string.
  353. This returns the last numCharacters characters from the end of the string. If the
  354. string is shorter than numCharacters, the whole string is returned.
  355. @see substring, dropLastCharacters, getLastCharacter
  356. */
  357. const String getLastCharacters (int numCharacters) const;
  358. //==============================================================================
  359. /** Returns a section of the string starting from a given substring.
  360. This will search for the first occurrence of the given substring, and
  361. return the section of the string starting from the point where this is
  362. found (optionally not including the substring itself).
  363. e.g. for the string "123456", fromFirstOccurrenceOf ("34", true) would return "3456", and
  364. fromFirstOccurrenceOf ("34", false) would return "56".
  365. If the substring isn't found, the method will return an empty string.
  366. If ignoreCase is true, the comparison will be case-insensitive.
  367. @see upToFirstOccurrenceOf, fromLastOccurrenceOf
  368. */
  369. const String fromFirstOccurrenceOf (const juce_wchar* substringToStartFrom,
  370. bool includeSubStringInResult,
  371. bool ignoreCase) const;
  372. /** Returns a section of the string starting from the last occurrence of a given substring.
  373. Similar to fromFirstOccurrenceOf(), but using the last occurrence of the substring, and
  374. unlike fromFirstOccurrenceOf(), if the substring isn't found, this method will
  375. return the whole of the original string.
  376. @see fromFirstOccurrenceOf, upToLastOccurrenceOf
  377. */
  378. const String fromLastOccurrenceOf (const juce_wchar* substringToFind,
  379. bool includeSubStringInResult,
  380. bool ignoreCase) const;
  381. /** Returns the start of this string, up to the first occurrence of a substring.
  382. This will search for the first occurrence of a given substring, and then
  383. return a copy of the string, up to the position of this substring,
  384. optionally including or excluding the substring itself in the result.
  385. e.g. for the string "123456", upTo ("34", false) would return "12", and
  386. upTo ("34", true) would return "1234".
  387. If the substring isn't found, this will return the whole of the original string.
  388. @see upToLastOccurrenceOf, fromFirstOccurrenceOf
  389. */
  390. const String upToFirstOccurrenceOf (const juce_wchar* substringToEndWith,
  391. bool includeSubStringInResult,
  392. bool ignoreCase) const;
  393. /** Returns the start of this string, up to the last occurrence of a substring.
  394. Similar to upToFirstOccurrenceOf(), but this finds the last occurrence rather than the first.
  395. If the substring isn't found, this will return an empty string.
  396. @see upToFirstOccurrenceOf, fromFirstOccurrenceOf
  397. */
  398. const String upToLastOccurrenceOf (const juce_wchar* substringToFind,
  399. bool includeSubStringInResult,
  400. bool ignoreCase) const;
  401. //==============================================================================
  402. /** Returns a copy of this string with any whitespace characters removed from the start and end. */
  403. const String trim() const;
  404. /** Returns a copy of this string with any whitespace characters removed from the start. */
  405. const String trimStart() const;
  406. /** Returns a copy of this string with any whitespace characters removed from the end. */
  407. const String trimEnd() const;
  408. /** Returns a copy of this string, having removed a specified set of characters from its start.
  409. Characters are removed from the start of the string until it finds one that is not in the
  410. specified set, and then it stops.
  411. @param charactersToTrim the set of characters to remove. This must not be null.
  412. @see trim, trimStart, trimCharactersAtEnd
  413. */
  414. const String trimCharactersAtStart (const juce_wchar* charactersToTrim) const;
  415. /** Returns a copy of this string, having removed a specified set of characters from its end.
  416. Characters are removed from the end of the string until it finds one that is not in the
  417. specified set, and then it stops.
  418. @param charactersToTrim the set of characters to remove. This must not be null.
  419. @see trim, trimEnd, trimCharactersAtStart
  420. */
  421. const String trimCharactersAtEnd (const juce_wchar* charactersToTrim) const;
  422. //==============================================================================
  423. /** Returns an upper-case version of this string. */
  424. const String toUpperCase() const;
  425. /** Returns an lower-case version of this string. */
  426. const String toLowerCase() const;
  427. //==============================================================================
  428. /** Replaces a sub-section of the string with another string.
  429. This will return a copy of this string, with a set of characters
  430. from startIndex to startIndex + numCharsToReplace removed, and with
  431. a new string inserted in their place.
  432. Note that this is a const method, and won't alter the string itself.
  433. @param startIndex the first character to remove. If this is beyond the bounds of the string,
  434. it will be constrained to a valid range.
  435. @param numCharactersToReplace the number of characters to remove. If zero or less, no
  436. characters will be taken out.
  437. @param stringToInsert the new string to insert at startIndex after the characters have been
  438. removed.
  439. */
  440. const String replaceSection (int startIndex,
  441. int numCharactersToReplace,
  442. const juce_wchar* stringToInsert) const;
  443. /** Replaces all occurrences of a substring with another string.
  444. Returns a copy of this string, with any occurrences of stringToReplace
  445. swapped for stringToInsertInstead.
  446. Note that this is a const method, and won't alter the string itself.
  447. */
  448. const String replace (const juce_wchar* stringToReplace,
  449. const juce_wchar* stringToInsertInstead,
  450. bool ignoreCase = false) const;
  451. /** Returns a string with all occurrences of a character replaced with a different one. */
  452. const String replaceCharacter (juce_wchar characterToReplace,
  453. juce_wchar characterToInsertInstead) const;
  454. /** Replaces a set of characters with another set.
  455. Returns a string in which each character from charactersToReplace has been replaced
  456. by the character at the equivalent position in newCharacters (so the two strings
  457. passed in must be the same length).
  458. e.g. translate ("abc", "def") replaces 'a' with 'd', 'b' with 'e', etc.
  459. Note that this is a const method, and won't affect the string itself.
  460. */
  461. const String replaceCharacters (const String& charactersToReplace,
  462. const juce_wchar* charactersToInsertInstead) const;
  463. /** Returns a version of this string that only retains a fixed set of characters.
  464. This will return a copy of this string, omitting any characters which are not
  465. found in the string passed-in.
  466. e.g. for "1122334455", retainCharacters ("432") would return "223344"
  467. Note that this is a const method, and won't alter the string itself.
  468. */
  469. const String retainCharacters (const juce_wchar* charactersToRetain) const;
  470. /** Returns a version of this string with a set of characters removed.
  471. This will return a copy of this string, omitting any characters which are
  472. found in the string passed-in.
  473. e.g. for "1122334455", removeCharacters ("432") would return "1155"
  474. Note that this is a const method, and won't alter the string itself.
  475. */
  476. const String removeCharacters (const juce_wchar* charactersToRemove) const;
  477. /** Returns a section from the start of the string that only contains a certain set of characters.
  478. This returns the leftmost section of the string, up to (and not including) the
  479. first character that doesn't appear in the string passed in.
  480. */
  481. const String initialSectionContainingOnly (const juce_wchar* permittedCharacters) const;
  482. /** Returns a section from the start of the string that only contains a certain set of characters.
  483. This returns the leftmost section of the string, up to (and not including) the
  484. first character that occurs in the string passed in.
  485. */
  486. const String initialSectionNotContaining (const juce_wchar* charactersToStopAt) const;
  487. //==============================================================================
  488. /** Checks whether the string might be in quotation marks.
  489. @returns true if the string begins with a quote character (either a double or single quote).
  490. It is also true if there is whitespace before the quote, but it doesn't check the end of the string.
  491. @see unquoted, quoted
  492. */
  493. bool isQuotedString() const;
  494. /** Removes quotation marks from around the string, (if there are any).
  495. Returns a copy of this string with any quotes removed from its ends. Quotes that aren't
  496. at the ends of the string are not affected. If there aren't any quotes, the original string
  497. is returned.
  498. Note that this is a const method, and won't alter the string itself.
  499. @see isQuotedString, quoted
  500. */
  501. const String unquoted() const;
  502. /** Adds quotation marks around a string.
  503. This will return a copy of the string with a quote at the start and end, (but won't
  504. add the quote if there's already one there, so it's safe to call this on strings that
  505. may already have quotes around them).
  506. Note that this is a const method, and won't alter the string itself.
  507. @param quoteCharacter the character to add at the start and end
  508. @see isQuotedString, unquoted
  509. */
  510. const String quoted (juce_wchar quoteCharacter = JUCE_T('"')) const;
  511. //==============================================================================
  512. /** Creates a string which is a version of a string repeated and joined together.
  513. @param stringToRepeat the string to repeat
  514. @param numberOfTimesToRepeat how many times to repeat it
  515. */
  516. static const String repeatedString (const juce_wchar* stringToRepeat,
  517. int numberOfTimesToRepeat);
  518. /** Returns a copy of this string with the specified character repeatedly added to its
  519. beginning until the total length is at least the minimum length specified.
  520. */
  521. const String paddedLeft (juce_wchar padCharacter, int minimumLength) const;
  522. /** Returns a copy of this string with the specified character repeatedly added to its
  523. end until the total length is at least the minimum length specified.
  524. */
  525. const String paddedRight (juce_wchar padCharacter, int minimumLength) const;
  526. /** Creates a string from data in an unknown format.
  527. This looks at some binary data and tries to guess whether it's Unicode
  528. or 8-bit characters, then returns a string that represents it correctly.
  529. Should be able to handle Unicode endianness correctly, by looking at
  530. the first two bytes.
  531. */
  532. static const String createStringFromData (const void* data, int size);
  533. /** Creates a String from a printf-style parameter list.
  534. I don't like this method. I don't use it myself, and I recommend avoiding it and
  535. using the operator<< methods or pretty much anything else instead. It's only provided
  536. here because of the popular unrest that was stirred-up when I tried to remove it...
  537. If you're really determined to use it, at least make sure that you never, ever,
  538. pass any String objects to it as parameters.
  539. */
  540. static const String formatted (const juce_wchar* formatString, ... );
  541. //==============================================================================
  542. // Numeric conversions..
  543. /** Creates a string containing this signed 32-bit integer as a decimal number.
  544. @see getIntValue, getFloatValue, getDoubleValue, toHexString
  545. */
  546. explicit String (int decimalInteger);
  547. /** Creates a string containing this unsigned 32-bit integer as a decimal number.
  548. @see getIntValue, getFloatValue, getDoubleValue, toHexString
  549. */
  550. explicit String (unsigned int decimalInteger);
  551. /** Creates a string containing this signed 16-bit integer as a decimal number.
  552. @see getIntValue, getFloatValue, getDoubleValue, toHexString
  553. */
  554. explicit String (short decimalInteger);
  555. /** Creates a string containing this unsigned 16-bit integer as a decimal number.
  556. @see getIntValue, getFloatValue, getDoubleValue, toHexString
  557. */
  558. explicit String (unsigned short decimalInteger);
  559. /** Creates a string containing this signed 64-bit integer as a decimal number.
  560. @see getLargeIntValue, getFloatValue, getDoubleValue, toHexString
  561. */
  562. explicit String (int64 largeIntegerValue);
  563. /** Creates a string containing this unsigned 64-bit integer as a decimal number.
  564. @see getLargeIntValue, getFloatValue, getDoubleValue, toHexString
  565. */
  566. explicit String (uint64 largeIntegerValue);
  567. /** Creates a string representing this floating-point number.
  568. @param floatValue the value to convert to a string
  569. @param numberOfDecimalPlaces if this is > 0, it will format the number using that many
  570. decimal places, and will not use exponent notation. If 0 or
  571. less, it will use exponent notation if necessary.
  572. @see getDoubleValue, getIntValue
  573. */
  574. explicit String (float floatValue,
  575. int numberOfDecimalPlaces = 0);
  576. /** Creates a string representing this floating-point number.
  577. @param doubleValue the value to convert to a string
  578. @param numberOfDecimalPlaces if this is > 0, it will format the number using that many
  579. decimal places, and will not use exponent notation. If 0 or
  580. less, it will use exponent notation if necessary.
  581. @see getFloatValue, getIntValue
  582. */
  583. explicit String (double doubleValue,
  584. int numberOfDecimalPlaces = 0);
  585. /** Reads the value of the string as a decimal number (up to 32 bits in size).
  586. @returns the value of the string as a 32 bit signed base-10 integer.
  587. @see getTrailingIntValue, getHexValue32, getHexValue64
  588. */
  589. int getIntValue() const throw();
  590. /** Reads the value of the string as a decimal number (up to 64 bits in size).
  591. @returns the value of the string as a 64 bit signed base-10 integer.
  592. */
  593. int64 getLargeIntValue() const throw();
  594. /** Parses a decimal number from the end of the string.
  595. This will look for a value at the end of the string.
  596. e.g. for "321 xyz654" it will return 654; for "2 3 4" it'll return 4.
  597. Negative numbers are not handled, so "xyz-5" returns 5.
  598. @see getIntValue
  599. */
  600. int getTrailingIntValue() const throw();
  601. /** Parses this string as a floating point number.
  602. @returns the value of the string as a 32-bit floating point value.
  603. @see getDoubleValue
  604. */
  605. float getFloatValue() const throw();
  606. /** Parses this string as a floating point number.
  607. @returns the value of the string as a 64-bit floating point value.
  608. @see getFloatValue
  609. */
  610. double getDoubleValue() const throw();
  611. /** Parses the string as a hexadecimal number.
  612. Non-hexadecimal characters in the string are ignored.
  613. If the string contains too many characters, then the lowest significant
  614. digits are returned, e.g. "ffff12345678" would produce 0x12345678.
  615. @returns a 32-bit number which is the value of the string in hex.
  616. */
  617. int getHexValue32() const throw();
  618. /** Parses the string as a hexadecimal number.
  619. Non-hexadecimal characters in the string are ignored.
  620. If the string contains too many characters, then the lowest significant
  621. digits are returned, e.g. "ffff1234567812345678" would produce 0x1234567812345678.
  622. @returns a 64-bit number which is the value of the string in hex.
  623. */
  624. int64 getHexValue64() const throw();
  625. /** Creates a string representing this 32-bit value in hexadecimal. */
  626. static const String toHexString (int number);
  627. /** Creates a string representing this 64-bit value in hexadecimal. */
  628. static const String toHexString (int64 number);
  629. /** Creates a string representing this 16-bit value in hexadecimal. */
  630. static const String toHexString (short number);
  631. /** Creates a string containing a hex dump of a block of binary data.
  632. @param data the binary data to use as input
  633. @param size how many bytes of data to use
  634. @param groupSize how many bytes are grouped together before inserting a
  635. space into the output. e.g. group size 0 has no spaces,
  636. group size 1 looks like: "be a1 c2 ff", group size 2 looks
  637. like "bea1 c2ff".
  638. */
  639. static const String toHexString (const unsigned char* data,
  640. int size,
  641. int groupSize = 1);
  642. //==============================================================================
  643. /** Returns a unicode version of this string.
  644. Because it returns a reference to the string's internal data, the pointer
  645. that is returned must not be stored anywhere, as it can become invalid whenever
  646. any string methods (even some const ones!) are called.
  647. */
  648. inline operator const juce_wchar*() const throw() { return text; }
  649. //==============================================================================
  650. /** Returns a unicode version of this string.
  651. Because it returns a reference to the string's internal data, the pointer
  652. that is returned must not be stored anywhere, as it can become invalid whenever
  653. any string methods (even some const ones!) are called.
  654. */
  655. inline operator juce_wchar*() throw() { return text; }
  656. //==============================================================================
  657. /** Returns a pointer to a UTF-8 version of this string.
  658. Because it returns a reference to the string's internal data, the pointer
  659. that is returned must not be stored anywhere, as it can be deleted whenever the
  660. string changes.
  661. @see getNumBytesAsUTF8, fromUTF8, copyToUTF8, toCString
  662. */
  663. const char* toUTF8() const;
  664. /** Creates a String from a UTF-8 encoded buffer.
  665. If the size is < 0, it'll keep reading until it hits a zero.
  666. */
  667. static const String fromUTF8 (const char* utf8buffer, int bufferSizeBytes = -1);
  668. /** Returns the number of bytes required to represent this string as UTF8.
  669. The number returned does NOT include the trailing zero.
  670. @see toUTF8, copyToUTF8
  671. */
  672. int getNumBytesAsUTF8() const throw();
  673. /** Copies the string to a buffer as UTF-8 characters.
  674. Returns the number of bytes copied to the buffer, including the terminating null
  675. character.
  676. @param destBuffer the place to copy it to; if this is a null pointer,
  677. the method just returns the number of bytes required
  678. (including the terminating null character).
  679. @param maxBufferSizeBytes the size of the destination buffer, in bytes. If the
  680. string won't fit, it'll put in as many as it can while
  681. still allowing for a terminating null char at the end, and
  682. will return the number of bytes that were actually used.
  683. */
  684. int copyToUTF8 (char* destBuffer, int maxBufferSizeBytes) const throw();
  685. //==============================================================================
  686. /** Returns a version of this string using the default 8-bit multi-byte system encoding.
  687. Because it returns a reference to the string's internal data, the pointer
  688. that is returned must not be stored anywhere, as it can be deleted whenever the
  689. string changes.
  690. @see getNumBytesAsCString, copyToCString, toUTF8
  691. */
  692. const char* toCString() const;
  693. /** Returns the number of bytes
  694. */
  695. int getNumBytesAsCString() const throw();
  696. /** Copies the string to a buffer.
  697. @param destBuffer the place to copy it to; if this is a null pointer,
  698. the method just returns the number of bytes required
  699. (including the terminating null character).
  700. @param maxBufferSizeBytes the size of the destination buffer, in bytes. If the
  701. string won't fit, it'll put in as many as it can while
  702. still allowing for a terminating null char at the end, and
  703. will return the number of bytes that were actually used.
  704. */
  705. int copyToCString (char* destBuffer, int maxBufferSizeBytes) const throw();
  706. //==============================================================================
  707. /** Copies the string to a unicode buffer.
  708. @param destBuffer the place to copy it to
  709. @param maxCharsToCopy the maximum number of characters to copy to the buffer,
  710. not including the tailing zero, so this shouldn't be
  711. larger than the size of your destination buffer - 1
  712. */
  713. void copyToUnicode (juce_wchar* destBuffer, int maxCharsToCopy) const throw();
  714. //==============================================================================
  715. /** Increases the string's internally allocated storage.
  716. Although the string's contents won't be affected by this call, it will
  717. increase the amount of memory allocated internally for the string to grow into.
  718. If you're about to make a large number of calls to methods such
  719. as += or <<, it's more efficient to preallocate enough extra space
  720. beforehand, so that these methods won't have to keep resizing the string
  721. to append the extra characters.
  722. @param numCharsNeeded the number of characters to allocate storage for. If this
  723. value is less than the currently allocated size, it will
  724. have no effect.
  725. */
  726. void preallocateStorage (size_t numCharsNeeded);
  727. /** Swaps the contents of this string with another one.
  728. This is a very fast operation, as no allocation or copying needs to be done.
  729. */
  730. void swapWith (String& other) throw();
  731. //==============================================================================
  732. /** A helper class to improve performance when concatenating many large strings
  733. together.
  734. Because appending one string to another involves measuring the length of
  735. both strings, repeatedly doing this for many long strings will become
  736. an exponentially slow operation. This class uses some internal state to
  737. avoid that, so that each append operation only needs to measure the length
  738. of the appended string.
  739. */
  740. class JUCE_API Concatenator
  741. {
  742. public:
  743. Concatenator (String& stringToAppendTo);
  744. ~Concatenator();
  745. void append (const String& s);
  746. private:
  747. String& result;
  748. int nextIndex;
  749. Concatenator (const Concatenator&);
  750. Concatenator& operator= (const Concatenator&);
  751. };
  752. //==============================================================================
  753. juce_UseDebuggingNewOperator // (adds debugging info to find leaked objects)
  754. private:
  755. //==============================================================================
  756. juce_wchar* text;
  757. //==============================================================================
  758. // internal constructor that preallocates a certain amount of memory
  759. String (size_t numChars, int dummyVariable);
  760. String (const String& stringToCopy, size_t charsToAllocate);
  761. void createInternal (const juce_wchar* text, size_t numChars);
  762. void appendInternal (const juce_wchar* text, int numExtraChars);
  763. };
  764. //==============================================================================
  765. /** Concatenates two strings. */
  766. const String JUCE_CALLTYPE operator+ (const char* string1, const String& string2);
  767. /** Concatenates two strings. */
  768. const String JUCE_CALLTYPE operator+ (const juce_wchar* string1, const String& string2);
  769. /** Concatenates two strings. */
  770. const String JUCE_CALLTYPE operator+ (char string1, const String& string2);
  771. /** Concatenates two strings. */
  772. const String JUCE_CALLTYPE operator+ (juce_wchar string1, const String& string2);
  773. /** Concatenates two strings. */
  774. const String JUCE_CALLTYPE operator+ (String string1, const String& string2);
  775. /** Concatenates two strings. */
  776. const String JUCE_CALLTYPE operator+ (String string1, const char* string2);
  777. /** Concatenates two strings. */
  778. const String JUCE_CALLTYPE operator+ (String string1, const juce_wchar* string2);
  779. /** Concatenates two strings. */
  780. const String JUCE_CALLTYPE operator+ (String string1, char characterToAppend);
  781. /** Concatenates two strings. */
  782. const String JUCE_CALLTYPE operator+ (String string1, juce_wchar characterToAppend);
  783. //==============================================================================
  784. /** Appends a character at the end of a string. */
  785. String& JUCE_CALLTYPE operator<< (String& string1, char characterToAppend);
  786. /** Appends a character at the end of a string. */
  787. String& JUCE_CALLTYPE operator<< (String& string1, juce_wchar characterToAppend);
  788. /** Appends a string to the end of the first one. */
  789. String& JUCE_CALLTYPE operator<< (String& string1, const char* string2);
  790. /** Appends a string to the end of the first one. */
  791. String& JUCE_CALLTYPE operator<< (String& string1, const juce_wchar* string2);
  792. /** Appends a string to the end of the first one. */
  793. String& JUCE_CALLTYPE operator<< (String& string1, const String& string2);
  794. /** Appends a decimal number at the end of a string. */
  795. String& JUCE_CALLTYPE operator<< (String& string1, short number);
  796. /** Appends a decimal number at the end of a string. */
  797. String& JUCE_CALLTYPE operator<< (String& string1, int number);
  798. /** Appends a decimal number at the end of a string. */
  799. String& JUCE_CALLTYPE operator<< (String& string1, unsigned int number);
  800. /** Appends a decimal number at the end of a string. */
  801. String& JUCE_CALLTYPE operator<< (String& string1, long number);
  802. /** Appends a decimal number at the end of a string. */
  803. String& JUCE_CALLTYPE operator<< (String& string1, unsigned long number);
  804. /** Appends a decimal number at the end of a string. */
  805. String& JUCE_CALLTYPE operator<< (String& string1, float number);
  806. /** Appends a decimal number at the end of a string. */
  807. String& JUCE_CALLTYPE operator<< (String& string1, double number);
  808. //==============================================================================
  809. /** Case-sensitive comparison of two strings. */
  810. bool JUCE_CALLTYPE operator== (const String& string1, const String& string2) throw();
  811. /** Case-sensitive comparison of two strings. */
  812. bool JUCE_CALLTYPE operator== (const String& string1, const char* string2) throw();
  813. /** Case-sensitive comparison of two strings. */
  814. bool JUCE_CALLTYPE operator== (const String& string1, const juce_wchar* string2) throw();
  815. /** Case-sensitive comparison of two strings. */
  816. bool JUCE_CALLTYPE operator!= (const String& string1, const String& string2) throw();
  817. /** Case-sensitive comparison of two strings. */
  818. bool JUCE_CALLTYPE operator!= (const String& string1, const char* string2) throw();
  819. /** Case-sensitive comparison of two strings. */
  820. bool JUCE_CALLTYPE operator!= (const String& string1, const juce_wchar* string2) throw();
  821. /** Case-sensitive comparison of two strings. */
  822. bool JUCE_CALLTYPE operator> (const String& string1, const String& string2) throw();
  823. /** Case-sensitive comparison of two strings. */
  824. bool JUCE_CALLTYPE operator< (const String& string1, const String& string2) throw();
  825. /** Case-sensitive comparison of two strings. */
  826. bool JUCE_CALLTYPE operator>= (const String& string1, const String& string2) throw();
  827. /** Case-sensitive comparison of two strings. */
  828. bool JUCE_CALLTYPE operator<= (const String& string1, const String& string2) throw();
  829. //==============================================================================
  830. /** This streaming override allows you to pass a juce String directly into std output streams.
  831. This is very handy for writing strings to std::cout, std::cerr, etc.
  832. */
  833. template <class charT, class traits>
  834. std::basic_ostream <charT, traits>& JUCE_CALLTYPE operator<< (std::basic_ostream <charT, traits>& stream, const String& stringToWrite)
  835. {
  836. return stream << stringToWrite.toUTF8();
  837. }
  838. /** Writes a string to an OutputStream as UTF8. */
  839. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const String& text);
  840. #endif // __JUCE_STRING_JUCEHEADER__