From 0174e552988ff92d820c3289196acd33619767bd Mon Sep 17 00:00:00 2001 From: jfbastien Date: Thu, 12 Mar 2009 12:28:58 +0000 Subject: [PATCH] Bug 2684989: flex_string insert char fails with SmallStringOpt git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@1006 7ec92016-0320-0410-acc4-a06ded1c099a --- include/loki/flex/flex_string_shell.h | 36 +++++++++++++-------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/include/loki/flex/flex_string_shell.h b/include/loki/flex/flex_string_shell.h index a7de00c..e6553b5 100644 --- a/include/loki/flex/flex_string_shell.h +++ b/include/loki/flex/flex_string_shell.h @@ -548,29 +548,27 @@ private: Invariant checker(*this); (void) checker; assert(begin() <= p && p <= end()); - if (capacity() - size() < n) + const size_type insertOffset(p - begin()); + const size_type originalSize(size()); + if(n < originalSize - insertOffset) { - const size_type sz = p - begin(); - reserve(size() + n); - p = begin() + sz; - } - const iterator oldEnd = end(); - //if (p + n < oldEnd) // replaced because of crash (pk) - if( n < size_type(oldEnd - p)) - { - append(oldEnd - n, oldEnd); - //std::copy( - // reverse_iterator(oldEnd - n), - // reverse_iterator(p), - // reverse_iterator(oldEnd)); - flex_string_details::pod_move(&*p, &*oldEnd - n, &*p + n); - std::fill(p, p + n, c); + // The new characters fit within the original string. + // The characters that are pushed back need to be moved because they're aliased. + // The appended characters will all be overwritten by the move. + append(n, value_type(0)); + value_type * begin(&*begin()); + flex_string_details::pod_move(begin + insertOffset, begin + originalSize, begin + insertOffset + n); + std::fill(begin + insertOffset, begin + insertOffset + n, c); } else { - append(n - (end() - p), c); - append(p, oldEnd); - std::fill(p, oldEnd, c); + // The new characters exceed the original string. + // The characters that are pushed back can simply be copied since they aren't aliased. + // The appended characters will partly be overwritten by the copy. + append(n, c); + value_type * begin(&*begin()); + flex_string_details::pod_copy(begin + insertOffset, begin + originalSize, begin + insertOffset + n); + std::fill(begin + insertOffset, begin + originalSize, c); } return *this; }