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.

509 lines
14KB

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