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.

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