insert now handles when source is inside the string

This commit is contained in:
Krystian Stasiowski
2019-10-19 12:20:44 -04:00
parent ab53af4660
commit 906c86acf2
2 changed files with 32 additions and 9 deletions

View File

@ -1340,7 +1340,6 @@ public:
return replace(pos1, n1, sv.substr(pos2, n2));
}
// impl
fixed_string&
replace(
size_type pos,
@ -1357,7 +1356,6 @@ public:
return replace(pos, n1, s, Traits::length(s));
}
// impl
fixed_string&
replace(
size_type pos,
@ -1422,7 +1420,13 @@ public:
}
template<typename InputIterator>
#if GENERATING_DOCUMENTATION
fixed_string&
#else
typename std::enable_if<
detail::is_input_iterator<InputIterator>::value,
fixed_string&>::type
#endif
replace(
const_iterator i1,
const_iterator i2,
@ -1568,7 +1572,7 @@ public:
{
return string_view_type(
*this).rfind(
s, pos);
s, pos, n);
}
/** Finds the last substring equal to the character

View File

@ -317,10 +317,28 @@ insert(
if(size() + count > max_size())
BOOST_THROW_EXCEPTION(std::length_error{
"size() + count > max_size()"});
Traits::move(
&s_[index + count], &s_[index], size() - index);
n_ += count;
const bool inside = s <= &s_[size()] && s >= &s_[0];
if (!inside || (inside && (&s[count - 1] < &s_[index])))
{
Traits::move(&s_[index + count], &s_[index], size() - index);
Traits::copy(&s_[index], s, count);
}
else
{
const size_type offset = s - &s_[0];
Traits::move(&s_[index + count], &s_[index], size() - index);
if (offset < index)
{
const size_type diff = index - offset;
Traits::copy(&s_[index], &s_[offset], diff);
Traits::copy(&s_[index + diff], &s_[index + count], count - diff);
}
else
{
Traits::copy(&s_[index], &s_[index + count + offset], count);
}
}
n_ += count;
term();
return *this;
}
@ -363,12 +381,13 @@ insert(
BOOST_THROW_EXCEPTION(std::length_error{
"size() + count > max_size()"});
std::size_t const index = pos - begin();
Traits::move(
&s_[index + count], &s_[index], size() - index);
n_ += count;
if (&*first <= &s_[size()] && &*first >= &s_[0])
return insert(index, &*first, count).begin() + index;
Traits::move(&s_[index + count], &s_[index], size() - index);
for(auto it = begin() + index;
first != last; ++it, ++first)
Traits::assign(*it, *first);
n_ += count;
term();
return begin() + index;
}