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.

170 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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()
  29. {
  30. }
  31. StringPairArray& StringPairArray::operator= (const StringPairArray& other)
  32. {
  33. keys = other.keys;
  34. values = other.values;
  35. return *this;
  36. }
  37. bool StringPairArray::operator== (const StringPairArray& other) const
  38. {
  39. auto num = size();
  40. if (num != other.size())
  41. return false;
  42. for (int i = 0; i < num; ++i)
  43. {
  44. if (keys[i] == other.keys[i]) // optimise for the case where the keys are in the same order
  45. {
  46. if (values[i] != other.values[i])
  47. return false;
  48. }
  49. else
  50. {
  51. // if we encounter keys that are in a different order, search remaining items by brute force..
  52. for (int j = i; j < num; ++j)
  53. {
  54. auto otherIndex = other.keys.indexOf (keys[j], other.ignoreCase);
  55. if (otherIndex < 0 || values[j] != other.values[otherIndex])
  56. return false;
  57. }
  58. return true;
  59. }
  60. }
  61. return true;
  62. }
  63. bool StringPairArray::operator!= (const StringPairArray& other) const
  64. {
  65. return ! operator== (other);
  66. }
  67. const String& StringPairArray::operator[] (StringRef key) const
  68. {
  69. return values[keys.indexOf (key, ignoreCase)];
  70. }
  71. String StringPairArray::getValue (StringRef key, const String& defaultReturnValue) const
  72. {
  73. auto i = keys.indexOf (key, ignoreCase);
  74. if (i >= 0)
  75. return values[i];
  76. return defaultReturnValue;
  77. }
  78. bool StringPairArray::containsKey (StringRef key) const noexcept
  79. {
  80. return keys.contains (key, ignoreCase);
  81. }
  82. void StringPairArray::set (const String& key, const String& value)
  83. {
  84. auto i = keys.indexOf (key, ignoreCase);
  85. if (i >= 0)
  86. {
  87. values.set (i, value);
  88. }
  89. else
  90. {
  91. keys.add (key);
  92. values.add (value);
  93. }
  94. }
  95. void StringPairArray::addArray (const StringPairArray& other)
  96. {
  97. for (int i = 0; i < other.size(); ++i)
  98. set (other.keys[i], other.values[i]);
  99. }
  100. void StringPairArray::clear()
  101. {
  102. keys.clear();
  103. values.clear();
  104. }
  105. void StringPairArray::remove (StringRef key)
  106. {
  107. remove (keys.indexOf (key, ignoreCase));
  108. }
  109. void StringPairArray::remove (int index)
  110. {
  111. keys.remove (index);
  112. values.remove (index);
  113. }
  114. void StringPairArray::setIgnoresCase (bool shouldIgnoreCase)
  115. {
  116. ignoreCase = shouldIgnoreCase;
  117. }
  118. String StringPairArray::getDescription() const
  119. {
  120. String s;
  121. for (int i = 0; i < keys.size(); ++i)
  122. {
  123. s << keys[i] << " = " << values[i];
  124. if (i < keys.size())
  125. s << ", ";
  126. }
  127. return s;
  128. }
  129. void StringPairArray::minimiseStorageOverheads()
  130. {
  131. keys.minimiseStorageOverheads();
  132. values.minimiseStorageOverheads();
  133. }
  134. } // namespace juce