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.

486 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. StringArray& StringArray::operator= (const StringArray& other)
  55. {
  56. strings = other.strings;
  57. return *this;
  58. }
  59. StringArray& StringArray::operator= (StringArray&& other) noexcept
  60. {
  61. strings = static_cast<Array<String>&&> (other.strings);
  62. return *this;
  63. }
  64. StringArray::~StringArray()
  65. {
  66. }
  67. bool StringArray::operator== (const StringArray& other) const noexcept
  68. {
  69. return strings == other.strings;
  70. }
  71. bool StringArray::operator!= (const StringArray& other) const noexcept
  72. {
  73. return ! operator== (other);
  74. }
  75. void StringArray::swapWith (StringArray& other) noexcept
  76. {
  77. strings.swapWith (other.strings);
  78. }
  79. void StringArray::clear()
  80. {
  81. strings.clear();
  82. }
  83. void StringArray::clearQuick()
  84. {
  85. strings.clearQuick();
  86. }
  87. const String& StringArray::operator[] (const int index) const noexcept
  88. {
  89. if (isPositiveAndBelow (index, strings.size()))
  90. return strings.getReference (index);
  91. #if JUCE_ALLOW_STATIC_NULL_VARIABLES
  92. return String::empty;
  93. #else
  94. static String empty;
  95. return empty;
  96. #endif
  97. }
  98. String& StringArray::getReference (const 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 (const int index, const String& newString)
  111. {
  112. strings.insert (index, newString);
  113. }
  114. bool StringArray::addIfNotAlreadyThere (const String& newString, const 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, const bool ignoreCase)
  134. {
  135. for (auto& s : otherArray)
  136. addIfNotAlreadyThere (s, ignoreCase);
  137. }
  138. void StringArray::set (const int index, const String& newString)
  139. {
  140. strings.set (index, newString);
  141. }
  142. bool StringArray::contains (StringRef stringToLookFor, const bool ignoreCase) const
  143. {
  144. return indexOf (stringToLookFor, ignoreCase) >= 0;
  145. }
  146. int StringArray::indexOf (StringRef stringToLookFor, const 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 (const int currentIndex, const 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, const 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 (const 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. struct InternalStringArrayComparator_CaseSensitive
  216. {
  217. static int compareElements (String& s1, String& s2) noexcept { return s1.compare (s2); }
  218. };
  219. struct InternalStringArrayComparator_CaseInsensitive
  220. {
  221. static int compareElements (String& s1, String& s2) noexcept { return s1.compareIgnoreCase (s2); }
  222. };
  223. struct InternalStringArrayComparator_Natural
  224. {
  225. static int compareElements (String& s1, String& s2) noexcept { return s1.compareNatural (s2); }
  226. };
  227. void StringArray::sort (const bool ignoreCase)
  228. {
  229. if (ignoreCase)
  230. {
  231. InternalStringArrayComparator_CaseInsensitive comp;
  232. strings.sort (comp);
  233. }
  234. else
  235. {
  236. InternalStringArrayComparator_CaseSensitive comp;
  237. strings.sort (comp);
  238. }
  239. }
  240. void StringArray::sortNatural()
  241. {
  242. InternalStringArrayComparator_Natural comp;
  243. strings.sort (comp);
  244. }
  245. //==============================================================================
  246. String StringArray::joinIntoString (StringRef separator, int start, int numberToJoin) const
  247. {
  248. auto last = (numberToJoin < 0) ? size()
  249. : jmin (size(), start + numberToJoin);
  250. if (start < 0)
  251. start = 0;
  252. if (start >= last)
  253. return {};
  254. if (start == last - 1)
  255. return strings.getReference (start);
  256. auto separatorBytes = separator.text.sizeInBytes() - sizeof (String::CharPointerType::CharType);
  257. auto bytesNeeded = separatorBytes * (size_t) (last - start - 1);
  258. for (int i = start; i < last; ++i)
  259. bytesNeeded += strings.getReference(i).getCharPointer().sizeInBytes() - sizeof (String::CharPointerType::CharType);
  260. String result;
  261. result.preallocateBytes (bytesNeeded);
  262. auto dest = result.getCharPointer();
  263. while (start < last)
  264. {
  265. auto& s = strings.getReference (start);
  266. if (! s.isEmpty())
  267. dest.writeAll (s.getCharPointer());
  268. if (++start < last && separatorBytes > 0)
  269. dest.writeAll (separator.text);
  270. }
  271. dest.writeNull();
  272. return result;
  273. }
  274. int StringArray::addTokens (StringRef text, const bool preserveQuotedStrings)
  275. {
  276. return addTokens (text, " \n\r\t", preserveQuotedStrings ? "\"" : "");
  277. }
  278. int StringArray::addTokens (StringRef text, StringRef breakCharacters, StringRef quoteCharacters)
  279. {
  280. int num = 0;
  281. if (text.isNotEmpty())
  282. {
  283. for (auto t = text.text;;)
  284. {
  285. auto tokenEnd = CharacterFunctions::findEndOfToken (t,
  286. breakCharacters.text,
  287. quoteCharacters.text);
  288. strings.add (String (t, tokenEnd));
  289. ++num;
  290. if (tokenEnd.isEmpty())
  291. break;
  292. t = ++tokenEnd;
  293. }
  294. }
  295. return num;
  296. }
  297. int StringArray::addLines (StringRef sourceText)
  298. {
  299. int numLines = 0;
  300. auto text = sourceText.text;
  301. bool finished = text.isEmpty();
  302. while (! finished)
  303. {
  304. for (auto startOfLine = text;;)
  305. {
  306. auto endOfLine = text;
  307. switch (text.getAndAdvance())
  308. {
  309. case 0: finished = true; break;
  310. case '\n': break;
  311. case '\r': if (*text == '\n') ++text; break;
  312. default: continue;
  313. }
  314. strings.add (String (startOfLine, endOfLine));
  315. ++numLines;
  316. break;
  317. }
  318. }
  319. return numLines;
  320. }
  321. StringArray StringArray::fromTokens (StringRef stringToTokenise, bool preserveQuotedStrings)
  322. {
  323. StringArray s;
  324. s.addTokens (stringToTokenise, preserveQuotedStrings);
  325. return s;
  326. }
  327. StringArray StringArray::fromTokens (StringRef stringToTokenise,
  328. StringRef breakCharacters,
  329. StringRef quoteCharacters)
  330. {
  331. StringArray s;
  332. s.addTokens (stringToTokenise, breakCharacters, quoteCharacters);
  333. return s;
  334. }
  335. StringArray StringArray::fromLines (StringRef stringToBreakUp)
  336. {
  337. StringArray s;
  338. s.addLines (stringToBreakUp);
  339. return s;
  340. }
  341. //==============================================================================
  342. void StringArray::removeDuplicates (const bool ignoreCase)
  343. {
  344. for (int i = 0; i < size() - 1; ++i)
  345. {
  346. auto s = strings.getReference(i);
  347. for (int nextIndex = i + 1;;)
  348. {
  349. nextIndex = indexOf (s, ignoreCase, nextIndex);
  350. if (nextIndex < 0)
  351. break;
  352. strings.remove (nextIndex);
  353. }
  354. }
  355. }
  356. void StringArray::appendNumbersToDuplicates (const bool ignoreCase,
  357. const bool appendNumberToFirstInstance,
  358. CharPointer_UTF8 preNumberString,
  359. CharPointer_UTF8 postNumberString)
  360. {
  361. if (preNumberString.getAddress() == nullptr)
  362. preNumberString = CharPointer_UTF8 (" (");
  363. if (postNumberString.getAddress() == nullptr)
  364. postNumberString = CharPointer_UTF8 (")");
  365. for (int i = 0; i < size() - 1; ++i)
  366. {
  367. auto& s = strings.getReference(i);
  368. auto nextIndex = indexOf (s, ignoreCase, i + 1);
  369. if (nextIndex >= 0)
  370. {
  371. auto original = s;
  372. int number = 0;
  373. if (appendNumberToFirstInstance)
  374. s = original + String (preNumberString) + String (++number) + String (postNumberString);
  375. else
  376. ++number;
  377. while (nextIndex >= 0)
  378. {
  379. set (nextIndex, (*this)[nextIndex] + String (preNumberString) + String (++number) + String (postNumberString));
  380. nextIndex = indexOf (original, ignoreCase, nextIndex + 1);
  381. }
  382. }
  383. }
  384. }
  385. void StringArray::ensureStorageAllocated (int minNumElements)
  386. {
  387. strings.ensureStorageAllocated (minNumElements);
  388. }
  389. void StringArray::minimiseStorageOverheads()
  390. {
  391. strings.minimiseStorageOverheads();
  392. }
  393. } // namespace juce