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.

StringArray.h 17KB

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