1// Reference-counted versatile string base -*- C++ -*-
2
3// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library.  This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING.  If not, write to the Free
18// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19// USA.
20
21// As a special exception, you may use this file as part of a free software
22// library without restriction.  Specifically, if other files instantiate
23// templates or use macros or inline functions from this file, or you compile
24// this file and link it with other files to produce an executable, this
25// file does not by itself cause the resulting executable to be covered by
26// the GNU General Public License.  This exception does not however
27// invalidate any other reasons why the executable file might be covered by
28// the GNU General Public License.
29
30/** @file ext/rc_string_base.h
31 *  This file is a GNU extension to the Standard C++ Library.
32 *  This is an internal header file, included by other library headers.
33 *  You should not attempt to use it directly.
34 */
35
36#ifndef _RC_STRING_BASE_H
37#define _RC_STRING_BASE_H 1
38
39#include <bits/atomicity.h>
40
41namespace __gnu_cxx
42{
43  /**
44   *  @if maint
45   *  Documentation?  What's that?
46   *  Nathan Myers <ncm@cantrip.org>.
47   *
48   *  A string looks like this:
49   *
50   *  @code
51   *                                        [_Rep]
52   *                                        _M_length
53   *   [__rc_string_base<char_type>]        _M_capacity
54   *   _M_dataplus                          _M_refcount
55   *   _M_p ---------------->               unnamed array of char_type
56   *  @endcode
57   *
58   *  Where the _M_p points to the first character in the string, and
59   *  you cast it to a pointer-to-_Rep and subtract 1 to get a
60   *  pointer to the header.
61   *
62   *  This approach has the enormous advantage that a string object
63   *  requires only one allocation.  All the ugliness is confined
64   *  within a single pair of inline functions, which each compile to
65   *  a single "add" instruction: _Rep::_M_refdata(), and
66   *  __rc_string_base::_M_rep(); and the allocation function which gets a
67   *  block of raw bytes and with room enough and constructs a _Rep
68   *  object at the front.
69   *
70   *  The reason you want _M_data pointing to the character array and
71   *  not the _Rep is so that the debugger can see the string
72   *  contents. (Probably we should add a non-inline member to get
73   *  the _Rep for the debugger to use, so users can check the actual
74   *  string length.)
75   *
76   *  Note that the _Rep object is a POD so that you can have a
77   *  static "empty string" _Rep object already "constructed" before
78   *  static constructors have run.  The reference-count encoding is
79   *  chosen so that a 0 indicates one reference, so you never try to
80   *  destroy the empty-string _Rep object.
81   *
82   *  All but the last paragraph is considered pretty conventional
83   *  for a C++ string implementation.
84   *  @endif
85  */
86 template<typename _CharT, typename _Traits, typename _Alloc>
87    class __rc_string_base
88    : protected __vstring_utility<_CharT, _Traits, _Alloc>
89    {
90    public:
91      typedef _Traits					    traits_type;
92      typedef typename _Traits::char_type		    value_type;
93      typedef _Alloc					    allocator_type;
94
95      typedef __vstring_utility<_CharT, _Traits, _Alloc>    _Util_Base;
96      typedef typename _Util_Base::_CharT_alloc_type        _CharT_alloc_type;
97      typedef typename _CharT_alloc_type::size_type	    size_type;
98
99    private:
100      // _Rep: string representation
101      //   Invariants:
102      //   1. String really contains _M_length + 1 characters: due to 21.3.4
103      //      must be kept null-terminated.
104      //   2. _M_capacity >= _M_length
105      //      Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
106      //   3. _M_refcount has three states:
107      //      -1: leaked, one reference, no ref-copies allowed, non-const.
108      //       0: one reference, non-const.
109      //     n>0: n + 1 references, operations require a lock, const.
110      //   4. All fields == 0 is an empty string, given the extra storage
111      //      beyond-the-end for a null terminator; thus, the shared
112      //      empty string representation needs no constructor.
113      struct _Rep
114      {
115	union
116	{
117	  struct
118	  {
119	    size_type	    _M_length;
120	    size_type	    _M_capacity;
121	    _Atomic_word    _M_refcount;
122	  }                 _M_info;
123
124	  // Only for alignment purposes.
125	  _CharT            _M_align;
126	};
127
128	typedef typename _Alloc::template rebind<_Rep>::other _Rep_alloc_type;
129
130 	_CharT*
131	_M_refdata() throw()
132	{ return reinterpret_cast<_CharT*>(this + 1); }
133
134	_CharT*
135	_M_refcopy() throw()
136	{
137	  __atomic_add(&_M_info._M_refcount, 1);
138	  return _M_refdata();
139	}  // XXX MT
140
141	void
142	_M_set_length(size_type __n)
143	{
144	  _M_info._M_refcount = 0;  // One reference.
145	  _M_info._M_length = __n;
146	  // grrr. (per 21.3.4)
147	  // You cannot leave those LWG people alone for a second.
148	  traits_type::assign(_M_refdata()[__n], _CharT());
149	}
150
151	// Create & Destroy
152	static _Rep*
153	_S_create(size_type, size_type, const _Alloc&);
154
155	void
156	_M_destroy(const _Alloc&) throw();
157
158	_CharT*
159	_M_clone(const _Alloc&, size_type __res = 0);
160      };
161
162      struct _Rep_empty
163      : public _Rep
164      {
165	_CharT              _M_terminal;
166      };
167
168      static _Rep_empty     _S_empty_rep;
169
170      // The maximum number of individual char_type elements of an
171      // individual string is determined by _S_max_size. This is the
172      // value that will be returned by max_size().  (Whereas npos
173      // is the maximum number of bytes the allocator can allocate.)
174      // If one was to divvy up the theoretical largest size string,
175      // with a terminating character and m _CharT elements, it'd
176      // look like this:
177      // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
178      //        + sizeof(_Rep) - 1
179      // (NB: last two terms for rounding reasons, see _M_create below)
180      // Solving for m:
181      // m = ((npos - 2 * sizeof(_Rep) + 1) / sizeof(_CharT)) - 1
182      // In addition, this implementation halfs this amount.
183      enum { _S_max_size = (((static_cast<size_type>(-1) - 2 * sizeof(_Rep)
184			      + 1) / sizeof(_CharT)) - 1) / 2 };
185
186      // Data Member (private):
187      mutable typename _Util_Base::template _Alloc_hider<_Alloc>  _M_dataplus;
188
189      void
190      _M_data(_CharT* __p)
191      { _M_dataplus._M_p = __p; }
192
193      _Rep*
194      _M_rep() const
195      { return &((reinterpret_cast<_Rep*>(_M_data()))[-1]); }
196
197      _CharT*
198      _M_grab(const _Alloc& __alloc) const
199      {
200	return (!_M_is_leaked() && _M_get_allocator() == __alloc)
201	        ? _M_rep()->_M_refcopy() : _M_rep()->_M_clone(__alloc);
202      }
203
204      void
205      _M_dispose()
206      {
207	if (__exchange_and_add(&_M_rep()->_M_info._M_refcount, -1) <= 0)
208	  _M_rep()->_M_destroy(_M_get_allocator());
209      }  // XXX MT
210
211      bool
212      _M_is_leaked() const
213      { return _M_rep()->_M_info._M_refcount < 0; }
214
215      void
216      _M_set_sharable()
217      { _M_rep()->_M_info._M_refcount = 0; }
218
219      void
220      _M_leak_hard();
221
222      // _S_construct_aux is used to implement the 21.3.1 para 15 which
223      // requires special behaviour if _InIterator is an integral type
224      template<typename _InIterator>
225        static _CharT*
226        _S_construct_aux(_InIterator __beg, _InIterator __end,
227			 const _Alloc& __a, __false_type)
228	{
229          typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
230          return _S_construct(__beg, __end, __a, _Tag());
231	}
232
233      template<typename _InIterator>
234        static _CharT*
235        _S_construct_aux(_InIterator __beg, _InIterator __end,
236			 const _Alloc& __a, __true_type)
237	{ return _S_construct(static_cast<size_type>(__beg),
238			      static_cast<value_type>(__end), __a); }
239
240      template<typename _InIterator>
241        static _CharT*
242        _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
243	{
244	  typedef typename std::__is_integer<_InIterator>::__type _Integral;
245	  return _S_construct_aux(__beg, __end, __a, _Integral());
246        }
247
248      // For Input Iterators, used in istreambuf_iterators, etc.
249      template<typename _InIterator>
250        static _CharT*
251         _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
252		      std::input_iterator_tag);
253
254      // For forward_iterators up to random_access_iterators, used for
255      // string::iterator, _CharT*, etc.
256      template<typename _FwdIterator>
257        static _CharT*
258        _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
259		     std::forward_iterator_tag);
260
261      static _CharT*
262      _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
263
264    public:
265      size_type
266      _M_max_size() const
267      { return size_type(_S_max_size); }
268
269      _CharT*
270      _M_data() const
271      { return _M_dataplus._M_p; }
272
273      size_type
274      _M_length() const
275      { return _M_rep()->_M_info._M_length; }
276
277      size_type
278      _M_capacity() const
279      { return _M_rep()->_M_info._M_capacity; }
280
281      bool
282      _M_is_shared() const
283      { return _M_rep()->_M_info._M_refcount > 0; }
284
285      void
286      _M_set_leaked()
287      { _M_rep()->_M_info._M_refcount = -1; }
288
289      void
290      _M_leak()    // for use in begin() & non-const op[]
291      {
292	if (!_M_is_leaked())
293	  _M_leak_hard();
294      }
295
296      void
297      _M_set_length(size_type __n)
298      { _M_rep()->_M_set_length(__n); }
299
300      __rc_string_base()
301      : _M_dataplus(_Alloc(), _S_empty_rep._M_refcopy()) { }
302
303      __rc_string_base(const _Alloc& __a);
304
305      __rc_string_base(const __rc_string_base& __rcs);
306
307      __rc_string_base(size_type __n, _CharT __c, const _Alloc& __a);
308
309      template<typename _InputIterator>
310        __rc_string_base(_InputIterator __beg, _InputIterator __end,
311			 const _Alloc& __a);
312
313      ~__rc_string_base()
314      { _M_dispose(); }
315
316      allocator_type&
317      _M_get_allocator()
318      { return _M_dataplus; }
319
320      const allocator_type&
321      _M_get_allocator() const
322      { return _M_dataplus; }
323
324      void
325      _M_swap(__rc_string_base& __rcs);
326
327      void
328      _M_assign(const __rc_string_base& __rcs);
329
330      void
331      _M_reserve(size_type __res);
332
333      void
334      _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
335		size_type __len2);
336
337      void
338      _M_erase(size_type __pos, size_type __n);
339
340      void
341      _M_clear()
342      { _M_erase(size_type(0), _M_length()); }
343
344      bool
345      _M_compare(const __rc_string_base&) const
346      { return false; }
347    };
348
349  template<typename _CharT, typename _Traits, typename _Alloc>
350    typename __rc_string_base<_CharT, _Traits, _Alloc>::_Rep_empty
351    __rc_string_base<_CharT, _Traits, _Alloc>::_S_empty_rep;
352
353  template<typename _CharT, typename _Traits, typename _Alloc>
354    typename __rc_string_base<_CharT, _Traits, _Alloc>::_Rep*
355    __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
356    _S_create(size_type __capacity, size_type __old_capacity,
357	      const _Alloc& __alloc)
358    {
359      // _GLIBCXX_RESOLVE_LIB_DEFECTS
360      // 83.  String::npos vs. string::max_size()
361      if (__capacity > size_type(_S_max_size))
362	std::__throw_length_error(__N("__rc_string_base::_Rep::_S_create"));
363
364      // The standard places no restriction on allocating more memory
365      // than is strictly needed within this layer at the moment or as
366      // requested by an explicit application call to reserve().
367
368      // Many malloc implementations perform quite poorly when an
369      // application attempts to allocate memory in a stepwise fashion
370      // growing each allocation size by only 1 char.  Additionally,
371      // it makes little sense to allocate less linear memory than the
372      // natural blocking size of the malloc implementation.
373      // Unfortunately, we would need a somewhat low-level calculation
374      // with tuned parameters to get this perfect for any particular
375      // malloc implementation.  Fortunately, generalizations about
376      // common features seen among implementations seems to suffice.
377
378      // __pagesize need not match the actual VM page size for good
379      // results in practice, thus we pick a common value on the low
380      // side.  __malloc_header_size is an estimate of the amount of
381      // overhead per memory allocation (in practice seen N * sizeof
382      // (void*) where N is 0, 2 or 4).  According to folklore,
383      // picking this value on the high side is better than
384      // low-balling it (especially when this algorithm is used with
385      // malloc implementations that allocate memory blocks rounded up
386      // to a size which is a power of 2).
387      const size_type __pagesize = 4096;
388      const size_type __malloc_header_size = 4 * sizeof(void*);
389
390      // The below implements an exponential growth policy, necessary to
391      // meet amortized linear time requirements of the library: see
392      // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
393      if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
394	{
395	  __capacity = 2 * __old_capacity;
396	  // Never allocate a string bigger than _S_max_size.
397	  if (__capacity > size_type(_S_max_size))
398	    __capacity = size_type(_S_max_size);
399	}
400
401      // NB: Need an array of char_type[__capacity], plus a terminating
402      // null char_type() element, plus enough for the _Rep data structure,
403      // plus sizeof(_Rep) - 1 to upper round to a size multiple of
404      // sizeof(_Rep).
405      // Whew. Seemingly so needy, yet so elemental.
406      size_type __size = ((__capacity + 1) * sizeof(_CharT)
407			  + 2 * sizeof(_Rep) - 1);
408
409      const size_type __adj_size = __size + __malloc_header_size;
410      if (__adj_size > __pagesize && __capacity > __old_capacity)
411	{
412	  const size_type __extra = __pagesize - __adj_size % __pagesize;
413	  __capacity += __extra / sizeof(_CharT);
414	  if (__capacity > size_type(_S_max_size))
415	    __capacity = size_type(_S_max_size);
416	  __size = (__capacity + 1) * sizeof(_CharT) + 2 * sizeof(_Rep) - 1;
417	}
418
419      // NB: Might throw, but no worries about a leak, mate: _Rep()
420      // does not throw.
421      _Rep* __place = _Rep_alloc_type(__alloc).allocate(__size / sizeof(_Rep));
422      _Rep* __p = new (__place) _Rep;
423      __p->_M_info._M_capacity = __capacity;
424      return __p;
425    }
426
427  template<typename _CharT, typename _Traits, typename _Alloc>
428    void
429    __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
430    _M_destroy(const _Alloc& __a) throw ()
431    {
432      const size_type __size = ((_M_info._M_capacity + 1) * sizeof(_CharT)
433				+ 2 * sizeof(_Rep) - 1);
434      _Rep_alloc_type(__a).deallocate(this, __size / sizeof(_Rep));
435    }
436
437  template<typename _CharT, typename _Traits, typename _Alloc>
438    _CharT*
439    __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
440    _M_clone(const _Alloc& __alloc, size_type __res)
441    {
442      // Requested capacity of the clone.
443      const size_type __requested_cap = _M_info._M_length + __res;
444      _Rep* __r = _Rep::_S_create(__requested_cap, _M_info._M_capacity,
445				  __alloc);
446
447      if (_M_info._M_length)
448	_S_copy(__r->_M_refdata(), _M_refdata(), _M_info._M_length);
449
450      __r->_M_set_length(_M_info._M_length);
451      return __r->_M_refdata();
452    }
453
454  template<typename _CharT, typename _Traits, typename _Alloc>
455    __rc_string_base<_CharT, _Traits, _Alloc>::
456    __rc_string_base(const _Alloc& __a)
457    : _M_dataplus(__a, _S_construct(size_type(), _CharT(), __a)) { }
458
459  template<typename _CharT, typename _Traits, typename _Alloc>
460    __rc_string_base<_CharT, _Traits, _Alloc>::
461    __rc_string_base(const __rc_string_base& __rcs)
462    : _M_dataplus(__rcs._M_get_allocator(),
463		  __rcs._M_grab(__rcs._M_get_allocator())) { }
464
465  template<typename _CharT, typename _Traits, typename _Alloc>
466    __rc_string_base<_CharT, _Traits, _Alloc>::
467    __rc_string_base(size_type __n, _CharT __c, const _Alloc& __a)
468    : _M_dataplus(__a, _S_construct(__n, __c, __a)) { }
469
470  template<typename _CharT, typename _Traits, typename _Alloc>
471    template<typename _InputIterator>
472    __rc_string_base<_CharT, _Traits, _Alloc>::
473    __rc_string_base(_InputIterator __beg, _InputIterator __end,
474		     const _Alloc& __a)
475    : _M_dataplus(__a, _S_construct(__beg, __end, __a)) { }
476
477  template<typename _CharT, typename _Traits, typename _Alloc>
478    void
479    __rc_string_base<_CharT, _Traits, _Alloc>::
480    _M_leak_hard()
481    {
482      if (_M_is_shared())
483	_M_erase(0, 0);
484      _M_set_leaked();
485    }
486
487  // NB: This is the special case for Input Iterators, used in
488  // istreambuf_iterators, etc.
489  // Input Iterators have a cost structure very different from
490  // pointers, calling for a different coding style.
491  template<typename _CharT, typename _Traits, typename _Alloc>
492    template<typename _InIterator>
493      _CharT*
494      __rc_string_base<_CharT, _Traits, _Alloc>::
495      _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
496		   std::input_iterator_tag)
497      {
498	if (__beg == __end && __a == _Alloc())
499	  return _S_empty_rep._M_refcopy();
500
501	// Avoid reallocation for common case.
502	_CharT __buf[128];
503	size_type __len = 0;
504	while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
505	  {
506	    __buf[__len++] = *__beg;
507	    ++__beg;
508	  }
509	_Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
510	_S_copy(__r->_M_refdata(), __buf, __len);
511	try
512	  {
513	    while (__beg != __end)
514	      {
515		if (__len == __r->_M_info._M_capacity)
516		  {
517		    // Allocate more space.
518		    _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
519		    _S_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
520		    __r->_M_destroy(__a);
521		    __r = __another;
522		  }
523		__r->_M_refdata()[__len++] = *__beg;
524		++__beg;
525	      }
526	  }
527	catch(...)
528	  {
529	    __r->_M_destroy(__a);
530	    __throw_exception_again;
531	  }
532	__r->_M_set_length(__len);
533	return __r->_M_refdata();
534      }
535
536  template<typename _CharT, typename _Traits, typename _Alloc>
537    template<typename _InIterator>
538      _CharT*
539      __rc_string_base<_CharT, _Traits, _Alloc>::
540      _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
541		   std::forward_iterator_tag)
542      {
543	if (__beg == __end && __a == _Alloc())
544	  return _S_empty_rep._M_refcopy();
545
546	// NB: Not required, but considered best practice.
547	if (__builtin_expect(_S_is_null_pointer(__beg) && __beg != __end, 0))
548	  std::__throw_logic_error(__N("__rc_string_base::"
549				       "_S_construct NULL not valid"));
550
551	const size_type __dnew = static_cast<size_type>(std::distance(__beg,
552								      __end));
553	// Check for out_of_range and length_error exceptions.
554	_Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
555	try
556	  { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
557	catch(...)
558	  {
559	    __r->_M_destroy(__a);
560	    __throw_exception_again;
561	  }
562	__r->_M_set_length(__dnew);
563	return __r->_M_refdata();
564      }
565
566  template<typename _CharT, typename _Traits, typename _Alloc>
567    _CharT*
568    __rc_string_base<_CharT, _Traits, _Alloc>::
569    _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
570    {
571      if (__n == 0 && __a == _Alloc())
572	return _S_empty_rep._M_refcopy();
573
574      // Check for out_of_range and length_error exceptions.
575      _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
576      if (__n)
577	_S_assign(__r->_M_refdata(), __n, __c);
578
579      __r->_M_set_length(__n);
580      return __r->_M_refdata();
581    }
582
583  template<typename _CharT, typename _Traits, typename _Alloc>
584    void
585    __rc_string_base<_CharT, _Traits, _Alloc>::
586    _M_swap(__rc_string_base& __rcs)
587    {
588      if (_M_is_leaked())
589	_M_set_sharable();
590      if (__rcs._M_is_leaked())
591	__rcs._M_set_sharable();
592
593      _CharT* __tmp = _M_data();
594      _M_data(__rcs._M_data());
595      __rcs._M_data(__tmp);
596
597      // NB: Implement Option 3 of DR 431 (see N1599).
598      std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(),
599						  __rcs._M_get_allocator());
600    }
601
602  template<typename _CharT, typename _Traits, typename _Alloc>
603    void
604    __rc_string_base<_CharT, _Traits, _Alloc>::
605    _M_assign(const __rc_string_base& __rcs)
606    {
607      if (_M_rep() != __rcs._M_rep())
608	{
609	  _CharT* __tmp = __rcs._M_grab(_M_get_allocator());
610	  _M_dispose();
611	  _M_data(__tmp);
612	}
613    }
614
615  template<typename _CharT, typename _Traits, typename _Alloc>
616    void
617    __rc_string_base<_CharT, _Traits, _Alloc>::
618    _M_reserve(size_type __res)
619    {
620      // Make sure we don't shrink below the current size.
621      if (__res < _M_length())
622	__res = _M_length();
623
624      if (__res != _M_capacity() || _M_is_shared())
625	{
626	  _CharT* __tmp = _M_rep()->_M_clone(_M_get_allocator(),
627					     __res - _M_length());
628	  _M_dispose();
629	  _M_data(__tmp);
630	}
631    }
632
633  template<typename _CharT, typename _Traits, typename _Alloc>
634    void
635    __rc_string_base<_CharT, _Traits, _Alloc>::
636    _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
637	      size_type __len2)
638    {
639      const size_type __how_much = _M_length() - __pos - __len1;
640
641      _Rep* __r = _Rep::_S_create(_M_length() + __len2 - __len1,
642				  _M_capacity(), _M_get_allocator());
643
644      if (__pos)
645	_S_copy(__r->_M_refdata(), _M_data(), __pos);
646      if (__s && __len2)
647	_S_copy(__r->_M_refdata() + __pos, __s, __len2);
648      if (__how_much)
649	_S_copy(__r->_M_refdata() + __pos + __len2,
650		_M_data() + __pos + __len1, __how_much);
651
652      _M_dispose();
653      _M_data(__r->_M_refdata());
654    }
655
656  template<typename _CharT, typename _Traits, typename _Alloc>
657    void
658    __rc_string_base<_CharT, _Traits, _Alloc>::
659    _M_erase(size_type __pos, size_type __n)
660    {
661      const size_type __new_size = _M_length() - __n;
662      const size_type __how_much = _M_length() - __pos - __n;
663
664      if (_M_is_shared())
665	{
666	  // Must reallocate.
667	  _Rep* __r = _Rep::_S_create(__new_size, _M_capacity(),
668				      _M_get_allocator());
669
670	  if (__pos)
671	    _S_copy(__r->_M_refdata(), _M_data(), __pos);
672	  if (__how_much)
673	    _S_copy(__r->_M_refdata() + __pos,
674		    _M_data() + __pos + __n, __how_much);
675
676	  _M_dispose();
677	  _M_data(__r->_M_refdata());
678	}
679      else if (__how_much && __n)
680	{
681	  // Work in-place.
682	  _S_move(_M_data() + __pos,
683		  _M_data() + __pos + __n, __how_much);
684	}
685
686      _M_rep()->_M_set_length(__new_size);
687    }
688
689  template<>
690    inline bool
691    __rc_string_base<char, std::char_traits<char>,
692		     std::allocator<char> >::
693    _M_compare(const __rc_string_base& __rcs) const
694    {
695      if (_M_rep() == __rcs._M_rep())
696	return true;
697      return false;
698    }
699
700  template<>
701    inline bool
702    __rc_string_base<wchar_t, std::char_traits<wchar_t>,
703		     std::allocator<wchar_t> >::
704    _M_compare(const __rc_string_base& __rcs) const
705    {
706      if (_M_rep() == __rcs._M_rep())
707	return true;
708      return false;
709    }
710} // namespace __gnu_cxx
711
712#endif /* _RC_STRING_BASE_H */
713