This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[v3] More basic_string clean ups
- From: Paolo Carlini <pcarlini at suse dot de>
- To: "'gcc-patches at gcc dot gnu dot org'" <gcc-patches at gcc dot gnu dot org>
- Date: Sat, 24 Jan 2004 12:25:36 +0100
- Subject: [v3] More basic_string clean ups
Hi,
the ChangeLog says it all, I only add that basic_string.tcc is
already 10% smaller than 7 days ago and definitely not slower
(more about this later)
Tested x86-linux, committing to mainline.
Paolo.
///////////
2004-01-24 Paolo Carlini <pcarlini@suse.de>
* include/bits/basic_string.h (assign(const basic_string&,
size_type, size_type)): Define inline here.
(replace(size_type, size_type, const basic_string&,
size_type, size_type)): Ditto.
(_M_replace_dispatch(iterator, iterator, _InputIterator,
_InputIterator, __false_type)): Only declare.
(_M_replace(iterator, iterator, _InputIterator,
_InputIterator)): Remove.
* include/bits/basic_string.tcc (assign(const basic_string&,
size_type, size_type)): Move inline.
(replace(size_type, size_type, const basic_string&,
size_type, size_type)): Ditto.
(_M_replace_dispatch(iterator, iterator, _InputIterator,
_InputIterator, __false_type)): Define, now does also what
_M_replace did before.
* src/string-inst.cc (_M_replace): Don't instantiate.
* include/bits/basic_string.tcc (find(const _CharT*,
size_type, size_type)): Tidy.
(rfind(_CharT, size_type)): Ditto.
(find_first_not_of(const _CharT*, size_type, size_type)): Ditto.
(find_first_not_of(_CharT, size_type)): Ditto.
(find_last_not_of(const _CharT*, size_type, size_type)): Ditto.
(find_last_not_of(_CharT, size_type)): Ditto.
diff -prN libstdc++-v3-orig/include/bits/basic_string.h libstdc++-v3/include/bits/basic_string.h
*** libstdc++-v3-orig/include/bits/basic_string.h Fri Jan 23 14:57:19 2004
--- libstdc++-v3/include/bits/basic_string.h Sat Jan 24 10:27:10 2004
*************** namespace std
*** 794,800 ****
* of available characters in @a str, the remainder of @a str is used.
*/
basic_string&
! assign(const basic_string& __str, size_type __pos, size_type __n);
/**
* @brief Set value to a C substring.
--- 794,803 ----
* of available characters in @a str, the remainder of @a str is used.
*/
basic_string&
! assign(const basic_string& __str, size_type __pos, size_type __n)
! { return this->assign(__str._M_data()
! + __str._M_check(__pos, "basic_string::assign"),
! __str._M_limit(__pos, __n)); }
/**
* @brief Set value to a C substring.
*************** namespace std
*** 1127,1133 ****
*/
basic_string&
replace(size_type __pos1, size_type __n1, const basic_string& __str,
! size_type __pos2, size_type __n2);
/**
* @brief Replace characters with value of a C substring.
--- 1130,1139 ----
*/
basic_string&
replace(size_type __pos1, size_type __n1, const basic_string& __str,
! size_type __pos2, size_type __n2)
! { return this->replace(__pos1, __n1, __str._M_data()
! + __str._M_check(__pos2, "basic_string::replace"),
! __str._M_limit(__pos2, __n2)); }
/**
* @brief Replace characters with value of a C substring.
*************** namespace std
*** 1357,1373 ****
template<class _InputIterator>
basic_string&
_M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
! _InputIterator __k2, __false_type)
! { return _M_replace(__i1, __i2, __k1, __k2); }
basic_string&
_M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, _CharT __c);
- template<class _InputIterator>
- basic_string&
- _M_replace(iterator __i1, iterator __i2, _InputIterator __k1,
- _InputIterator __k2);
-
basic_string&
_M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
size_type __n2);
--- 1363,1373 ----
template<class _InputIterator>
basic_string&
_M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
! _InputIterator __k2, __false_type);
basic_string&
_M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, _CharT __c);
basic_string&
_M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
size_type __n2);
diff -prN libstdc++-v3-orig/include/bits/basic_string.tcc libstdc++-v3/include/bits/basic_string.tcc
*** libstdc++-v3-orig/include/bits/basic_string.tcc Sat Jan 24 01:04:40 2004
--- libstdc++-v3/include/bits/basic_string.tcc Sat Jan 24 11:57:38 2004
*************** namespace std
*** 270,285 ****
template<typename _CharT, typename _Traits, typename _Alloc>
basic_string<_CharT, _Traits, _Alloc>&
basic_string<_CharT, _Traits, _Alloc>::
- assign(const basic_string& __str, size_type __pos, size_type __n)
- {
- return this->assign(__str._M_data()
- + __str._M_check(__pos, "basic_string::assign"),
- __str._M_limit(__pos, __n));
- }
-
- template<typename _CharT, typename _Traits, typename _Alloc>
- basic_string<_CharT, _Traits, _Alloc>&
- basic_string<_CharT, _Traits, _Alloc>::
assign(const _CharT* __s, size_type __n)
{
__glibcxx_requires_string_len(__s, __n);
--- 270,275 ----
*************** namespace std
*** 581,587 ****
__requested_cap : 2*this->_M_capacity, __alloc);
else
__r = _Rep::_S_create(__requested_cap, __alloc);
!
if (this->_M_length)
traits_type::copy(__r->_M_refdata(), _M_refdata(),
this->_M_length);
--- 571,577 ----
__requested_cap : 2*this->_M_capacity, __alloc);
else
__r = _Rep::_S_create(__requested_cap, __alloc);
!
if (this->_M_length)
traits_type::copy(__r->_M_refdata(), _M_refdata(),
this->_M_length);
*************** namespace std
*** 606,637 ****
}
template<typename _CharT, typename _Traits, typename _Alloc>
- basic_string<_CharT, _Traits, _Alloc>&
- basic_string<_CharT, _Traits, _Alloc>::
- _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
- _CharT __c)
- {
- if (this->max_size() - (this->size() - __n1) < __n2)
- __throw_length_error("basic_string::_M_replace_aux");
- _M_mutate(__pos1, __n1, __n2);
- if (__n2)
- traits_type::assign(_M_data() + __pos1, __n2, __c);
- return *this;
- }
-
- // This is the general replace helper. It buffers internally and then calls
- // _M_replace_safe.
- template<typename _CharT, typename _Traits, typename _Alloc>
template<typename _InputIterator>
basic_string<_CharT, _Traits, _Alloc>&
basic_string<_CharT, _Traits, _Alloc>::
! _M_replace(iterator __i1, iterator __i2, _InputIterator __k1,
! _InputIterator __k2)
{
const basic_string __s(__k1, __k2);
const size_type __n1 = __i2 - __i1;
if (this->max_size() - (this->size() - __n1) < __s.size())
! __throw_length_error("basic_string::_M_replace");
return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
__s.size());
}
--- 596,611 ----
}
template<typename _CharT, typename _Traits, typename _Alloc>
template<typename _InputIterator>
basic_string<_CharT, _Traits, _Alloc>&
basic_string<_CharT, _Traits, _Alloc>::
! _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
! _InputIterator __k2, __false_type)
{
const basic_string __s(__k1, __k2);
const size_type __n1 = __i2 - __i1;
if (this->max_size() - (this->size() - __n1) < __s.size())
! __throw_length_error("basic_string::_M_replace_dispatch");
return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
__s.size());
}
*************** namespace std
*** 653,664 ****
template<typename _CharT, typename _Traits, typename _Alloc>
basic_string<_CharT, _Traits, _Alloc>&
basic_string<_CharT, _Traits, _Alloc>::
! replace(size_type __pos1, size_type __n1, const basic_string& __str,
! size_type __pos2, size_type __n2)
{
! return this->replace(__pos1, __n1, __str._M_data()
! + __str._M_check(__pos2, "basic_string::replace"),
! __str._M_limit(__pos2, __n2));
}
template<typename _CharT, typename _Traits, typename _Alloc>
--- 627,641 ----
template<typename _CharT, typename _Traits, typename _Alloc>
basic_string<_CharT, _Traits, _Alloc>&
basic_string<_CharT, _Traits, _Alloc>::
! _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
! _CharT __c)
{
! if (this->max_size() - (this->size() - __n1) < __n2)
! __throw_length_error("basic_string::_M_replace_aux");
! _M_mutate(__pos1, __n1, __n2);
! if (__n2)
! traits_type::assign(_M_data() + __pos1, __n2, __c);
! return *this;
}
template<typename _CharT, typename _Traits, typename _Alloc>
*************** namespace std
*** 756,768 ****
find(const _CharT* __s, size_type __pos, size_type __n) const
{
__glibcxx_requires_string_len(__s, __n);
-
const size_type __size = this->size();
- size_t __xpos = __pos;
const _CharT* __data = _M_data();
! for (; __xpos + __n <= __size; ++__xpos)
! if (traits_type::compare(__data + __xpos, __s, __n) == 0)
! return __xpos;
return npos;
}
--- 733,743 ----
find(const _CharT* __s, size_type __pos, size_type __n) const
{
__glibcxx_requires_string_len(__s, __n);
const size_type __size = this->size();
const _CharT* __data = _M_data();
! for (; __pos + __n <= __size; ++__pos)
! if (traits_type::compare(__data + __pos, __s, __n) == 0)
! return __pos;
return npos;
}
*************** namespace std
*** 790,796 ****
rfind(const _CharT* __s, size_type __pos, size_type __n) const
{
__glibcxx_requires_string_len(__s, __n);
-
const size_type __size = this->size();
if (__n <= __size)
{
--- 765,770 ----
*************** namespace std
*** 814,826 ****
const size_type __size = this->size();
if (__size)
{
! size_t __xpos = __size - 1;
! if (__xpos > __pos)
! __xpos = __pos;
!
! for (++__xpos; __xpos-- > 0; )
! if (traits_type::eq(_M_data()[__xpos], __c))
! return __xpos;
}
return npos;
}
--- 788,797 ----
const size_type __size = this->size();
if (__size)
{
! __pos = std::min(size_type(__size - 1), __pos);
! for (++__pos; __pos-- > 0; )
! if (traits_type::eq(_M_data()[__pos], __c))
! return __pos;
}
return npos;
}
*************** namespace std
*** 831,837 ****
find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
{
__glibcxx_requires_string_len(__s, __n);
-
for (; __n && __pos < this->size(); ++__pos)
{
const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
--- 802,807 ----
*************** namespace std
*** 847,853 ****
find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
{
__glibcxx_requires_string_len(__s, __n);
-
size_type __size = this->size();
if (__size && __n)
{
--- 817,822 ----
*************** namespace std
*** 869,879 ****
find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
{
__glibcxx_requires_string_len(__s, __n);
!
! size_t __xpos = __pos;
! for (; __xpos < this->size(); ++__xpos)
! if (!traits_type::find(__s, __n, _M_data()[__xpos]))
! return __xpos;
return npos;
}
--- 838,846 ----
find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
{
__glibcxx_requires_string_len(__s, __n);
! for (; __pos < this->size(); ++__pos)
! if (!traits_type::find(__s, __n, _M_data()[__pos]))
! return __pos;
return npos;
}
*************** namespace std
*** 882,891 ****
basic_string<_CharT, _Traits, _Alloc>::
find_first_not_of(_CharT __c, size_type __pos) const
{
! size_t __xpos = __pos;
! for (; __xpos < this->size(); ++__xpos)
! if (!traits_type::eq(_M_data()[__xpos], __c))
! return __xpos;
return npos;
}
--- 849,857 ----
basic_string<_CharT, _Traits, _Alloc>::
find_first_not_of(_CharT __c, size_type __pos) const
{
! for (; __pos < this->size(); ++__pos)
! if (!traits_type::eq(_M_data()[__pos], __c))
! return __pos;
return npos;
}
*************** namespace std
*** 895,912 ****
find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
{
__glibcxx_requires_string_len(__s, __n);
!
! size_type __size = this->size();
if (__size)
{
! if (--__size > __pos)
! __size = __pos;
do
{
! if (!traits_type::find(__s, __n, _M_data()[__size]))
! return __size;
}
! while (__size--);
}
return npos;
}
--- 861,876 ----
find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
{
__glibcxx_requires_string_len(__s, __n);
! const size_type __size = this->size();
if (__size)
{
! __pos = std::min(size_type(__size - 1), __pos);
do
{
! if (!traits_type::find(__s, __n, _M_data()[__pos]))
! return __pos;
}
! while (__pos--);
}
return npos;
}
*************** namespace std
*** 916,932 ****
basic_string<_CharT, _Traits, _Alloc>::
find_last_not_of(_CharT __c, size_type __pos) const
{
! size_type __size = this->size();
if (__size)
! {
! if (--__size > __pos)
! __size = __pos;
do
{
! if (!traits_type::eq(_M_data()[__size], __c))
! return __size;
}
! while (__size--);
}
return npos;
}
--- 880,895 ----
basic_string<_CharT, _Traits, _Alloc>::
find_last_not_of(_CharT __c, size_type __pos) const
{
! const size_type __size = this->size();
if (__size)
! {
! __pos = std::min(size_type(__size - 1), __pos);
do
{
! if (!traits_type::eq(_M_data()[__pos], __c))
! return __pos;
}
! while (__pos--);
}
return npos;
}
diff -prN libstdc++-v3-orig/src/string-inst.cc libstdc++-v3/src/string-inst.cc
*** libstdc++-v3-orig/src/string-inst.cc Thu Jan 22 13:46:28 2004
--- libstdc++-v3/src/string-inst.cc Sat Jan 24 10:19:31 2004
*************** namespace std
*** 63,72 ****
template
S::basic_string(S::iterator, S::iterator, const allocator<C>&);
- template
- S&
- S::_M_replace(S::iterator, S::iterator, const C*, const C*);
-
template
C*
S::_S_construct(S::iterator, S::iterator,
--- 63,68 ----