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.

312 lines
8.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. StringPairArray::StringPairArray (bool shouldIgnoreCase) : ignoreCase (shouldIgnoreCase)
  20. {
  21. }
  22. StringPairArray::StringPairArray (const StringPairArray& other)
  23. : keys (other.keys),
  24. values (other.values),
  25. ignoreCase (other.ignoreCase)
  26. {
  27. }
  28. StringPairArray& StringPairArray::operator= (const StringPairArray& other)
  29. {
  30. keys = other.keys;
  31. values = other.values;
  32. return *this;
  33. }
  34. bool StringPairArray::operator== (const StringPairArray& other) const
  35. {
  36. auto num = size();
  37. if (num != other.size())
  38. return false;
  39. for (int i = 0; i < num; ++i)
  40. {
  41. if (keys[i] == other.keys[i]) // optimise for the case where the keys are in the same order
  42. {
  43. if (values[i] != other.values[i])
  44. return false;
  45. }
  46. else
  47. {
  48. // if we encounter keys that are in a different order, search remaining items by brute force..
  49. for (int j = i; j < num; ++j)
  50. {
  51. auto otherIndex = other.keys.indexOf (keys[j], other.ignoreCase);
  52. if (otherIndex < 0 || values[j] != other.values[otherIndex])
  53. return false;
  54. }
  55. return true;
  56. }
  57. }
  58. return true;
  59. }
  60. bool StringPairArray::operator!= (const StringPairArray& other) const
  61. {
  62. return ! operator== (other);
  63. }
  64. const String& StringPairArray::operator[] (StringRef key) const
  65. {
  66. return values[keys.indexOf (key, ignoreCase)];
  67. }
  68. String StringPairArray::getValue (StringRef key, const String& defaultReturnValue) const
  69. {
  70. auto i = keys.indexOf (key, ignoreCase);
  71. if (i >= 0)
  72. return values[i];
  73. return defaultReturnValue;
  74. }
  75. bool StringPairArray::containsKey (StringRef key) const noexcept
  76. {
  77. return keys.contains (key, ignoreCase);
  78. }
  79. void StringPairArray::set (const String& key, const String& value)
  80. {
  81. auto i = keys.indexOf (key, ignoreCase);
  82. if (i >= 0)
  83. {
  84. values.set (i, value);
  85. }
  86. else
  87. {
  88. keys.add (key);
  89. values.add (value);
  90. }
  91. }
  92. void StringPairArray::addArray (const StringPairArray& other)
  93. {
  94. for (int i = 0; i < other.size(); ++i)
  95. set (other.keys[i], other.values[i]);
  96. }
  97. void StringPairArray::clear()
  98. {
  99. keys.clear();
  100. values.clear();
  101. }
  102. void StringPairArray::remove (StringRef key)
  103. {
  104. remove (keys.indexOf (key, ignoreCase));
  105. }
  106. void StringPairArray::remove (int index)
  107. {
  108. keys.remove (index);
  109. values.remove (index);
  110. }
  111. void StringPairArray::setIgnoresCase (bool shouldIgnoreCase)
  112. {
  113. ignoreCase = shouldIgnoreCase;
  114. }
  115. bool StringPairArray::getIgnoresCase() const noexcept
  116. {
  117. return ignoreCase;
  118. }
  119. String StringPairArray::getDescription() const
  120. {
  121. String s;
  122. for (int i = 0; i < keys.size(); ++i)
  123. {
  124. s << keys[i] << " = " << values[i];
  125. if (i < keys.size())
  126. s << ", ";
  127. }
  128. return s;
  129. }
  130. void StringPairArray::minimiseStorageOverheads()
  131. {
  132. keys.minimiseStorageOverheads();
  133. values.minimiseStorageOverheads();
  134. }
  135. template <typename Map>
  136. void StringPairArray::addMapImpl (const Map& toAdd)
  137. {
  138. // If we just called `set` for each item in `toAdd`, that would
  139. // perform badly when adding to large StringPairArrays, as `set`
  140. // has to loop through the whole container looking for matching keys.
  141. // Instead, we use a temporary map to give us better lookup performance.
  142. std::map<String, int> contents;
  143. const auto normaliseKey = [this] (const String& key)
  144. {
  145. return ignoreCase ? key.toLowerCase() : key;
  146. };
  147. for (auto i = 0; i != size(); ++i)
  148. contents.emplace (normaliseKey (getAllKeys().getReference (i)), i);
  149. for (const auto& pair : toAdd)
  150. {
  151. const auto key = normaliseKey (pair.first);
  152. const auto it = contents.find (key);
  153. if (it != contents.cend())
  154. {
  155. values.getReference (it->second) = pair.second;
  156. }
  157. else
  158. {
  159. contents.emplace (key, static_cast<int> (contents.size()));
  160. keys.add (pair.first);
  161. values.add (pair.second);
  162. }
  163. }
  164. }
  165. void StringPairArray::addUnorderedMap (const std::unordered_map<String, String>& toAdd) { addMapImpl (toAdd); }
  166. void StringPairArray::addMap (const std::map<String, String>& toAdd) { addMapImpl (toAdd); }
  167. //==============================================================================
  168. //==============================================================================
  169. #if JUCE_UNIT_TESTS
  170. static String operator""_S (const char* chars, size_t)
  171. {
  172. return String { chars };
  173. }
  174. class StringPairArrayTests final : public UnitTest
  175. {
  176. public:
  177. StringPairArrayTests()
  178. : UnitTest ("StringPairArray", UnitTestCategories::text)
  179. {}
  180. void runTest() override
  181. {
  182. beginTest ("addMap respects case sensitivity of StringPairArray");
  183. {
  184. StringPairArray insensitive { true };
  185. insensitive.addMap ({ { "duplicate", "a" },
  186. { "Duplicate", "b" } });
  187. expect (insensitive.size() == 1);
  188. expectEquals (insensitive["DUPLICATE"], "a"_S);
  189. StringPairArray sensitive { false };
  190. sensitive.addMap ({ { "duplicate", "a"_S },
  191. { "Duplicate", "b"_S } });
  192. expect (sensitive.size() == 2);
  193. expectEquals (sensitive["duplicate"], "a"_S);
  194. expectEquals (sensitive["Duplicate"], "b"_S);
  195. expectEquals (sensitive["DUPLICATE"], ""_S);
  196. }
  197. beginTest ("addMap overwrites existing pairs");
  198. {
  199. StringPairArray insensitive { true };
  200. insensitive.set ("key", "value");
  201. insensitive.addMap ({ { "KEY", "VALUE" } });
  202. expect (insensitive.size() == 1);
  203. expectEquals (insensitive.getAllKeys()[0], "key"_S);
  204. expectEquals (insensitive.getAllValues()[0], "VALUE"_S);
  205. StringPairArray sensitive { false };
  206. sensitive.set ("key", "value");
  207. sensitive.addMap ({ { "KEY", "VALUE" },
  208. { "key", "another value" } });
  209. expect (sensitive.size() == 2);
  210. expect (sensitive.getAllKeys() == StringArray { "key", "KEY" });
  211. expect (sensitive.getAllValues() == StringArray { "another value", "VALUE" });
  212. }
  213. beginTest ("addMap doesn't change the order of existing keys");
  214. {
  215. StringPairArray array;
  216. array.set ("a", "a");
  217. array.set ("z", "z");
  218. array.set ("b", "b");
  219. array.set ("y", "y");
  220. array.set ("c", "c");
  221. array.addMap ({ { "B", "B" },
  222. { "0", "0" },
  223. { "Z", "Z" } });
  224. expect (array.getAllKeys() == StringArray { "a", "z", "b", "y", "c", "0" });
  225. expect (array.getAllValues() == StringArray { "a", "Z", "B", "y", "c", "0" });
  226. }
  227. beginTest ("addMap has equivalent behaviour to addArray");
  228. {
  229. StringPairArray initial;
  230. initial.set ("aaa", "aaa");
  231. initial.set ("zzz", "zzz");
  232. initial.set ("bbb", "bbb");
  233. auto withAddMap = initial;
  234. withAddMap.addMap ({ { "ZZZ", "ZZZ" },
  235. { "ddd", "ddd" } });
  236. auto withAddArray = initial;
  237. withAddArray.addArray ([]
  238. {
  239. StringPairArray toAdd;
  240. toAdd.set ("ZZZ", "ZZZ");
  241. toAdd.set ("ddd", "ddd");
  242. return toAdd;
  243. }());
  244. expect (withAddMap == withAddArray);
  245. }
  246. }
  247. };
  248. static StringPairArrayTests stringPairArrayTests;
  249. #endif
  250. } // namespace juce