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.

461 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2022 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. StringArray::StringArray (const String& firstValue)
  30. {
  31. strings.add (firstValue);
  32. }
  33. StringArray::StringArray (const String* initialStrings, int numberOfStrings)
  34. {
  35. strings.addArray (initialStrings, numberOfStrings);
  36. }
  37. StringArray::StringArray (const char* const* initialStrings)
  38. {
  39. strings.addNullTerminatedArray (initialStrings);
  40. }
  41. StringArray::StringArray (const char* const* initialStrings, int numberOfStrings)
  42. {
  43. strings.addArray (initialStrings, numberOfStrings);
  44. }
  45. StringArray& StringArray::operator= (const StringArray& other)
  46. {
  47. strings = other.strings;
  48. return *this;
  49. }
  50. StringArray::~StringArray()
  51. {
  52. }
  53. bool StringArray::operator== (const StringArray& other) const noexcept
  54. {
  55. return strings == other.strings;
  56. }
  57. bool StringArray::operator!= (const StringArray& other) const noexcept
  58. {
  59. return ! operator== (other);
  60. }
  61. void StringArray::swapWith (StringArray& other) noexcept
  62. {
  63. strings.swapWith (other.strings);
  64. }
  65. void StringArray::clear()
  66. {
  67. strings.clear();
  68. }
  69. void StringArray::clearQuick()
  70. {
  71. strings.clearQuick();
  72. }
  73. const String& StringArray::operator[] (const int index) const noexcept
  74. {
  75. if (isPositiveAndBelow (index, strings.size()))
  76. return strings.getReference (index);
  77. static String empty;
  78. return empty;
  79. }
  80. String& StringArray::getReference (const int index) noexcept
  81. {
  82. return strings.getReference (index);
  83. }
  84. bool StringArray::add (const String& newString)
  85. {
  86. return strings.add (newString);
  87. }
  88. bool StringArray::insert (const int index, const String& newString)
  89. {
  90. return strings.insert (index, newString);
  91. }
  92. bool StringArray::addIfNotAlreadyThere (const String& newString, const bool ignoreCase)
  93. {
  94. if (contains (newString, ignoreCase))
  95. return false;
  96. return add (newString);
  97. }
  98. void StringArray::addArray (const StringArray& otherArray, int startIndex, int numElementsToAdd)
  99. {
  100. if (startIndex < 0)
  101. {
  102. wassertfalse;
  103. startIndex = 0;
  104. }
  105. if (numElementsToAdd < 0 || startIndex + numElementsToAdd > otherArray.size())
  106. numElementsToAdd = otherArray.size() - startIndex;
  107. while (--numElementsToAdd >= 0)
  108. strings.add (otherArray.strings.getReference (startIndex++));
  109. }
  110. void StringArray::mergeArray (const StringArray& otherArray, const bool ignoreCase)
  111. {
  112. for (int i = 0; i < otherArray.size(); ++i)
  113. addIfNotAlreadyThere (otherArray[i], ignoreCase);
  114. }
  115. void StringArray::set (const int index, const String& newString)
  116. {
  117. strings.set (index, newString);
  118. }
  119. bool StringArray::contains (StringRef stringToLookFor, const bool ignoreCase) const
  120. {
  121. return indexOf (stringToLookFor, ignoreCase) >= 0;
  122. }
  123. int StringArray::indexOf (StringRef stringToLookFor, const bool ignoreCase, int i) const
  124. {
  125. if (i < 0)
  126. i = 0;
  127. const int numElements = size();
  128. if (ignoreCase)
  129. {
  130. for (; i < numElements; ++i)
  131. if (strings.getReference(i).equalsIgnoreCase (stringToLookFor))
  132. return i;
  133. }
  134. else
  135. {
  136. for (; i < numElements; ++i)
  137. if (stringToLookFor == strings.getReference (i))
  138. return i;
  139. }
  140. return -1;
  141. }
  142. //==============================================================================
  143. void StringArray::remove (const int index)
  144. {
  145. strings.remove (index);
  146. }
  147. void StringArray::removeString (StringRef stringToRemove, const bool ignoreCase)
  148. {
  149. if (ignoreCase)
  150. {
  151. for (int i = size(); --i >= 0;)
  152. if (strings.getReference(i).equalsIgnoreCase (stringToRemove))
  153. strings.remove (i);
  154. }
  155. else
  156. {
  157. for (int i = size(); --i >= 0;)
  158. if (stringToRemove == strings.getReference (i))
  159. strings.remove (i);
  160. }
  161. }
  162. void StringArray::removeRange (int startIndex, int numberToRemove)
  163. {
  164. strings.removeRange (startIndex, numberToRemove);
  165. }
  166. //==============================================================================
  167. void StringArray::removeEmptyStrings (const bool removeWhitespaceStrings)
  168. {
  169. if (removeWhitespaceStrings)
  170. {
  171. for (int i = size(); --i >= 0;)
  172. if (! strings.getReference(i).containsNonWhitespaceChars())
  173. strings.remove (i);
  174. }
  175. else
  176. {
  177. for (int i = size(); --i >= 0;)
  178. if (strings.getReference(i).isEmpty())
  179. strings.remove (i);
  180. }
  181. }
  182. void StringArray::trim()
  183. {
  184. for (int i = size(); --i >= 0;)
  185. {
  186. String& s = strings.getReference(i);
  187. s = s.trim();
  188. }
  189. }
  190. //==============================================================================
  191. struct InternalStringArrayComparator_CaseSensitive
  192. {
  193. static int compareElements (String& s1, String& s2) noexcept { return s1.compare (s2); }
  194. };
  195. struct InternalStringArrayComparator_CaseInsensitive
  196. {
  197. static int compareElements (String& s1, String& s2) noexcept { return s1.compareIgnoreCase (s2); }
  198. };
  199. struct InternalStringArrayComparator_Natural
  200. {
  201. static int compareElements (String& s1, String& s2) noexcept { return s1.compareNatural (s2); }
  202. };
  203. void StringArray::sort (const bool ignoreCase)
  204. {
  205. if (ignoreCase)
  206. {
  207. InternalStringArrayComparator_CaseInsensitive comp;
  208. strings.sort (comp);
  209. }
  210. else
  211. {
  212. InternalStringArrayComparator_CaseSensitive comp;
  213. strings.sort (comp);
  214. }
  215. }
  216. void StringArray::sortNatural()
  217. {
  218. InternalStringArrayComparator_Natural comp;
  219. strings.sort (comp);
  220. }
  221. //==============================================================================
  222. String StringArray::joinIntoString (StringRef separator, int start, int numberToJoin) const
  223. {
  224. const int last = (numberToJoin < 0) ? size()
  225. : jmin (size(), start + numberToJoin);
  226. if (start < 0)
  227. start = 0;
  228. if (start >= last)
  229. return String();
  230. if (start == last - 1)
  231. return strings.getReference (start);
  232. const size_t separatorBytes = separator.text.sizeInBytes() - sizeof (CharPointer_UTF8::CharType);
  233. size_t bytesNeeded = separatorBytes * (size_t) (last - start - 1);
  234. for (int i = start; i < last; ++i)
  235. bytesNeeded += strings.getReference(i).getCharPointer().sizeInBytes() - sizeof (CharPointer_UTF8::CharType);
  236. String result;
  237. result.preallocateBytes (bytesNeeded);
  238. CharPointer_UTF8 dest (result.getCharPointer());
  239. while (start < last)
  240. {
  241. const String& s = strings.getReference (start);
  242. if (! s.isEmpty())
  243. dest.writeAll (s.getCharPointer());
  244. if (++start < last && separatorBytes > 0)
  245. dest.writeAll (separator.text);
  246. }
  247. dest.writeNull();
  248. return result;
  249. }
  250. int StringArray::addTokens (StringRef text, const bool preserveQuotedStrings)
  251. {
  252. return addTokens (text, " \n\r\t", preserveQuotedStrings ? "\"" : "");
  253. }
  254. int StringArray::addTokens (StringRef text, StringRef breakCharacters, StringRef quoteCharacters)
  255. {
  256. int num = 0;
  257. if (text.isNotEmpty())
  258. {
  259. for (CharPointer_UTF8 t (text.text);;)
  260. {
  261. CharPointer_UTF8 tokenEnd (CharacterFunctions::findEndOfToken (t,
  262. breakCharacters.text,
  263. quoteCharacters.text));
  264. strings.add (String (t, tokenEnd));
  265. ++num;
  266. if (tokenEnd.isEmpty())
  267. break;
  268. t = ++tokenEnd;
  269. }
  270. }
  271. return num;
  272. }
  273. int StringArray::addLines (StringRef sourceText)
  274. {
  275. int numLines = 0;
  276. CharPointer_UTF8 text (sourceText.text);
  277. bool finished = text.isEmpty();
  278. while (! finished)
  279. {
  280. for (CharPointer_UTF8 startOfLine (text);;)
  281. {
  282. const CharPointer_UTF8 endOfLine (text);
  283. switch (text.getAndAdvance())
  284. {
  285. case 0: finished = true; break;
  286. case '\n': break;
  287. case '\r': if (*text == '\n') ++text; break;
  288. default: continue;
  289. }
  290. strings.add (String (startOfLine, endOfLine));
  291. ++numLines;
  292. break;
  293. }
  294. }
  295. return numLines;
  296. }
  297. StringArray StringArray::fromTokens (StringRef stringToTokenise, bool preserveQuotedStrings)
  298. {
  299. StringArray s;
  300. s.addTokens (stringToTokenise, preserveQuotedStrings);
  301. return s;
  302. }
  303. StringArray StringArray::fromTokens (StringRef stringToTokenise,
  304. StringRef breakCharacters,
  305. StringRef quoteCharacters)
  306. {
  307. StringArray s;
  308. s.addTokens (stringToTokenise, breakCharacters, quoteCharacters);
  309. return s;
  310. }
  311. StringArray StringArray::fromLines (StringRef stringToBreakUp)
  312. {
  313. StringArray s;
  314. s.addLines (stringToBreakUp);
  315. return s;
  316. }
  317. //==============================================================================
  318. void StringArray::removeDuplicates (const bool ignoreCase)
  319. {
  320. for (int i = 0; i < size() - 1; ++i)
  321. {
  322. const String s (strings.getReference(i));
  323. for (int nextIndex = i + 1;;)
  324. {
  325. nextIndex = indexOf (s, ignoreCase, nextIndex);
  326. if (nextIndex < 0)
  327. break;
  328. strings.remove (nextIndex);
  329. }
  330. }
  331. }
  332. void StringArray::appendNumbersToDuplicates (const bool ignoreCase,
  333. const bool appendNumberToFirstInstance,
  334. CharPointer_UTF8 preNumberString,
  335. CharPointer_UTF8 postNumberString)
  336. {
  337. CharPointer_UTF8 defaultPre (" ("), defaultPost (")");
  338. if (preNumberString.getAddress() == nullptr)
  339. preNumberString = defaultPre;
  340. if (postNumberString.getAddress() == nullptr)
  341. postNumberString = defaultPost;
  342. for (int i = 0; i < size() - 1; ++i)
  343. {
  344. String& s = strings.getReference(i);
  345. int nextIndex = indexOf (s, ignoreCase, i + 1);
  346. if (nextIndex >= 0)
  347. {
  348. const String original (s);
  349. int number = 0;
  350. if (appendNumberToFirstInstance)
  351. s = original + String (preNumberString) + String (++number) + String (postNumberString);
  352. else
  353. ++number;
  354. while (nextIndex >= 0)
  355. {
  356. set (nextIndex, (*this)[nextIndex] + String (preNumberString) + String (++number) + String (postNumberString));
  357. nextIndex = indexOf (original, ignoreCase, nextIndex + 1);
  358. }
  359. }
  360. }
  361. }
  362. void StringArray::ensureStorageAllocated (int minNumElements)
  363. {
  364. strings.ensureStorageAllocated (minNumElements);
  365. }
  366. void StringArray::minimiseStorageOverheads()
  367. {
  368. strings.minimiseStorageOverheads();
  369. }
  370. }