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.

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