juce_String.h 63 KB

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