juce_StringArray.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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_STRINGARRAY_H_INCLUDED
  22. #define JUCE_STRINGARRAY_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. A special array for holding a list of strings.
  26. @see String, StringPairArray
  27. */
  28. class JUCE_API StringArray
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates an empty string array */
  33. StringArray() noexcept;
  34. /** Creates a copy of another string array */
  35. StringArray (const StringArray&);
  36. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  37. StringArray (StringArray&&) noexcept;
  38. #endif
  39. /** Creates an array containing a single string. */
  40. explicit StringArray (const String& firstValue);
  41. /** Creates an array from a raw array of strings.
  42. @param strings an array of strings to add
  43. @param numberOfStrings how many items there are in the array
  44. */
  45. StringArray (const String* strings, int numberOfStrings);
  46. /** Creates a copy of an array of string literals.
  47. @param strings an array of strings to add. Null pointers in the array will be
  48. treated as empty strings
  49. @param numberOfStrings how many items there are in the array
  50. */
  51. StringArray (const char* const* strings, int numberOfStrings);
  52. /** Creates a copy of a null-terminated array of string literals.
  53. Each item from the array passed-in is added, until it encounters a null pointer,
  54. at which point it stops.
  55. */
  56. explicit StringArray (const char* const* strings);
  57. /** Creates a copy of a null-terminated array of string literals.
  58. Each item from the array passed-in is added, until it encounters a null pointer,
  59. at which point it stops.
  60. */
  61. explicit StringArray (const wchar_t* const* strings);
  62. /** Creates a copy of an array of string literals.
  63. @param strings an array of strings to add. Null pointers in the array will be
  64. treated as empty strings
  65. @param numberOfStrings how many items there are in the array
  66. */
  67. StringArray (const wchar_t* const* strings, int numberOfStrings);
  68. #if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS
  69. StringArray (const std::initializer_list<const char*>& strings);
  70. #endif
  71. /** Destructor. */
  72. ~StringArray();
  73. /** Copies the contents of another string array into this one */
  74. StringArray& operator= (const StringArray&);
  75. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  76. StringArray& operator= (StringArray&&) noexcept;
  77. #endif
  78. /** Swaps the contents of this and another StringArray. */
  79. void swapWith (StringArray&) noexcept;
  80. //==============================================================================
  81. /** Compares two arrays.
  82. Comparisons are case-sensitive.
  83. @returns true only if the other array contains exactly the same strings in the same order
  84. */
  85. bool operator== (const StringArray&) const noexcept;
  86. /** Compares two arrays.
  87. Comparisons are case-sensitive.
  88. @returns false if the other array contains exactly the same strings in the same order
  89. */
  90. bool operator!= (const StringArray&) const noexcept;
  91. //==============================================================================
  92. /** Returns the number of strings in the array */
  93. inline int size() const noexcept { return strings.size(); }
  94. /** Returns true if the array is empty, false otherwise. */
  95. inline bool isEmpty() const noexcept { return size() == 0; }
  96. /** Returns one of the strings from the array.
  97. If the index is out-of-range, an empty string is returned.
  98. Obviously the reference returned shouldn't be stored for later use, as the
  99. string it refers to may disappear when the array changes.
  100. */
  101. const String& operator[] (int index) const noexcept;
  102. /** Returns a reference to one of the strings in the array.
  103. This lets you modify a string in-place in the array, but you must be sure that
  104. the index is in-range.
  105. */
  106. String& getReference (int index) noexcept;
  107. /** Returns a pointer to the first String in the array.
  108. This method is provided for compatibility with standard C++ iteration mechanisms.
  109. */
  110. inline String* begin() const noexcept { return strings.begin(); }
  111. /** Returns a pointer to the String which follows the last element in the array.
  112. This method is provided for compatibility with standard C++ iteration mechanisms.
  113. */
  114. inline String* end() const noexcept { return strings.end(); }
  115. /** Searches for a string in the array.
  116. The comparison will be case-insensitive if the ignoreCase parameter is true.
  117. @returns true if the string is found inside the array
  118. */
  119. bool contains (StringRef stringToLookFor,
  120. bool ignoreCase = false) const;
  121. /** Searches for a string in the array.
  122. The comparison will be case-insensitive if the ignoreCase parameter is true.
  123. @param stringToLookFor the string to try to find
  124. @param ignoreCase whether the comparison should be case-insensitive
  125. @param startIndex the first index to start searching from
  126. @returns the index of the first occurrence of the string in this array,
  127. or -1 if it isn't found.
  128. */
  129. int indexOf (StringRef stringToLookFor,
  130. bool ignoreCase = false,
  131. int startIndex = 0) const;
  132. //==============================================================================
  133. /** Appends a string at the end of the array. */
  134. void add (const String& stringToAdd);
  135. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  136. /** Appends a string at the end of the array. */
  137. void add (String&& stringToAdd);
  138. #endif
  139. /** Inserts a string into the array.
  140. This will insert a string into the array at the given index, moving
  141. up the other elements to make room for it.
  142. If the index is less than zero or greater than the size of the array,
  143. the new string will be added to the end of the array.
  144. */
  145. void insert (int index, const String& stringToAdd);
  146. /** Adds a string to the array as long as it's not already in there.
  147. The search can optionally be case-insensitive.
  148. */
  149. void addIfNotAlreadyThere (const String& stringToAdd, bool ignoreCase = false);
  150. /** Replaces one of the strings in the array with another one.
  151. If the index is higher than the array's size, the new string will be
  152. added to the end of the array; if it's less than zero nothing happens.
  153. */
  154. void set (int index, const String& newString);
  155. /** Appends some strings from another array to the end of this one.
  156. @param other the array to add
  157. @param startIndex the first element of the other array to add
  158. @param numElementsToAdd the maximum number of elements to add (if this is
  159. less than zero, they are all added)
  160. */
  161. void addArray (const StringArray& other,
  162. int startIndex = 0,
  163. int numElementsToAdd = -1);
  164. /** Merges the strings from another array into this one.
  165. This will not add a string that already exists.
  166. @param other the array to add
  167. @param ignoreCase ignore case when merging
  168. */
  169. void mergeArray (const StringArray& other,
  170. bool ignoreCase = false);
  171. /** Breaks up a string into tokens and adds them to this array.
  172. This will tokenise the given string using whitespace characters as the
  173. token delimiters, and will add these tokens to the end of the array.
  174. @returns the number of tokens added
  175. @see fromTokens
  176. */
  177. int addTokens (StringRef stringToTokenise, bool preserveQuotedStrings);
  178. /** Breaks up a string into tokens and adds them to this array.
  179. This will tokenise the given string (using the string passed in to define the
  180. token delimiters), and will add these tokens to the end of the array.
  181. @param stringToTokenise the string to tokenise
  182. @param breakCharacters a string of characters, any of which will be considered
  183. to be a token delimiter.
  184. @param quoteCharacters if this string isn't empty, it defines a set of characters
  185. which are treated as quotes. Any text occurring
  186. between quotes is not broken up into tokens.
  187. @returns the number of tokens added
  188. @see fromTokens
  189. */
  190. int addTokens (StringRef stringToTokenise,
  191. StringRef breakCharacters,
  192. StringRef quoteCharacters);
  193. /** Breaks up a string into lines and adds them to this array.
  194. This breaks a string down into lines separated by \\n or \\r\\n, and adds each line
  195. to the array. Line-break characters are omitted from the strings that are added to
  196. the array.
  197. */
  198. int addLines (StringRef stringToBreakUp);
  199. /** Returns an array containing the tokens in a given string.
  200. This will tokenise the given string using whitespace characters as the
  201. token delimiters, and return the parsed tokens as an array.
  202. @see addTokens
  203. */
  204. static StringArray fromTokens (StringRef stringToTokenise,
  205. bool preserveQuotedStrings);
  206. /** Returns an array containing the tokens in a given string.
  207. This will tokenise the given string using the breakCharacters string to define
  208. the token delimiters, and will return the parsed tokens as an array.
  209. @param stringToTokenise the string to tokenise
  210. @param breakCharacters a string of characters, any of which will be considered
  211. to be a token delimiter.
  212. @param quoteCharacters if this string isn't empty, it defines a set of characters
  213. which are treated as quotes. Any text occurring
  214. between quotes is not broken up into tokens.
  215. @see addTokens
  216. */
  217. static StringArray fromTokens (StringRef stringToTokenise,
  218. StringRef breakCharacters,
  219. StringRef quoteCharacters);
  220. /** Returns an array containing the lines in a given string.
  221. This breaks a string down into lines separated by \\n or \\r\\n, and returns an
  222. array containing these lines. Line-break characters are omitted from the strings that
  223. are added to the array.
  224. */
  225. static StringArray fromLines (StringRef stringToBreakUp);
  226. //==============================================================================
  227. /** Removes all elements from the array. */
  228. void clear();
  229. /** Removes all elements from the array without freeing the array's allocated storage.
  230. @see clear
  231. */
  232. void clearQuick();
  233. /** Removes a string from the array.
  234. If the index is out-of-range, no action will be taken.
  235. */
  236. void remove (int index);
  237. /** Finds a string in the array and removes it.
  238. This will remove all occurrences of the given string from the array.
  239. The comparison may be case-insensitive depending on the ignoreCase parameter.
  240. */
  241. void removeString (StringRef stringToRemove,
  242. bool ignoreCase = false);
  243. /** Removes a range of elements from the array.
  244. This will remove a set of elements, starting from the given index,
  245. and move subsequent elements down to close the gap.
  246. If the range extends beyond the bounds of the array, it will
  247. be safely clipped to the size of the array.
  248. @param startIndex the index of the first element to remove
  249. @param numberToRemove how many elements should be removed
  250. */
  251. void removeRange (int startIndex, int numberToRemove);
  252. /** Removes any duplicated elements from the array.
  253. If any string appears in the array more than once, only the first occurrence of
  254. it will be retained.
  255. @param ignoreCase whether to use a case-insensitive comparison
  256. */
  257. void removeDuplicates (bool ignoreCase);
  258. /** Removes empty strings from the array.
  259. @param removeWhitespaceStrings if true, strings that only contain whitespace
  260. characters will also be removed
  261. */
  262. void removeEmptyStrings (bool removeWhitespaceStrings = true);
  263. /** Moves one of the strings to a different position.
  264. This will move the string to a specified index, shuffling along
  265. any intervening elements as required.
  266. So for example, if you have the array { 0, 1, 2, 3, 4, 5 } then calling
  267. move (2, 4) would result in { 0, 1, 3, 4, 2, 5 }.
  268. @param currentIndex the index of the value to be moved. If this isn't a
  269. valid index, then nothing will be done
  270. @param newIndex the index at which you'd like this value to end up. If this
  271. is less than zero, the value will be moved to the end
  272. of the array
  273. */
  274. void move (int currentIndex, int newIndex) noexcept;
  275. /** Deletes any whitespace characters from the starts and ends of all the strings. */
  276. void trim();
  277. /** Adds numbers to the strings in the array, to make each string unique.
  278. This will add numbers to the ends of groups of similar strings.
  279. e.g. if there are two "moose" strings, they will become "moose (1)" and "moose (2)"
  280. @param ignoreCaseWhenComparing whether the comparison used is case-insensitive
  281. @param appendNumberToFirstInstance whether the first of a group of similar strings
  282. also has a number appended to it.
  283. @param preNumberString when adding a number, this string is added before the number.
  284. If you pass nullptr, a default string will be used, which adds
  285. brackets around the number.
  286. @param postNumberString this string is appended after any numbers that are added.
  287. If you pass nullptr, a default string will be used, which adds
  288. brackets around the number.
  289. */
  290. void appendNumbersToDuplicates (bool ignoreCaseWhenComparing,
  291. bool appendNumberToFirstInstance,
  292. CharPointer_UTF8 preNumberString = CharPointer_UTF8 (nullptr),
  293. CharPointer_UTF8 postNumberString = CharPointer_UTF8 (nullptr));
  294. //==============================================================================
  295. /** Joins the strings in the array together into one string.
  296. This will join a range of elements from the array into a string, separating
  297. them with a given string.
  298. e.g. joinIntoString (",") will turn an array of "a" "b" and "c" into "a,b,c".
  299. @param separatorString the string to insert between all the strings
  300. @param startIndex the first element to join
  301. @param numberOfElements how many elements to join together. If this is less
  302. than zero, all available elements will be used.
  303. */
  304. String joinIntoString (StringRef separatorString,
  305. int startIndex = 0,
  306. int numberOfElements = -1) const;
  307. //==============================================================================
  308. /** Sorts the array into alphabetical order.
  309. @param ignoreCase if true, the comparisons used will be case-sensitive.
  310. */
  311. void sort (bool ignoreCase);
  312. /** Sorts the array using extra language-aware rules to do a better job of comparing
  313. words containing spaces and numbers.
  314. @see String::compareNatural()
  315. */
  316. void sortNatural();
  317. //==============================================================================
  318. /** Increases the array's internal storage to hold a minimum number of elements.
  319. Calling this before adding a large known number of elements means that
  320. the array won't have to keep dynamically resizing itself as the elements
  321. are added, and it'll therefore be more efficient.
  322. */
  323. void ensureStorageAllocated (int minNumElements);
  324. /** Reduces the amount of storage being used by the array.
  325. Arrays typically allocate slightly more storage than they need, and after
  326. removing elements, they may have quite a lot of unused space allocated.
  327. This method will reduce the amount of allocated storage to a minimum.
  328. */
  329. void minimiseStorageOverheads();
  330. /** This is the array holding the actual strings. This is public to allow direct access
  331. to array methods that may not already be provided by the StringArray class.
  332. */
  333. Array<String> strings;
  334. private:
  335. JUCE_LEAK_DETECTOR (StringArray)
  336. };
  337. #endif // JUCE_STRINGARRAY_H_INCLUDED