Better inside string check, and addtional tests for better coverage

This commit is contained in:
Krystian Stasiowski
2019-10-31 11:09:13 -04:00
parent 7a8a158857
commit 71804b8acf
2 changed files with 11 additions and 3 deletions

View File

@ -320,7 +320,7 @@ insert(
BOOST_FIXED_STRING_THROW(std::length_error{ BOOST_FIXED_STRING_THROW(std::length_error{
"size() + count > max_size()"}); "size() + count > max_size()"});
const bool inside = s <= &s_[size()] && s >= &s_[0]; const bool inside = s <= &s_[size()] && s >= &s_[0];
if (!inside || (inside && (&s[count - 1] < &s_[index]))) if (!inside || (inside && ((s - s_) + count <= index)))
{ {
Traits::move(&s_[index + count], &s_[index], size() - index + 1); Traits::move(&s_[index + count], &s_[index], size() - index + 1);
Traits::copy(&s_[index], s, count); Traits::copy(&s_[index], s, count);
@ -597,7 +597,7 @@ replace(
const bool inside = s <= &s_[size()] && s >= &s_[0]; const bool inside = s <= &s_[size()] && s >= &s_[0];
if (inside && (s - &s_[0]) == pos && n1 == n2) if (inside && (s - &s_[0]) == pos && n1 == n2)
return *this; return *this;
if (!inside || (inside && (&s[n2 - 1] < &s_[pos]))) if (!inside || (inside && ((s - s_) + n2 <= pos)))
{ {
// source outside // source outside
Traits::move(&s_[pos + n2], &s_[pos + n1], size() - pos - n1 + 1); Traits::move(&s_[pos + n2], &s_[pos + n1], size() - pos - n1 + 1);

View File

@ -23,7 +23,7 @@ testS(const S& s, typename S::size_type pos, typename S::size_type n)
if (pos <= s.size()) if (pos <= s.size())
{ {
typename S::string_view_type str = s.substr(pos, n); typename S::string_view_type str = s.substr(pos, n);
typename S::size_type rlen = std::min(n, s.size() - pos); typename S::size_type rlen = (std::min)(n, s.size() - pos);
return (S::traits_type::compare(s.data() + pos, str.data(), rlen) == 0); return (S::traits_type::compare(s.data() + pos, str.data(), rlen) == 0);
} }
else else
@ -319,6 +319,10 @@ testAssignment()
BOOST_TEST(fixed_string<3>{}.assign(fixed_string<5>{"abc"}) == "abc"); BOOST_TEST(fixed_string<3>{}.assign(fixed_string<5>{"abc"}) == "abc");
BOOST_TEST(fixed_string<3>{"*"}.assign(fixed_string<5>{"abc"}) == "abc"); BOOST_TEST(fixed_string<3>{"*"}.assign(fixed_string<5>{"abc"}) == "abc");
BOOST_TEST(fixed_string<3>{"***"}.assign(fixed_string<5>{"abc"}) == "abc"); BOOST_TEST(fixed_string<3>{"***"}.assign(fixed_string<5>{"abc"}) == "abc");
{
fixed_string<3> s("***");
BOOST_TEST(s.assign(s) == s);
}
BOOST_TEST_THROWS(fixed_string<3>{}.assign(fixed_string<5>{"abcde"}), std::length_error); BOOST_TEST_THROWS(fixed_string<3>{}.assign(fixed_string<5>{"abcde"}), std::length_error);
// assign(fixed_string<M, CharT, Traits> const& s, size_type pos, size_type count = npos) // assign(fixed_string<M, CharT, Traits> const& s, size_type pos, size_type count = npos)
@ -5685,6 +5689,10 @@ testReplace()
fixed_string<20> fs2 = "0123456789"; fixed_string<20> fs2 = "0123456789";
BOOST_TEST(fs2.replace(0, 10, fs2.data(), 5) == "01234"); BOOST_TEST(fs2.replace(0, 10, fs2.data(), 5) == "01234");
} }
{
fixed_string<20> fs1 = "helloworld";
BOOST_TEST(fs1.replace(4, 3, fs1.data() + 1, 3) == "hellellrld");
}
// replace(size_type pos1, size_type n1, const basic_string& str); // replace(size_type pos1, size_type n1, const basic_string& str);
{ {