Skip to content

Commit

Permalink
parse_into clears strings before filling them
Browse files Browse the repository at this point in the history
  • Loading branch information
grisumbras committed Oct 9, 2023
1 parent 75981e7 commit e19edde
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/boost/json/detail/parse_into.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ class converting_handler<string_like_conversion_tag, V, P>
{
private:
V* value_;
bool cleared_ = false;

public:
converting_handler( V* v, P* p )
Expand All @@ -374,12 +375,23 @@ class converting_handler<string_like_conversion_tag, V, P>

bool on_string_part( error_code&, string_view sv )
{
if( !cleared_ )
{
cleared_ = true;
value_->clear();
}

value_->append( sv.begin(), sv.end() );
return true;
}

bool on_string( error_code&, string_view sv )
{
if( !cleared_ )
value_->clear();
else
cleared_ = false;

value_->append( sv.begin(), sv.end() );

this->parent_->signal_value();
Expand Down
7 changes: 7 additions & 0 deletions test/parse_into.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ class parse_into_test
testParseInto<std::string>( "12345" );

testParseIntoErrors< string >( error::not_string, UINT64_MAX );

std::string s;
parse_into(s, R"( "foobar" )");
BOOST_TEST( s.size() == 6 );

parse_into(s, R"( "qwertyuiop" )");
BOOST_TEST( s.size() == 10 );
}

void testSequence()
Expand Down

0 comments on commit e19edde

Please sign in to comment.