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