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.

422 lines
18KB

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