0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-07-21 09:58:46 +02:00

ircd::json: Various fixes/cleanup; debug related.

This commit is contained in:
Jason Volk 2017-10-11 18:15:09 -07:00
parent bc5be1c6be
commit 1cede9a21f
2 changed files with 24 additions and 12 deletions

View file

@ -32,10 +32,10 @@ namespace ircd::json
IRCD_EXCEPTION(error, type_error); IRCD_EXCEPTION(error, type_error);
IRCD_EXCEPTION(error, not_found); IRCD_EXCEPTION(error, not_found);
struct array;
struct object;
struct value; struct value;
struct member; struct member;
struct object;
struct array;
struct iov; struct iov;
enum type enum type
@ -169,12 +169,16 @@ ircd::json::string(T&&... t)
std::string ret(size, char{}); std::string ret(size, char{});
const auto buf{const_cast<char *>(ret.data())}; const auto buf{const_cast<char *>(ret.data())};
const auto max{ret.size() + 1}; const auto max{ret.size() + 1};
const auto printed const auto printed
{ {
print(buf, max, std::forward<T>(t)...) print(buf, max, std::forward<T>(t)...)
}; };
#ifdef RB_DEBUG
if(unlikely(printed != ret.size()))
std::cerr << printed << " != " << ret.size() << std::endl << ret << std::endl;
#endif
assert(printed == ret.size()); assert(printed == ret.size());
return ret; return ret;
} }

View file

@ -914,7 +914,7 @@ template<class T>
typename std::enable_if<serialized_lex_cast<T>(), bool>::type typename std::enable_if<serialized_lex_cast<T>(), bool>::type
defined(T&& t) defined(T&& t)
{ {
return t != T{}; return t != T{0};
} }
template<class... T> template<class... T>
@ -926,24 +926,26 @@ serialized(const tuple<T...> &t)
tuple<T...>::size() tuple<T...>::size()
}; };
std::array<size_t, member_count> sizes; std::array<size_t, member_count> sizes {0};
const auto e{_member_transform_if(t, begin(sizes), end(sizes), [] const auto e{_member_transform_if(t, begin(sizes), end(sizes), []
(auto&& ret, const string_view &key, auto&& val) (auto &ret, const string_view &key, auto&& val)
{ {
if(!defined(val)) if(!defined(val))
return false; return false;
// " " : , // " " : ,
ret = 1 + key.size() + 1 + 1 + serialized(val) + 1; ret = 1 + key.size() + 1 + 1 + serialized(val) + 1;
return true; return true;
})}; })};
// Subtract one to get the final size when an extra comma is // Subtract one to get the final size when an extra comma is
// accumulated on non-empty objects. // accumulated on non-empty objects.
const size_t overhead{2}; const auto overhead
auto ret{std::accumulate(begin(sizes), e, overhead)}; {
ret -= e != begin(sizes); 1 + std::all_of(begin(sizes), e, is_zero{})
return ret; };
return std::accumulate(begin(sizes), e, size_t(overhead));
} }
template<class... T> template<class... T>
@ -952,8 +954,14 @@ stringify(mutable_buffer &buf,
const tuple<T...> &tuple) const tuple<T...> &tuple)
{ {
std::array<member, tuple.size()> members; std::array<member, tuple.size()> members;
std::sort(begin(members), end(members), []
(const auto &a, const auto &b)
{
return a.first < b.first;
});
const auto e{_member_transform_if(tuple, begin(members), end(members), [] const auto e{_member_transform_if(tuple, begin(members), end(members), []
(auto&& ret, const string_view &key, auto&& val) (auto &ret, const string_view &key, auto&& val)
{ {
if(!defined(val)) if(!defined(val))
return false; return false;