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