The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

495 lines
13KB

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