OpenShot Library | libopenshot-audio  0.2.0
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);
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
juce::StringPairArray::addArray
void addArray(const StringPairArray &other)
Adds the items from another array to this one.
Definition: juce_StringPairArray.cpp:120
juce::StringArray::indexOf
int indexOf(StringRef stringToLookFor, bool ignoreCase=false, int startIndex=0) const
Searches for a string in the array.
Definition: juce_StringArray.cpp:193
juce::StringRef
A simple class for holding temporary references to a string literal or String.
Definition: juce_StringRef.h:65
juce::StringArray::set
void set(int index, String newString)
Replaces one of the strings in the array with another one.
Definition: juce_StringArray.cpp:183
juce::StringPairArray::setIgnoresCase
void setIgnoresCase(bool shouldIgnoreCase)
Indicates whether to use a case-insensitive search when looking up a key string.
Definition: juce_StringPairArray.cpp:143
juce::StringPairArray::operator=
StringPairArray & operator=(const StringPairArray &other)
Copies the contents of another string array into this one.
Definition: juce_StringPairArray.cpp:41
juce::StringPairArray::operator!=
bool operator!=(const StringPairArray &other) const
Compares two arrays.
Definition: juce_StringPairArray.cpp:80
juce::StringPairArray::clear
void clear()
Removes all elements from the array.
Definition: juce_StringPairArray.cpp:126
juce::StringArray::size
int size() const noexcept
Returns the number of strings in the array.
Definition: juce_StringArray.h:139
juce::StringPairArray
A container for holding a set of strings which are keyed by another string.
Definition: juce_StringPairArray.h:38
juce::StringPairArray::containsKey
bool containsKey(StringRef key) const noexcept
Returns true if the given key exists.
Definition: juce_StringPairArray.cpp:100
juce::StringArray::remove
void remove(int index)
Removes a string from the array.
Definition: juce_StringArray.cpp:222
juce::StringPairArray::set
void set(const String &key, const String &value)
Adds or amends a key/value pair.
Definition: juce_StringPairArray.cpp:105
juce::StringPairArray::remove
void remove(StringRef key)
Removes a string from the array based on its key.
Definition: juce_StringPairArray.cpp:132
juce::StringPairArray::getDescription
String getDescription() const
Returns a descriptive string containing the items.
Definition: juce_StringPairArray.cpp:148
juce::StringPairArray::minimiseStorageOverheads
void minimiseStorageOverheads()
Reduces the amount of storage being used by the array.
Definition: juce_StringPairArray.cpp:163
juce::String
The JUCE String class!
Definition: juce_String.h:42
juce::StringArray::add
void add(String stringToAdd)
Appends a string at the end of the array.
Definition: juce_StringArray.cpp:135
juce::StringArray::clear
void clear()
Removes all elements from the array.
Definition: juce_StringArray.cpp:111
juce::StringPairArray::StringPairArray
StringPairArray(bool ignoreCaseWhenComparingKeys=true)
Creates an empty array.
Definition: juce_StringPairArray.cpp:26
juce::StringPairArray::~StringPairArray
~StringPairArray()
Destructor.
Definition: juce_StringPairArray.cpp:37
juce::StringPairArray::size
int size() const noexcept
Returns the number of strings in the array.
Definition: juce_StringPairArray.h:97
juce::StringPairArray::operator[]
const String & operator[](StringRef key) const
Finds the value corresponding to a key string.
Definition: juce_StringPairArray.cpp:85
juce::StringPairArray::getValue
String getValue(StringRef, const String &defaultReturnValue) const
Finds the value corresponding to a key string.
Definition: juce_StringPairArray.cpp:90
juce::StringArray::minimiseStorageOverheads
void minimiseStorageOverheads()
Reduces the amount of storage being used by the array.
Definition: juce_StringArray.cpp:468
juce::StringPairArray::operator==
bool operator==(const StringPairArray &other) const
Compares two arrays.
Definition: juce_StringPairArray.cpp:48