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.

483 lines
13KB

  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. wassertfalse;
  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. //==============================================================================
  162. void StringArray::remove (const int index)
  163. {
  164. strings.remove (index);
  165. }
  166. void StringArray::removeString (StringRef stringToRemove, const bool ignoreCase)
  167. {
  168. if (ignoreCase)
  169. {
  170. for (int i = size(); --i >= 0;)
  171. if (strings.getReference(i).equalsIgnoreCase (stringToRemove))
  172. strings.remove (i);
  173. }
  174. else
  175. {
  176. for (int i = size(); --i >= 0;)
  177. if (stringToRemove == strings.getReference (i))
  178. strings.remove (i);
  179. }
  180. }
  181. void StringArray::removeRange (int startIndex, int numberToRemove)
  182. {
  183. strings.removeRange (startIndex, numberToRemove);
  184. }
  185. //==============================================================================
  186. void StringArray::removeEmptyStrings (const bool removeWhitespaceStrings)
  187. {
  188. if (removeWhitespaceStrings)
  189. {
  190. for (int i = size(); --i >= 0;)
  191. if (! strings.getReference(i).containsNonWhitespaceChars())
  192. strings.remove (i);
  193. }
  194. else
  195. {
  196. for (int i = size(); --i >= 0;)
  197. if (strings.getReference(i).isEmpty())
  198. strings.remove (i);
  199. }
  200. }
  201. void StringArray::trim()
  202. {
  203. for (int i = size(); --i >= 0;)
  204. {
  205. String& s = strings.getReference(i);
  206. s = s.trim();
  207. }
  208. }
  209. //==============================================================================
  210. struct InternalStringArrayComparator_CaseSensitive
  211. {
  212. static int compareElements (String& s1, String& s2) noexcept { return s1.compare (s2); }
  213. };
  214. struct InternalStringArrayComparator_CaseInsensitive
  215. {
  216. static int compareElements (String& s1, String& s2) noexcept { return s1.compareIgnoreCase (s2); }
  217. };
  218. struct InternalStringArrayComparator_Natural
  219. {
  220. static int compareElements (String& s1, String& s2) noexcept { return s1.compareNatural (s2); }
  221. };
  222. void StringArray::sort (const bool ignoreCase)
  223. {
  224. if (ignoreCase)
  225. {
  226. InternalStringArrayComparator_CaseInsensitive comp;
  227. strings.sort (comp);
  228. }
  229. else
  230. {
  231. InternalStringArrayComparator_CaseSensitive comp;
  232. strings.sort (comp);
  233. }
  234. }
  235. void StringArray::sortNatural()
  236. {
  237. InternalStringArrayComparator_Natural comp;
  238. strings.sort (comp);
  239. }
  240. //==============================================================================
  241. String StringArray::joinIntoString (StringRef separator, int start, int numberToJoin) const
  242. {
  243. const int last = (numberToJoin < 0) ? size()
  244. : jmin (size(), start + numberToJoin);
  245. if (start < 0)
  246. start = 0;
  247. if (start >= last)
  248. return String();
  249. if (start == last - 1)
  250. return strings.getReference (start);
  251. const size_t separatorBytes = separator.text.sizeInBytes() - sizeof (String::CharPointerType::CharType);
  252. size_t bytesNeeded = separatorBytes * (size_t) (last - start - 1);
  253. for (int i = start; i < last; ++i)
  254. bytesNeeded += strings.getReference(i).getCharPointer().sizeInBytes() - sizeof (String::CharPointerType::CharType);
  255. String result;
  256. result.preallocateBytes (bytesNeeded);
  257. String::CharPointerType dest (result.getCharPointer());
  258. while (start < last)
  259. {
  260. const String& s = strings.getReference (start);
  261. if (! s.isEmpty())
  262. dest.writeAll (s.getCharPointer());
  263. if (++start < last && separatorBytes > 0)
  264. dest.writeAll (separator.text);
  265. }
  266. dest.writeNull();
  267. return result;
  268. }
  269. int StringArray::addTokens (StringRef text, const bool preserveQuotedStrings)
  270. {
  271. return addTokens (text, " \n\r\t", preserveQuotedStrings ? "\"" : "");
  272. }
  273. int StringArray::addTokens (StringRef text, StringRef breakCharacters, StringRef quoteCharacters)
  274. {
  275. int num = 0;
  276. if (text.isNotEmpty())
  277. {
  278. for (String::CharPointerType t (text.text);;)
  279. {
  280. String::CharPointerType tokenEnd (CharacterFunctions::findEndOfToken (t,
  281. breakCharacters.text,
  282. quoteCharacters.text));
  283. strings.add (String (t, tokenEnd));
  284. ++num;
  285. if (tokenEnd.isEmpty())
  286. break;
  287. t = ++tokenEnd;
  288. }
  289. }
  290. return num;
  291. }
  292. int StringArray::addLines (StringRef sourceText)
  293. {
  294. int numLines = 0;
  295. String::CharPointerType text (sourceText.text);
  296. bool finished = text.isEmpty();
  297. while (! finished)
  298. {
  299. for (String::CharPointerType startOfLine (text);;)
  300. {
  301. const String::CharPointerType endOfLine (text);
  302. switch (text.getAndAdvance())
  303. {
  304. case 0: finished = true; break;
  305. case '\n': break;
  306. case '\r': if (*text == '\n') ++text; break;
  307. default: continue;
  308. }
  309. strings.add (String (startOfLine, endOfLine));
  310. ++numLines;
  311. break;
  312. }
  313. }
  314. return numLines;
  315. }
  316. StringArray StringArray::fromTokens (StringRef stringToTokenise, bool preserveQuotedStrings)
  317. {
  318. StringArray s;
  319. s.addTokens (stringToTokenise, preserveQuotedStrings);
  320. return s;
  321. }
  322. StringArray StringArray::fromTokens (StringRef stringToTokenise,
  323. StringRef breakCharacters,
  324. StringRef quoteCharacters)
  325. {
  326. StringArray s;
  327. s.addTokens (stringToTokenise, breakCharacters, quoteCharacters);
  328. return s;
  329. }
  330. StringArray StringArray::fromLines (StringRef stringToBreakUp)
  331. {
  332. StringArray s;
  333. s.addLines (stringToBreakUp);
  334. return s;
  335. }
  336. //==============================================================================
  337. void StringArray::removeDuplicates (const bool ignoreCase)
  338. {
  339. for (int i = 0; i < size() - 1; ++i)
  340. {
  341. const String s (strings.getReference(i));
  342. for (int nextIndex = i + 1;;)
  343. {
  344. nextIndex = indexOf (s, ignoreCase, nextIndex);
  345. if (nextIndex < 0)
  346. break;
  347. strings.remove (nextIndex);
  348. }
  349. }
  350. }
  351. void StringArray::appendNumbersToDuplicates (const bool ignoreCase,
  352. const bool appendNumberToFirstInstance,
  353. CharPointer_UTF8 preNumberString,
  354. CharPointer_UTF8 postNumberString)
  355. {
  356. CharPointer_UTF8 defaultPre (" ("), defaultPost (")");
  357. if (preNumberString.getAddress() == nullptr)
  358. preNumberString = defaultPre;
  359. if (postNumberString.getAddress() == nullptr)
  360. postNumberString = defaultPost;
  361. for (int i = 0; i < size() - 1; ++i)
  362. {
  363. String& s = strings.getReference(i);
  364. int nextIndex = indexOf (s, ignoreCase, i + 1);
  365. if (nextIndex >= 0)
  366. {
  367. const String original (s);
  368. int number = 0;
  369. if (appendNumberToFirstInstance)
  370. s = original + String (preNumberString) + String (++number) + String (postNumberString);
  371. else
  372. ++number;
  373. while (nextIndex >= 0)
  374. {
  375. set (nextIndex, (*this)[nextIndex] + String (preNumberString) + String (++number) + String (postNumberString));
  376. nextIndex = indexOf (original, ignoreCase, nextIndex + 1);
  377. }
  378. }
  379. }
  380. }
  381. void StringArray::ensureStorageAllocated (int minNumElements)
  382. {
  383. strings.ensureStorageAllocated (minNumElements);
  384. }
  385. void StringArray::minimiseStorageOverheads()
  386. {
  387. strings.minimiseStorageOverheads();
  388. }
  389. }