Skip to content

Commit 7281753

Browse files
author
Roy Bellingan
committed
In progress
1 parent 65a4846 commit 7281753

4 files changed

Lines changed: 326 additions & 369 deletions

File tree

include/boost/json/impl/pointer.ipp

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -531,75 +531,49 @@ value::erase_at_pointer (
531531
return false;
532532
}
533533

534-
string_view previous_segment;
535-
536-
string_view segment = detail::next_segment(sv, ec);
537-
538-
auto result = this;
539-
auto previous_result = this;
540-
534+
string_view walk = sv;
535+
string_view last_segment;
541536
while (true)
542537
{
538+
last_segment = detail::next_segment(walk, ec);
543539
if (ec.failed())
544540
return false;
545-
546-
if (!result)
547-
{
548-
BOOST_JSON_FAIL(ec, error::not_found);
549-
return false;
550-
}
551-
552-
if( segment.empty() )
541+
if (walk.empty())
553542
break;
543+
}
554544

555-
previous_segment = segment;
556-
previous_result = result;
545+
string_view const parent_sv(
546+
sv.data(),
547+
static_cast<std::size_t>(last_segment.data() - sv.data()));
557548

558-
switch (result->kind())
549+
value* parent = detail::walk_pointer(
550+
*this,
551+
parent_sv,
552+
ec,
553+
[]( object& obj, detail::pointer_token token )
559554
{
560-
case boost::json::kind::object: {
561-
auto& obj = result->get_object();
562-
563-
detail::pointer_token const token(segment);
564-
segment = detail::next_segment(sv, ec);
565-
if (ec.failed())
566-
return false;
567-
568-
result = detail::if_contains_token(obj, token);
569-
if( !result )
570-
{
571-
BOOST_JSON_FAIL(ec, error::not_found);
572-
return false;
573-
}
574-
break;
575-
}
576-
case boost::json::kind::array: {
577-
auto const index = detail::parse_number_token(segment, ec);
578-
segment = detail::next_segment(sv, ec);
579-
if (ec.failed())
580-
return false;
581-
582-
auto& arr = result->get_array();
583-
result = arr.if_contains(index);
584-
if( !result )
585-
{
586-
BOOST_JSON_FAIL(ec, error::past_the_end);
587-
return false;
588-
}
589-
break;
590-
}
591-
default: {
592-
BOOST_JSON_FAIL(ec, error::value_is_scalar);
593-
return false;
594-
}
595-
}
596-
}
555+
return detail::if_contains_token(obj, token);
556+
},
557+
[]( array& arr, std::size_t index, system::error_code& ec ) -> value*
558+
{
559+
if( ec )
560+
return nullptr;
561+
562+
return arr.if_contains(index);
563+
},
564+
[]( value&, string_view)
565+
{
566+
return std::false_type();
567+
});
568+
569+
if (!parent)
570+
return false;
597571

598-
switch (previous_result->kind())
572+
switch (parent->kind())
599573
{
600574
case boost::json::kind::object: {
601-
auto& obj = previous_result->get_object();
602-
detail::pointer_token const token(previous_segment);
575+
auto& obj = parent->get_object();
576+
detail::pointer_token const token(last_segment);
603577
key_value_pair* kv = detail::find_in_object(obj, token).first;
604578
if (kv) {
605579
obj.erase(kv);
@@ -608,8 +582,8 @@ value::erase_at_pointer (
608582
return false;
609583
}
610584
case boost::json::kind::array: {
611-
auto const index = detail::parse_number_token(previous_segment, ec);
612-
auto& arr = previous_result->get_array();
585+
auto const index = detail::parse_number_token(last_segment, ec);
586+
auto& arr = parent->get_array();
613587
if (arr.if_contains(index)){
614588
arr.erase(arr.begin() + index);
615589
return true;

test/Jamfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ local SOURCES =
2323
doc_uses_allocator.cpp
2424
doc_using_numbers.cpp
2525
double.cpp
26-
erase_at_pointer.cpp
2726
error.cpp
2827
fwd.cpp
2928
json.cpp

0 commit comments

Comments
 (0)