Skip to content
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
15 changes: 12 additions & 3 deletions include/boost/json/impl/string.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,18 @@ assign(
char const* s,
size_type count)
{
std::char_traits<char>::copy(
impl_.assign(count, sp_),
s, count);
auto const p = impl_.data();
if(detail::ptr_in_range(p, p + impl_.size(), s))
{
std::char_traits<char>::move(p, s, count);
impl_.term(count);
}
else
{
std::char_traits<char>::copy(
impl_.assign(count, sp_),
s, count);
}
return *this;
}

Expand Down
30 changes: 30 additions & 0 deletions test/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,36 @@ class string_test
});
};

// assign(char const*, size_type) self-referencing
{
// non-SBO: assign from a substring of itself
{
string s("abcdefghijklmnopqrstuvwxyz0123456789");
string_view sub(s.data() + 3, s.size() - 3);
std::string expected(sub.data(), sub.size());
s.assign(sub);
BOOST_TEST(s == expected);
}

// non-SBO: assign from beginning (prefix)
{
string s("abcdefghijklmnopqrstuvwxyz0123456789");
string_view sub(s.data(), 10);
std::string expected(sub.data(), sub.size());
s.assign(sub);
BOOST_TEST(s == expected);
}

// non-SBO: self-assign via string_view
{
string s("abcdefghijklmnopqrstuvwxyz0123456789");
string_view sub(s.data(), s.size());
std::string expected(sub.data(), sub.size());
s.assign(sub);
BOOST_TEST(s == expected);
}
}

// assign(char const* s)
{
fail_loop([&](storage_ptr const& sp)
Expand Down
Loading