Skip to content

Commit

Permalink
removed whitespace_handled
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenberry committed Nov 11, 2022
1 parent 559027a commit 9a901a6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 57 deletions.
18 changes: 0 additions & 18 deletions include/glaze/core/opts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace glz
bool allow_hash_check = false; // Will replace some string equality checks with hash checks

// meant for internal use
bool whitespace_handled = false;
bool opening_handled = false; // the opening character has been handled
};

Expand All @@ -29,21 +28,4 @@ namespace glz
ret.opening_handled = true;
return ret;
};

template <opts Opts>
constexpr auto ws_handled()
{
opts ret = Opts;
ret.whitespace_handled = true;
return ret;
};

template <opts Opts>
constexpr auto ws_and_opening_handled()
{
opts ret = Opts;
ret.whitespace_handled = true;
ret.opening_handled = true;
return ret;
};
}
55 changes: 16 additions & 39 deletions include/glaze/json/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ namespace glz
template <auto Opts>
static void op(bool_t auto&& value, auto&& it, auto&& end)
{
if constexpr (!Opts.whitespace_handled) {
skip_ws(it, end);
}
skip_ws(it, end);

if (it < end) [[likely]] {
switch (*it) {
Expand Down Expand Up @@ -101,9 +99,7 @@ namespace glz
template <auto Opts, class It>
static void op(auto&& value, It&& it, auto&& end)
{
if constexpr (!Opts.whitespace_handled) {
skip_ws(it, end);
}
skip_ws(it, end);

if (it == end) [[unlikely]] {
throw std::runtime_error("Unexpected end of buffer");
Expand Down Expand Up @@ -158,9 +154,7 @@ namespace glz
{
// TODO: this does not handle control chars like \t and \n

if constexpr (!Opts.whitespace_handled) {
skip_ws(it, end);
}
skip_ws(it, end);

if constexpr (!Opts.opening_handled) {
match<'"'>(it, end);
Expand Down Expand Up @@ -312,10 +306,7 @@ namespace glz
template <auto Opts>
static void op(auto& value, auto&& it, auto&& end)
{
if constexpr (!Opts.whitespace_handled) {
skip_ws(it, end);
}

skip_ws(it, end);
match<'['>(it, end);
skip_ws(it, end);
if (it == end) {
Expand All @@ -335,7 +326,7 @@ namespace glz
auto value_it = value.begin();

for (size_t i = 0; i < n; ++i) {
read<json>::op<ws_handled<Opts>()>(*value_it++, it, end);
read<json>::op<Opts>(*value_it++, it, end);
skip_ws(it, end);
if (it == end) {
throw std::runtime_error("Unexpected end");
Expand All @@ -359,7 +350,7 @@ namespace glz
// growing
if constexpr (emplace_backable<T>) {
while (it < end) {
read<json>::op<ws_handled<Opts>()>(value.emplace_back(), it, end);
read<json>::op<Opts>(value.emplace_back(), it, end);
skip_ws(it, end);
if (*it == ',') [[likely]] {
++it;
Expand Down Expand Up @@ -392,10 +383,7 @@ namespace glz
static thread_local std::vector<value_t> buffer{};
buffer.clear();

if constexpr (!Opts.whitespace_handled) {
skip_ws(it, end);
}

skip_ws(it, end);
match<'['>(it, end);
skip_ws(it, end);
for (size_t i = 0; it < end; ++i) {
Expand Down Expand Up @@ -435,10 +423,7 @@ namespace glz
}
();

if constexpr (!Opts.whitespace_handled) {
skip_ws(it, end);
}

skip_ws(it, end);
match<'['>(it, end);
skip_ws(it, end);

Expand All @@ -451,10 +436,10 @@ namespace glz
skip_ws(it, end);
}
if constexpr (glaze_array_t<T>) {
read<json>::op<ws_handled<Opts>()>(value.*glz::tuplet::get<I>(meta_v<T>), it, end);
read<json>::op<Opts>(value.*glz::tuplet::get<I>(meta_v<T>), it, end);
}
else {
read<json>::op<ws_handled<Opts>()>(glz::tuplet::get<I>(value), it, end);
read<json>::op<Opts>(glz::tuplet::get<I>(value), it, end);
}
skip_ws(it, end);
});
Expand All @@ -481,10 +466,7 @@ namespace glz
}
();

if constexpr (!Opts.whitespace_handled) {
skip_ws(it, end);
}

skip_ws(it, end);
match<'['>(it, end);
skip_ws(it, end);

Expand All @@ -497,10 +479,10 @@ namespace glz
skip_ws(it, end);
}
if constexpr (glaze_array_t<T>) {
read<json>::op<ws_handled<Opts>()>(value.*std::get<I>(meta_v<T>), it, end);
read<json>::op<Opts>(value.*std::get<I>(meta_v<T>), it, end);
}
else {
read<json>::op<ws_handled<Opts>()>(std::get<I>(value), it, end);
read<json>::op<Opts>(std::get<I>(value), it, end);
}
skip_ws(it, end);
});
Expand All @@ -516,10 +498,7 @@ namespace glz
template <auto Opts, class It>
static void op(auto& value, It&& it, auto&& end)
{
if constexpr (!Opts.whitespace_handled) {
skip_ws(it, end);
}

skip_ws(it, end);
match<'{'>(it, end);
skip_ws(it, end);
bool first = true;
Expand Down Expand Up @@ -547,7 +526,7 @@ namespace glz
// we dont' optimize this currently because it would increase binary size significantly with the complexity of generating escaped compile time versions of keys
it = start;
static thread_local std::string static_key{};
read<json>::op<ws_and_opening_handled<Opts>()>(static_key, it, end);
read<json>::op<opening_handled<Opts>()>(static_key, it, end);
key = static_key;
}
else [[likely]] {
Expand Down Expand Up @@ -617,9 +596,7 @@ namespace glz
template <auto Opts>
static void op(auto& value, auto&& it, auto&& end)
{
if constexpr (!Opts.whitespace_handled) {
skip_ws(it, end);
}
skip_ws(it, end);

if (it == end) {
throw std::runtime_error("Unexexpected eof");
Expand Down
43 changes: 43 additions & 0 deletions tests/json_test/json_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,49 @@ suite variant_tests = [] {
};
};

struct holder0_t {
int i{};
};

template <>
struct glz::meta<holder0_t>
{
using T = holder0_t;
static constexpr auto value = object("i", &T::i);
};

struct holder1_t {
holder0_t a{};
};

template <>
struct glz::meta<holder1_t>
{
using T = holder1_t;
static constexpr auto value = object("a", &T::a);
};

struct holder2_t {
std::vector<holder1_t> vec{};
};

template <>
struct glz::meta<holder2_t>
{
using T = holder2_t;
static constexpr auto value = object("vec", &T::vec);
};

suite array_of_objects = [] {
"array_of_objects_tests"_test = [] {
std::string s = R"({"vec": [{"a": {"i":5}}, {"a":{ "i":2 }}]})";
holder2_t arr{};
expect(nothrow([&] {
glz::read_json(arr, s);
}));
};
};

struct macro_t
{
double x = 5.0;
Expand Down

0 comments on commit 9a901a6

Please sign in to comment.