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.

488 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. //==============================================================================
  19. StringArray::StringArray() noexcept
  20. {
  21. }
  22. StringArray::StringArray (const StringArray& other)
  23. : strings (other.strings)
  24. {
  25. }
  26. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  27. StringArray::StringArray (StringArray&& other) noexcept
  28. : strings (static_cast <Array <String>&&> (other.strings))
  29. {
  30. }
  31. #endif
  32. StringArray::StringArray (const String& firstValue)
  33. {
  34. strings.add (firstValue);
  35. }
  36. namespace StringArrayHelpers
  37. {
  38. template <typename CharType>
  39. void addArray (Array<String>& dest, const CharType* const* strings)
  40. {
  41. if (strings != nullptr)
  42. while (*strings != nullptr)
  43. dest.add (*strings++);
  44. }
  45. template <typename CharType>
  46. void addArray (Array<String>& dest, const CharType* const* const strings, const int numberOfStrings)
  47. {
  48. for (int i = 0; i < numberOfStrings; ++i)
  49. dest.add (strings [i]);
  50. }
  51. }
  52. StringArray::StringArray (const char* const* const initialStrings)
  53. {
  54. StringArrayHelpers::addArray (strings, initialStrings);
  55. }
  56. StringArray::StringArray (const char* const* const initialStrings, const int numberOfStrings)
  57. {
  58. StringArrayHelpers::addArray (strings, initialStrings, numberOfStrings);
  59. }
  60. StringArray::StringArray (const wchar_t* const* const initialStrings)
  61. {
  62. StringArrayHelpers::addArray (strings, initialStrings);
  63. }
  64. StringArray::StringArray (const wchar_t* const* const initialStrings, const int numberOfStrings)
  65. {
  66. StringArrayHelpers::addArray (strings, initialStrings, numberOfStrings);
  67. }
  68. StringArray& StringArray::operator= (const StringArray& other)
  69. {
  70. strings = other.strings;
  71. return *this;
  72. }
  73. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  74. StringArray& StringArray::operator= (StringArray&& other) noexcept
  75. {
  76. strings = static_cast <Array<String>&&> (other.strings);
  77. return *this;
  78. }
  79. #endif
  80. StringArray::~StringArray()
  81. {
  82. }
  83. bool StringArray::operator== (const StringArray& other) const noexcept
  84. {
  85. if (other.size() != size())
  86. return false;
  87. for (int i = size(); --i >= 0;)
  88. if (other.strings.getReference(i) != strings.getReference(i))
  89. return false;
  90. return true;
  91. }
  92. bool StringArray::operator!= (const StringArray& other) const noexcept
  93. {
  94. return ! operator== (other);
  95. }
  96. void StringArray::clear()
  97. {
  98. strings.clear();
  99. }
  100. const String& StringArray::operator[] (const int index) const noexcept
  101. {
  102. if (isPositiveAndBelow (index, strings.size()))
  103. return strings.getReference (index);
  104. return String::empty;
  105. }
  106. String& StringArray::getReference (const int index) noexcept
  107. {
  108. jassert (isPositiveAndBelow (index, strings.size()));
  109. return strings.getReference (index);
  110. }
  111. void StringArray::add (const String& newString)
  112. {
  113. strings.add (newString);
  114. }
  115. void StringArray::insert (const int index, const String& newString)
  116. {
  117. strings.insert (index, newString);
  118. }
  119. void StringArray::addIfNotAlreadyThere (const String& newString, const bool ignoreCase)
  120. {
  121. if (! contains (newString, ignoreCase))
  122. add (newString);
  123. }
  124. void StringArray::addArray (const StringArray& otherArray, int startIndex, int numElementsToAdd)
  125. {
  126. if (startIndex < 0)
  127. {
  128. jassertfalse;
  129. startIndex = 0;
  130. }
  131. if (numElementsToAdd < 0 || startIndex + numElementsToAdd > otherArray.size())
  132. numElementsToAdd = otherArray.size() - startIndex;
  133. while (--numElementsToAdd >= 0)
  134. strings.add (otherArray.strings.getReference (startIndex++));
  135. }
  136. void StringArray::set (const int index, const String& newString)
  137. {
  138. strings.set (index, newString);
  139. }
  140. bool StringArray::contains (const String& stringToLookFor, const bool ignoreCase) const
  141. {
  142. if (ignoreCase)
  143. {
  144. for (int i = size(); --i >= 0;)
  145. if (strings.getReference(i).equalsIgnoreCase (stringToLookFor))
  146. return true;
  147. }
  148. else
  149. {
  150. for (int i = size(); --i >= 0;)
  151. if (stringToLookFor == strings.getReference(i))
  152. return true;
  153. }
  154. return false;
  155. }
  156. int StringArray::indexOf (const String& stringToLookFor, const bool ignoreCase, int i) const
  157. {
  158. if (i < 0)
  159. i = 0;
  160. const int numElements = size();
  161. if (ignoreCase)
  162. {
  163. while (i < numElements)
  164. {
  165. if (strings.getReference(i).equalsIgnoreCase (stringToLookFor))
  166. return i;
  167. ++i;
  168. }
  169. }
  170. else
  171. {
  172. while (i < numElements)
  173. {
  174. if (stringToLookFor == strings.getReference (i))
  175. return i;
  176. ++i;
  177. }
  178. }
  179. return -1;
  180. }
  181. //==============================================================================
  182. void StringArray::remove (const int index)
  183. {
  184. strings.remove (index);
  185. }
  186. void StringArray::removeString (const String& stringToRemove,
  187. const bool ignoreCase)
  188. {
  189. if (ignoreCase)
  190. {
  191. for (int i = size(); --i >= 0;)
  192. if (strings.getReference(i).equalsIgnoreCase (stringToRemove))
  193. strings.remove (i);
  194. }
  195. else
  196. {
  197. for (int i = size(); --i >= 0;)
  198. if (stringToRemove == strings.getReference (i))
  199. strings.remove (i);
  200. }
  201. }
  202. void StringArray::removeRange (int startIndex, int numberToRemove)
  203. {
  204. strings.removeRange (startIndex, numberToRemove);
  205. }
  206. //==============================================================================
  207. void StringArray::removeEmptyStrings (const bool removeWhitespaceStrings)
  208. {
  209. if (removeWhitespaceStrings)
  210. {
  211. for (int i = size(); --i >= 0;)
  212. if (! strings.getReference(i).containsNonWhitespaceChars())
  213. strings.remove (i);
  214. }
  215. else
  216. {
  217. for (int i = size(); --i >= 0;)
  218. if (strings.getReference(i).isEmpty())
  219. strings.remove (i);
  220. }
  221. }
  222. void StringArray::trim()
  223. {
  224. for (int i = size(); --i >= 0;)
  225. {
  226. String& s = strings.getReference(i);
  227. s = s.trim();
  228. }
  229. }
  230. //==============================================================================
  231. struct InternalStringArrayComparator_CaseSensitive
  232. {
  233. static int compareElements (String& first, String& second) { return first.compare (second); }
  234. };
  235. struct InternalStringArrayComparator_CaseInsensitive
  236. {
  237. static int compareElements (String& first, String& second) { return first.compareIgnoreCase (second); }
  238. };
  239. void StringArray::sort (const bool ignoreCase)
  240. {
  241. if (ignoreCase)
  242. {
  243. InternalStringArrayComparator_CaseInsensitive comp;
  244. strings.sort (comp);
  245. }
  246. else
  247. {
  248. InternalStringArrayComparator_CaseSensitive comp;
  249. strings.sort (comp);
  250. }
  251. }
  252. void StringArray::move (const int currentIndex, int newIndex) noexcept
  253. {
  254. strings.move (currentIndex, newIndex);
  255. }
  256. //==============================================================================
  257. String StringArray::joinIntoString (const String& separator, int start, int numberToJoin) const
  258. {
  259. const int last = (numberToJoin < 0) ? size()
  260. : jmin (size(), start + numberToJoin);
  261. if (start < 0)
  262. start = 0;
  263. if (start >= last)
  264. return String::empty;
  265. if (start == last - 1)
  266. return strings.getReference (start);
  267. const size_t separatorBytes = separator.getCharPointer().sizeInBytes() - sizeof (String::CharPointerType::CharType);
  268. size_t bytesNeeded = separatorBytes * (last - start - 1);
  269. for (int i = start; i < last; ++i)
  270. bytesNeeded += strings.getReference(i).getCharPointer().sizeInBytes() - sizeof (String::CharPointerType::CharType);
  271. String result;
  272. result.preallocateBytes (bytesNeeded);
  273. String::CharPointerType dest (result.getCharPointer());
  274. while (start < last)
  275. {
  276. const String& s = strings.getReference (start);
  277. if (! s.isEmpty())
  278. dest.writeAll (s.getCharPointer());
  279. if (++start < last && separatorBytes > 0)
  280. dest.writeAll (separator.getCharPointer());
  281. }
  282. dest.writeNull();
  283. return result;
  284. }
  285. int StringArray::addTokens (const String& text, const bool preserveQuotedStrings)
  286. {
  287. return addTokens (text, " \n\r\t", preserveQuotedStrings ? "\"" : "");
  288. }
  289. int StringArray::addTokens (const String& text, const String& breakCharacters, const String& quoteCharacters)
  290. {
  291. int num = 0;
  292. String::CharPointerType t (text.getCharPointer());
  293. if (! t.isEmpty())
  294. {
  295. for (;;)
  296. {
  297. String::CharPointerType tokenEnd (CharacterFunctions::findEndOfToken (t,
  298. breakCharacters.getCharPointer(),
  299. quoteCharacters.getCharPointer()));
  300. add (String (t, tokenEnd));
  301. ++num;
  302. if (tokenEnd.isEmpty())
  303. break;
  304. t = ++tokenEnd;
  305. }
  306. }
  307. return num;
  308. }
  309. int StringArray::addLines (const String& sourceText)
  310. {
  311. int numLines = 0;
  312. String::CharPointerType text (sourceText.getCharPointer());
  313. bool finished = text.isEmpty();
  314. while (! finished)
  315. {
  316. String::CharPointerType startOfLine (text);
  317. size_t numChars = 0;
  318. for (;;)
  319. {
  320. const juce_wchar c = text.getAndAdvance();
  321. if (c == 0)
  322. {
  323. finished = true;
  324. break;
  325. }
  326. if (c == '\n')
  327. break;
  328. if (c == '\r')
  329. {
  330. if (*text == '\n')
  331. ++text;
  332. break;
  333. }
  334. ++numChars;
  335. }
  336. add (String (startOfLine, numChars));
  337. ++numLines;
  338. }
  339. return numLines;
  340. }
  341. //==============================================================================
  342. void StringArray::removeDuplicates (const bool ignoreCase)
  343. {
  344. for (int i = 0; i < size() - 1; ++i)
  345. {
  346. const String s (strings.getReference(i));
  347. int nextIndex = i + 1;
  348. for (;;)
  349. {
  350. nextIndex = indexOf (s, ignoreCase, nextIndex);
  351. if (nextIndex < 0)
  352. break;
  353. strings.remove (nextIndex);
  354. }
  355. }
  356. }
  357. void StringArray::appendNumbersToDuplicates (const bool ignoreCase,
  358. const bool appendNumberToFirstInstance,
  359. CharPointer_UTF8 preNumberString,
  360. CharPointer_UTF8 postNumberString)
  361. {
  362. CharPointer_UTF8 defaultPre (" ("), defaultPost (")");
  363. if (preNumberString.getAddress() == nullptr)
  364. preNumberString = defaultPre;
  365. if (postNumberString.getAddress() == nullptr)
  366. postNumberString = defaultPost;
  367. for (int i = 0; i < size() - 1; ++i)
  368. {
  369. String& s = strings.getReference(i);
  370. int nextIndex = indexOf (s, ignoreCase, i + 1);
  371. if (nextIndex >= 0)
  372. {
  373. const String original (s);
  374. int number = 0;
  375. if (appendNumberToFirstInstance)
  376. s = original + String (preNumberString) + String (++number) + String (postNumberString);
  377. else
  378. ++number;
  379. while (nextIndex >= 0)
  380. {
  381. set (nextIndex, (*this)[nextIndex] + String (preNumberString) + String (++number) + String (postNumberString));
  382. nextIndex = indexOf (original, ignoreCase, nextIndex + 1);
  383. }
  384. }
  385. }
  386. }
  387. void StringArray::minimiseStorageOverheads()
  388. {
  389. strings.minimiseStorageOverheads();
  390. }