Skip to content

Commit 640158f

Browse files
kozyilmazjgarzik
authored andcommitted
Private findKey() method becomes size_t clean, and returns bool on failure.
1 parent 7099135 commit 640158f

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

include/univalue.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class UniValue {
7272
bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes);
7373
const UniValue& operator[](const std::string& key) const;
7474
const UniValue& operator[](unsigned int index) const;
75-
bool exists(const std::string& key) const { return (findKey(key) >= 0); }
75+
bool exists(const std::string& key) const { size_t i; return findKey(key, i); }
7676

7777
bool isNull() const { return (typ == VNULL); }
7878
bool isTrue() const { return (typ == VBOOL) && (val == "1"); }
@@ -148,7 +148,7 @@ class UniValue {
148148
std::vector<std::string> keys;
149149
std::vector<UniValue> values;
150150

151-
int findKey(const std::string& key) const;
151+
bool findKey(const std::string& key, size_t& retIdx) const;
152152
void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
153153
void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
154154

lib/univalue.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,22 +212,24 @@ bool UniValue::pushKVs(const UniValue& obj)
212212
return true;
213213
}
214214

215-
int UniValue::findKey(const std::string& key) const
215+
bool UniValue::findKey(const std::string& key, size_t& retIdx) const
216216
{
217-
for (unsigned int i = 0; i < keys.size(); i++) {
218-
if (keys[i] == key)
219-
return (int) i;
217+
for (size_t i = 0; i < keys.size(); i++) {
218+
if (keys[i] == key) {
219+
retIdx = i;
220+
return true;
221+
}
220222
}
221223

222-
return -1;
224+
return false;
223225
}
224226

225227
bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t)
226228
{
227229
for (std::map<std::string,UniValue::VType>::const_iterator it = t.begin();
228230
it != t.end(); ++it) {
229-
int idx = findKey(it->first);
230-
if (idx < 0)
231+
size_t idx = 0;
232+
if (!findKey(it->first, idx))
231233
return false;
232234

233235
if (values.at(idx).getType() != it->second)
@@ -242,8 +244,8 @@ const UniValue& UniValue::operator[](const std::string& key) const
242244
if (typ != VOBJ)
243245
return NullUniValue;
244246

245-
int index = findKey(key);
246-
if (index < 0)
247+
size_t index = 0;
248+
if (!findKey(key, index))
247249
return NullUniValue;
248250

249251
return values.at(index);

0 commit comments

Comments
 (0)