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.

466 lines
12KB

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