This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[patch] string::_M_set_lengt_and_sharable clean up
- From: Paolo Carlini <pcarlini at suse dot de>
- To: libstdc++ <libstdc++ at gcc dot gnu dot org>
- Date: Thu, 21 Oct 2004 20:51:27 +0200
- Subject: [patch] string::_M_set_lengt_and_sharable clean up
Hi,
this is clean up originally conceived by Benjamin, that I have
further extended to include _M_set_sharable and thus _S_create
too. Probably, will also make easier other rearrangments ...
Tested x86-linux for now, I will further test and commit.
Paolo.
//////////
2004-10-21 Paolo Carlini <pcarlini@suse.de>
Benjamin Kosnik <bkoz@redhat.com>
* include/bits/basic_string.h (_M_set_length_and_sharable): New.
* include/bits/basic_string.tcc (_S_construct, assign(const _CharT*,
size_type), _M_mutate, _M_clone): Use it.
(_Rep::_S_create): Don't set/call _M_length/_M_set_sharable here.
diff -urN libstdc++-v3-orig/include/bits/basic_string.h libstdc++-v3/include/bits/basic_string.h
--- libstdc++-v3-orig/include/bits/basic_string.h 2004-10-18 10:43:17.000000000 +0200
+++ libstdc++-v3/include/bits/basic_string.h 2004-10-21 20:17:00.000000000 +0200
@@ -193,6 +193,15 @@
_M_set_sharable()
{ this->_M_refcount = 0; }
+ void
+ _M_set_length_and_sharable(size_type __n)
+ {
+ this->_M_set_sharable(); // One reference.
+ this->_M_length = __n;
+ this->_M_refdata()[__n] = _S_terminal; // grrr. (per 21.3.4)
+ // You cannot leave those LWG people alone for a second.
+ }
+
_CharT*
_M_refdata() throw()
{ return reinterpret_cast<_CharT*>(this + 1); }
diff -urN libstdc++-v3-orig/include/bits/basic_string.tcc libstdc++-v3/include/bits/basic_string.tcc
--- libstdc++-v3-orig/include/bits/basic_string.tcc 2004-10-18 10:43:17.000000000 +0200
+++ libstdc++-v3/include/bits/basic_string.tcc 2004-10-21 20:18:22.000000000 +0200
@@ -124,8 +124,7 @@
__r->_M_destroy(__a);
__throw_exception_again;
}
- __r->_M_length = __len;
- __r->_M_refdata()[__len] = _Rep::_S_terminal; // grrr.
+ __r->_M_set_length_and_sharable(__len);
return __r->_M_refdata();
}
@@ -155,8 +154,7 @@
__r->_M_destroy(__a);
__throw_exception_again;
}
- __r->_M_length = __dnew;
- __r->_M_refdata()[__dnew] = _Rep::_S_terminal; // grrr.
+ __r->_M_set_length_and_sharable(__dnew);
return __r->_M_refdata();
}
@@ -174,8 +172,7 @@
if (__n)
traits_type::assign(__r->_M_refdata(), __n, __c);
- __r->_M_length = __n;
- __r->_M_refdata()[__n] = _Rep::_S_terminal; // grrr
+ __r->_M_set_length_and_sharable(__n);
return __r->_M_refdata();
}
@@ -278,9 +275,7 @@
traits_type::copy(_M_data(), __s, __n);
else if (__pos)
traits_type::move(_M_data(), __s, __n);
- _M_rep()->_M_set_sharable();
- _M_rep()->_M_length = __n;
- _M_data()[__n] = _Rep::_S_terminal; // grr.
+ _M_rep()->_M_set_length_and_sharable(__n);
return *this;
}
}
@@ -415,10 +410,7 @@
traits_type::move(_M_data() + __pos + __len2,
_M_data() + __pos + __len1, __how_much);
}
- _M_rep()->_M_set_sharable();
- _M_rep()->_M_length = __new_size;
- _M_data()[__new_size] = _Rep::_S_terminal; // grrr. (per 21.3.4)
- // You cannot leave those LWG people alone for a second.
+ _M_rep()->_M_set_length_and_sharable(__new_size);
}
template<typename _CharT, typename _Traits, typename _Alloc>
@@ -535,8 +527,6 @@
void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
_Rep *__p = new (__place) _Rep;
__p->_M_capacity = __capacity;
- __p->_M_set_sharable(); // One reference.
- __p->_M_length = 0;
return __p;
}
@@ -550,11 +540,9 @@
_Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
__alloc);
if (this->_M_length)
- traits_type::copy(__r->_M_refdata(), _M_refdata(),
- this->_M_length);
+ traits_type::copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
- __r->_M_length = this->_M_length;
- __r->_M_refdata()[this->_M_length] = _Rep::_S_terminal;
+ __r->_M_set_length_and_sharable(this->_M_length);
return __r->_M_refdata();
}