diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index a8c0074b0a03..15973591c7e2 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,28 @@ +2004-01-22 Paolo Carlini + + * include/bits/basic_string.h (_M_replace_safe): Change + signatures to take size_types and const _CharT*. + (_M_replace_aux): Likewise, takes size_types instead of + iterators. + (append(size_type, _CharT)): Update call. + (assign(size_type, _CharT)): Ditto. + (replace(iterator, iterator, size_type, _CharT)): Ditto. + (_M_replace_dispatch(iterator, iterator, _Integer, + _Integer, __true_type)): Ditto. + * include/bits/basic_string.tcc (assign(const _CharT*, + size_type)): Ditto. + (insert(size_type, const _CharT*, size_type)): Ditto. + (replace(size_type, size_type, const _CharT*, + size_type)): Ditto. + (_M_replace(iterator, iterator, _InputIterator, + _InputIterator)): Ditto. + (append(const basic_string&)): Ditto. + (append(const basic_string&, size_type, size_type): Ditto. + (append(const _CharT*, size_type): Ditto. + (_M_replace_safe, _M_replace_safe): Change definitions + accordingly, simplify. + * string-inst.cc (_M_replace_safe): Don't instantiate. + 2004-01-21 Paolo Carlini * include/bits/basic_string.tcc (append(const basic_string&)): diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 216c935670a9..56efb5441cd2 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -750,7 +750,7 @@ namespace std */ basic_string& append(size_type __n, _CharT __c) - { return _M_replace_aux(_M_iend(), _M_iend(), __n, __c); } + { return _M_replace_aux(this->size(), size_type(0), __n, __c); } /** * @brief Append a range of characters. @@ -836,7 +836,7 @@ namespace std */ basic_string& assign(size_type __n, _CharT __c) - { return _M_replace_aux(_M_ibegin(), _M_iend(), __n, __c); } + { return _M_replace_aux(size_type(0), this->size(), __n, __c); } /** * @brief Set value to a range of characters. @@ -1281,7 +1281,7 @@ namespace std { _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 && __i2 <= _M_iend()); - return _M_replace_aux(__i1, __i2, __n, __c); + return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c); } /** @@ -1359,7 +1359,7 @@ namespace std basic_string& _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n, _Integer __val, __true_type) - { return _M_replace_aux(__i1, __i2, __n, __val); } + { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); } template basic_string& @@ -1368,17 +1368,16 @@ namespace std { return _M_replace(__i1, __i2, __k1, __k2); } basic_string& - _M_replace_aux(iterator __i1, iterator __i2, size_type __n2, _CharT __c); + _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, _CharT __c); template basic_string& _M_replace(iterator __i1, iterator __i2, _InputIterator __k1, _InputIterator __k2); - template - basic_string& - _M_replace_safe(iterator __i1, iterator __i2, _ForwardIterator __k1, - _ForwardIterator __k2); + basic_string& + _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s, + size_type __n2); // _S_construct_aux is used to implement the 21.3.1 para 15 which // requires special behaviour if _InIter is an integral type diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index ac886774536d..bf6af34767da 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -287,7 +287,7 @@ namespace std __throw_length_error("basic_string::assign"); if (_M_rep()->_M_is_shared() || less()(__s, _M_data()) || less()(_M_data() + this->size(), __s)) - return _M_replace_safe(_M_ibegin(), _M_iend(), __s, __s + __n); + return _M_replace_safe(size_type(0), this->size(), __s, __n); else { // Work in-place @@ -324,8 +324,7 @@ namespace std __throw_length_error("basic_string::insert"); if (_M_rep()->_M_is_shared() || less()(__s, _M_data()) || less()(_M_data() + this->size(), __s)) - return _M_replace_safe(_M_ibegin() + __pos, _M_ibegin() + __pos, - __s, __s + __n); + return _M_replace_safe(__pos, size_type(0), __s, __n); else { // Work in-place. If _M_mutate reallocates the string, __s @@ -361,8 +360,7 @@ namespace std __throw_length_error("basic_string::replace"); if (_M_rep()->_M_is_shared() || less()(__s, _M_data()) || less()(_M_data() + this->size(), __s)) - return _M_replace_safe(_M_ibegin() + __pos, - _M_ibegin() + __pos + __n1, __s, __s + __n2); + return _M_replace_safe(__pos, __n1, __s, __n2); // Todo: optimized in-place replace. else return _M_replace(_M_ibegin() + __pos, _M_ibegin() + __pos + __n1, @@ -608,16 +606,14 @@ namespace std template basic_string<_CharT, _Traits, _Alloc>& basic_string<_CharT, _Traits, _Alloc>:: - _M_replace_aux(iterator __i1, iterator __i2, size_type __n2, _CharT __c) + _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, + _CharT __c) { - const size_type __n1 = __i2 - __i1; - const size_type __off1 = __i1 - _M_ibegin(); - if (max_size() - (this->size() - __n1) <= __n2) + if (this->size() - __n1 > this->max_size() - __n2) __throw_length_error("basic_string::_M_replace_aux"); - _M_mutate(__off1, __n1, __n2); - // Invalidated __i1, __i2 + _M_mutate(__pos1, __n1, __n2); if (__n2) - traits_type::assign(_M_data() + __off1, __n2, __c); + traits_type::assign(_M_data() + __pos1, __n2, __c); return *this; } @@ -630,36 +626,26 @@ namespace std _M_replace(iterator __i1, iterator __i2, _InputIterator __k1, _InputIterator __k2) { - // Save concerned source string data in a temporary. const basic_string __s(__k1, __k2); - return _M_replace_safe(__i1, __i2, __s._M_ibegin(), __s._M_iend()); + return _M_replace_safe(__i1 - _M_ibegin(), __i2 - __i1, __s._M_data(), + __s.size()); } - // This is a special replace helper, which does not buffer internally - // and can be used in "safe" situations involving forward iterators, + // This helper doesn't buffer internally and can be used in "safe" situations, // i.e., when source and destination ranges are known to not overlap. template - template - basic_string<_CharT, _Traits, _Alloc>& - basic_string<_CharT, _Traits, _Alloc>:: - _M_replace_safe(iterator __i1, iterator __i2, _ForwardIterator __k1, - _ForwardIterator __k2) - { - const size_type __dnew = static_cast(std::distance(__k1, __k2)); - const size_type __dold = __i2 - __i1; - const size_type __dmax = this->max_size(); - - if (__dmax <= __dnew) - __throw_length_error("basic_string::_M_replace_safe"); - const size_type __off = __i1 - _M_ibegin(); - _M_mutate(__off, __dold, __dnew); - - // Invalidated __i1, __i2 - if (__dnew) - _S_copy_chars(_M_data() + __off, __k1, __k2); - - return *this; - } + basic_string<_CharT, _Traits, _Alloc>& + basic_string<_CharT, _Traits, _Alloc>:: + _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s, + size_type __n2) + { + if (this->size() - __n1 > this->max_size() - __n2) + __throw_length_error("basic_string::_M_replace_safe"); + _M_mutate(__pos1, __n1, __n2); + if (__n2) + traits_type::copy(_M_data() + __pos1, __s, __n2); + return *this; + } template basic_string<_CharT, _Traits, _Alloc>& @@ -684,8 +670,8 @@ namespace std const size_type __len = __size + this->size(); if (__len > this->capacity()) this->reserve(__len); - return _M_replace_safe(_M_iend(), _M_iend(), __str._M_ibegin(), - __str._M_iend()); + return _M_replace_safe(this->size(), size_type(0), __str._M_data(), + __str.size()); } template @@ -701,8 +687,8 @@ namespace std const size_type __len = __n + this->size(); if (__len > this->capacity()) this->reserve(__len); - return _M_replace_safe(_M_iend(), _M_iend(), __str._M_ibegin() - + __pos, __str._M_ibegin() + __pos + __n); + return _M_replace_safe(this->size(), size_type(0), __str._M_data() + + __pos, __n); } template @@ -714,7 +700,7 @@ namespace std const size_type __len = __n + this->size(); if (__len > this->capacity()) this->reserve(__len); - return _M_replace_safe(_M_iend(), _M_iend(), __s, __s + __n); + return _M_replace_safe(this->size(), size_type(0), __s, __n); } template diff --git a/libstdc++-v3/src/string-inst.cc b/libstdc++-v3/src/string-inst.cc index d400d4fbf548..db0417996892 100644 --- a/libstdc++-v3/src/string-inst.cc +++ b/libstdc++-v3/src/string-inst.cc @@ -67,14 +67,6 @@ namespace std S& S::_M_replace(S::iterator, S::iterator, const C*, const C*); - template - S& - S::_M_replace_safe(S::iterator, S::iterator, S::iterator, S::iterator); - - template - S& - S::_M_replace_safe(S::iterator, S::iterator, const C*, const C*); - template C* S::_S_construct(S::iterator, S::iterator,