32 values (
other.values),
33 ignoreCase (
other.ignoreCase)
40 values =
other.values;
48 if (num !=
other.size())
51 for (
int i = 0; i < num; ++i)
53 if (keys[i] ==
other.keys[i])
55 if (values[i] !=
other.values[i])
61 for (
int j = i;
j < num; ++
j)
83 return values[keys.
indexOf (key, ignoreCase)];
88 auto i = keys.
indexOf (key, ignoreCase);
98 return keys.contains (key, ignoreCase);
103 auto i = keys.
indexOf (key, ignoreCase);
107 values.
set (i, value);
118 for (
int i = 0; i <
other.size(); ++i)
153 for (
int i = 0; i < keys.
size(); ++i)
155 s << keys[i] <<
" = " << values[i];
170template <
typename Map>
171void StringPairArray::addMapImpl (
const Map&
toAdd)
181 return ignoreCase ? key.toLowerCase() : key;
184 for (
auto i = 0; i !=
size(); ++i)
187 for (
const auto& pair :
toAdd)
190 const auto it = contents.
find (key);
192 if (
it != contents.
cend())
198 contents.
emplace (key,
static_cast<int> (contents.
size()));
199 keys.
add (pair.first);
200 values.
add (pair.second);
224 void runTest()
override
226 beginTest (
"addMap respects case sensitivity of StringPairArray");
230 {
"Duplicate",
"b" } });
237 {
"Duplicate",
"b"_S } });
245 beginTest (
"addMap overwrites existing pairs");
258 {
"key",
"another value" } });
261 expect (
sensitive.getAllKeys() == StringArray {
"key",
"KEY" });
262 expect (
sensitive.getAllValues() == StringArray {
"another value",
"VALUE" });
265 beginTest (
"addMap doesn't change the order of existing keys");
267 StringPairArray array;
268 array.set (
"a",
"a");
269 array.set (
"z",
"z");
270 array.set (
"b",
"b");
271 array.set (
"y",
"y");
272 array.set (
"c",
"c");
274 array.addMap ({ {
"B",
"B" },
278 expect (array.getAllKeys() == StringArray {
"a",
"z",
"b",
"y",
"c",
"0" });
279 expect (array.getAllValues() == StringArray {
"a",
"Z",
"B",
"y",
"c",
"0" });
282 beginTest (
"addMap has equivalent behaviour to addArray");
296 StringPairArray
toAdd;
297 toAdd.set (
"ZZZ",
"ZZZ");
298 toAdd.set (
"ddd",
"ddd");
String & getReference(int index) noexcept
Returns a reference to one of the strings in the array.
int indexOf(StringRef stringToLookFor, bool ignoreCase=false, int startIndex=0) const
Searches for a string in the array.
void minimiseStorageOverheads()
Reduces the amount of storage being used by the array.
void clear()
Removes all elements from the array.
int size() const noexcept
Returns the number of strings in the array.
void add(String stringToAdd)
Appends a string at the end of the array.
void set(int index, String newString)
Replaces one of the strings in the array with another one.
void remove(int index)
Removes a string from the array.
A container for holding a set of strings which are keyed by another string.
void setIgnoresCase(bool shouldIgnoreCase)
Indicates whether to use a case-insensitive search when looking up a key string.
String getValue(StringRef, const String &defaultReturnValue) 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 containsKey(StringRef key) const noexcept
Returns true if the given key exists.
void set(const String &key, const String &value)
Adds or amends a key/value pair.
void clear()
Removes all elements from the array.
String getDescription() const
Returns a descriptive string containing the items.
void remove(StringRef key)
Removes a string from the array based on its key.
StringPairArray(bool ignoreCaseWhenComparingKeys=true)
Creates an empty array.
bool getIgnoresCase() const noexcept
Indicates whether a case-insensitive search is used when looking up a key string.
const String & operator[](StringRef key) const
Finds the value corresponding to a key string.
bool operator!=(const StringPairArray &other) const
Compares two arrays.
void minimiseStorageOverheads()
Reduces the amount of storage being used by the array.
void addMap(const std::map< String, String > &mapToAdd)
Adds the contents of a map to this StringPairArray.
void addArray(const StringPairArray &other)
Adds the items from another array to this one.
void addUnorderedMap(const std::unordered_map< String, String > &mapToAdd)
Adds the contents of an unordered map to this StringPairArray.
int size() const noexcept
Returns the number of strings in the array.
bool operator==(const StringPairArray &other) const
Compares two arrays.
const StringArray & getAllKeys() const noexcept
Returns a list of all keys in the array.
A simple class for holding temporary references to a string literal or String.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...