Extracted method getMatchingPair()

This commit is contained in:
Benoit Blanchon
2014-08-02 16:25:18 +02:00
parent d2fe9ddf49
commit 1bc45f1fd7
2 changed files with 18 additions and 22 deletions

View File

@ -4,6 +4,7 @@
*/ */
#include "JsonObjectBase.h" #include "JsonObjectBase.h"
#include <string.h> // for strcmp
using namespace ArduinoJson::Generator; using namespace ArduinoJson::Generator;
using namespace ArduinoJson::Internals; using namespace ArduinoJson::Internals;
@ -38,25 +39,34 @@ size_t JsonObjectBase::printTo(Print& p) const
return n; return n;
} }
JsonValue& JsonObjectBase::operator[](char const* key) JsonObjectBase::KeyValuePair* JsonObjectBase::getMatchingPair(char const* key) const
{ {
KeyValuePair* p = items; KeyValuePair* p = items;
for (int i = count; i > 0; --i) for (int i = count; i > 0; --i)
{ {
if (p->matches(key)) if (!strcmp(p->key, key))
return p->value; return p;
p++; p++;
} }
return 0;
}
JsonValue& JsonObjectBase::operator[](char const* key)
{
KeyValuePair* match = getMatchingPair(key);
JsonValue* value; JsonValue* value;
if (match)
return match->value;
if (count < capacity) if (count < capacity)
{ {
items[count].key = key;
value = &items[count].value;
count++; count++;
p->key = key;
value = &p->value;
} }
else else
{ {
@ -69,15 +79,5 @@ JsonValue& JsonObjectBase::operator[](char const* key)
bool JsonObjectBase::containsKey(char const* key) const bool JsonObjectBase::containsKey(char const* key) const
{ {
KeyValuePair* p = items; return getMatchingPair(key) != 0;
for (int i = count; i > 0; --i)
{
if (p->matches(key))
return true;
p++;
}
return false;
} }

View File

@ -7,7 +7,6 @@
#include "JsonPrintable.h" #include "JsonPrintable.h"
#include "EscapedString.h" #include "EscapedString.h"
#include <string.h> // for strcmp
namespace ArduinoJson namespace ArduinoJson
{ {
@ -43,11 +42,6 @@ namespace ArduinoJson
{ {
const char* key; const char* key;
JsonValue value; JsonValue value;
bool matches(const char* candidateKey) const
{
return strcmp(key, candidateKey) == 0;
}
}; };
JsonObjectBase(KeyValuePair* items, int capacity) JsonObjectBase(KeyValuePair* items, int capacity)
@ -60,6 +54,8 @@ namespace ArduinoJson
int capacity, count; int capacity, count;
static JsonValue nullValue; static JsonValue nullValue;
KeyValuePair* getMatchingPair(const char* key) const;
}; };
} }
} }