0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-17 15:30:52 +01:00

ircd::json: Move exception construction branch to unlikely seg; minor cleanup.

This commit is contained in:
Jason Volk 2020-02-12 11:23:35 -08:00
parent 9dfcd63797
commit 7669fd53d9

View file

@ -188,11 +188,15 @@ T
ircd::json::at(const object &object) ircd::json::at(const object &object)
try try
{ {
const auto it(object.find(key)); const auto it
if(it == end(object)) {
object.find(key)
};
if(unlikely(it == end(object)))
throw not_found throw not_found
{ {
"[key hash] '%zu'", key "[key hash] '%lu'", ulong(key)
}; };
return lex_cast<T>(it->second); return lex_cast<T>(it->second);
@ -201,8 +205,8 @@ catch(const bad_lex_cast &e)
{ {
throw type_error throw type_error
{ {
"[key hash] '%zu' must cast to type %s", "[key hash] '%lu' must cast to type %s",
key, ulong(key),
typeid(T).name() typeid(T).name()
}; };
} }
@ -212,8 +216,12 @@ T
ircd::json::object::at(const string_view &key) ircd::json::object::at(const string_view &key)
const try const try
{ {
const auto it(find(key)); const auto it
if(it == end()) {
find(key)
};
if(unlikely(it == end()))
throw not_found throw not_found
{ {
"'%s'", key "'%s'", key
@ -238,12 +246,22 @@ ircd::json::get(const object &object,
const T &def) const T &def)
try try
{ {
const auto it{object.find(key)}; const auto it
{
object.find(key)
};
if(it == end(object)) if(it == end(object))
return def; return def;
const string_view sv{it->second}; const string_view sv
return !sv.empty()? lex_cast<T>(sv) : def; {
it->second
};
return !sv.empty()?
lex_cast<T>(sv):
def;
} }
catch(const bad_lex_cast &e) catch(const bad_lex_cast &e)
{ {
@ -256,8 +274,14 @@ ircd::json::object::get(const string_view &key,
const T &def) const T &def)
const try const try
{ {
const string_view sv(operator[](key)); const string_view sv
return !sv.empty()? lex_cast<T>(sv) : def; {
operator[](key)
};
return !sv.empty()?
lex_cast<T>(sv):
def;
} }
catch(const bad_lex_cast &e) catch(const bad_lex_cast &e)
{ {