2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 ==============================================================================
26 #ifndef WATER_STRINGARRAY_H_INCLUDED
27 #define WATER_STRINGARRAY_H_INCLUDED
30 #include "../containers/Array.h"
34 //==============================================================================
36 A special array for holding a list of strings.
38 @see String, StringPairArray
43 //==============================================================================
44 /** Creates an empty string array */
45 StringArray() noexcept
;
47 /** Creates a copy of another string array */
48 StringArray (const StringArray
&);
50 /** Creates an array containing a single string. */
51 explicit StringArray (const String
& firstValue
);
53 /** Creates an array from a raw array of strings.
54 @param strings an array of strings to add
55 @param numberOfStrings how many items there are in the array
57 StringArray (const String
* strings
, int numberOfStrings
);
59 /** Creates a copy of an array of string literals.
60 @param strings an array of strings to add. Null pointers in the array will be
61 treated as empty strings
62 @param numberOfStrings how many items there are in the array
64 StringArray (const char* const* strings
, int numberOfStrings
);
66 /** Creates a copy of a null-terminated array of string literals.
68 Each item from the array passed-in is added, until it encounters a null pointer,
69 at which point it stops.
71 explicit StringArray (const char* const* strings
);
76 /** Copies the contents of another string array into this one */
77 StringArray
& operator= (const StringArray
&);
79 /** Swaps the contents of this and another StringArray. */
80 void swapWith (StringArray
&) noexcept
;
82 //==============================================================================
83 /** Compares two arrays.
84 Comparisons are case-sensitive.
85 @returns true only if the other array contains exactly the same strings in the same order
87 bool operator== (const StringArray
&) const noexcept
;
89 /** Compares two arrays.
90 Comparisons are case-sensitive.
91 @returns false if the other array contains exactly the same strings in the same order
93 bool operator!= (const StringArray
&) const noexcept
;
95 //==============================================================================
96 /** Returns the number of strings in the array */
97 inline int size() const noexcept
{ return strings
.size(); }
99 /** Returns true if the array is empty, false otherwise. */
100 inline bool isEmpty() const noexcept
{ return size() == 0; }
102 /** Returns one of the strings from the array.
104 If the index is out-of-range, an empty string is returned.
106 Obviously the reference returned shouldn't be stored for later use, as the
107 string it refers to may disappear when the array changes.
109 const String
& operator[] (int index
) const noexcept
;
111 /** Returns a reference to one of the strings in the array.
112 This lets you modify a string in-place in the array, but you must be sure that
113 the index is in-range.
115 String
& getReference (int index
) noexcept
;
117 /** Returns a pointer to the first String in the array.
118 This method is provided for compatibility with standard C++ iteration mechanisms.
120 inline String
* begin() const noexcept
{ return strings
.begin(); }
122 /** Returns a pointer to the String which follows the last element in the array.
123 This method is provided for compatibility with standard C++ iteration mechanisms.
125 inline String
* end() const noexcept
{ return strings
.end(); }
127 /** Searches for a string in the array.
129 The comparison will be case-insensitive if the ignoreCase parameter is true.
131 @returns true if the string is found inside the array
133 bool contains (StringRef stringToLookFor
,
134 bool ignoreCase
= false) const;
136 /** Searches for a string in the array.
138 The comparison will be case-insensitive if the ignoreCase parameter is true.
140 @param stringToLookFor the string to try to find
141 @param ignoreCase whether the comparison should be case-insensitive
142 @param startIndex the first index to start searching from
143 @returns the index of the first occurrence of the string in this array,
144 or -1 if it isn't found.
146 int indexOf (StringRef stringToLookFor
,
147 bool ignoreCase
= false,
148 int startIndex
= 0) const;
150 //==============================================================================
151 /** Appends a string at the end of the array. */
152 bool add (const String
& stringToAdd
);
154 /** Inserts a string into the array.
156 This will insert a string into the array at the given index, moving
157 up the other elements to make room for it.
158 If the index is less than zero or greater than the size of the array,
159 the new string will be added to the end of the array.
161 bool insert (int index
, const String
& stringToAdd
);
163 /** Adds a string to the array as long as it's not already in there.
164 The search can optionally be case-insensitive.
166 @return true if the string has been added, false otherwise.
168 bool addIfNotAlreadyThere (const String
& stringToAdd
, bool ignoreCase
= false);
170 /** Replaces one of the strings in the array with another one.
172 If the index is higher than the array's size, the new string will be
173 added to the end of the array; if it's less than zero nothing happens.
175 void set (int index
, const String
& newString
);
177 /** Appends some strings from another array to the end of this one.
179 @param other the array to add
180 @param startIndex the first element of the other array to add
181 @param numElementsToAdd the maximum number of elements to add (if this is
182 less than zero, they are all added)
184 void addArray (const StringArray
& other
,
186 int numElementsToAdd
= -1);
188 /** Merges the strings from another array into this one.
189 This will not add a string that already exists.
191 @param other the array to add
192 @param ignoreCase ignore case when merging
194 void mergeArray (const StringArray
& other
,
195 bool ignoreCase
= false);
197 /** Breaks up a string into tokens and adds them to this array.
199 This will tokenise the given string using whitespace characters as the
200 token delimiters, and will add these tokens to the end of the array.
201 @returns the number of tokens added
204 int addTokens (StringRef stringToTokenise
, bool preserveQuotedStrings
);
206 /** Breaks up a string into tokens and adds them to this array.
208 This will tokenise the given string (using the string passed in to define the
209 token delimiters), and will add these tokens to the end of the array.
211 @param stringToTokenise the string to tokenise
212 @param breakCharacters a string of characters, any of which will be considered
213 to be a token delimiter.
214 @param quoteCharacters if this string isn't empty, it defines a set of characters
215 which are treated as quotes. Any text occurring
216 between quotes is not broken up into tokens.
217 @returns the number of tokens added
220 int addTokens (StringRef stringToTokenise
,
221 StringRef breakCharacters
,
222 StringRef quoteCharacters
);
224 /** Breaks up a string into lines and adds them to this array.
226 This breaks a string down into lines separated by \\n or \\r\\n, and adds each line
227 to the array. Line-break characters are omitted from the strings that are added to
230 int addLines (StringRef stringToBreakUp
);
232 /** Returns an array containing the tokens in a given string.
234 This will tokenise the given string using whitespace characters as the
235 token delimiters, and return the parsed tokens as an array.
238 static StringArray
fromTokens (StringRef stringToTokenise
,
239 bool preserveQuotedStrings
);
241 /** Returns an array containing the tokens in a given string.
243 This will tokenise the given string using the breakCharacters string to define
244 the token delimiters, and will return the parsed tokens as an array.
246 @param stringToTokenise the string to tokenise
247 @param breakCharacters a string of characters, any of which will be considered
248 to be a token delimiter.
249 @param quoteCharacters if this string isn't empty, it defines a set of characters
250 which are treated as quotes. Any text occurring
251 between quotes is not broken up into tokens.
254 static StringArray
fromTokens (StringRef stringToTokenise
,
255 StringRef breakCharacters
,
256 StringRef quoteCharacters
);
258 /** Returns an array containing the lines in a given string.
260 This breaks a string down into lines separated by \\n or \\r\\n, and returns an
261 array containing these lines. Line-break characters are omitted from the strings that
262 are added to the array.
264 static StringArray
fromLines (StringRef stringToBreakUp
);
266 //==============================================================================
267 /** Removes all elements from the array. */
270 /** Removes all elements from the array without freeing the array's allocated storage.
275 /** Removes a string from the array.
276 If the index is out-of-range, no action will be taken.
278 void remove (int index
);
280 /** Finds a string in the array and removes it.
281 This will remove all occurrences of the given string from the array.
282 The comparison may be case-insensitive depending on the ignoreCase parameter.
284 void removeString (StringRef stringToRemove
,
285 bool ignoreCase
= false);
287 /** Removes a range of elements from the array.
289 This will remove a set of elements, starting from the given index,
290 and move subsequent elements down to close the gap.
292 If the range extends beyond the bounds of the array, it will
293 be safely clipped to the size of the array.
295 @param startIndex the index of the first element to remove
296 @param numberToRemove how many elements should be removed
298 void removeRange (int startIndex
, int numberToRemove
);
300 /** Removes any duplicated elements from the array.
302 If any string appears in the array more than once, only the first occurrence of
305 @param ignoreCase whether to use a case-insensitive comparison
307 void removeDuplicates (bool ignoreCase
);
309 /** Removes empty strings from the array.
310 @param removeWhitespaceStrings if true, strings that only contain whitespace
311 characters will also be removed
313 void removeEmptyStrings (bool removeWhitespaceStrings
= true);
315 /** Deletes any whitespace characters from the starts and ends of all the strings. */
318 /** Adds numbers to the strings in the array, to make each string unique.
320 This will add numbers to the ends of groups of similar strings.
321 e.g. if there are two "moose" strings, they will become "moose (1)" and "moose (2)"
323 @param ignoreCaseWhenComparing whether the comparison used is case-insensitive
324 @param appendNumberToFirstInstance whether the first of a group of similar strings
325 also has a number appended to it.
326 @param preNumberString when adding a number, this string is added before the number.
327 If you pass nullptr, a default string will be used, which adds
328 brackets around the number.
329 @param postNumberString this string is appended after any numbers that are added.
330 If you pass nullptr, a default string will be used, which adds
331 brackets around the number.
333 void appendNumbersToDuplicates (bool ignoreCaseWhenComparing
,
334 bool appendNumberToFirstInstance
,
335 CharPointer_UTF8 preNumberString
= CharPointer_UTF8 (nullptr),
336 CharPointer_UTF8 postNumberString
= CharPointer_UTF8 (nullptr));
338 //==============================================================================
339 /** Joins the strings in the array together into one string.
341 This will join a range of elements from the array into a string, separating
342 them with a given string.
344 e.g. joinIntoString (",") will turn an array of "a" "b" and "c" into "a,b,c".
346 @param separatorString the string to insert between all the strings
347 @param startIndex the first element to join
348 @param numberOfElements how many elements to join together. If this is less
349 than zero, all available elements will be used.
351 String
joinIntoString (StringRef separatorString
,
353 int numberOfElements
= -1) const;
355 //==============================================================================
356 /** Sorts the array into alphabetical order.
357 @param ignoreCase if true, the comparisons used will be case-sensitive.
359 void sort (bool ignoreCase
);
361 /** Sorts the array using extra language-aware rules to do a better job of comparing
362 words containing spaces and numbers.
363 @see String::compareNatural()
367 //==============================================================================
368 /** Increases the array's internal storage to hold a minimum number of elements.
370 Calling this before adding a large known number of elements means that
371 the array won't have to keep dynamically resizing itself as the elements
372 are added, and it'll therefore be more efficient.
374 void ensureStorageAllocated (int minNumElements
);
376 /** Reduces the amount of storage being used by the array.
378 Arrays typically allocate slightly more storage than they need, and after
379 removing elements, they may have quite a lot of unused space allocated.
380 This method will reduce the amount of allocated storage to a minimum.
382 void minimiseStorageOverheads();
384 /** This is the array holding the actual strings. This is public to allow direct access
385 to array methods that may not already be provided by the StringArray class.
387 Array
<String
> strings
;
392 #endif // WATER_STRINGARRAY_H_INCLUDED