basic_string.h (_M_set_length_and_sharable): New.
authorPaolo Carlini <pcarlini@suse.de>
Thu, 21 Oct 2004 22:01:41 +0000 (22:01 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Thu, 21 Oct 2004 22:01:41 +0000 (22:01 +0000)
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.

Co-Authored-By: Benjamin Kosnik <bkoz@redhat.com>
From-SVN: r89415

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/basic_string.h
libstdc++-v3/include/bits/basic_string.tcc

index f1d6e12395b620ea6707136b0efbdd0ab551839f..a9a8d2113649004b698659b1dadec9f9598c995e 100644 (file)
@@ -1,3 +1,11 @@
+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.
+
 2004-10-21  Paolo Carlini  <pcarlini@suse.de>
            Dhruv Matani  <dhruvbird@gmx.net>
            Nathan Myers  <ncm@cantrip.org>
index 676f1c534349aab0d3109d4d5d6bad4398831bbf..5471299045ea3b79ca5b6aca6eab5cd1c48049c0 100644 (file)
@@ -193,6 +193,15 @@ namespace std
        _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); }
index 612cde1c680d113859daaab0579146a2d454a246..583e447780d6804669e989ec569cc4a76831ba30 100644 (file)
@@ -124,8 +124,7 @@ namespace std
            __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 @@ namespace std
            __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 @@ namespace std
       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 @@ namespace std
             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 @@ namespace std
          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>
@@ -473,7 +465,6 @@ namespace std
     _S_create(size_type __capacity, size_type __old_capacity,
              const _Alloc& __alloc)
     {
-      typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
       // _GLIBCXX_RESOLVE_LIB_DEFECTS
       // 83.  String::npos vs. string::max_size()
       if (__capacity > _S_max_size)
@@ -535,8 +526,6 @@ namespace std
       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 +539,9 @@ namespace std
       _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();
     }