Skip to content

Commit

Permalink
Improve parse_true, parse_false, parse_null
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 7, 2024
1 parent 0a7743a commit 20a4cc0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 60 deletions.
78 changes: 18 additions & 60 deletions include/jsoncons/json_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1451,25 +1451,18 @@ class basic_json_parser : public ser_context

void parse_null(basic_json_visitor<char_type>& visitor, std::error_code& ec)
{
static const char_type value[] = {'n','u','l','l'};
static const char_type pat[] = {'n','u','l','l'};

saved_position_ = position_;

std::ptrdiff_t n = input_end_ - input_ptr_;
std::ptrdiff_t m = 4 - n;
if (JSONCONS_UNLIKELY(n < 4))
{
bool matches = true;
for (std::ptrdiff_t i = 1; i < n && matches; ++i)
{
if (*(input_ptr_+(i)) != value[i])
{
matches = false;
}
}
std::ptrdiff_t m = 4 - n;
int matches = std::memcmp(input_ptr_, pat, n*sizeof(char_type));
input_ptr_ += n;
position_ += n;
if (!matches)
if (matches != 0)
{
ec = json_errc::invalid_value;
more_ = false;
Expand All @@ -1483,17 +1476,10 @@ class basic_json_parser : public ser_context
}
else
{
matches = true;
for (std::ptrdiff_t i = 0; i < m && matches; ++i)
{
if (*(input_ptr_+(i)) != value[n+i])
{
matches = false;
}
}
matches = std::memcmp(input_ptr_, pat+n, m*sizeof(char_type));
input_ptr_ += m;
position_ += m;
if (!matches)
if (matches != 0)
{
ec = json_errc::invalid_value;
more_ = false;
Expand Down Expand Up @@ -1536,25 +1522,18 @@ class basic_json_parser : public ser_context

void parse_true(basic_json_visitor<char_type>& visitor, std::error_code& ec)
{
static const char_type value[] = {'t','r','u','e'};
static const char_type pat[] = {'t','r','u','e'};

saved_position_ = position_;

std::ptrdiff_t n = input_end_ - input_ptr_;
std::ptrdiff_t m = 4 - n;
if (JSONCONS_UNLIKELY(n < 4))
{
bool matches = true;
for (std::ptrdiff_t i = 1; i < n && matches; ++i)
{
if (*(input_ptr_+(i)) != value[i])
{
matches = false;
}
}
std::ptrdiff_t m = 4 - n;
int matches = std::memcmp(input_ptr_, pat, n*sizeof(char_type));
input_ptr_ += n;
position_ += n;
if (!matches)
if (matches != 0)
{
ec = json_errc::invalid_value;
more_ = false;
Expand All @@ -1568,17 +1547,10 @@ class basic_json_parser : public ser_context
}
else
{
matches = true;
for (std::ptrdiff_t i = 0; i < m && matches; ++i)
{
if (*(input_ptr_+(i)) != value[n+i])
{
matches = false;
}
}
matches = std::memcmp(input_ptr_, pat+n, m*sizeof(char_type));
input_ptr_ += m;
position_ += m;
if (!matches)
if (matches != 0)
{
ec = json_errc::invalid_value;
more_ = false;
Expand Down Expand Up @@ -1621,25 +1593,18 @@ class basic_json_parser : public ser_context

void parse_false(basic_json_visitor<char_type>& visitor, std::error_code& ec)
{
static const char_type value[] = {'f','a','l','s','e'};
static const char_type pat[] = {'f','a','l','s','e'};

saved_position_ = position_;

std::ptrdiff_t n = input_end_ - input_ptr_;
std::ptrdiff_t m = 5 - n;
if (JSONCONS_UNLIKELY(n < 5))
{
bool matches = true;
for (std::ptrdiff_t i = 1; i < n && matches; ++i)
{
if (*(input_ptr_+(i)) != value[i])
{
matches = false;
}
}
std::ptrdiff_t m = 5 - n;
int matches = std::memcmp(input_ptr_, pat, n*sizeof(char_type));
input_ptr_ += n;
position_ += n;
if (!matches)
if (matches != 0)
{
ec = json_errc::invalid_value;
more_ = false;
Expand All @@ -1653,17 +1618,10 @@ class basic_json_parser : public ser_context
}
else
{
matches = true;
for (std::ptrdiff_t i = 0; i < m && matches; ++i)
{
if (*(input_ptr_+(i)) != value[n+i])
{
matches = false;
}
}
matches = std::memcmp(input_ptr_, pat+n, m*sizeof(char_type));
input_ptr_ += m;
position_ += m;
if (!matches)
if (matches != 0)
{
ec = json_errc::invalid_value;
more_ = false;
Expand Down
9 changes: 9 additions & 0 deletions include/jsoncons/utility/binary.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef JSONCONS_UTILITY_BINARY_HPP
#define JSONCONS_UTILITY_BINARY_HPP

#include <jsoncons/config/jsoncons_config.hpp>

namespace jsoncons { namespace utility {

} // utility
} // jsoncons

0 comments on commit 20a4cc0

Please sign in to comment.