Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy of or 1071 to re execute the automated check #1072

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions include/boost/json/impl/pointer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,108 @@ value::find_pointer(string_view ptr, std::error_code& ec) noexcept
return const_cast<value*>(self.find_pointer(ptr, ec));
}

std::pair<bool, string_view>
value::delete_at_pointer(
string_view sv,
system::error_code& ec)
{
ec.clear();


string_view previous_segment;
string_view sv_copy = sv;
string_view err_position;
string_view segment = detail::next_segment(sv, ec);
size_t shift = 0;

auto result = this;
auto previous_result = this;

while (true)
{
if (ec.failed())
return {false, err_position};

if (!result)
{
BOOST_JSON_FAIL(ec, error::not_found);
return {false, err_position};
}

if( segment.empty() )
break;

shift += segment.size();
err_position = sv_copy.substr(0, shift);

previous_segment = segment;
previous_result = result;

switch (result->kind())
{
case kind::object: {
auto& obj = result->get_object();

detail::pointer_token const token(segment);
segment = detail::next_segment(sv, ec);

result = detail::if_contains_token(obj, token);
if( !result )
{
BOOST_JSON_FAIL(ec, error::not_found);
return {false, err_position};
}
break;
}
case kind::array: {
auto const index = detail::parse_number_token(segment, ec);
segment = detail::next_segment(sv, ec);

auto& arr = result->get_array();
result = arr.if_contains(index);
if( !result )
{
BOOST_JSON_FAIL(ec, error::past_the_end);
return {false, err_position};
}
break;
}
default: {
BOOST_JSON_FAIL(ec, error::value_is_scalar);
return {false, err_position};
}
}
}

err_position = {};

switch (previous_result->kind())
{
case kind::object: {
auto& obj = previous_result->get_object();
detail::pointer_token const token(previous_segment);
key_value_pair* kv = detail::find_in_object(obj, token).first;
if (kv) {
obj.erase(kv);
return {true, err_position};
}
}
case kind::array: {
auto const index = detail::parse_number_token(previous_segment, ec);
auto& arr = previous_result->get_array();
if (arr.if_contains(index)){
arr.erase(arr.begin() + index);
return {true, err_position};
}
}
default: {
BOOST_JSON_FAIL(ec, error::value_is_scalar);
return {false, err_position};
}
}
return {false, err_position};
}

value*
value::set_at_pointer(
string_view sv,
Expand Down
7 changes: 7 additions & 0 deletions include/boost/json/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3996,6 +3996,13 @@ class value
set_pointer_options const& opts = {} );
/** @} */

/** Remove an element via JSON Pointer.
*/
BOOST_JSON_DECL
std::pair<bool, string_view>
delete_at_pointer(
string_view sv,
system::error_code& ec);
//------------------------------------------------------

/** Return `true` if two values are equal.
Expand Down