diff --git a/include/univalue.h b/include/univalue.h index 8f671888b52974..4efc64aba211f5 100644 --- a/include/univalue.h +++ b/include/univalue.h @@ -72,7 +72,7 @@ class UniValue { bool checkObject(const std::map& memberTypes); const UniValue& operator[](const std::string& key) const; const UniValue& operator[](unsigned int index) const; - bool exists(const std::string& key) const { return (findKey(key) >= 0); } + bool exists(const std::string& key) const { size_t i; return findKey(key, i); } bool isNull() const { return (typ == VNULL); } bool isTrue() const { return (typ == VBOOL) && (val == "1"); } @@ -148,7 +148,7 @@ class UniValue { std::vector keys; std::vector values; - int findKey(const std::string& key) const; + bool findKey(const std::string& key, size_t& retIdx) const; void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const; void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const; diff --git a/lib/univalue.cpp b/lib/univalue.cpp index 6058622a549c87..a973d2680cc630 100644 --- a/lib/univalue.cpp +++ b/lib/univalue.cpp @@ -212,22 +212,24 @@ bool UniValue::pushKVs(const UniValue& obj) return true; } -int UniValue::findKey(const std::string& key) const +bool UniValue::findKey(const std::string& key, size_t& retIdx) const { - for (unsigned int i = 0; i < keys.size(); i++) { - if (keys[i] == key) - return (int) i; + for (size_t i = 0; i < keys.size(); i++) { + if (keys[i] == key) { + retIdx = i; + return true; + } } - return -1; + return false; } bool UniValue::checkObject(const std::map& t) { for (std::map::const_iterator it = t.begin(); it != t.end(); ++it) { - int idx = findKey(it->first); - if (idx < 0) + size_t idx = 0; + if (!findKey(it->first, idx)) return false; if (values.at(idx).getType() != it->second) @@ -242,8 +244,8 @@ const UniValue& UniValue::operator[](const std::string& key) const if (typ != VOBJ) return NullUniValue; - int index = findKey(key); - if (index < 0) + size_t index = 0; + if (!findKey(key, index)) return NullUniValue; return values.at(index);