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.

499 lines
13KB

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