JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_StringArray.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
27{
28}
29
31 : strings (other.strings)
32{
33}
34
36 : strings (std::move (other.strings))
37{
38}
39
41 : strings (std::move (other))
42{
43}
44
49
54
56{
57 strings.addNullTerminatedArray (initialStrings);
58}
59
64
66{
67 strings.addNullTerminatedArray (initialStrings);
68}
69
74
79
85
87{
88 strings = std::move (other.strings);
89 return *this;
90}
91
92bool StringArray::operator== (const StringArray& other) const noexcept
93{
94 return strings == other.strings;
95}
96
97bool StringArray::operator!= (const StringArray& other) const noexcept
98{
99 return ! operator== (other);
100}
101
103{
104 strings.swapWith (other.strings);
105}
106
108{
109 strings.clear();
110}
111
113{
114 strings.clearQuick();
115}
116
117const String& StringArray::operator[] (int index) const noexcept
118{
119 if (isPositiveAndBelow (index, strings.size()))
120 return strings.getReference (index);
121
122 static String empty;
123 return empty;
124}
125
127{
128 return strings.getReference (index);
129}
130
131const String& StringArray::getReference (int index) const noexcept
132{
133 return strings.getReference (index);
134}
135
137{
138 // NB: the local temp copy is to avoid a dangling pointer if the
139 // argument being passed-in is a reference into this array.
140 strings.add (std::move (newString));
141}
142
144{
145 // NB: the local temp copy is to avoid a dangling pointer if the
146 // argument being passed-in is a reference into this array.
147 strings.insert (index, std::move (newString));
148}
149
151{
152 if (contains (newString, ignoreCase))
153 return false;
154
155 add (newString);
156 return true;
157}
158
160{
161 jassert (this != &otherArray); // can't add from our own elements!
162
163 if (startIndex < 0)
164 {
166 startIndex = 0;
167 }
168
170 numElementsToAdd = otherArray.size() - startIndex;
171
172 while (--numElementsToAdd >= 0)
173 strings.add (otherArray.strings.getReference (startIndex++));
174}
175
176void StringArray::mergeArray (const StringArray& otherArray, bool ignoreCase)
177{
178 jassert (this != &otherArray); // can't add from our own elements!
179
180 for (auto& s : otherArray)
181 addIfNotAlreadyThere (s, ignoreCase);
182}
183
185{
186 strings.set (index, std::move (newString));
187}
188
190{
191 return indexOf (stringToLookFor, ignoreCase) >= 0;
192}
193
194int StringArray::indexOf (StringRef stringToLookFor, bool ignoreCase, int i) const
195{
196 if (i < 0)
197 i = 0;
198
199 auto numElements = size();
200
201 if (ignoreCase)
202 {
203 for (; i < numElements; ++i)
204 if (strings.getReference (i).equalsIgnoreCase (stringToLookFor))
205 return i;
206 }
207 else
208 {
209 for (; i < numElements; ++i)
210 if (stringToLookFor == strings.getReference (i))
211 return i;
212 }
213
214 return -1;
215}
216
218{
219 strings.move (currentIndex, newIndex);
220}
221
222//==============================================================================
223void StringArray::remove (int index)
224{
225 strings.remove (index);
226}
227
229{
230 if (ignoreCase)
231 {
232 for (int i = size(); --i >= 0;)
233 if (strings.getReference (i).equalsIgnoreCase (stringToRemove))
234 strings.remove (i);
235 }
236 else
237 {
238 for (int i = size(); --i >= 0;)
239 if (stringToRemove == strings.getReference (i))
240 strings.remove (i);
241 }
242}
243
244void StringArray::removeRange (int startIndex, int numberToRemove)
245{
246 strings.removeRange (startIndex, numberToRemove);
247}
248
249//==============================================================================
251{
253 {
254 for (int i = size(); --i >= 0;)
255 if (! strings.getReference (i).containsNonWhitespaceChars())
256 strings.remove (i);
257 }
258 else
259 {
260 for (int i = size(); --i >= 0;)
261 if (strings.getReference (i).isEmpty())
262 strings.remove (i);
263 }
264}
265
267{
268 for (auto& s : strings)
269 s = s.trim();
270}
271
272//==============================================================================
273void StringArray::sort (bool ignoreCase)
274{
275 if (ignoreCase)
276 std::sort (strings.begin(), strings.end(),
277 [] (const String& a, const String& b) { return a.compareIgnoreCase (b) < 0; });
278 else
279 std::sort (strings.begin(), strings.end());
280}
281
283{
284 std::sort (strings.begin(), strings.end(),
285 [] (const String& a, const String& b) { return a.compareNatural (b) < 0; });
286}
287
288//==============================================================================
290{
291 auto last = (numberToJoin < 0) ? size()
292 : jmin (size(), start + numberToJoin);
293
294 if (start < 0)
295 start = 0;
296
297 if (start >= last)
298 return {};
299
300 if (start == last - 1)
301 return strings.getReference (start);
302
303 auto separatorBytes = separator.text.sizeInBytes() - sizeof (String::CharPointerType::CharType);
304 auto bytesNeeded = (size_t) (last - start - 1) * separatorBytes;
305
306 for (int i = start; i < last; ++i)
307 bytesNeeded += strings.getReference (i).getCharPointer().sizeInBytes() - sizeof (String::CharPointerType::CharType);
308
309 String result;
311
312 auto dest = result.getCharPointer();
313
314 while (start < last)
315 {
316 auto& s = strings.getReference (start);
317
318 if (! s.isEmpty())
319 dest.writeAll (s.getCharPointer());
320
322 dest.writeAll (separator.text);
323 }
324
325 dest.writeNull();
326 return result;
327}
328
330{
331 return addTokens (text, " \n\r\t", preserveQuotedStrings ? "\"" : "");
332}
333
335{
336 int num = 0;
337
338 if (text.isNotEmpty())
339 {
340 for (auto t = text.text;;)
341 {
343 breakCharacters.text,
344 quoteCharacters.text);
345 strings.add (String (t, tokenEnd));
346 ++num;
347
348 if (tokenEnd.isEmpty())
349 break;
350
351 t = ++tokenEnd;
352 }
353 }
354
355 return num;
356}
357
359{
360 int numLines = 0;
361 auto text = sourceText.text;
362 bool finished = text.isEmpty();
363
364 while (! finished)
365 {
366 for (auto startOfLine = text;;)
367 {
368 auto endOfLine = text;
369
370 switch (text.getAndAdvance())
371 {
372 case 0: finished = true; break;
373 case '\n': break;
374 case '\r': if (*text == '\n') ++text; break;
375 default: continue;
376 }
377
379 ++numLines;
380 break;
381 }
382 }
383
384 return numLines;
385}
386
393
402
409
410//==============================================================================
411void StringArray::removeDuplicates (bool ignoreCase)
412{
413 for (int i = 0; i < size() - 1; ++i)
414 {
415 auto s = strings.getReference (i);
416
417 for (int nextIndex = i + 1;;)
418 {
419 nextIndex = indexOf (s, ignoreCase, nextIndex);
420
421 if (nextIndex < 0)
422 break;
423
424 strings.remove (nextIndex);
425 }
426 }
427}
428
433{
434 if (preNumberString.getAddress() == nullptr)
436
437 if (postNumberString.getAddress() == nullptr)
439
440 for (int i = 0; i < size() - 1; ++i)
441 {
442 auto& s = strings.getReference (i);
443 auto nextIndex = indexOf (s, ignoreCase, i + 1);
444
445 if (nextIndex >= 0)
446 {
447 auto original = s;
448 int number = 0;
449
452 else
453 ++number;
454
455 while (nextIndex >= 0)
456 {
457 set (nextIndex, (*this)[nextIndex] + String (preNumberString) + String (++number) + String (postNumberString));
458 nextIndex = indexOf (original, ignoreCase, nextIndex + 1);
459 }
460 }
461 }
462}
463
465{
466 strings.ensureStorageAllocated (minNumElements);
467}
468
470{
471 strings.minimiseStorageOverheads();
472}
473
474} // namespace juce
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
Wraps a pointer to a null-terminated UTF-8 character string, and provides various methods to operate ...
void writeAll(const CharPointer src) noexcept
Copies a source string to this pointer, advancing this pointer as it goes.
size_t sizeInBytes() const noexcept
Returns the number of bytes that are used to represent this string.
static Type findEndOfToken(Type text, BreakType breakCharacters, Type quoteCharacters)
Returns a pointer to the first character in the string which is found in the breakCharacters string.
A special array for holding a list of strings.
bool operator==(const StringArray &) const noexcept
Compares two arrays.
String joinIntoString(StringRef separatorString, int startIndex=0, int numberOfElements=-1) const
Joins the strings in the array together into one string.
void appendNumbersToDuplicates(bool ignoreCaseWhenComparing, bool appendNumberToFirstInstance, CharPointer_UTF8 preNumberString=CharPointer_UTF8(nullptr), CharPointer_UTF8 postNumberString=CharPointer_UTF8(nullptr))
Adds numbers to the strings in the array, to make each string unique.
String & getReference(int index) noexcept
Returns a reference to one of the strings in the array.
void removeEmptyStrings(bool removeWhitespaceStrings=true)
Removes empty strings from the array.
void addArray(const StringArray &other, int startIndex=0, int numElementsToAdd=-1)
Appends some strings from another array to the end of this one.
int indexOf(StringRef stringToLookFor, bool ignoreCase=false, int startIndex=0) const
Searches for a string in the array.
void sortNatural()
Sorts the array using extra language-aware rules to do a better job of comparing words containing spa...
bool contains(StringRef stringToLookFor, bool ignoreCase=false) const
Searches for a string in the array.
void removeDuplicates(bool ignoreCase)
Removes any duplicated elements from the array.
static StringArray fromTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Returns an array containing the tokens in a given string.
void insert(int index, String stringToAdd)
Inserts a string into the array.
static StringArray fromLines(StringRef stringToBreakUp)
Returns an array containing the lines in a given string.
void minimiseStorageOverheads()
Reduces the amount of storage being used by the array.
void clear()
Removes all elements from the array.
Array< String > strings
This is the array holding the actual strings.
StringArray() noexcept
Creates an empty string array.
const String & operator[](int index) const noexcept
Returns one of the strings from the array.
void removeString(StringRef stringToRemove, bool ignoreCase=false)
Finds a string in the array and removes it.
void sort(bool ignoreCase)
Sorts the array into alphabetical order.
void move(int currentIndex, int newIndex) noexcept
Moves one of the strings to a different position.
void swapWith(StringArray &) noexcept
Swaps the contents of this and another StringArray.
int size() const noexcept
Returns the number of strings in the array.
bool operator!=(const StringArray &) const noexcept
Compares two arrays.
void trim()
Deletes any whitespace characters from the starts and ends of all the strings.
void add(String stringToAdd)
Appends a string at the end of the array.
int addLines(StringRef stringToBreakUp)
Breaks up a string into lines and adds them to this array.
void clearQuick()
Removes all elements from the array without freeing the array's allocated storage.
void mergeArray(const StringArray &other, bool ignoreCase=false)
Merges the strings from another array into this one.
bool addIfNotAlreadyThere(const String &stringToAdd, bool ignoreCase=false)
Adds a string to the array as long as it's not already in there.
void removeRange(int startIndex, int numberToRemove)
Removes a range of elements from the array.
StringArray & operator=(const StringArray &)
Copies the contents of another string array into this one.
void set(int index, String newString)
Replaces one of the strings in the array with another one.
void remove(int index)
Removes a string from the array.
void ensureStorageAllocated(int minNumElements)
Increases the array's internal storage to hold a minimum number of elements.
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Breaks up a string into tokens and adds them to this array.
A simple class for holding temporary references to a string literal or String.
bool isNotEmpty() const noexcept
Returns true if the string is not empty.
String::CharPointerType text
The text that is referenced.
The JUCE String class!
Definition juce_String.h:53
CharPointerType getCharPointer() const noexcept
Returns the character pointer currently being used to store this string.
void preallocateBytes(size_t numBytesNeeded)
Increases the string's internally allocated storage.
#define jassert(expression)
Platform-independent assertion macro.
#define jassertfalse
This will always cause an assertion failure.
JUCE Namespace.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Returns true if a value is at least zero, and also below a specified upper limit.
T sort(T... args)
typedef size_t