juce_StringArray.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. StringArray::StringArray() noexcept
  22. {
  23. }
  24. StringArray::StringArray (const StringArray& other)
  25. : strings (other.strings)
  26. {
  27. }
  28. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  29. StringArray::StringArray (StringArray&& other) noexcept
  30. : strings (static_cast<Array <String>&&> (other.strings))
  31. {
  32. }
  33. #endif
  34. StringArray::StringArray (const String& firstValue)
  35. {
  36. strings.add (firstValue);
  37. }
  38. StringArray::StringArray (const String* initialStrings, int numberOfStrings)
  39. {
  40. strings.addArray (initialStrings, numberOfStrings);
  41. }
  42. StringArray::StringArray (const char* const* initialStrings)
  43. {
  44. strings.addNullTerminatedArray (initialStrings);
  45. }
  46. StringArray::StringArray (const char* const* initialStrings, int numberOfStrings)
  47. {
  48. strings.addArray (initialStrings, numberOfStrings);
  49. }
  50. StringArray::StringArray (const wchar_t* const* initialStrings)
  51. {
  52. strings.addNullTerminatedArray (initialStrings);
  53. }
  54. StringArray::StringArray (const wchar_t* const* initialStrings, int numberOfStrings)
  55. {
  56. strings.addArray (initialStrings, numberOfStrings);
  57. }
  58. StringArray& StringArray::operator= (const StringArray& other)
  59. {
  60. strings = other.strings;
  61. return *this;
  62. }
  63. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  64. StringArray& StringArray::operator= (StringArray&& other) noexcept
  65. {
  66. strings = static_cast<Array<String>&&> (other.strings);
  67. return *this;
  68. }
  69. #endif
  70. #if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS
  71. StringArray::StringArray (const std::initializer_list<const char*>& stringList)
  72. {
  73. strings.addArray (stringList);
  74. }
  75. #endif
  76. StringArray::~StringArray()
  77. {
  78. }
  79. bool StringArray::operator== (const StringArray& other) const noexcept
  80. {
  81. return strings == other.strings;
  82. }
  83. bool StringArray::operator!= (const StringArray& other) const noexcept
  84. {
  85. return ! operator== (other);
  86. }
  87. void StringArray::swapWith (StringArray& other) noexcept
  88. {
  89. strings.swapWith (other.strings);
  90. }
  91. void StringArray::clear()
  92. {
  93. strings.clear();
  94. }
  95. void StringArray::clearQuick()
  96. {
  97. strings.clearQuick();
  98. }
  99. const String& StringArray::operator[] (const int index) const noexcept
  100. {
  101. if (isPositiveAndBelow (index, strings.size()))
  102. return strings.getReference (index);
  103. return String::empty;
  104. }
  105. String& StringArray::getReference (const int index) noexcept
  106. {
  107. return strings.getReference (index);
  108. }
  109. void StringArray::add (const String& newString)
  110. {
  111. strings.add (newString);
  112. }
  113. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  114. void StringArray::add (String&& stringToAdd)
  115. {
  116. strings.add (static_cast<String&&> (stringToAdd));
  117. }
  118. #endif
  119. void StringArray::insert (const int index, const String& newString)
  120. {
  121. strings.insert (index, newString);
  122. }
  123. void StringArray::addIfNotAlreadyThere (const String& newString, const bool ignoreCase)
  124. {
  125. if (! contains (newString, ignoreCase))
  126. add (newString);
  127. }
  128. void StringArray::addArray (const StringArray& otherArray, int startIndex, int numElementsToAdd)
  129. {
  130. if (startIndex < 0)
  131. {
  132. jassertfalse;
  133. startIndex = 0;
  134. }
  135. if (numElementsToAdd < 0 || startIndex + numElementsToAdd > otherArray.size())
  136. numElementsToAdd = otherArray.size() - startIndex;
  137. while (--numElementsToAdd >= 0)
  138. strings.add (otherArray.strings.getReference (startIndex++));
  139. }
  140. void StringArray::mergeArray (const StringArray& otherArray, const bool ignoreCase)
  141. {
  142. for (int i = 0; i < otherArray.size(); ++i)
  143. addIfNotAlreadyThere (otherArray[i], ignoreCase);
  144. }
  145. void StringArray::set (const int index, const String& newString)
  146. {
  147. strings.set (index, newString);
  148. }
  149. bool StringArray::contains (StringRef stringToLookFor, const bool ignoreCase) const
  150. {
  151. return indexOf (stringToLookFor, ignoreCase) >= 0;
  152. }
  153. int StringArray::indexOf (StringRef stringToLookFor, const bool ignoreCase, int i) const
  154. {
  155. if (i < 0)
  156. i = 0;
  157. const int numElements = size();
  158. if (ignoreCase)
  159. {
  160. for (; i < numElements; ++i)
  161. if (strings.getReference(i).equalsIgnoreCase (stringToLookFor))
  162. return i;
  163. }
  164. else
  165. {
  166. for (; i < numElements; ++i)
  167. if (stringToLookFor == strings.getReference (i))
  168. return i;
  169. }
  170. return -1;
  171. }
  172. void StringArray::move (const int currentIndex, const int newIndex) noexcept
  173. {
  174. strings.move (currentIndex, newIndex);
  175. }
  176. //==============================================================================
  177. void StringArray::remove (const int index)
  178. {
  179. strings.remove (index);
  180. }
  181. void StringArray::removeString (StringRef stringToRemove, const bool ignoreCase)
  182. {
  183. if (ignoreCase)
  184. {
  185. for (int i = size(); --i >= 0;)
  186. if (strings.getReference(i).equalsIgnoreCase (stringToRemove))
  187. strings.remove (i);
  188. }
  189. else
  190. {
  191. for (int i = size(); --i >= 0;)
  192. if (stringToRemove == strings.getReference (i))
  193. strings.remove (i);
  194. }
  195. }
  196. void StringArray::removeRange (int startIndex, int numberToRemove)
  197. {
  198. strings.removeRange (startIndex, numberToRemove);
  199. }
  200. //==============================================================================
  201. void StringArray::removeEmptyStrings (const bool removeWhitespaceStrings)
  202. {
  203. if (removeWhitespaceStrings)
  204. {
  205. for (int i = size(); --i >= 0;)
  206. if (! strings.getReference(i).containsNonWhitespaceChars())
  207. strings.remove (i);
  208. }
  209. else
  210. {
  211. for (int i = size(); --i >= 0;)
  212. if (strings.getReference(i).isEmpty())
  213. strings.remove (i);
  214. }
  215. }
  216. void StringArray::trim()
  217. {
  218. for (int i = size(); --i >= 0;)
  219. {
  220. String& s = strings.getReference(i);
  221. s = s.trim();
  222. }
  223. }
  224. //==============================================================================
  225. struct InternalStringArrayComparator_CaseSensitive
  226. {
  227. static int compareElements (String& s1, String& s2) noexcept { return s1.compare (s2); }
  228. };
  229. struct InternalStringArrayComparator_CaseInsensitive
  230. {
  231. static int compareElements (String& s1, String& s2) noexcept { return s1.compareIgnoreCase (s2); }
  232. };
  233. struct InternalStringArrayComparator_Natural
  234. {
  235. static int compareElements (String& s1, String& s2) noexcept { return s1.compareNatural (s2); }
  236. };
  237. void StringArray::sort (const bool ignoreCase)
  238. {
  239. if (ignoreCase)
  240. {
  241. InternalStringArrayComparator_CaseInsensitive comp;
  242. strings.sort (comp);
  243. }
  244. else
  245. {
  246. InternalStringArrayComparator_CaseSensitive comp;
  247. strings.sort (comp);
  248. }
  249. }
  250. void StringArray::sortNatural()
  251. {
  252. InternalStringArrayComparator_Natural comp;
  253. strings.sort (comp);
  254. }
  255. //==============================================================================
  256. String StringArray::joinIntoString (StringRef separator, int start, int numberToJoin) const
  257. {
  258. const int last = (numberToJoin < 0) ? size()
  259. : jmin (size(), start + numberToJoin);
  260. if (start < 0)
  261. start = 0;
  262. if (start >= last)
  263. return String();
  264. if (start == last - 1)
  265. return strings.getReference (start);
  266. const size_t separatorBytes = separator.text.sizeInBytes() - sizeof (String::CharPointerType::CharType);
  267. size_t bytesNeeded = separatorBytes * (size_t) (last - start - 1);
  268. for (int i = start; i < last; ++i)
  269. bytesNeeded += strings.getReference(i).getCharPointer().sizeInBytes() - sizeof (String::CharPointerType::CharType);
  270. String result;
  271. result.preallocateBytes (bytesNeeded);
  272. String::CharPointerType dest (result.getCharPointer());
  273. while (start < last)
  274. {
  275. const String& s = strings.getReference (start);
  276. if (! s.isEmpty())
  277. dest.writeAll (s.getCharPointer());
  278. if (++start < last && separatorBytes > 0)
  279. dest.writeAll (separator.text);
  280. }
  281. dest.writeNull();
  282. return result;
  283. }
  284. int StringArray::addTokens (StringRef text, const bool preserveQuotedStrings)
  285. {
  286. return addTokens (text, " \n\r\t", preserveQuotedStrings ? "\"" : "");
  287. }
  288. int StringArray::addTokens (StringRef text, StringRef breakCharacters, StringRef quoteCharacters)
  289. {
  290. int num = 0;
  291. if (text.isNotEmpty())
  292. {
  293. for (String::CharPointerType t (text.text);;)
  294. {
  295. String::CharPointerType tokenEnd (CharacterFunctions::findEndOfToken (t,
  296. breakCharacters.text,
  297. quoteCharacters.text));
  298. strings.add (String (t, tokenEnd));
  299. ++num;
  300. if (tokenEnd.isEmpty())
  301. break;
  302. t = ++tokenEnd;
  303. }
  304. }
  305. return num;
  306. }
  307. int StringArray::addLines (StringRef sourceText)
  308. {
  309. int numLines = 0;
  310. String::CharPointerType text (sourceText.text);
  311. bool finished = text.isEmpty();
  312. while (! finished)
  313. {
  314. for (String::CharPointerType startOfLine (text);;)
  315. {
  316. const String::CharPointerType endOfLine (text);
  317. switch (text.getAndAdvance())
  318. {
  319. case 0: finished = true; break;
  320. case '\n': break;
  321. case '\r': if (*text == '\n') ++text; break;
  322. default: continue;
  323. }
  324. strings.add (String (startOfLine, endOfLine));
  325. ++numLines;
  326. break;
  327. }
  328. }
  329. return numLines;
  330. }
  331. StringArray StringArray::fromTokens (StringRef stringToTokenise, bool preserveQuotedStrings)
  332. {
  333. StringArray s;
  334. s.addTokens (stringToTokenise, preserveQuotedStrings);
  335. return s;
  336. }
  337. StringArray StringArray::fromTokens (StringRef stringToTokenise,
  338. StringRef breakCharacters,
  339. StringRef quoteCharacters)
  340. {
  341. StringArray s;
  342. s.addTokens (stringToTokenise, breakCharacters, quoteCharacters);
  343. return s;
  344. }
  345. StringArray StringArray::fromLines (StringRef stringToBreakUp)
  346. {
  347. StringArray s;
  348. s.addLines (stringToBreakUp);
  349. return s;
  350. }
  351. //==============================================================================
  352. void StringArray::removeDuplicates (const bool ignoreCase)
  353. {
  354. for (int i = 0; i < size() - 1; ++i)
  355. {
  356. const String s (strings.getReference(i));
  357. for (int nextIndex = i + 1;;)
  358. {
  359. nextIndex = indexOf (s, ignoreCase, nextIndex);
  360. if (nextIndex < 0)
  361. break;
  362. strings.remove (nextIndex);
  363. }
  364. }
  365. }
  366. void StringArray::appendNumbersToDuplicates (const bool ignoreCase,
  367. const bool appendNumberToFirstInstance,
  368. CharPointer_UTF8 preNumberString,
  369. CharPointer_UTF8 postNumberString)
  370. {
  371. CharPointer_UTF8 defaultPre (" ("), defaultPost (")");
  372. if (preNumberString.getAddress() == nullptr)
  373. preNumberString = defaultPre;
  374. if (postNumberString.getAddress() == nullptr)
  375. postNumberString = defaultPost;
  376. for (int i = 0; i < size() - 1; ++i)
  377. {
  378. String& s = strings.getReference(i);
  379. int nextIndex = indexOf (s, ignoreCase, i + 1);
  380. if (nextIndex >= 0)
  381. {
  382. const String original (s);
  383. int number = 0;
  384. if (appendNumberToFirstInstance)
  385. s = original + String (preNumberString) + String (++number) + String (postNumberString);
  386. else
  387. ++number;
  388. while (nextIndex >= 0)
  389. {
  390. set (nextIndex, (*this)[nextIndex] + String (preNumberString) + String (++number) + String (postNumberString));
  391. nextIndex = indexOf (original, ignoreCase, nextIndex + 1);
  392. }
  393. }
  394. }
  395. }
  396. void StringArray::ensureStorageAllocated (int minNumElements)
  397. {
  398. strings.ensureStorageAllocated (minNumElements);
  399. }
  400. void StringArray::minimiseStorageOverheads()
  401. {
  402. strings.minimiseStorageOverheads();
  403. }