OpenShot Audio Library | OpenShotAudio  0.3.2
juce_StringPairArray.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 StringPairArray::StringPairArray (bool shouldIgnoreCase) : ignoreCase (shouldIgnoreCase)
27 {
28 }
29 
31  : keys (other.keys),
32  values (other.values),
33  ignoreCase (other.ignoreCase)
34 {
35 }
36 
38 {
39 }
40 
42 {
43  keys = other.keys;
44  values = other.values;
45  return *this;
46 }
47 
49 {
50  auto num = size();
51 
52  if (num != other.size())
53  return false;
54 
55  for (int i = 0; i < num; ++i)
56  {
57  if (keys[i] == other.keys[i]) // optimise for the case where the keys are in the same order
58  {
59  if (values[i] != other.values[i])
60  return false;
61  }
62  else
63  {
64  // if we encounter keys that are in a different order, search remaining items by brute force..
65  for (int j = i; j < num; ++j)
66  {
67  auto otherIndex = other.keys.indexOf (keys[j], other.ignoreCase);
68 
69  if (otherIndex < 0 || values[j] != other.values[otherIndex])
70  return false;
71  }
72 
73  return true;
74  }
75  }
76 
77  return true;
78 }
79 
81 {
82  return ! operator== (other);
83 }
84 
86 {
87  return values[keys.indexOf (key, ignoreCase)];
88 }
89 
90 String StringPairArray::getValue (StringRef key, const String& defaultReturnValue) const
91 {
92  auto i = keys.indexOf (key, ignoreCase);
93 
94  if (i >= 0)
95  return values[i];
96 
97  return defaultReturnValue;
98 }
99 
100 bool StringPairArray::containsKey (StringRef key) const noexcept
101 {
102  return keys.contains (key, ignoreCase);
103 }
104 
105 void StringPairArray::set (const String& key, const String& value)
106 {
107  auto i = keys.indexOf (key, ignoreCase);
108 
109  if (i >= 0)
110  {
111  values.set (i, value);
112  }
113  else
114  {
115  keys.add (key);
116  values.add (value);
117  }
118 }
119 
121 {
122  for (int i = 0; i < other.size(); ++i)
123  set (other.keys[i], other.values[i]);
124 }
125 
127 {
128  keys.clear();
129  values.clear();
130 }
131 
133 {
134  remove (keys.indexOf (key, ignoreCase));
135 }
136 
137 void StringPairArray::remove (int index)
138 {
139  keys.remove (index);
140  values.remove (index);
141 }
142 
143 void StringPairArray::setIgnoresCase (bool shouldIgnoreCase)
144 {
145  ignoreCase = shouldIgnoreCase;
146 }
147 
149 {
150  String s;
151 
152  for (int i = 0; i < keys.size(); ++i)
153  {
154  s << keys[i] << " = " << values[i];
155 
156  if (i < keys.size())
157  s << ", ";
158  }
159 
160  return s;
161 }
162 
164 {
166  values.minimiseStorageOverheads();
167 }
168 
169 } // namespace juce
int indexOf(StringRef stringToLookFor, bool ignoreCase=false, int startIndex=0) const
int size() const noexcept
void add(String stringToAdd)
void set(int index, String newString)
void remove(int index)
void setIgnoresCase(bool shouldIgnoreCase)
String getValue(StringRef, const String &defaultReturnValue) const
StringPairArray & operator=(const StringPairArray &other)
bool containsKey(StringRef key) const noexcept
void set(const String &key, const String &value)
void remove(StringRef key)
StringPairArray(bool ignoreCaseWhenComparingKeys=true)
const String & operator[](StringRef key) const
bool operator!=(const StringPairArray &other) const
void addArray(const StringPairArray &other)
int size() const noexcept
bool operator==(const StringPairArray &other) const