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.

475 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. namespace juce
  18. {
  19. StringArray::StringArray() noexcept
  20. {
  21. }
  22. StringArray::StringArray (const StringArray& other)
  23. : strings (other.strings)
  24. {
  25. }
  26. StringArray::StringArray (StringArray&& other) noexcept
  27. : strings (std::move (other.strings))
  28. {
  29. }
  30. StringArray::StringArray (Array<String>&& other) noexcept
  31. : strings (std::move (other))
  32. {
  33. }
  34. StringArray::StringArray (const String& firstValue)
  35. {
  36. strings.add (firstValue);
  37. }
  38. StringArray::StringArray (const String* initialStrings, int numberOfStrings)
  39. {
  40. strings.addArray (initialStrings, numberOfStrings);
  41. }
  42. StringArray::StringArray (const char* const* initialStrings)
  43. {
  44. strings.addNullTerminatedArray (initialStrings);
  45. }
  46. StringArray::StringArray (const char* const* initialStrings, int numberOfStrings)
  47. {
  48. strings.addArray (initialStrings, numberOfStrings);
  49. }
  50. StringArray::StringArray (const wchar_t* const* initialStrings)
  51. {
  52. strings.addNullTerminatedArray (initialStrings);
  53. }
  54. StringArray::StringArray (const wchar_t* const* initialStrings, int numberOfStrings)
  55. {
  56. strings.addArray (initialStrings, numberOfStrings);
  57. }
  58. StringArray::StringArray (const std::initializer_list<const char*>& stringList)
  59. {
  60. strings.addArray (stringList);
  61. }
  62. StringArray& StringArray::operator= (const StringArray& other)
  63. {
  64. strings = other.strings;
  65. return *this;
  66. }
  67. StringArray& StringArray::operator= (StringArray&& other) noexcept
  68. {
  69. strings = std::move (other.strings);
  70. return *this;
  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[] (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 (int index) noexcept
  100. {
  101. return strings.getReference (index);
  102. }
  103. const String& StringArray::getReference (int index) const noexcept
  104. {
  105. return strings.getReference (index);
  106. }
  107. void StringArray::add (String newString)
  108. {
  109. // NB: the local temp copy is to avoid a dangling pointer if the
  110. // argument being passed-in is a reference into this array.
  111. strings.add (std::move (newString));
  112. }
  113. void StringArray::insert (int index, String newString)
  114. {
  115. // NB: the local temp copy is to avoid a dangling pointer if the
  116. // argument being passed-in is a reference into this array.
  117. strings.insert (index, std::move (newString));
  118. }
  119. bool StringArray::addIfNotAlreadyThere (const String& newString, bool ignoreCase)
  120. {
  121. if (contains (newString, ignoreCase))
  122. return false;
  123. add (newString);
  124. return true;
  125. }
  126. void StringArray::addArray (const StringArray& otherArray, int startIndex, int numElementsToAdd)
  127. {
  128. jassert (this != &otherArray); // can't add from our own elements!
  129. if (startIndex < 0)
  130. {
  131. jassertfalse;
  132. startIndex = 0;
  133. }
  134. if (numElementsToAdd < 0 || startIndex + numElementsToAdd > otherArray.size())
  135. numElementsToAdd = otherArray.size() - startIndex;
  136. while (--numElementsToAdd >= 0)
  137. strings.add (otherArray.strings.getReference (startIndex++));
  138. }
  139. void StringArray::mergeArray (const StringArray& otherArray, bool ignoreCase)
  140. {
  141. jassert (this != &otherArray); // can't add from our own elements!
  142. for (auto& s : otherArray)
  143. addIfNotAlreadyThere (s, ignoreCase);
  144. }
  145. void StringArray::set (int index, String newString)
  146. {
  147. strings.set (index, std::move (newString));
  148. }
  149. bool StringArray::contains (StringRef stringToLookFor, bool ignoreCase) const
  150. {
  151. return indexOf (stringToLookFor, ignoreCase) >= 0;
  152. }
  153. int StringArray::indexOf (StringRef stringToLookFor, bool ignoreCase, int i) const
  154. {
  155. if (i < 0)
  156. i = 0;
  157. auto numElements = size();
  158. if (ignoreCase)
  159. {
  160. for (; i < numElements; ++i)
  161. if (strings.getReference (i).equalsIgnoreCase (stringToLookFor))
  162. return i;
  163. }
  164. else
  165. {
  166. for (; i < numElements; ++i)
  167. if (stringToLookFor == strings.getReference (i))
  168. return i;
  169. }
  170. return -1;
  171. }
  172. void StringArray::move (int currentIndex, int newIndex) noexcept
  173. {
  174. strings.move (currentIndex, newIndex);
  175. }
  176. //==============================================================================
  177. void StringArray::remove (int index)
  178. {
  179. strings.remove (index);
  180. }
  181. void StringArray::removeString (StringRef stringToRemove, bool ignoreCase)
  182. {
  183. if (ignoreCase)
  184. {
  185. for (int i = size(); --i >= 0;)
  186. if (strings.getReference (i).equalsIgnoreCase (stringToRemove))
  187. strings.remove (i);
  188. }
  189. else
  190. {
  191. for (int i = size(); --i >= 0;)
  192. if (stringToRemove == strings.getReference (i))
  193. strings.remove (i);
  194. }
  195. }
  196. void StringArray::removeRange (int startIndex, int numberToRemove)
  197. {
  198. strings.removeRange (startIndex, numberToRemove);
  199. }
  200. //==============================================================================
  201. void StringArray::removeEmptyStrings (bool removeWhitespaceStrings)
  202. {
  203. if (removeWhitespaceStrings)
  204. {
  205. for (int i = size(); --i >= 0;)
  206. if (! strings.getReference (i).containsNonWhitespaceChars())
  207. strings.remove (i);
  208. }
  209. else
  210. {
  211. for (int i = size(); --i >= 0;)
  212. if (strings.getReference (i).isEmpty())
  213. strings.remove (i);
  214. }
  215. }
  216. void StringArray::trim()
  217. {
  218. for (auto& s : strings)
  219. s = s.trim();
  220. }
  221. //==============================================================================
  222. void StringArray::sort (bool ignoreCase)
  223. {
  224. if (ignoreCase)
  225. std::sort (strings.begin(), strings.end(),
  226. [] (const String& a, const String& b) { return a.compareIgnoreCase (b) < 0; });
  227. else
  228. std::sort (strings.begin(), strings.end());
  229. }
  230. void StringArray::sortNatural()
  231. {
  232. std::sort (strings.begin(), strings.end(),
  233. [] (const String& a, const String& b) { return a.compareNatural (b) < 0; });
  234. }
  235. //==============================================================================
  236. String StringArray::joinIntoString (StringRef separator, int start, int numberToJoin) const
  237. {
  238. auto last = (numberToJoin < 0) ? size()
  239. : jmin (size(), start + numberToJoin);
  240. if (start < 0)
  241. start = 0;
  242. if (start >= last)
  243. return {};
  244. if (start == last - 1)
  245. return strings.getReference (start);
  246. auto separatorBytes = separator.text.sizeInBytes() - sizeof (String::CharPointerType::CharType);
  247. auto bytesNeeded = (size_t) (last - start - 1) * separatorBytes;
  248. for (int i = start; i < last; ++i)
  249. bytesNeeded += strings.getReference (i).getCharPointer().sizeInBytes() - sizeof (String::CharPointerType::CharType);
  250. String result;
  251. result.preallocateBytes (bytesNeeded);
  252. auto dest = result.getCharPointer();
  253. while (start < last)
  254. {
  255. auto& s = strings.getReference (start);
  256. if (! s.isEmpty())
  257. dest.writeAll (s.getCharPointer());
  258. if (++start < last && separatorBytes > 0)
  259. dest.writeAll (separator.text);
  260. }
  261. dest.writeNull();
  262. return result;
  263. }
  264. int StringArray::addTokens (StringRef text, const bool preserveQuotedStrings)
  265. {
  266. return addTokens (text, " \n\r\t", preserveQuotedStrings ? "\"" : "");
  267. }
  268. int StringArray::addTokens (StringRef text, StringRef breakCharacters, StringRef quoteCharacters)
  269. {
  270. int num = 0;
  271. if (text.isNotEmpty())
  272. {
  273. for (auto t = text.text;;)
  274. {
  275. auto tokenEnd = CharacterFunctions::findEndOfToken (t,
  276. breakCharacters.text,
  277. quoteCharacters.text);
  278. strings.add (String (t, tokenEnd));
  279. ++num;
  280. if (tokenEnd.isEmpty())
  281. break;
  282. t = ++tokenEnd;
  283. }
  284. }
  285. return num;
  286. }
  287. int StringArray::addLines (StringRef sourceText)
  288. {
  289. int numLines = 0;
  290. auto text = sourceText.text;
  291. bool finished = text.isEmpty();
  292. while (! finished)
  293. {
  294. for (auto startOfLine = text;;)
  295. {
  296. auto endOfLine = text;
  297. switch (text.getAndAdvance())
  298. {
  299. case 0: finished = true; break;
  300. case '\n': break;
  301. case '\r': if (*text == '\n') ++text; break;
  302. default: continue;
  303. }
  304. strings.add (String (startOfLine, endOfLine));
  305. ++numLines;
  306. break;
  307. }
  308. }
  309. return numLines;
  310. }
  311. StringArray StringArray::fromTokens (StringRef stringToTokenise, bool preserveQuotedStrings)
  312. {
  313. StringArray s;
  314. s.addTokens (stringToTokenise, preserveQuotedStrings);
  315. return s;
  316. }
  317. StringArray StringArray::fromTokens (StringRef stringToTokenise,
  318. StringRef breakCharacters,
  319. StringRef quoteCharacters)
  320. {
  321. StringArray s;
  322. s.addTokens (stringToTokenise, breakCharacters, quoteCharacters);
  323. return s;
  324. }
  325. StringArray StringArray::fromLines (StringRef stringToBreakUp)
  326. {
  327. StringArray s;
  328. s.addLines (stringToBreakUp);
  329. return s;
  330. }
  331. //==============================================================================
  332. void StringArray::removeDuplicates (bool ignoreCase)
  333. {
  334. for (int i = 0; i < size() - 1; ++i)
  335. {
  336. auto s = strings.getReference (i);
  337. for (int nextIndex = i + 1;;)
  338. {
  339. nextIndex = indexOf (s, ignoreCase, nextIndex);
  340. if (nextIndex < 0)
  341. break;
  342. strings.remove (nextIndex);
  343. }
  344. }
  345. }
  346. void StringArray::appendNumbersToDuplicates (bool ignoreCase,
  347. bool appendNumberToFirstInstance,
  348. CharPointer_UTF8 preNumberString,
  349. CharPointer_UTF8 postNumberString)
  350. {
  351. if (preNumberString.getAddress() == nullptr)
  352. preNumberString = CharPointer_UTF8 (" (");
  353. if (postNumberString.getAddress() == nullptr)
  354. postNumberString = CharPointer_UTF8 (")");
  355. for (int i = 0; i < size() - 1; ++i)
  356. {
  357. auto& s = strings.getReference (i);
  358. auto nextIndex = indexOf (s, ignoreCase, i + 1);
  359. if (nextIndex >= 0)
  360. {
  361. auto original = s;
  362. int number = 0;
  363. if (appendNumberToFirstInstance)
  364. s = original + String (preNumberString) + String (++number) + String (postNumberString);
  365. else
  366. ++number;
  367. while (nextIndex >= 0)
  368. {
  369. set (nextIndex, (*this)[nextIndex] + String (preNumberString) + String (++number) + String (postNumberString));
  370. nextIndex = indexOf (original, ignoreCase, nextIndex + 1);
  371. }
  372. }
  373. }
  374. }
  375. void StringArray::ensureStorageAllocated (int minNumElements)
  376. {
  377. strings.ensureStorageAllocated (minNumElements);
  378. }
  379. void StringArray::minimiseStorageOverheads()
  380. {
  381. strings.minimiseStorageOverheads();
  382. }
  383. } // namespace juce