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.

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