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.

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