Skip to content

Commit

Permalink
Removing macros (#1614)
Browse files Browse the repository at this point in the history
* Replacing GLZ_INVALID_END macro

* Replace GLZ_ADD_LEVEL and GLZ_SUB_LEVEL

* Add max guard for MSVC

* Replace GLZ_MATCH_QUOTE

* Simplify with match_quote_invalid_end

* More generic match_invalid_end

* Remove GLZ_MATCH_COMMA

* Remove GLZ_MATCH_COLON

* Remove GLZ_MATCH_OPEN_BRACKET

* match returns a boolean and removing match_quote

* Remove GLZ_MATCH_CLOSE_BRACKET

* Remove GLZ_MATCH_CLOSE_BRACE

* Replace GLZ_VALID_END

* Remove GLZ_MATCH_OPEN_BRACE

* Remove GLZ_SKIP_WS

* Replace GLZ_CSV_NL

* Replace GLZ_END_CHECK

* Replace GLZ_PARSE_WS_COLON
  • Loading branch information
stephenberry authored Feb 14, 2025
1 parent a308fd8 commit 19a189e
Show file tree
Hide file tree
Showing 13 changed files with 1,212 additions and 545 deletions.
20 changes: 15 additions & 5 deletions include/glaze/beve/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
#include "glaze/core/context.hpp"
#include "glaze/util/inline.hpp"

#define GLZ_END_CHECK(RETURN) \
if (it >= end) [[unlikely]] { \
ctx.error = error_code::unexpected_end; \
return RETURN; \
namespace glz::detail
{
GLZ_ALWAYS_INLINE bool invalid_end(is_context auto& ctx, auto&& it, auto&& end) noexcept
{
if (it >= end) [[unlikely]] {
ctx.error = error_code::unexpected_end;
return true;
}
else [[likely]] {
return false;
}
}
}

namespace glz::tag
{
Expand Down Expand Up @@ -116,7 +124,9 @@ namespace glz::detail

GLZ_ALWAYS_INLINE constexpr void skip_compressed_int(is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}

uint8_t header;
std::memcpy(&header, it, 1);
Expand Down
100 changes: 75 additions & 25 deletions include/glaze/beve/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ namespace glz
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto&&, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
if (uint8_t(*it)) [[unlikely]] {
ctx.error = error_code::syntax_error;
return;
Expand All @@ -108,7 +110,9 @@ namespace glz
template <auto Opts>
static void op(auto&& value, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);

constexpr uint8_t type = uint8_t(3) << 3;
Expand All @@ -127,7 +131,9 @@ namespace glz

const auto num_bytes = (value.size() + 7) / 8;
for (size_t byte_i{}, i{}; byte_i < num_bytes; ++byte_i, ++it) {
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
uint8_t byte;
std::memcpy(&byte, it, 1);
for (size_t bit_i = 0; bit_i < 8 && i < n; ++bit_i, ++i) {
Expand Down Expand Up @@ -184,7 +190,9 @@ namespace glz
GLZ_ALWAYS_INLINE static void op(auto&& value, const uint8_t tag, is_context auto&& ctx, auto&& it,
auto&& end) noexcept
{
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}

using V = std::decay_t<decltype(value)>;

Expand Down Expand Up @@ -298,7 +306,9 @@ namespace glz
requires(not has_no_header(Opts))
GLZ_ALWAYS_INLINE static void op(auto&& value, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);
++it;
op<no_header_on<Opts>()>(value, tag, ctx, it, end);
Expand Down Expand Up @@ -328,7 +338,9 @@ namespace glz
std::floating_point<V> ? 0 : (std::is_signed_v<V> ? 0b000'01'000 : 0b000'10'000);
constexpr uint8_t header = tag::number | type | (byte_count<V> << 5);

GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);
if (tag != header) {
ctx.error = error_code::syntax_error;
Expand Down Expand Up @@ -367,7 +379,9 @@ namespace glz
else {
constexpr uint8_t header = tag::extensions | 0b00011'000;

GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);
if (tag != header) {
ctx.error = error_code::syntax_error;
Expand All @@ -381,7 +395,9 @@ namespace glz
constexpr uint8_t complex_number = 0;
constexpr uint8_t complex_header = complex_number | type | (byte_count<V> << 5);

GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto complex_tag = uint8_t(*it);
if (complex_tag != complex_header) {
ctx.error = error_code::syntax_error;
Expand All @@ -406,7 +422,9 @@ namespace glz
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto&& value, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);
if ((tag & 0b0000'1111) != tag::boolean) {
ctx.error = error_code::syntax_error;
Expand Down Expand Up @@ -464,7 +482,9 @@ namespace glz
GLZ_ALWAYS_INLINE static void op(auto&& value, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
constexpr uint8_t header = tag::extensions | 0b00001'000;
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);
if (tag != header) [[unlikely]] {
ctx.error = error_code::syntax_error;
Expand Down Expand Up @@ -513,7 +533,9 @@ namespace glz
{
constexpr uint8_t header = tag::string;

GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);
if (tag != header) [[unlikely]] {
ctx.error = error_code::syntax_error;
Expand Down Expand Up @@ -551,7 +573,9 @@ namespace glz
{
using V = range_value_t<std::decay_t<T>>;

GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);

if constexpr (boolean_like<V>) {
Expand All @@ -573,7 +597,9 @@ namespace glz

const auto num_bytes = (value.size() + 7) / 8;
for (size_t byte_i{}, i{}; byte_i < num_bytes; ++byte_i, ++it) {
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
uint8_t byte;
std::memcpy(&byte, it, 1);
for (size_t bit_i = 7; bit_i < 8 && i < n; --bit_i, ++i) {
Expand Down Expand Up @@ -685,7 +711,9 @@ namespace glz
{
using V = range_value_t<std::decay_t<T>>;

GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);

if constexpr (boolean_like<V>) {
Expand Down Expand Up @@ -713,7 +741,9 @@ namespace glz

const auto num_bytes = (value.size() + 7) / 8;
for (size_t byte_i{}, i{}; byte_i < num_bytes; ++byte_i, ++it) {
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
uint8_t byte;
std::memcpy(&byte, it, 1);
for (size_t bit_i = 7; bit_i < 8 && i < n; --bit_i, ++i) {
Expand All @@ -728,7 +758,9 @@ namespace glz

auto prepare = [&](const size_t element_size) -> size_t {
++it;
GLZ_END_CHECK(0);
if (invalid_end(ctx, it, end)) {
return 0;
}

std::conditional_t<Opts.partial_read, size_t, const size_t> n = int_from_compressed(ctx, it, end);
if (bool(ctx.error)) [[unlikely]] {
Expand Down Expand Up @@ -892,7 +924,9 @@ namespace glz
return;
}
++it;
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}

using X = typename V::value_type;
constexpr uint8_t complex_array = 1;
Expand Down Expand Up @@ -980,7 +1014,9 @@ namespace glz
constexpr uint8_t byte_cnt = str_t<Key> ? 0 : byte_count<Key>;
constexpr uint8_t header = tag::object | type | (byte_cnt << 5);

GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);
if (tag != header) [[unlikely]] {
if constexpr (Opts.allow_conversions) {
Expand Down Expand Up @@ -1044,7 +1080,9 @@ namespace glz
constexpr uint8_t byte_cnt = str_t<Key> ? 0 : byte_count<Key>;
constexpr uint8_t header = tag::object | type | (byte_cnt << 5);

GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);
if (tag != header) [[unlikely]] {
ctx.error = error_code::syntax_error;
Expand Down Expand Up @@ -1080,7 +1118,9 @@ namespace glz
constexpr uint8_t byte_cnt = str_t<Key> ? 0 : byte_count<Key>;
constexpr uint8_t header = tag::object | type | (byte_cnt << 5);

GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);
if (tag != header) [[unlikely]] {
if constexpr (Opts.allow_conversions) {
Expand Down Expand Up @@ -1168,7 +1208,9 @@ namespace glz
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto&& value, is_context auto&& ctx, auto&& it, auto&& end)
{
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);

if (tag == tag::null) {
Expand Down Expand Up @@ -1214,7 +1256,9 @@ namespace glz
else {
constexpr uint8_t header = tag::string;

GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);
if (tag != header) [[unlikely]] {
ctx.error = error_code::syntax_error;
Expand Down Expand Up @@ -1270,7 +1314,9 @@ namespace glz
constexpr uint8_t type = 0; // string key
constexpr uint8_t header = tag::object | type;

GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);
if (tag != header) [[unlikely]] {
ctx.error = error_code::syntax_error;
Expand Down Expand Up @@ -1395,7 +1441,9 @@ namespace glz
template <auto Opts>
static void op(auto&& value, is_context auto&& ctx, auto&& it, auto&& end)
{
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);
if (tag != tag::generic_array) [[unlikely]] {
ctx.error = error_code::syntax_error;
Expand Down Expand Up @@ -1425,7 +1473,9 @@ namespace glz
template <auto Opts>
static void op(auto&& value, is_context auto&& ctx, auto&& it, auto&& end)
{
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);
if (tag != tag::generic_array) [[unlikely]] {
ctx.error = error_code::syntax_error;
Expand Down
8 changes: 6 additions & 2 deletions include/glaze/beve/skip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ namespace glz::detail
template <opts Opts>
inline void skip_object_beve(is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
const auto tag = uint8_t(*it);
++it;

Expand Down Expand Up @@ -198,7 +200,9 @@ namespace glz::detail
template <opts Opts>
inline void skip_value<BEVE>::op(is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
GLZ_END_CHECK();
if (invalid_end(ctx, it, end)) {
return;
}
switch (uint8_t(*it) & 0b00000'111) {
case tag::null: {
++it;
Expand Down
10 changes: 0 additions & 10 deletions include/glaze/core/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,3 @@ namespace glz
template <class T>
concept is_context = std::same_as<std::decay_t<T>, context>;
}

#define GLZ_ADD_LEVEL \
if constexpr (not Opts.null_terminated) { \
++ctx.indentation_level; \
}

#define GLZ_SUB_LEVEL \
if constexpr (not Opts.null_terminated) { \
--ctx.indentation_level; \
}
Loading

0 comments on commit 19a189e

Please sign in to comment.