juce_String.h 65 KB

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