mirror of
https://github.com/matrix-construct/construct
synced 2024-11-25 16:22:35 +01:00
ircd::json: Improve and inline value comparison operators.
This commit is contained in:
parent
0344a12b40
commit
4d8d2a4dd7
2 changed files with 69 additions and 40 deletions
|
@ -342,3 +342,27 @@ ircd::json::value::value(const char (&str)[N],
|
|||
const enum type &type)
|
||||
:value{string_view{str, strnlen(str, N)}, type}
|
||||
{}
|
||||
|
||||
inline bool
|
||||
ircd::json::operator>(const value &a, const value &b)
|
||||
{
|
||||
return !operator<=(a, b);
|
||||
}
|
||||
|
||||
inline bool
|
||||
ircd::json::operator>=(const value &a, const value &b)
|
||||
{
|
||||
return operator>(a, b) || operator==(a, b);
|
||||
}
|
||||
|
||||
inline bool
|
||||
ircd::json::operator<=(const value &a, const value &b)
|
||||
{
|
||||
return operator<(a, b) || operator==(a, b);
|
||||
}
|
||||
|
||||
inline bool
|
||||
ircd::json::operator!=(const value &a, const value &b)
|
||||
{
|
||||
return !operator==(a, b);
|
||||
}
|
||||
|
|
85
ircd/json.cc
85
ircd/json.cc
|
@ -3795,58 +3795,63 @@ ircd::json::value::create_string(const size_t &len,
|
|||
this->string = string.release();
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::json::operator>(const value &a, const value &b)
|
||||
{
|
||||
if(unlikely(type(a) != STRING || type(b) != STRING))
|
||||
throw type_error("cannot compare values");
|
||||
|
||||
return static_cast<string_view>(a) > static_cast<string_view>(b);
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::json::operator<(const value &a, const value &b)
|
||||
{
|
||||
if(unlikely(type(a) != STRING || type(b) != STRING))
|
||||
throw type_error("cannot compare values");
|
||||
if(type(a) == type(b)) switch(type(b))
|
||||
{
|
||||
case NUMBER:
|
||||
assert(!a.serial && !b.serial);
|
||||
assert(a.floats == b.floats);
|
||||
return b.floats?
|
||||
a.floating < b.floating:
|
||||
a.integer < b.integer;
|
||||
|
||||
return static_cast<string_view>(a) < static_cast<string_view>(b);
|
||||
}
|
||||
case STRING:
|
||||
return static_cast<string_view>(a) < static_cast<string_view>(b);
|
||||
|
||||
bool
|
||||
ircd::json::operator>=(const value &a, const value &b)
|
||||
{
|
||||
if(unlikely(type(a) != STRING || type(b) != STRING))
|
||||
throw type_error("cannot compare values");
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return static_cast<string_view>(a) >= static_cast<string_view>(b);
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::json::operator<=(const value &a, const value &b)
|
||||
{
|
||||
if(unlikely(type(a) != STRING || type(b) != STRING))
|
||||
throw type_error("cannot compare values");
|
||||
|
||||
return static_cast<string_view>(a) <= static_cast<string_view>(b);
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::json::operator!=(const value &a, const value &b)
|
||||
{
|
||||
if(unlikely(type(a) != STRING || type(b) != STRING))
|
||||
throw type_error("cannot compare values");
|
||||
|
||||
return static_cast<string_view>(a) != static_cast<string_view>(b);
|
||||
throw type_error
|
||||
{
|
||||
"Cannot compare type[%u] %s to type[%u] %s",
|
||||
uint(type(a)),
|
||||
reflect(type(a)),
|
||||
uint(type(b)),
|
||||
reflect(type(b)),
|
||||
};
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::json::operator==(const value &a, const value &b)
|
||||
{
|
||||
if(unlikely(type(a) != STRING || type(b) != STRING))
|
||||
throw type_error("cannot compare values");
|
||||
if(type(a) == type(b)) switch(type(b))
|
||||
{
|
||||
case NUMBER:
|
||||
assert(!a.serial && !b.serial);
|
||||
assert(!a.floats && !b.floats);
|
||||
if(unlikely(a.floats || b.floats))
|
||||
break;
|
||||
|
||||
return static_cast<string_view>(a) == static_cast<string_view>(b);
|
||||
return a.integer == b.integer;
|
||||
|
||||
case STRING:
|
||||
return static_cast<string_view>(a) == static_cast<string_view>(b);
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
throw type_error
|
||||
{
|
||||
"Cannot compare type[%u] %s to type[%u] %s",
|
||||
uint(type(a)),
|
||||
reflect(type(a)),
|
||||
uint(type(b)),
|
||||
reflect(type(b)),
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Reference in a new issue