fix basic_string::replace for integral types

From-SVN: r68286
This commit is contained in:
Doug Gregor 2003-06-20 22:12:18 +00:00 committed by Doug Gregor
parent 49a2166f17
commit bdb0f0f5d2
4 changed files with 54 additions and 19 deletions

View File

@ -1,3 +1,15 @@
2003-06-20 Doug Gregor <dgregor@apple.com>
* include/bits/basic_string.h (basic_string::replace): Dispatch
_InputIterator version based on _Is_integer.
* include/bits/basic_string.tcc (basic_string::replace):
Renamed replace(iterator, iterator, size_type, _CharT) to
_M_replace_aux.
* testsuite/21_strings/basic_string/assign/char/1.cc (test01):
Test basic_string::assign(_InputIterator, _InputIterator),
which calls basic_string::replace(iterator, iterator,
_Input_iterator, _InputIterator).
2003-06-20 Benjamin Kosnik <bkoz@redhat.com>
* testsuite/testsuite_performance.h (resource_counter): Don't use

View File

@ -625,14 +625,15 @@ namespace std
{ return this->replace(__i1, __i2, __s, traits_type::length(__s)); }
basic_string&
replace(iterator __i1, iterator __i2, size_type __n, _CharT __c);
replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
{ return _M_replace_aux(__i1, __i2, __n, __c); }
template<class _InputIterator>
basic_string&
replace(iterator __i1, iterator __i2,
_InputIterator __k1, _InputIterator __k2)
{ return _M_replace(__i1, __i2, __k1, __k2,
typename iterator_traits<_InputIterator>::iterator_category()); }
{ typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); }
// Specializations for the common case of pointer and iterator:
// useful to avoid the overhead of temporary buffering in _M_replace.
@ -659,6 +660,25 @@ namespace std
}
private:
template<class _Integer>
basic_string&
_M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
_Integer __val, __true_type)
{ return _M_replace_aux(__i1, __i2, __n, __val); }
template<class _InputIterator>
basic_string&
_M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
_InputIterator __k2, __false_type)
{
typedef typename iterator_traits<_InputIterator>::iterator_category
_Category;
return _M_replace(__i1, __i2, __k1, __k2, _Category());
}
basic_string&
_M_replace_aux(iterator __i1, iterator __i2, size_type __n2, _CharT __c);
template<class _InputIterator>
basic_string&
_M_replace(iterator __i1, iterator __i2, _InputIterator __k1,

View File

@ -621,6 +621,22 @@ namespace std
// else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
}
template<typename _CharT, typename _Traits, typename _Alloc>
basic_string<_CharT, _Traits, _Alloc>&
basic_string<_CharT, _Traits, _Alloc>::
_M_replace_aux(iterator __i1, iterator __i2, size_type __n2, _CharT __c)
{
size_type __n1 = __i2 - __i1;
size_type __off1 = __i1 - _M_ibegin();
if (max_size() - (this->size() - __n1) <= __n2)
__throw_length_error("basic_string::replace");
_M_mutate (__off1, __n1, __n2);
// Invalidated __i1, __i2
if (__n2)
traits_type::assign(_M_data() + __off1, __n2, __c);
return *this;
}
// This is the general replace helper, which currently gets instantiated both
// for input iterators and reverse iterators. It buffers internally and then
// calls _M_replace_safe.
@ -760,22 +776,6 @@ namespace std
return __str;
}
template<typename _CharT, typename _Traits, typename _Alloc>
basic_string<_CharT, _Traits, _Alloc>&
basic_string<_CharT, _Traits, _Alloc>::
replace(iterator __i1, iterator __i2, 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)
__throw_length_error("basic_string::replace");
_M_mutate (__off1, __n1, __n2);
// Invalidated __i1, __i2
if (__n2)
traits_type::assign(_M_data() + __off1, __n2, __c);
return *this;
}
template<typename _CharT, typename _Traits, typename _Alloc>
typename basic_string<_CharT, _Traits, _Alloc>::size_type
basic_string<_CharT, _Traits, _Alloc>::

View File

@ -44,6 +44,9 @@ test01()
aux.assign(aux, i + 1, string::npos);
VERIFY(aux.c_str()[9] == 'B');
VERIFY(aux == "/Hanalei Bay/Kauai/Hawaii");
aux.assign(10, 0);
VERIFY(aux.length() == 10);
}
int main()