Skip to content

Commit

Permalink
Private findKey() method becomes size_t clean, and returns bool on fa…
Browse files Browse the repository at this point in the history
…ilure.
  • Loading branch information
kozyilmaz authored and jgarzik committed May 2, 2017
1 parent 7099135 commit 640158f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions include/univalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class UniValue {
bool checkObject(const std::map<std::string,UniValue::VType>& 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"); }
Expand Down Expand Up @@ -148,7 +148,7 @@ class UniValue {
std::vector<std::string> keys;
std::vector<UniValue> 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;

Expand Down
20 changes: 11 additions & 9 deletions lib/univalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string,UniValue::VType>& t)
{
for (std::map<std::string,UniValue::VType>::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)
Expand All @@ -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);
Expand Down

0 comments on commit 640158f

Please sign in to comment.