Skip to content
Merged
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
11 changes: 8 additions & 3 deletions include/boost/container/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ class basic_string
: base_t()
{
this->priv_short_terminate();
this->assign(s, n); // assign checks for s != 0
this->assign(s, n); // assign checks for s != 0 when n != 0
}

//! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
Expand All @@ -902,7 +902,7 @@ class basic_string
: base_t(a)
{
this->priv_short_terminate();
this->assign(s, n); // assign checks for s != 0
this->assign(s, n); // assign checks for s != 0 when n != 0
}

//! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
Expand Down Expand Up @@ -1710,6 +1710,7 @@ class basic_string
}

//! <b>Requires</b>: s points to an array of at least n elements of CharT.
//! If n is zero, s may be null.
//!
//! <b>Effects</b>: Replaces the string controlled by *this with a string of
//! length n whose elements are a copy of those pointed to by s.
Expand All @@ -1719,7 +1720,11 @@ class basic_string
//! <b>Returns</b>: *this
basic_string& assign(const CharT* s, size_type n)
{
BOOST_ASSERT(s != 0);
BOOST_ASSERT(s != 0 || n == 0);
if(n == 0){
this->clear();
return *this;
}
return this->assign(s, s + difference_type(n));
}

Expand Down
47 changes: 46 additions & 1 deletion test/string_view_compat_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,49 @@ void assign_test()
BOOST_TEST(sv == s);
}

template<class StringViewType>
void empty_assign_test()
{
typedef typename StringViewType::value_type char_type;
typedef boost::container::basic_string<char_type> string_type;
const char_type empty[] = { char_type(0) };
const char_type value = char_type('y');
const StringViewType views[] = { StringViewType(), StringViewType(empty) };
const std::size_t sizes[] = { 0u, 7u, 128u };
for(std::size_t i = 0; i != sizeof(views)/sizeof(views[0]); ++i){
const StringViewType sv = views[i];
string_type constructed(sv);
BOOST_TEST(constructed.empty());
BOOST_TEST(constructed.c_str()[0] == char_type(0));
string_type constructed_with_allocator(sv, constructed.get_allocator());
BOOST_TEST(constructed_with_allocator.empty());
BOOST_TEST(constructed_with_allocator.c_str()[0] == char_type(0));

for(std::size_t j = 0; j != sizeof(sizes)/sizeof(sizes[0]); ++j){
string_type s(sizes[j], char_type('x'));
const std::size_t capacity = s.capacity();
BOOST_TEST(&s.assign(sv) == &s);
BOOST_TEST(s.empty());
BOOST_TEST(s.c_str()[0] == char_type(0));
BOOST_TEST(s.capacity() == capacity);

s.assign(sizes[j], char_type('x'));
BOOST_TEST(&(s = sv) == &s);
BOOST_TEST(s.empty());
BOOST_TEST(s.c_str()[0] == char_type(0));

s.assign(sizes[j], char_type('x'));
BOOST_TEST(&s.assign(sv.data(), sv.size()) == &s);
BOOST_TEST(s.empty());
BOOST_TEST(s.c_str()[0] == char_type(0));

s.assign(StringViewType(&value, 1u));
BOOST_TEST(s.size() == 1u);
BOOST_TEST(s[0] == value);
}
}
}

template<class StringViewType>
void plus_equal_test()
{
Expand Down Expand Up @@ -310,6 +353,7 @@ void test_all()
constructor_test<StringViewType>();
assignment_test<StringViewType>();
assign_test<StringViewType>();
empty_assign_test<StringViewType>();
plus_equal_test<StringViewType>();
append_test<StringViewType>();
insert_test<StringViewType>();
Expand All @@ -327,10 +371,11 @@ void test_all()
int main()
{
test_all<boost::string_view>();
empty_assign_test<boost::wstring_view>();
#ifdef BOOST_CONTAINER_TEST_HAS_STD_STRING_VIEW
test_all<std::string_view>();
empty_assign_test<std::wstring_view>();
#endif

return boost::report_errors();
}