Audio plugin host https://kx.studio/carla
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.

442 lines
19KB

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