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.

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