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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. /** Deletes any whitespace characters from the starts and ends of all the strings. */
  253. void trim();
  254. /** Adds numbers to the strings in the array, to make each string unique.
  255. This will add numbers to the ends of groups of similar strings.
  256. e.g. if there are two "moose" strings, they will become "moose (1)" and "moose (2)"
  257. @param ignoreCaseWhenComparing whether the comparison used is case-insensitive
  258. @param appendNumberToFirstInstance whether the first of a group of similar strings
  259. also has a number appended to it.
  260. @param preNumberString when adding a number, this string is added before the number.
  261. If you pass nullptr, a default string will be used, which adds
  262. brackets around the number.
  263. @param postNumberString this string is appended after any numbers that are added.
  264. If you pass nullptr, a default string will be used, which adds
  265. brackets around the number.
  266. */
  267. void appendNumbersToDuplicates (bool ignoreCaseWhenComparing,
  268. bool appendNumberToFirstInstance,
  269. CharPointer_UTF8 preNumberString = CharPointer_UTF8 (nullptr),
  270. CharPointer_UTF8 postNumberString = CharPointer_UTF8 (nullptr));
  271. //==============================================================================
  272. /** Joins the strings in the array together into one string.
  273. This will join a range of elements from the array into a string, separating
  274. them with a given string.
  275. e.g. joinIntoString (",") will turn an array of "a" "b" and "c" into "a,b,c".
  276. @param separatorString the string to insert between all the strings
  277. @param startIndex the first element to join
  278. @param numberOfElements how many elements to join together. If this is less
  279. than zero, all available elements will be used.
  280. */
  281. String joinIntoString (StringRef separatorString,
  282. int startIndex = 0,
  283. int numberOfElements = -1) const;
  284. //==============================================================================
  285. /** Sorts the array into alphabetical order.
  286. @param ignoreCase if true, the comparisons used will be case-sensitive.
  287. */
  288. void sort (bool ignoreCase);
  289. /** Sorts the array using extra language-aware rules to do a better job of comparing
  290. words containing spaces and numbers.
  291. @see String::compareNatural()
  292. */
  293. void sortNatural();
  294. //==============================================================================
  295. /** Increases the array's internal storage to hold a minimum number of elements.
  296. Calling this before adding a large known number of elements means that
  297. the array won't have to keep dynamically resizing itself as the elements
  298. are added, and it'll therefore be more efficient.
  299. */
  300. void ensureStorageAllocated (int minNumElements);
  301. /** Reduces the amount of storage being used by the array.
  302. Arrays typically allocate slightly more storage than they need, and after
  303. removing elements, they may have quite a lot of unused space allocated.
  304. This method will reduce the amount of allocated storage to a minimum.
  305. */
  306. void minimiseStorageOverheads();
  307. /** This is the array holding the actual strings. This is public to allow direct access
  308. to array methods that may not already be provided by the StringArray class.
  309. */
  310. Array<String> strings;
  311. };
  312. }
  313. #endif // WATER_STRINGARRAY_H_INCLUDED