Audio plugin host https://kx.studio/carla
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.

StringArray.cpp 13KB

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