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.

518 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. StringArray::StringArray() noexcept
  22. {
  23. }
  24. StringArray::StringArray (const StringArray& other)
  25. : strings (other.strings)
  26. {
  27. }
  28. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  29. StringArray::StringArray (StringArray&& other) noexcept
  30. : strings (static_cast <Array <String>&&> (other.strings))
  31. {
  32. }
  33. #endif
  34. StringArray::StringArray (const String& firstValue)
  35. {
  36. strings.add (firstValue);
  37. }
  38. namespace StringArrayHelpers
  39. {
  40. template <typename CharType>
  41. void addArray (Array<String>& dest, const CharType* const* strings)
  42. {
  43. if (strings != nullptr)
  44. while (*strings != nullptr)
  45. dest.add (*strings++);
  46. }
  47. template <typename Type>
  48. void addArray (Array<String>& dest, const Type* const strings, const int numberOfStrings)
  49. {
  50. for (int i = 0; i < numberOfStrings; ++i)
  51. dest.add (strings [i]);
  52. }
  53. }
  54. StringArray::StringArray (const String* initialStrings, int numberOfStrings)
  55. {
  56. StringArrayHelpers::addArray (strings, initialStrings, numberOfStrings);
  57. }
  58. StringArray::StringArray (const char* const* const initialStrings)
  59. {
  60. StringArrayHelpers::addArray (strings, initialStrings);
  61. }
  62. StringArray::StringArray (const char* const* const initialStrings, const int numberOfStrings)
  63. {
  64. StringArrayHelpers::addArray (strings, initialStrings, numberOfStrings);
  65. }
  66. StringArray::StringArray (const wchar_t* const* const initialStrings)
  67. {
  68. StringArrayHelpers::addArray (strings, initialStrings);
  69. }
  70. StringArray::StringArray (const wchar_t* const* const initialStrings, const int numberOfStrings)
  71. {
  72. StringArrayHelpers::addArray (strings, initialStrings, numberOfStrings);
  73. }
  74. StringArray& StringArray::operator= (const StringArray& other)
  75. {
  76. strings = other.strings;
  77. return *this;
  78. }
  79. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  80. StringArray& StringArray::operator= (StringArray&& other) noexcept
  81. {
  82. strings = static_cast <Array<String>&&> (other.strings);
  83. return *this;
  84. }
  85. #endif
  86. StringArray::~StringArray()
  87. {
  88. }
  89. bool StringArray::operator== (const StringArray& other) const noexcept
  90. {
  91. if (other.size() != size())
  92. return false;
  93. for (int i = size(); --i >= 0;)
  94. if (other.strings.getReference(i) != strings.getReference(i))
  95. return false;
  96. return true;
  97. }
  98. bool StringArray::operator!= (const StringArray& other) const noexcept
  99. {
  100. return ! operator== (other);
  101. }
  102. void StringArray::swapWith (StringArray& other) noexcept
  103. {
  104. strings.swapWith (other.strings);
  105. }
  106. void StringArray::clear()
  107. {
  108. strings.clear();
  109. }
  110. void StringArray::clearQuick()
  111. {
  112. strings.clearQuick();
  113. }
  114. const String& StringArray::operator[] (const int index) const noexcept
  115. {
  116. if (isPositiveAndBelow (index, strings.size()))
  117. return strings.getReference (index);
  118. return String::empty;
  119. }
  120. String& StringArray::getReference (const int index) noexcept
  121. {
  122. jassert (isPositiveAndBelow (index, strings.size()));
  123. return strings.getReference (index);
  124. }
  125. void StringArray::add (const String& newString)
  126. {
  127. strings.add (newString);
  128. }
  129. void StringArray::insert (const int index, const String& newString)
  130. {
  131. strings.insert (index, newString);
  132. }
  133. void StringArray::addIfNotAlreadyThere (const String& newString, const bool ignoreCase)
  134. {
  135. if (! contains (newString, ignoreCase))
  136. add (newString);
  137. }
  138. void StringArray::addArray (const StringArray& otherArray, int startIndex, int numElementsToAdd)
  139. {
  140. if (startIndex < 0)
  141. {
  142. jassertfalse;
  143. startIndex = 0;
  144. }
  145. if (numElementsToAdd < 0 || startIndex + numElementsToAdd > otherArray.size())
  146. numElementsToAdd = otherArray.size() - startIndex;
  147. while (--numElementsToAdd >= 0)
  148. strings.add (otherArray.strings.getReference (startIndex++));
  149. }
  150. void StringArray::set (const int index, const String& newString)
  151. {
  152. strings.set (index, newString);
  153. }
  154. bool StringArray::contains (StringRef stringToLookFor, const bool ignoreCase) const
  155. {
  156. if (ignoreCase)
  157. {
  158. for (int i = size(); --i >= 0;)
  159. if (strings.getReference(i).equalsIgnoreCase (stringToLookFor))
  160. return true;
  161. }
  162. else
  163. {
  164. for (int i = size(); --i >= 0;)
  165. if (stringToLookFor == strings.getReference(i))
  166. return true;
  167. }
  168. return false;
  169. }
  170. int StringArray::indexOf (StringRef stringToLookFor, const bool ignoreCase, int i) const
  171. {
  172. if (i < 0)
  173. i = 0;
  174. const int numElements = size();
  175. if (ignoreCase)
  176. {
  177. while (i < numElements)
  178. {
  179. if (strings.getReference(i).equalsIgnoreCase (stringToLookFor))
  180. return i;
  181. ++i;
  182. }
  183. }
  184. else
  185. {
  186. while (i < numElements)
  187. {
  188. if (stringToLookFor == strings.getReference (i))
  189. return i;
  190. ++i;
  191. }
  192. }
  193. return -1;
  194. }
  195. //==============================================================================
  196. void StringArray::remove (const int index)
  197. {
  198. strings.remove (index);
  199. }
  200. void StringArray::removeString (StringRef stringToRemove, const bool ignoreCase)
  201. {
  202. if (ignoreCase)
  203. {
  204. for (int i = size(); --i >= 0;)
  205. if (strings.getReference(i).equalsIgnoreCase (stringToRemove))
  206. strings.remove (i);
  207. }
  208. else
  209. {
  210. for (int i = size(); --i >= 0;)
  211. if (stringToRemove == strings.getReference (i))
  212. strings.remove (i);
  213. }
  214. }
  215. void StringArray::removeRange (int startIndex, int numberToRemove)
  216. {
  217. strings.removeRange (startIndex, numberToRemove);
  218. }
  219. //==============================================================================
  220. void StringArray::removeEmptyStrings (const bool removeWhitespaceStrings)
  221. {
  222. if (removeWhitespaceStrings)
  223. {
  224. for (int i = size(); --i >= 0;)
  225. if (! strings.getReference(i).containsNonWhitespaceChars())
  226. strings.remove (i);
  227. }
  228. else
  229. {
  230. for (int i = size(); --i >= 0;)
  231. if (strings.getReference(i).isEmpty())
  232. strings.remove (i);
  233. }
  234. }
  235. void StringArray::trim()
  236. {
  237. for (int i = size(); --i >= 0;)
  238. {
  239. String& s = strings.getReference(i);
  240. s = s.trim();
  241. }
  242. }
  243. //==============================================================================
  244. struct InternalStringArrayComparator_CaseSensitive
  245. {
  246. static int compareElements (String& first, String& second) { return first.compare (second); }
  247. };
  248. struct InternalStringArrayComparator_CaseInsensitive
  249. {
  250. static int compareElements (String& first, String& second) { return first.compareIgnoreCase (second); }
  251. };
  252. void StringArray::sort (const bool ignoreCase)
  253. {
  254. if (ignoreCase)
  255. {
  256. InternalStringArrayComparator_CaseInsensitive comp;
  257. strings.sort (comp);
  258. }
  259. else
  260. {
  261. InternalStringArrayComparator_CaseSensitive comp;
  262. strings.sort (comp);
  263. }
  264. }
  265. void StringArray::move (const int currentIndex, int newIndex) noexcept
  266. {
  267. strings.move (currentIndex, newIndex);
  268. }
  269. //==============================================================================
  270. String StringArray::joinIntoString (StringRef separator, int start, int numberToJoin) const
  271. {
  272. const int last = (numberToJoin < 0) ? size()
  273. : jmin (size(), start + numberToJoin);
  274. if (start < 0)
  275. start = 0;
  276. if (start >= last)
  277. return String::empty;
  278. if (start == last - 1)
  279. return strings.getReference (start);
  280. const size_t separatorBytes = separator.text.sizeInBytes() - sizeof (String::CharPointerType::CharType);
  281. size_t bytesNeeded = separatorBytes * (size_t) (last - start - 1);
  282. for (int i = start; i < last; ++i)
  283. bytesNeeded += strings.getReference(i).getCharPointer().sizeInBytes() - sizeof (String::CharPointerType::CharType);
  284. String result;
  285. result.preallocateBytes (bytesNeeded);
  286. String::CharPointerType dest (result.getCharPointer());
  287. while (start < last)
  288. {
  289. const String& s = strings.getReference (start);
  290. if (! s.isEmpty())
  291. dest.writeAll (s.getCharPointer());
  292. if (++start < last && separatorBytes > 0)
  293. dest.writeAll (separator.text);
  294. }
  295. dest.writeNull();
  296. return result;
  297. }
  298. int StringArray::addTokens (StringRef text, const bool preserveQuotedStrings)
  299. {
  300. return addTokens (text, " \n\r\t", preserveQuotedStrings ? "\"" : "");
  301. }
  302. int StringArray::addTokens (StringRef text, StringRef breakCharacters, StringRef quoteCharacters)
  303. {
  304. int num = 0;
  305. if (text.isNotEmpty())
  306. {
  307. for (String::CharPointerType t (text.text);;)
  308. {
  309. String::CharPointerType tokenEnd (CharacterFunctions::findEndOfToken (t,
  310. breakCharacters.text,
  311. quoteCharacters.text));
  312. strings.add (String (t, tokenEnd));
  313. ++num;
  314. if (tokenEnd.isEmpty())
  315. break;
  316. t = ++tokenEnd;
  317. }
  318. }
  319. return num;
  320. }
  321. int StringArray::addLines (StringRef sourceText)
  322. {
  323. int numLines = 0;
  324. String::CharPointerType text (sourceText.text);
  325. bool finished = text.isEmpty();
  326. while (! finished)
  327. {
  328. for (String::CharPointerType startOfLine (text);;)
  329. {
  330. const String::CharPointerType endOfLine (text);
  331. switch (text.getAndAdvance())
  332. {
  333. case 0: finished = true; break;
  334. case '\n': break;
  335. case '\r': if (*text == '\n') ++text; break;
  336. default: continue;
  337. }
  338. strings.add (String (startOfLine, endOfLine));
  339. ++numLines;
  340. break;
  341. }
  342. }
  343. return numLines;
  344. }
  345. StringArray StringArray::fromTokens (StringRef stringToTokenise, bool preserveQuotedStrings)
  346. {
  347. StringArray s;
  348. s.addTokens (stringToTokenise, preserveQuotedStrings);
  349. return s;
  350. }
  351. StringArray StringArray::fromTokens (StringRef stringToTokenise,
  352. StringRef breakCharacters,
  353. StringRef quoteCharacters)
  354. {
  355. StringArray s;
  356. s.addTokens (stringToTokenise, breakCharacters, quoteCharacters);
  357. return s;
  358. }
  359. StringArray StringArray::fromLines (StringRef stringToBreakUp)
  360. {
  361. StringArray s;
  362. s.addLines (stringToBreakUp);
  363. return s;
  364. }
  365. //==============================================================================
  366. void StringArray::removeDuplicates (const bool ignoreCase)
  367. {
  368. for (int i = 0; i < size() - 1; ++i)
  369. {
  370. const String s (strings.getReference(i));
  371. int nextIndex = i + 1;
  372. for (;;)
  373. {
  374. nextIndex = indexOf (s, ignoreCase, nextIndex);
  375. if (nextIndex < 0)
  376. break;
  377. strings.remove (nextIndex);
  378. }
  379. }
  380. }
  381. void StringArray::appendNumbersToDuplicates (const bool ignoreCase,
  382. const bool appendNumberToFirstInstance,
  383. CharPointer_UTF8 preNumberString,
  384. CharPointer_UTF8 postNumberString)
  385. {
  386. CharPointer_UTF8 defaultPre (" ("), defaultPost (")");
  387. if (preNumberString.getAddress() == nullptr)
  388. preNumberString = defaultPre;
  389. if (postNumberString.getAddress() == nullptr)
  390. postNumberString = defaultPost;
  391. for (int i = 0; i < size() - 1; ++i)
  392. {
  393. String& s = strings.getReference(i);
  394. int nextIndex = indexOf (s, ignoreCase, i + 1);
  395. if (nextIndex >= 0)
  396. {
  397. const String original (s);
  398. int number = 0;
  399. if (appendNumberToFirstInstance)
  400. s = original + String (preNumberString) + String (++number) + String (postNumberString);
  401. else
  402. ++number;
  403. while (nextIndex >= 0)
  404. {
  405. set (nextIndex, (*this)[nextIndex] + String (preNumberString) + String (++number) + String (postNumberString));
  406. nextIndex = indexOf (original, ignoreCase, nextIndex + 1);
  407. }
  408. }
  409. }
  410. }
  411. void StringArray::ensureStorageAllocated (int minNumElements)
  412. {
  413. strings.ensureStorageAllocated (minNumElements);
  414. }
  415. void StringArray::minimiseStorageOverheads()
  416. {
  417. strings.minimiseStorageOverheads();
  418. }