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.

419 lines
18KB

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