Merge from vendor branch GCC:
[dragonfly.git] / contrib / gcc-4.1 / libstdc++-v3 / include / ext / rc_string_base.h
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
41 namespace __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       // Solving for m:
179       // m = ((npos - sizeof(_Rep)) / sizeof(_CharT)) - 1
180       // In addition, this implementation quarters this amount.
181       enum { _S_max_size = (((static_cast<size_type>(-1) - sizeof(_Rep))
182                              / sizeof(_CharT)) - 1) / 4 };
183
184       // Data Member (private):
185       mutable typename _Util_Base::template _Alloc_hider<_Alloc>  _M_dataplus;
186
187       void
188       _M_data(_CharT* __p)
189       { _M_dataplus._M_p = __p; }
190
191       _Rep*
192       _M_rep() const
193       { return &((reinterpret_cast<_Rep*>(_M_data()))[-1]); }
194
195       _CharT*
196       _M_grab(const _Alloc& __alloc) const
197       {
198         return (!_M_is_leaked() && _M_get_allocator() == __alloc)
199                 ? _M_rep()->_M_refcopy() : _M_rep()->_M_clone(__alloc);
200       }
201
202       void
203       _M_dispose()
204       {
205         if (__exchange_and_add(&_M_rep()->_M_info._M_refcount, -1) <= 0)
206           _M_rep()->_M_destroy(_M_get_allocator());
207       }  // XXX MT
208
209       bool
210       _M_is_leaked() const
211       { return _M_rep()->_M_info._M_refcount < 0; }
212
213       void
214       _M_set_sharable()
215       { _M_rep()->_M_info._M_refcount = 0; }
216
217       void
218       _M_leak_hard();
219
220       // _S_construct_aux is used to implement the 21.3.1 para 15 which
221       // requires special behaviour if _InIterator is an integral type
222       template<typename _InIterator>
223         static _CharT*
224         _S_construct_aux(_InIterator __beg, _InIterator __end,
225                          const _Alloc& __a, __false_type)
226         {
227           typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
228           return _S_construct(__beg, __end, __a, _Tag());
229         }
230
231       template<typename _InIterator>
232         static _CharT*
233         _S_construct_aux(_InIterator __beg, _InIterator __end,
234                          const _Alloc& __a, __true_type)
235         { return _S_construct(static_cast<size_type>(__beg),
236                               static_cast<value_type>(__end), __a); }
237
238       template<typename _InIterator>
239         static _CharT*
240         _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
241         {
242           typedef typename std::__is_integer<_InIterator>::__type _Integral;
243           return _S_construct_aux(__beg, __end, __a, _Integral());
244         }
245
246       // For Input Iterators, used in istreambuf_iterators, etc.
247       template<typename _InIterator>
248         static _CharT*
249          _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
250                       std::input_iterator_tag);
251       
252       // For forward_iterators up to random_access_iterators, used for
253       // string::iterator, _CharT*, etc.
254       template<typename _FwdIterator>
255         static _CharT*
256         _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
257                      std::forward_iterator_tag);
258
259       static _CharT*
260       _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
261
262     public:
263       size_type
264       _M_max_size() const
265       { return size_type(_S_max_size); }
266
267       _CharT*
268       _M_data() const
269       { return _M_dataplus._M_p; }
270
271       size_type
272       _M_length() const
273       { return _M_rep()->_M_info._M_length; }
274
275       size_type
276       _M_capacity() const
277       { return _M_rep()->_M_info._M_capacity; }
278
279       bool
280       _M_is_shared() const
281       { return _M_rep()->_M_info._M_refcount > 0; }
282
283       void
284       _M_set_leaked()
285       { _M_rep()->_M_info._M_refcount = -1; }
286
287       void
288       _M_leak()    // for use in begin() & non-const op[]
289       {
290         if (!_M_is_leaked())
291           _M_leak_hard();
292       }
293
294       void
295       _M_set_length(size_type __n)
296       { _M_rep()->_M_set_length(__n); }
297
298       __rc_string_base()
299       : _M_dataplus(_Alloc(), _S_empty_rep._M_refcopy()) { }
300
301       __rc_string_base(const _Alloc& __a);
302
303       __rc_string_base(const __rc_string_base& __rcs);
304
305       __rc_string_base(size_type __n, _CharT __c, const _Alloc& __a);
306
307       template<typename _InputIterator>
308         __rc_string_base(_InputIterator __beg, _InputIterator __end,
309                          const _Alloc& __a);
310
311       ~__rc_string_base()
312       { _M_dispose(); }      
313
314       allocator_type&
315       _M_get_allocator()
316       { return _M_dataplus; }
317
318       const allocator_type&
319       _M_get_allocator() const
320       { return _M_dataplus; }
321
322       void
323       _M_swap(__rc_string_base& __rcs);
324
325       void
326       _M_assign(const __rc_string_base& __rcs);
327
328       void
329       _M_reserve(size_type __res);
330
331       void
332       _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
333                 size_type __len2);
334       
335       void
336       _M_erase(size_type __pos, size_type __n);
337
338       bool
339       _M_compare(const __rc_string_base&) const
340       { return false; }
341     };
342
343   template<typename _CharT, typename _Traits, typename _Alloc>
344     typename __rc_string_base<_CharT, _Traits, _Alloc>::_Rep_empty
345     __rc_string_base<_CharT, _Traits, _Alloc>::_S_empty_rep;
346
347   template<typename _CharT, typename _Traits, typename _Alloc>
348     typename __rc_string_base<_CharT, _Traits, _Alloc>::_Rep*
349     __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
350     _S_create(size_type __capacity, size_type __old_capacity,
351               const _Alloc& __alloc)
352     {
353       // _GLIBCXX_RESOLVE_LIB_DEFECTS
354       // 83.  String::npos vs. string::max_size()
355       if (__capacity > size_type(_S_max_size))
356         std::__throw_length_error(__N("__rc_string_base::_Rep::_S_create"));
357
358       // The standard places no restriction on allocating more memory
359       // than is strictly needed within this layer at the moment or as
360       // requested by an explicit application call to reserve().
361
362       // Many malloc implementations perform quite poorly when an
363       // application attempts to allocate memory in a stepwise fashion
364       // growing each allocation size by only 1 char.  Additionally,
365       // it makes little sense to allocate less linear memory than the
366       // natural blocking size of the malloc implementation.
367       // Unfortunately, we would need a somewhat low-level calculation
368       // with tuned parameters to get this perfect for any particular
369       // malloc implementation.  Fortunately, generalizations about
370       // common features seen among implementations seems to suffice.
371
372       // __pagesize need not match the actual VM page size for good
373       // results in practice, thus we pick a common value on the low
374       // side.  __malloc_header_size is an estimate of the amount of
375       // overhead per memory allocation (in practice seen N * sizeof
376       // (void*) where N is 0, 2 or 4).  According to folklore,
377       // picking this value on the high side is better than
378       // low-balling it (especially when this algorithm is used with
379       // malloc implementations that allocate memory blocks rounded up
380       // to a size which is a power of 2).
381       const size_type __pagesize = 4096;
382       const size_type __malloc_header_size = 4 * sizeof(void*);
383
384       // The below implements an exponential growth policy, necessary to
385       // meet amortized linear time requirements of the library: see
386       // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
387       if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
388         __capacity = 2 * __old_capacity;
389
390       // NB: Need an array of char_type[__capacity], plus a terminating
391       // null char_type() element, plus enough for the _Rep data structure,
392       // plus sizeof(_Rep) - 1 to upper round to a size multiple of
393       // sizeof(_Rep).
394       // Whew. Seemingly so needy, yet so elemental.
395       size_type __size = ((__capacity + 1) * sizeof(_CharT)
396                           + 2 * sizeof(_Rep) - 1);
397
398       const size_type __adj_size = __size + __malloc_header_size;
399       if (__adj_size > __pagesize && __capacity > __old_capacity)
400         {
401           const size_type __extra = __pagesize - __adj_size % __pagesize;
402           __capacity += __extra / sizeof(_CharT);
403           // Never allocate a string bigger than _S_max_size.
404           if (__capacity > size_type(_S_max_size))
405             __capacity = size_type(_S_max_size);
406           __size = (__capacity + 1) * sizeof(_CharT) + 2 * sizeof(_Rep) - 1;
407         }
408
409       // NB: Might throw, but no worries about a leak, mate: _Rep()
410       // does not throw.
411       _Rep* __place = _Rep_alloc_type(__alloc).allocate(__size / sizeof(_Rep));
412       _Rep* __p = new (__place) _Rep;
413       __p->_M_info._M_capacity = __capacity;
414       return __p;
415     }
416
417   template<typename _CharT, typename _Traits, typename _Alloc>
418     void
419     __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
420     _M_destroy(const _Alloc& __a) throw ()
421     {
422       const size_type __size = ((_M_info._M_capacity + 1) * sizeof(_CharT)
423                                 + 2 * sizeof(_Rep) - 1);
424       _Rep_alloc_type(__a).deallocate(this, __size / sizeof(_Rep));
425     }
426
427   template<typename _CharT, typename _Traits, typename _Alloc>
428     _CharT*
429     __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
430     _M_clone(const _Alloc& __alloc, size_type __res)
431     {
432       // Requested capacity of the clone.
433       const size_type __requested_cap = _M_info._M_length + __res;
434       _Rep* __r = _Rep::_S_create(__requested_cap, _M_info._M_capacity,
435                                   __alloc);
436
437       if (_M_info._M_length)
438         _S_copy(__r->_M_refdata(), _M_refdata(), _M_info._M_length);
439
440       __r->_M_set_length(_M_info._M_length);
441       return __r->_M_refdata();
442     }
443
444   template<typename _CharT, typename _Traits, typename _Alloc>
445     __rc_string_base<_CharT, _Traits, _Alloc>::
446     __rc_string_base(const _Alloc& __a)
447     : _M_dataplus(__a, _S_construct(size_type(), _CharT(), __a)) { }
448
449   template<typename _CharT, typename _Traits, typename _Alloc>
450     __rc_string_base<_CharT, _Traits, _Alloc>::
451     __rc_string_base(const __rc_string_base& __rcs)
452     : _M_dataplus(__rcs._M_get_allocator(),
453                   __rcs._M_grab(__rcs._M_get_allocator())) { }
454
455   template<typename _CharT, typename _Traits, typename _Alloc>
456     __rc_string_base<_CharT, _Traits, _Alloc>::
457     __rc_string_base(size_type __n, _CharT __c, const _Alloc& __a)
458     : _M_dataplus(__a, _S_construct(__n, __c, __a)) { }
459
460   template<typename _CharT, typename _Traits, typename _Alloc>
461     template<typename _InputIterator>
462     __rc_string_base<_CharT, _Traits, _Alloc>::
463     __rc_string_base(_InputIterator __beg, _InputIterator __end,
464                      const _Alloc& __a)
465     : _M_dataplus(__a, _S_construct(__beg, __end, __a)) { }
466
467   template<typename _CharT, typename _Traits, typename _Alloc>
468     void
469     __rc_string_base<_CharT, _Traits, _Alloc>::
470     _M_leak_hard()
471     {
472       if (_M_is_shared())
473         _M_erase(0, 0);
474       _M_set_leaked();
475     }
476
477   // NB: This is the special case for Input Iterators, used in
478   // istreambuf_iterators, etc.
479   // Input Iterators have a cost structure very different from
480   // pointers, calling for a different coding style.
481   template<typename _CharT, typename _Traits, typename _Alloc>
482     template<typename _InIterator>
483       _CharT*
484       __rc_string_base<_CharT, _Traits, _Alloc>::
485       _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
486                    std::input_iterator_tag)
487       {
488         if (__beg == __end && __a == _Alloc())
489           return _S_empty_rep._M_refcopy();
490
491         // Avoid reallocation for common case.
492         _CharT __buf[128];
493         size_type __len = 0;
494         while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
495           {
496             __buf[__len++] = *__beg;
497             ++__beg;
498           }
499         _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
500         _S_copy(__r->_M_refdata(), __buf, __len);
501         try
502           {
503             while (__beg != __end)
504               {
505                 if (__len == __r->_M_info._M_capacity)
506                   {
507                     // Allocate more space.
508                     _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
509                     _S_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
510                     __r->_M_destroy(__a);
511                     __r = __another;
512                   }
513                 __r->_M_refdata()[__len++] = *__beg;
514                 ++__beg;
515               }
516           }
517         catch(...)
518           {
519             __r->_M_destroy(__a);
520             __throw_exception_again;
521           }
522         __r->_M_set_length(__len);
523         return __r->_M_refdata();
524       }
525
526   template<typename _CharT, typename _Traits, typename _Alloc>
527     template<typename _InIterator>
528       _CharT*
529       __rc_string_base<_CharT, _Traits, _Alloc>::
530       _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
531                    std::forward_iterator_tag)
532       {
533         if (__beg == __end && __a == _Alloc())
534           return _S_empty_rep._M_refcopy();
535
536         // NB: Not required, but considered best practice.
537         if (__builtin_expect(_S_is_null_pointer(__beg) && __beg != __end, 0))
538           std::__throw_logic_error(__N("__rc_string_base::"
539                                        "_S_construct NULL not valid"));
540
541         const size_type __dnew = static_cast<size_type>(std::distance(__beg,
542                                                                       __end));
543         // Check for out_of_range and length_error exceptions.
544         _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
545         try
546           { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
547         catch(...)
548           {
549             __r->_M_destroy(__a);
550             __throw_exception_again;
551           }
552         __r->_M_set_length(__dnew);
553         return __r->_M_refdata();
554       }
555
556   template<typename _CharT, typename _Traits, typename _Alloc>
557     _CharT*
558     __rc_string_base<_CharT, _Traits, _Alloc>::
559     _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
560     {
561       if (__n == 0 && __a == _Alloc())
562         return _S_empty_rep._M_refcopy();
563
564       // Check for out_of_range and length_error exceptions.
565       _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
566       if (__n)
567         _S_assign(__r->_M_refdata(), __n, __c);
568
569       __r->_M_set_length(__n);
570       return __r->_M_refdata();
571     }
572
573   template<typename _CharT, typename _Traits, typename _Alloc>
574     void
575     __rc_string_base<_CharT, _Traits, _Alloc>::
576     _M_swap(__rc_string_base& __rcs)
577     {
578       if (_M_is_leaked())
579         _M_set_sharable();
580       if (__rcs._M_is_leaked())
581         __rcs._M_set_sharable();
582       
583       _CharT* __tmp = _M_data();
584       _M_data(__rcs._M_data());
585       __rcs._M_data(__tmp);
586       
587       // NB: Implement Option 3 of DR 431 (see N1599).
588       std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(),
589                                                   __rcs._M_get_allocator());
590     } 
591
592   template<typename _CharT, typename _Traits, typename _Alloc>
593     void
594     __rc_string_base<_CharT, _Traits, _Alloc>::
595     _M_assign(const __rc_string_base& __rcs)
596     {
597       if (_M_rep() != __rcs._M_rep())
598         {
599           _CharT* __tmp = __rcs._M_grab(_M_get_allocator());
600           _M_dispose();
601           _M_data(__tmp);
602         }
603     }
604
605   template<typename _CharT, typename _Traits, typename _Alloc>
606     void
607     __rc_string_base<_CharT, _Traits, _Alloc>::
608     _M_reserve(size_type __res)
609     {
610       // Make sure we don't shrink below the current size.
611       if (__res < _M_length())
612         __res = _M_length();
613       
614       if (__res != _M_capacity() || _M_is_shared())
615         {
616           _CharT* __tmp = _M_rep()->_M_clone(_M_get_allocator(),
617                                              __res - _M_length());
618           _M_dispose();
619           _M_data(__tmp);
620         }
621     }
622
623   template<typename _CharT, typename _Traits, typename _Alloc>
624     void
625     __rc_string_base<_CharT, _Traits, _Alloc>::
626     _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
627               size_type __len2)
628     {
629       const size_type __how_much = _M_length() - __pos - __len1;
630       
631       _Rep* __r = _Rep::_S_create(_M_length() + __len2 - __len1,
632                                   _M_capacity(), _M_get_allocator());
633       
634       if (__pos)
635         _S_copy(__r->_M_refdata(), _M_data(), __pos);
636       if (__s && __len2)
637         _S_copy(__r->_M_refdata() + __pos, __s, __len2);
638       if (__how_much)
639         _S_copy(__r->_M_refdata() + __pos + __len2,
640                 _M_data() + __pos + __len1, __how_much);
641       
642       _M_dispose();
643       _M_data(__r->_M_refdata());
644     }
645
646   template<typename _CharT, typename _Traits, typename _Alloc>
647     void
648     __rc_string_base<_CharT, _Traits, _Alloc>::
649     _M_erase(size_type __pos, size_type __n)
650     {
651       const size_type __new_size = _M_length() - __n;
652       const size_type __how_much = _M_length() - __pos - __n;
653       
654       if (_M_is_shared())
655         {
656           // Must reallocate.
657           _Rep* __r = _Rep::_S_create(__new_size, _M_capacity(),
658                                       _M_get_allocator());
659
660           if (__pos)
661             _S_copy(__r->_M_refdata(), _M_data(), __pos);
662           if (__how_much)
663             _S_copy(__r->_M_refdata() + __pos,
664                     _M_data() + __pos + __n, __how_much);
665
666           _M_dispose();
667           _M_data(__r->_M_refdata());
668         }
669       else if (__how_much && __n)
670         {
671           // Work in-place.
672           _S_move(_M_data() + __pos,
673                   _M_data() + __pos + __n, __how_much);
674         }
675
676       _M_rep()->_M_set_length(__new_size);      
677     }
678
679   template<>
680     inline bool
681     __rc_string_base<char, std::char_traits<char>,
682                      std::allocator<char> >::
683     _M_compare(const __rc_string_base& __rcs) const
684     {
685       if (_M_rep() == __rcs._M_rep())
686         return true;
687       return false;
688     }
689
690   template<>
691     inline bool
692     __rc_string_base<wchar_t, std::char_traits<wchar_t>,
693                      std::allocator<wchar_t> >::
694     _M_compare(const __rc_string_base& __rcs) const
695     {
696       if (_M_rep() == __rcs._M_rep())
697         return true;
698       return false;
699     }
700 } // namespace __gnu_cxx
701
702 #endif /* _RC_STRING_BASE_H */