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