juce_StringPairArray.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. StringPairArray::StringPairArray (const bool ignoreCase_)
  22. : ignoreCase (ignoreCase_)
  23. {
  24. }
  25. StringPairArray::StringPairArray (const StringPairArray& other)
  26. : keys (other.keys),
  27. values (other.values),
  28. ignoreCase (other.ignoreCase)
  29. {
  30. }
  31. StringPairArray::~StringPairArray()
  32. {
  33. }
  34. StringPairArray& StringPairArray::operator= (const StringPairArray& other)
  35. {
  36. keys = other.keys;
  37. values = other.values;
  38. return *this;
  39. }
  40. bool StringPairArray::operator== (const StringPairArray& other) const
  41. {
  42. for (int i = keys.size(); --i >= 0;)
  43. if (other [keys[i]] != values[i])
  44. return false;
  45. return true;
  46. }
  47. bool StringPairArray::operator!= (const StringPairArray& other) const
  48. {
  49. return ! operator== (other);
  50. }
  51. const String& StringPairArray::operator[] (StringRef key) const
  52. {
  53. return values [keys.indexOf (key, ignoreCase)];
  54. }
  55. String StringPairArray::getValue (StringRef key, const String& defaultReturnValue) const
  56. {
  57. const int i = keys.indexOf (key, ignoreCase);
  58. if (i >= 0)
  59. return values[i];
  60. return defaultReturnValue;
  61. }
  62. bool StringPairArray::containsKey (StringRef key) const noexcept
  63. {
  64. return keys.contains (key);
  65. }
  66. void StringPairArray::set (const String& key, const String& value)
  67. {
  68. const int i = keys.indexOf (key, ignoreCase);
  69. if (i >= 0)
  70. {
  71. values.set (i, value);
  72. }
  73. else
  74. {
  75. keys.add (key);
  76. values.add (value);
  77. }
  78. }
  79. void StringPairArray::addArray (const StringPairArray& other)
  80. {
  81. for (int i = 0; i < other.size(); ++i)
  82. set (other.keys[i], other.values[i]);
  83. }
  84. void StringPairArray::clear()
  85. {
  86. keys.clear();
  87. values.clear();
  88. }
  89. void StringPairArray::remove (StringRef key)
  90. {
  91. remove (keys.indexOf (key, ignoreCase));
  92. }
  93. void StringPairArray::remove (const int index)
  94. {
  95. keys.remove (index);
  96. values.remove (index);
  97. }
  98. void StringPairArray::setIgnoresCase (const bool shouldIgnoreCase)
  99. {
  100. ignoreCase = shouldIgnoreCase;
  101. }
  102. String StringPairArray::getDescription() const
  103. {
  104. String s;
  105. for (int i = 0; i < keys.size(); ++i)
  106. {
  107. s << keys[i] << " = " << values[i];
  108. if (i < keys.size())
  109. s << ", ";
  110. }
  111. return s;
  112. }
  113. void StringPairArray::minimiseStorageOverheads()
  114. {
  115. keys.minimiseStorageOverheads();
  116. values.minimiseStorageOverheads();
  117. }