The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

440 lines
19KB

  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. @return true if the string has been added, false otherwise.
  149. */
  150. bool addIfNotAlreadyThere (const String& stringToAdd, bool ignoreCase = false);
  151. /** Replaces one of the strings in the array with another one.
  152. If the index is higher than the array's size, the new string will be
  153. added to the end of the array; if it's less than zero nothing happens.
  154. */
  155. void set (int index, const String& newString);
  156. /** Appends some strings from another array to the end of this one.
  157. @param other the array to add
  158. @param startIndex the first element of the other array to add
  159. @param numElementsToAdd the maximum number of elements to add (if this is
  160. less than zero, they are all added)
  161. */
  162. void addArray (const StringArray& other,
  163. int startIndex = 0,
  164. int numElementsToAdd = -1);
  165. /** Merges the strings from another array into this one.
  166. This will not add a string that already exists.
  167. @param other the array to add
  168. @param ignoreCase ignore case when merging
  169. */
  170. void mergeArray (const StringArray& other,
  171. bool ignoreCase = false);
  172. /** Breaks up a string into tokens and adds them to this array.
  173. This will tokenise the given string using whitespace characters as the
  174. token delimiters, and will add these tokens to the end of the array.
  175. @returns the number of tokens added
  176. @see fromTokens
  177. */
  178. int addTokens (StringRef stringToTokenise, bool preserveQuotedStrings);
  179. /** Breaks up a string into tokens and adds them to this array.
  180. This will tokenise the given string (using the string passed in to define the
  181. token delimiters), and will add these tokens to the end of the array.
  182. @param stringToTokenise the string to tokenise
  183. @param breakCharacters a string of characters, any of which will be considered
  184. to be a token delimiter.
  185. @param quoteCharacters if this string isn't empty, it defines a set of characters
  186. which are treated as quotes. Any text occurring
  187. between quotes is not broken up into tokens.
  188. @returns the number of tokens added
  189. @see fromTokens
  190. */
  191. int addTokens (StringRef stringToTokenise,
  192. StringRef breakCharacters,
  193. StringRef quoteCharacters);
  194. /** Breaks up a string into lines and adds them to this array.
  195. This breaks a string down into lines separated by \\n or \\r\\n, and adds each line
  196. to the array. Line-break characters are omitted from the strings that are added to
  197. the array.
  198. */
  199. int addLines (StringRef stringToBreakUp);
  200. /** Returns an array containing the tokens in a given string.
  201. This will tokenise the given string using whitespace characters as the
  202. token delimiters, and return the parsed tokens as an array.
  203. @see addTokens
  204. */
  205. static StringArray fromTokens (StringRef stringToTokenise,
  206. bool preserveQuotedStrings);
  207. /** Returns an array containing the tokens in a given string.
  208. This will tokenise the given string using the breakCharacters string to define
  209. the token delimiters, and will return the parsed tokens as an array.
  210. @param stringToTokenise the string to tokenise
  211. @param breakCharacters a string of characters, any of which will be considered
  212. to be a token delimiter.
  213. @param quoteCharacters if this string isn't empty, it defines a set of characters
  214. which are treated as quotes. Any text occurring
  215. between quotes is not broken up into tokens.
  216. @see addTokens
  217. */
  218. static StringArray fromTokens (StringRef stringToTokenise,
  219. StringRef breakCharacters,
  220. StringRef quoteCharacters);
  221. /** Returns an array containing the lines in a given string.
  222. This breaks a string down into lines separated by \\n or \\r\\n, and returns an
  223. array containing these lines. Line-break characters are omitted from the strings that
  224. are added to the array.
  225. */
  226. static StringArray fromLines (StringRef stringToBreakUp);
  227. //==============================================================================
  228. /** Removes all elements from the array. */
  229. void clear();
  230. /** Removes all elements from the array without freeing the array's allocated storage.
  231. @see clear
  232. */
  233. void clearQuick();
  234. /** Removes a string from the array.
  235. If the index is out-of-range, no action will be taken.
  236. */
  237. void remove (int index);
  238. /** Finds a string in the array and removes it.
  239. This will remove all occurrences of the given string from the array.
  240. The comparison may be case-insensitive depending on the ignoreCase parameter.
  241. */
  242. void removeString (StringRef stringToRemove,
  243. bool ignoreCase = false);
  244. /** Removes a range of elements from the array.
  245. This will remove a set of elements, starting from the given index,
  246. and move subsequent elements down to close the gap.
  247. If the range extends beyond the bounds of the array, it will
  248. be safely clipped to the size of the array.
  249. @param startIndex the index of the first element to remove
  250. @param numberToRemove how many elements should be removed
  251. */
  252. void removeRange (int startIndex, int numberToRemove);
  253. /** Removes any duplicated elements from the array.
  254. If any string appears in the array more than once, only the first occurrence of
  255. it will be retained.
  256. @param ignoreCase whether to use a case-insensitive comparison
  257. */
  258. void removeDuplicates (bool ignoreCase);
  259. /** Removes empty strings from the array.
  260. @param removeWhitespaceStrings if true, strings that only contain whitespace
  261. characters will also be removed
  262. */
  263. void removeEmptyStrings (bool removeWhitespaceStrings = true);
  264. /** Moves one of the strings to a different position.
  265. This will move the string to a specified index, shuffling along
  266. any intervening elements as required.
  267. So for example, if you have the array { 0, 1, 2, 3, 4, 5 } then calling
  268. move (2, 4) would result in { 0, 1, 3, 4, 2, 5 }.
  269. @param currentIndex the index of the value to be moved. If this isn't a
  270. valid index, then nothing will be done
  271. @param newIndex the index at which you'd like this value to end up. If this
  272. is less than zero, the value will be moved to the end
  273. of the array
  274. */
  275. void move (int currentIndex, int newIndex) noexcept;
  276. /** Deletes any whitespace characters from the starts and ends of all the strings. */
  277. void trim();
  278. /** Adds numbers to the strings in the array, to make each string unique.
  279. This will add numbers to the ends of groups of similar strings.
  280. e.g. if there are two "moose" strings, they will become "moose (1)" and "moose (2)"
  281. @param ignoreCaseWhenComparing whether the comparison used is case-insensitive
  282. @param appendNumberToFirstInstance whether the first of a group of similar strings
  283. also has a number appended to it.
  284. @param preNumberString when adding a number, this string is added before the number.
  285. If you pass nullptr, a default string will be used, which adds
  286. brackets around the number.
  287. @param postNumberString this string is appended after any numbers that are added.
  288. If you pass nullptr, a default string will be used, which adds
  289. brackets around the number.
  290. */
  291. void appendNumbersToDuplicates (bool ignoreCaseWhenComparing,
  292. bool appendNumberToFirstInstance,
  293. CharPointer_UTF8 preNumberString = CharPointer_UTF8 (nullptr),
  294. CharPointer_UTF8 postNumberString = CharPointer_UTF8 (nullptr));
  295. //==============================================================================
  296. /** Joins the strings in the array together into one string.
  297. This will join a range of elements from the array into a string, separating
  298. them with a given string.
  299. e.g. joinIntoString (",") will turn an array of "a" "b" and "c" into "a,b,c".
  300. @param separatorString the string to insert between all the strings
  301. @param startIndex the first element to join
  302. @param numberOfElements how many elements to join together. If this is less
  303. than zero, all available elements will be used.
  304. */
  305. String joinIntoString (StringRef separatorString,
  306. int startIndex = 0,
  307. int numberOfElements = -1) const;
  308. //==============================================================================
  309. /** Sorts the array into alphabetical order.
  310. @param ignoreCase if true, the comparisons used will be case-sensitive.
  311. */
  312. void sort (bool ignoreCase);
  313. /** Sorts the array using extra language-aware rules to do a better job of comparing
  314. words containing spaces and numbers.
  315. @see String::compareNatural()
  316. */
  317. void sortNatural();
  318. //==============================================================================
  319. /** Increases the array's internal storage to hold a minimum number of elements.
  320. Calling this before adding a large known number of elements means that
  321. the array won't have to keep dynamically resizing itself as the elements
  322. are added, and it'll therefore be more efficient.
  323. */
  324. void ensureStorageAllocated (int minNumElements);
  325. /** Reduces the amount of storage being used by the array.
  326. Arrays typically allocate slightly more storage than they need, and after
  327. removing elements, they may have quite a lot of unused space allocated.
  328. This method will reduce the amount of allocated storage to a minimum.
  329. */
  330. void minimiseStorageOverheads();
  331. /** This is the array holding the actual strings. This is public to allow direct access
  332. to array methods that may not already be provided by the StringArray class.
  333. */
  334. Array<String> strings;
  335. private:
  336. JUCE_LEAK_DETECTOR (StringArray)
  337. };
  338. #endif // JUCE_STRINGARRAY_H_INCLUDED