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.

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