1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2017 - ROLI Ltd.
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
StringArray::StringArray() noexcept
{
}
StringArray::StringArray (const StringArray& other)
: strings (other.strings)
{
}
StringArray::StringArray (StringArray&& other) noexcept
: strings (std::move (other.strings))
{
}
StringArray::StringArray (Array<String>&& other) noexcept
: strings (std::move (other))
{
}
StringArray::StringArray (const String& firstValue)
{
strings.add (firstValue);
}
StringArray::StringArray (const String* initialStrings, int numberOfStrings)
{
strings.addArray (initialStrings, numberOfStrings);
}
StringArray::StringArray (const char* const* initialStrings)
{
strings.addNullTerminatedArray (initialStrings);
}
StringArray::StringArray (const char* const* initialStrings, int numberOfStrings)
{
strings.addArray (initialStrings, numberOfStrings);
}
StringArray::StringArray (const wchar_t* const* initialStrings)
{
strings.addNullTerminatedArray (initialStrings);
}
StringArray::StringArray (const wchar_t* const* initialStrings, int numberOfStrings)
{
strings.addArray (initialStrings, numberOfStrings);
}
StringArray::StringArray (const std::initializer_list<const char*>& stringList)
{
strings.addArray (stringList);
}
StringArray& StringArray::operator= (const StringArray& other)
{
strings = other.strings;
return *this;
}
StringArray& StringArray::operator= (StringArray&& other) noexcept
{
strings = std::move (other.strings);
return *this;
}
StringArray::~StringArray()
{
}
bool StringArray::operator== (const StringArray& other) const noexcept
{
return strings == other.strings;
}
bool StringArray::operator!= (const StringArray& other) const noexcept
{
return ! operator== (other);
}
void StringArray::swapWith (StringArray& other) noexcept
{
strings.swapWith (other.strings);
}
void StringArray::clear()
{
strings.clear();
}
void StringArray::clearQuick()
{
strings.clearQuick();
}
const String& StringArray::operator[] (int index) const noexcept
{
if (isPositiveAndBelow (index, strings.size()))
return strings.getReference (index);
static String empty;
return empty;
}
String& StringArray::getReference (int index) noexcept
{
return strings.getReference (index);
}
void StringArray::add (String newString)
{
// NB: the local temp copy is to avoid a dangling pointer if the
// argument being passed-in is a reference into this array.
strings.add (std::move (newString));
}
void StringArray::insert (int index, String newString)
{
// NB: the local temp copy is to avoid a dangling pointer if the
// argument being passed-in is a reference into this array.
strings.insert (index, std::move (newString));
}
bool StringArray::addIfNotAlreadyThere (const String& newString, bool ignoreCase)
{
if (contains (newString, ignoreCase))
return false;
add (newString);
return true;
}
void StringArray::addArray (const StringArray& otherArray, int startIndex, int numElementsToAdd)
{
jassert (this != &otherArray); // can't add from our own elements!
if (startIndex < 0)
{
jassertfalse;
startIndex = 0;
}
if (numElementsToAdd < 0 || startIndex + numElementsToAdd > otherArray.size())
numElementsToAdd = otherArray.size() - startIndex;
while (--numElementsToAdd >= 0)
strings.add (otherArray.strings.getReference (startIndex++));
}
void StringArray::mergeArray (const StringArray& otherArray, bool ignoreCase)
{
jassert (this != &otherArray); // can't add from our own elements!
for (auto& s : otherArray)
addIfNotAlreadyThere (s, ignoreCase);
}
void StringArray::set (int index, String newString)
{
strings.set (index, std::move (newString));
}
bool StringArray::contains (StringRef stringToLookFor, bool ignoreCase) const
{
return indexOf (stringToLookFor, ignoreCase) >= 0;
}
int StringArray::indexOf (StringRef stringToLookFor, bool ignoreCase, int i) const
{
if (i < 0)
i = 0;
auto numElements = size();
if (ignoreCase)
{
for (; i < numElements; ++i)
if (strings.getReference(i).equalsIgnoreCase (stringToLookFor))
return i;
}
else
{
for (; i < numElements; ++i)
if (stringToLookFor == strings.getReference (i))
return i;
}
return -1;
}
void StringArray::move (int currentIndex, int newIndex) noexcept
{
strings.move (currentIndex, newIndex);
}
//==============================================================================
void StringArray::remove (int index)
{
strings.remove (index);
}
void StringArray::removeString (StringRef stringToRemove, bool ignoreCase)
{
if (ignoreCase)
{
for (int i = size(); --i >= 0;)
if (strings.getReference(i).equalsIgnoreCase (stringToRemove))
strings.remove (i);
}
else
{
for (int i = size(); --i >= 0;)
if (stringToRemove == strings.getReference (i))
strings.remove (i);
}
}
void StringArray::removeRange (int startIndex, int numberToRemove)
{
strings.removeRange (startIndex, numberToRemove);
}
//==============================================================================
void StringArray::removeEmptyStrings (bool removeWhitespaceStrings)
{
if (removeWhitespaceStrings)
{
for (int i = size(); --i >= 0;)
if (! strings.getReference(i).containsNonWhitespaceChars())
strings.remove (i);
}
else
{
for (int i = size(); --i >= 0;)
if (strings.getReference(i).isEmpty())
strings.remove (i);
}
}
void StringArray::trim()
{
for (auto& s : strings)
s = s.trim();
}
//==============================================================================
void StringArray::sort (bool ignoreCase)
{
if (ignoreCase)
std::sort (strings.begin(), strings.end(),
[] (const String& a, const String& b) { return a.compareIgnoreCase (b) < 0; });
else
std::sort (strings.begin(), strings.end());
}
void StringArray::sortNatural()
{
std::sort (strings.begin(), strings.end(),
[] (const String& a, const String& b) { return a.compareNatural (b) < 0; });
}
//==============================================================================
String StringArray::joinIntoString (StringRef separator, int start, int numberToJoin) const
{
auto last = (numberToJoin < 0) ? size()
: jmin (size(), start + numberToJoin);
if (start < 0)
start = 0;
if (start >= last)
return {};
if (start == last - 1)
return strings.getReference (start);
auto separatorBytes = separator.text.sizeInBytes() - sizeof (String::CharPointerType::CharType);
auto bytesNeeded = separatorBytes * (size_t) (last - start - 1);
for (int i = start; i < last; ++i)
bytesNeeded += strings.getReference(i).getCharPointer().sizeInBytes() - sizeof (String::CharPointerType::CharType);
String result;
result.preallocateBytes (bytesNeeded);
auto dest = result.getCharPointer();
while (start < last)
{
auto& s = strings.getReference (start);
if (! s.isEmpty())
dest.writeAll (s.getCharPointer());
if (++start < last && separatorBytes > 0)
dest.writeAll (separator.text);
}
dest.writeNull();
return result;
}
int StringArray::addTokens (StringRef text, const bool preserveQuotedStrings)
{
return addTokens (text, " \n\r\t", preserveQuotedStrings ? "\"" : "");
}
int StringArray::addTokens (StringRef text, StringRef breakCharacters, StringRef quoteCharacters)
{
int num = 0;
if (text.isNotEmpty())
{
for (auto t = text.text;;)
{
auto tokenEnd = CharacterFunctions::findEndOfToken (t,
breakCharacters.text,
quoteCharacters.text);
strings.add (String (t, tokenEnd));
++num;
if (tokenEnd.isEmpty())
break;
t = ++tokenEnd;
}
}
return num;
}
int StringArray::addLines (StringRef sourceText)
{
int numLines = 0;
auto text = sourceText.text;
bool finished = text.isEmpty();
while (! finished)
{
for (auto startOfLine = text;;)
{
auto endOfLine = text;
switch (text.getAndAdvance())
{
case 0: finished = true; break;
case '\n': break;
case '\r': if (*text == '\n') ++text; break;
default: continue;
}
strings.add (String (startOfLine, endOfLine));
++numLines;
break;
}
}
return numLines;
}
StringArray StringArray::fromTokens (StringRef stringToTokenise, bool preserveQuotedStrings)
{
StringArray s;
s.addTokens (stringToTokenise, preserveQuotedStrings);
return s;
}
StringArray StringArray::fromTokens (StringRef stringToTokenise,
StringRef breakCharacters,
StringRef quoteCharacters)
{
StringArray s;
s.addTokens (stringToTokenise, breakCharacters, quoteCharacters);
return s;
}
StringArray StringArray::fromLines (StringRef stringToBreakUp)
{
StringArray s;
s.addLines (stringToBreakUp);
return s;
}
//==============================================================================
void StringArray::removeDuplicates (bool ignoreCase)
{
for (int i = 0; i < size() - 1; ++i)
{
auto s = strings.getReference(i);
for (int nextIndex = i + 1;;)
{
nextIndex = indexOf (s, ignoreCase, nextIndex);
if (nextIndex < 0)
break;
strings.remove (nextIndex);
}
}
}
void StringArray::appendNumbersToDuplicates (bool ignoreCase,
bool appendNumberToFirstInstance,
CharPointer_UTF8 preNumberString,
CharPointer_UTF8 postNumberString)
{
if (preNumberString.getAddress() == nullptr)
preNumberString = CharPointer_UTF8 (" (");
if (postNumberString.getAddress() == nullptr)
postNumberString = CharPointer_UTF8 (")");
for (int i = 0; i < size() - 1; ++i)
{
auto& s = strings.getReference(i);
auto nextIndex = indexOf (s, ignoreCase, i + 1);
if (nextIndex >= 0)
{
auto original = s;
int number = 0;
if (appendNumberToFirstInstance)
s = original + String (preNumberString) + String (++number) + String (postNumberString);
else
++number;
while (nextIndex >= 0)
{
set (nextIndex, (*this)[nextIndex] + String (preNumberString) + String (++number) + String (postNumberString));
nextIndex = indexOf (original, ignoreCase, nextIndex + 1);
}
}
}
}
void StringArray::ensureStorageAllocated (int minNumElements)
{
strings.ensureStorageAllocated (minNumElements);
}
void StringArray::minimiseStorageOverheads()
{
strings.minimiseStorageOverheads();
}
} // namespace juce
|