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.

349 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_STRINGARRAY_JUCEHEADER__
  19. #define __JUCE_STRINGARRAY_JUCEHEADER__
  20. #include "juce_String.h"
  21. #include "../containers/juce_Array.h"
  22. //==============================================================================
  23. /**
  24. A special array for holding a list of strings.
  25. @see String, StringPairArray
  26. */
  27. class JUCE_API StringArray
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates an empty string array */
  32. StringArray() noexcept;
  33. /** Creates a copy of another string array */
  34. StringArray (const StringArray& other);
  35. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  36. StringArray (StringArray&& other) noexcept;
  37. #endif
  38. /** Creates an array containing a single string. */
  39. explicit StringArray (const String& firstValue);
  40. /** Creates a copy of an array of string literals.
  41. @param strings an array of strings to add. Null pointers in the array will be
  42. treated as empty strings
  43. @param numberOfStrings how many items there are in the array
  44. */
  45. StringArray (const char* const* strings, int numberOfStrings);
  46. /** Creates a copy of a null-terminated array of string literals.
  47. Each item from the array passed-in is added, until it encounters a null pointer,
  48. at which point it stops.
  49. */
  50. explicit StringArray (const char* const* strings);
  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 wchar_t* const* strings);
  56. /** Creates a copy of an array of string literals.
  57. @param strings an array of strings to add. Null pointers in the array will be
  58. treated as empty strings
  59. @param numberOfStrings how many items there are in the array
  60. */
  61. StringArray (const wchar_t* const* strings, int numberOfStrings);
  62. /** Destructor. */
  63. ~StringArray();
  64. /** Copies the contents of another string array into this one */
  65. StringArray& operator= (const StringArray& other);
  66. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  67. StringArray& operator= (StringArray&& other) noexcept;
  68. #endif
  69. //==============================================================================
  70. /** Compares two arrays.
  71. Comparisons are case-sensitive.
  72. @returns true only if the other array contains exactly the same strings in the same order
  73. */
  74. bool operator== (const StringArray& other) const noexcept;
  75. /** Compares two arrays.
  76. Comparisons are case-sensitive.
  77. @returns false if the other array contains exactly the same strings in the same order
  78. */
  79. bool operator!= (const StringArray& other) const noexcept;
  80. //==============================================================================
  81. /** Returns the number of strings in the array */
  82. inline int size() const noexcept { return strings.size(); };
  83. /** Returns one of the strings from the array.
  84. If the index is out-of-range, an empty string is returned.
  85. Obviously the reference returned shouldn't be stored for later use, as the
  86. string it refers to may disappear when the array changes.
  87. */
  88. const String& operator[] (int index) const noexcept;
  89. /** Returns a reference to one of the strings in the array.
  90. This lets you modify a string in-place in the array, but you must be sure that
  91. the index is in-range.
  92. */
  93. String& getReference (int index) noexcept;
  94. /** Searches for a string in the array.
  95. The comparison will be case-insensitive if the ignoreCase parameter is true.
  96. @returns true if the string is found inside the array
  97. */
  98. bool contains (const String& stringToLookFor,
  99. bool ignoreCase = false) const;
  100. /** Searches for a string in the array.
  101. The comparison will be case-insensitive if the ignoreCase parameter is true.
  102. @param stringToLookFor the string to try to find
  103. @param ignoreCase whether the comparison should be case-insensitive
  104. @param startIndex the first index to start searching from
  105. @returns the index of the first occurrence of the string in this array,
  106. or -1 if it isn't found.
  107. */
  108. int indexOf (const String& stringToLookFor,
  109. bool ignoreCase = false,
  110. int startIndex = 0) const;
  111. //==============================================================================
  112. /** Appends a string at the end of the array. */
  113. void add (const String& stringToAdd);
  114. /** Inserts a string into the array.
  115. This will insert a string into the array at the given index, moving
  116. up the other elements to make room for it.
  117. If the index is less than zero or greater than the size of the array,
  118. the new string will be added to the end of the array.
  119. */
  120. void insert (int index, const String& stringToAdd);
  121. /** Adds a string to the array as long as it's not already in there.
  122. The search can optionally be case-insensitive.
  123. */
  124. void addIfNotAlreadyThere (const String& stringToAdd, bool ignoreCase = false);
  125. /** Replaces one of the strings in the array with another one.
  126. If the index is higher than the array's size, the new string will be
  127. added to the end of the array; if it's less than zero nothing happens.
  128. */
  129. void set (int index, const String& newString);
  130. /** Appends some strings from another array to the end of this one.
  131. @param other the array to add
  132. @param startIndex the first element of the other array to add
  133. @param numElementsToAdd the maximum number of elements to add (if this is
  134. less than zero, they are all added)
  135. */
  136. void addArray (const StringArray& other,
  137. int startIndex = 0,
  138. int numElementsToAdd = -1);
  139. /** Breaks up a string into tokens and adds them to this array.
  140. This will tokenise the given string using whitespace characters as the
  141. token delimiters, and will add these tokens to the end of the array.
  142. @returns the number of tokens added
  143. */
  144. int addTokens (const String& stringToTokenise,
  145. bool preserveQuotedStrings);
  146. /** Breaks up a string into tokens and adds them to this array.
  147. This will tokenise the given string (using the string passed in to define the
  148. token delimiters), and will add these tokens to the end of the array.
  149. @param stringToTokenise the string to tokenise
  150. @param breakCharacters a string of characters, any of which will be considered
  151. to be a token delimiter.
  152. @param quoteCharacters if this string isn't empty, it defines a set of characters
  153. which are treated as quotes. Any text occurring
  154. between quotes is not broken up into tokens.
  155. @returns the number of tokens added
  156. */
  157. int addTokens (const String& stringToTokenise,
  158. const String& breakCharacters,
  159. const String& quoteCharacters);
  160. /** Breaks up a string into lines and adds them to this array.
  161. This breaks a string down into lines separated by \\n or \\r\\n, and adds each line
  162. to the array. Line-break characters are omitted from the strings that are added to
  163. the array.
  164. */
  165. int addLines (const String& stringToBreakUp);
  166. //==============================================================================
  167. /** Removes all elements from the array. */
  168. void clear();
  169. /** Removes a string from the array.
  170. If the index is out-of-range, no action will be taken.
  171. */
  172. void remove (int index);
  173. /** Finds a string in the array and removes it.
  174. This will remove the first occurrence of the given string from the array. The
  175. comparison may be case-insensitive depending on the ignoreCase parameter.
  176. */
  177. void removeString (const String& stringToRemove,
  178. bool ignoreCase = false);
  179. /** Removes a range of elements from the array.
  180. This will remove a set of elements, starting from the given index,
  181. and move subsequent elements down to close the gap.
  182. If the range extends beyond the bounds of the array, it will
  183. be safely clipped to the size of the array.
  184. @param startIndex the index of the first element to remove
  185. @param numberToRemove how many elements should be removed
  186. */
  187. void removeRange (int startIndex, int numberToRemove);
  188. /** Removes any duplicated elements from the array.
  189. If any string appears in the array more than once, only the first occurrence of
  190. it will be retained.
  191. @param ignoreCase whether to use a case-insensitive comparison
  192. */
  193. void removeDuplicates (bool ignoreCase);
  194. /** Removes empty strings from the array.
  195. @param removeWhitespaceStrings if true, strings that only contain whitespace
  196. characters will also be removed
  197. */
  198. void removeEmptyStrings (bool removeWhitespaceStrings = true);
  199. /** Moves one of the strings to a different position.
  200. This will move the string to a specified index, shuffling along
  201. any intervening elements as required.
  202. So for example, if you have the array { 0, 1, 2, 3, 4, 5 } then calling
  203. move (2, 4) would result in { 0, 1, 3, 4, 2, 5 }.
  204. @param currentIndex the index of the value to be moved. If this isn't a
  205. valid index, then nothing will be done
  206. @param newIndex the index at which you'd like this value to end up. If this
  207. is less than zero, the value will be moved to the end
  208. of the array
  209. */
  210. void move (int currentIndex, int newIndex) noexcept;
  211. /** Deletes any whitespace characters from the starts and ends of all the strings. */
  212. void trim();
  213. /** Adds numbers to the strings in the array, to make each string unique.
  214. This will add numbers to the ends of groups of similar strings.
  215. e.g. if there are two "moose" strings, they will become "moose (1)" and "moose (2)"
  216. @param ignoreCaseWhenComparing whether the comparison used is case-insensitive
  217. @param appendNumberToFirstInstance whether the first of a group of similar strings
  218. also has a number appended to it.
  219. @param preNumberString when adding a number, this string is added before the number.
  220. If you pass 0, a default string will be used, which adds
  221. brackets around the number.
  222. @param postNumberString this string is appended after any numbers that are added.
  223. If you pass 0, a default string will be used, which adds
  224. brackets around the number.
  225. */
  226. void appendNumbersToDuplicates (bool ignoreCaseWhenComparing,
  227. bool appendNumberToFirstInstance,
  228. CharPointer_UTF8 preNumberString = CharPointer_UTF8 (nullptr),
  229. CharPointer_UTF8 postNumberString = CharPointer_UTF8 (nullptr));
  230. //==============================================================================
  231. /** Joins the strings in the array together into one string.
  232. This will join a range of elements from the array into a string, separating
  233. them with a given string.
  234. e.g. joinIntoString (",") will turn an array of "a" "b" and "c" into "a,b,c".
  235. @param separatorString the string to insert between all the strings
  236. @param startIndex the first element to join
  237. @param numberOfElements how many elements to join together. If this is less
  238. than zero, all available elements will be used.
  239. */
  240. String joinIntoString (const String& separatorString,
  241. int startIndex = 0,
  242. int numberOfElements = -1) const;
  243. //==============================================================================
  244. /** Sorts the array into alphabetical order.
  245. @param ignoreCase if true, the comparisons used will be case-sensitive.
  246. */
  247. void sort (bool ignoreCase);
  248. //==============================================================================
  249. /** Reduces the amount of storage being used by the array.
  250. Arrays typically allocate slightly more storage than they need, and after
  251. removing elements, they may have quite a lot of unused space allocated.
  252. This method will reduce the amount of allocated storage to a minimum.
  253. */
  254. void minimiseStorageOverheads();
  255. private:
  256. //==============================================================================
  257. Array <String> strings;
  258. JUCE_LEAK_DETECTOR (StringArray);
  259. };
  260. #endif // __JUCE_STRINGARRAY_JUCEHEADER__