Skip to content

Commit

Permalink
tidy code
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Dec 29, 2024
1 parent 68f10e4 commit 1caef39
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 83 deletions.
52 changes: 26 additions & 26 deletions include/jsoncons_ext/jsonpath/jsonpath_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ namespace detail {
{
}

std::size_t line() const
std::size_t line() const final
{
return line_;
}

std::size_t column() const
std::size_t column() const final
{
return column_;
}
Expand Down Expand Up @@ -1446,20 +1446,19 @@ namespace detail {
ec = jsonpath_errc::invalid_number;
return path_expression_type(alloc_);
}
else
{
int64_t n{0};
auto r = jsoncons::detail::to_integer(buffer.data(), buffer.size(), n);
if (!r)
{
ec = jsonpath_errc::invalid_number;
return path_expression_type(alloc_);
}
push_token(resources, token_type(resources.new_selector(index_selector<Json,JsonReference>(n))), ec);
if (ec) {return path_expression_type(alloc_);}

buffer.clear();
int64_t n{0};
auto r = jsoncons::detail::to_integer(buffer.data(), buffer.size(), n);
if (!r)
{
ec = jsonpath_errc::invalid_number;
return path_expression_type(alloc_);
}
push_token(resources, token_type(resources.new_selector(index_selector<Json,JsonReference>(n))), ec);
if (ec) {return path_expression_type(alloc_);}

buffer.clear();

push_token(resources, token_type(separator_arg), ec);
if (ec) {return path_expression_type(alloc_);}
buffer.clear();
Expand Down Expand Up @@ -1724,20 +1723,19 @@ namespace detail {
ec = jsonpath_errc::invalid_number;
return path_expression_type(alloc_);
}
else

int64_t n{0};
auto r = jsoncons::detail::to_integer(buffer.data(), buffer.size(), n);
if (!r)
{
int64_t n{0};
auto r = jsoncons::detail::to_integer(buffer.data(), buffer.size(), n);
if (!r)
{
ec = jsonpath_errc::invalid_number;
return path_expression_type(alloc_);
}
push_token(resources, token_type(resources.new_selector(index_selector<Json,JsonReference>(n))), ec);
if (ec) {return path_expression_type(alloc_);}

buffer.clear();
ec = jsonpath_errc::invalid_number;
return path_expression_type(alloc_);
}
push_token(resources, token_type(resources.new_selector(index_selector<Json,JsonReference>(n))), ec);
if (ec) {return path_expression_type(alloc_);}

buffer.clear();

state_stack_.pop_back(); // bracket_specifier
break;
}
Expand Down Expand Up @@ -2120,7 +2118,9 @@ namespace detail {
break;
case '\r':
if (p_+1 < end_input_ && *(p_+1) == '\n')
{
++p_;
}
++line_;
column_ = 1;
++p_;
Expand Down
61 changes: 25 additions & 36 deletions include/jsoncons_ext/jsonpath/jsonpath_selector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,14 @@ namespace detail {
using path_generator_type = path_generator<Json,JsonReference>;
using node_receiver_type = typename supertype::node_receiver_type;

current_node_selector()
{
}

current_node_selector() = default;
current_node_selector(const current_node_selector&) = default;
current_node_selector(current_node_selector&&) = default;
~current_node_selector() = default;

current_node_selector& operator=(const current_node_selector&) = default;
current_node_selector& operator=(current_node_selector&&) = default;

void select(eval_context<Json,JsonReference>& context,
reference root,
const path_node_type& last,
Expand Down Expand Up @@ -522,9 +524,14 @@ namespace detail {
: ancestor_depth_(ancestor_depth)
{
}
parent_node_selector(const parent_node_selector&) = default;
parent_node_selector(parent_node_selector&&) = default;

~parent_node_selector() = default;

parent_node_selector& operator=(const parent_node_selector&) = default;
parent_node_selector& operator=(parent_node_selector&&) = default;

void select(eval_context<Json,JsonReference>& context,
reference root,
const path_node_type& last,
Expand Down Expand Up @@ -624,7 +631,7 @@ namespace detail {
auto slen = static_cast<int64_t>(current.size());
if (index_ >= 0 && index_ < slen)
{
std::size_t i = static_cast<std::size_t>(index_);
auto i = static_cast<std::size_t>(index_);
this->tail_select(context, root,
path_generator_type::generate(context, last, i, options),
current.at(i), receiver, options);
Expand All @@ -634,7 +641,7 @@ namespace detail {
int64_t index = slen + index_;
if (index >= 0 && index < slen)
{
std::size_t i = static_cast<std::size_t>(index);
auto i = static_cast<std::size_t>(index);
this->tail_select(context, root,
path_generator_type::generate(context, last, i, options),
current.at(i), receiver, options);
Expand All @@ -655,31 +662,22 @@ namespace detail {
auto slen = static_cast<int64_t>(current.size());
if (index_ >= 0 && index_ < slen)
{
std::size_t i = static_cast<std::size_t>(index_);
auto i = static_cast<std::size_t>(index_);
return this->evaluate_tail(context, root,
path_generator_type::generate(context, last, i, options),
current.at(i), options, ec);
}
else
int64_t index = slen + index_;
if (index >= 0 && index < slen)
{
int64_t index = slen + index_;
if (index >= 0 && index < slen)
{
std::size_t i = static_cast<std::size_t>(index);
return this->evaluate_tail(context, root,
path_generator_type::generate(context, last, i, options),
current.at(i), options, ec);
}
else
{
return context.null_value();
}
auto i = static_cast<std::size_t>(index);
return this->evaluate_tail(context, root,
path_generator_type::generate(context, last, i, options),
current.at(i), options, ec);
}
}
else
{
return context.null_value();
}
return context.null_value();
}
};

Expand Down Expand Up @@ -1076,19 +1074,13 @@ namespace detail {
std::size_t start = j.template as<std::size_t>();
return this->evaluate_tail(context, root, last, current.at(start), options, ec);
}
else if (j.is_string() && current.is_object())
if (j.is_string() && current.is_object())
{
return this->evaluate_tail(context, root, last, current.at(j.as_string_view()), options, ec);
}
else
{
return context.null_value();
}
}
else
{
return context.null_value();
}
return context.null_value();
}

std::string to_string(int level) const override
Expand Down Expand Up @@ -1242,12 +1234,9 @@ namespace detail {
if (!ec)
{
return this->evaluate_tail(context, root, last, *context.create_json(std::move(ref)),
options, ec);
}
else
{
return context.null_value();
options, ec);
}
return context.null_value();
}

std::string to_string(int level) const override
Expand Down
31 changes: 10 additions & 21 deletions include/jsoncons_ext/jsonpath/path_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,18 @@ namespace jsonpath {
std::size_t size_;
path_node_kind node_kind_;
string_view_type name_;
std::size_t index_;
std::size_t index_{0};

public:
basic_path_node()
: parent_(nullptr), size_(1),
node_kind_(path_node_kind::root),
index_(0)
node_kind_(path_node_kind::root)
{
}

basic_path_node(const basic_path_node* parent, string_view_type name)
: parent_(parent), size_(parent == nullptr ? 1 : parent->size()+1),
node_kind_(path_node_kind::name), name_(name), index_(0)
node_kind_(path_node_kind::name), name_(name)
{
}

Expand All @@ -57,23 +56,13 @@ namespace jsonpath {
{
}

basic_path_node(const basic_path_node& other)
: parent_(other.parent_), size_(other.size()),
node_kind_(other.node_kind_),
name_(other.name_),
index_(other.index_)
{
}
basic_path_node(const basic_path_node& other) = default;
basic_path_node(basic_path_node&& other) = default;

~basic_path_node() = default;

basic_path_node& operator=(const basic_path_node& other)
{
parent_ = other.parent_;
size_ = other.size();
node_kind_ = other.node_kind_;
index_ = other.index_;
name_ = other.name_;
return *this;
}
basic_path_node& operator=(const basic_path_node& other) = default;
basic_path_node& operator=(basic_path_node&& other) = default;

const basic_path_node* parent() const { return parent_;}

Expand All @@ -97,7 +86,7 @@ namespace jsonpath {
return index_;
}

void swap(basic_path_node& node)
void swap(basic_path_node& node) noexcept
{
std::swap(parent_, node.parent_);
std::swap(node_kind_, node.node_kind_);
Expand Down

0 comments on commit 1caef39

Please sign in to comment.