OpenShot Library | OpenShotAudio  0.2.1
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
A simple class for holding temporary references to a string literal or String.
void remove(int index)
Removes a string from the array.
String getValue(StringRef, const String &defaultReturnValue) const
Finds the value corresponding to a key string.
The JUCE String class!
Definition: juce_String.h:42
void clear()
Removes all elements from the array.
StringPairArray(bool ignoreCaseWhenComparingKeys=true)
Creates an empty array.
int size() const noexcept
Returns the number of strings in the array.
int indexOf(StringRef stringToLookFor, bool ignoreCase=false, int startIndex=0) const
Searches for a string in the array.
bool operator==(const StringPairArray &other) const
Compares two arrays.
void minimiseStorageOverheads()
Reduces the amount of storage being used by the array.
void set(int index, String newString)
Replaces one of the strings in the array with another one.
void clear()
Removes all elements from the array.
const String & operator[](StringRef key) const
Finds the value corresponding to a key string.
StringPairArray & operator=(const StringPairArray &other)
Copies the contents of another string array into this one.
bool operator!=(const StringPairArray &other) const
Compares two arrays.
void set(const String &key, const String &value)
Adds or amends a key/value pair.
bool contains(StringRef stringToLookFor, bool ignoreCase=false) const
Searches for a string in the array.
void minimiseStorageOverheads()
Reduces the amount of storage being used by the array.
String getDescription() const
Returns a descriptive string containing the items.
bool containsKey(StringRef key) const noexcept
Returns true if the given key exists.
int size() const noexcept
Returns the number of strings in the array.
void remove(StringRef key)
Removes a string from the array based on its key.
A container for holding a set of strings which are keyed by another string.
void addArray(const StringPairArray &other)
Adds the items from another array to this one.
void setIgnoresCase(bool shouldIgnoreCase)
Indicates whether to use a case-insensitive search when looking up a key string.
void add(String stringToAdd)
Appends a string at the end of the array.