basic_string.h (_S_construct(const _CharT*, size_type, const _Alloc&)): New, declare.
[gcc.git] / libstdc++-v3 / include / bits / basic_string.tcc
1 // Components for manipulating sequences of characters -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
21
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
26
27 /** @file basic_string.tcc
28 * This is an internal header file, included by other library headers.
29 * You should not attempt to use it directly.
30 */
31
32 //
33 // ISO C++ 14882: 21 Strings library
34 //
35
36 // Written by Jason Merrill based upon the specification by Takanori Adachi
37 // in ANSI X3J16/94-0013R2. Rewritten by Nathan Myers to ISO-14882.
38
39 #ifndef _BASIC_STRING_TCC
40 #define _BASIC_STRING_TCC 1
41
42 #pragma GCC system_header
43
44 #include <cxxabi-forced.h>
45
46 _GLIBCXX_BEGIN_NAMESPACE(std)
47
48 template<typename _CharT, typename _Traits, typename _Alloc>
49 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
50 basic_string<_CharT, _Traits, _Alloc>::
51 _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
52
53 template<typename _CharT, typename _Traits, typename _Alloc>
54 const _CharT
55 basic_string<_CharT, _Traits, _Alloc>::
56 _Rep::_S_terminal = _CharT();
57
58 template<typename _CharT, typename _Traits, typename _Alloc>
59 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
60 basic_string<_CharT, _Traits, _Alloc>::npos;
61
62 // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
63 // at static init time (before static ctors are run).
64 template<typename _CharT, typename _Traits, typename _Alloc>
65 typename basic_string<_CharT, _Traits, _Alloc>::size_type
66 basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
67 (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
68 sizeof(size_type)];
69
70 // NB: This is the special case for Input Iterators, used in
71 // istreambuf_iterators, etc.
72 // Input Iterators have a cost structure very different from
73 // pointers, calling for a different coding style.
74 template<typename _CharT, typename _Traits, typename _Alloc>
75 template<typename _InIterator>
76 _CharT*
77 basic_string<_CharT, _Traits, _Alloc>::
78 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
79 input_iterator_tag)
80 {
81 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
82 if (__beg == __end && __a == _Alloc())
83 return _S_empty_rep()._M_refdata();
84 #endif
85 // Avoid reallocation for common case.
86 _CharT __buf[128];
87 size_type __len = 0;
88 while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
89 {
90 __buf[__len++] = *__beg;
91 ++__beg;
92 }
93 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
94 _M_copy(__r->_M_refdata(), __buf, __len);
95 __try
96 {
97 while (__beg != __end)
98 {
99 if (__len == __r->_M_capacity)
100 {
101 // Allocate more space.
102 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
103 _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
104 __r->_M_destroy(__a);
105 __r = __another;
106 }
107 __r->_M_refdata()[__len++] = *__beg;
108 ++__beg;
109 }
110 }
111 __catch(...)
112 {
113 __r->_M_destroy(__a);
114 __throw_exception_again;
115 }
116 __r->_M_set_length_and_sharable(__len);
117 return __r->_M_refdata();
118 }
119
120 template<typename _CharT, typename _Traits, typename _Alloc>
121 template <typename _FwdIterator>
122 _CharT*
123 basic_string<_CharT, _Traits, _Alloc>::
124 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
125 forward_iterator_tag)
126 {
127 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
128 if (__beg == __end && __a == _Alloc())
129 return _S_empty_rep()._M_refdata();
130 #endif
131 // NB: Not required, but considered best practice.
132 if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
133 __throw_logic_error(__N("basic_string::_S_construct NULL not valid"));
134
135 const size_type __dnew = static_cast<size_type>(std::distance(__beg,
136 __end));
137 // Check for out_of_range and length_error exceptions.
138 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
139 __try
140 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
141 __catch(...)
142 {
143 __r->_M_destroy(__a);
144 __throw_exception_again;
145 }
146 __r->_M_set_length_and_sharable(__dnew);
147 return __r->_M_refdata();
148 }
149
150 template<typename _CharT, typename _Traits, typename _Alloc>
151 _CharT*
152 basic_string<_CharT, _Traits, _Alloc>::
153 _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
154 {
155 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
156 if (__n == 0 && __a == _Alloc())
157 return _S_empty_rep()._M_refdata();
158 #endif
159 // Check for out_of_range and length_error exceptions.
160 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
161 if (__n)
162 _M_assign(__r->_M_refdata(), __n, __c);
163
164 __r->_M_set_length_and_sharable(__n);
165 return __r->_M_refdata();
166 }
167
168 template<typename _CharT, typename _Traits, typename _Alloc>
169 _CharT*
170 basic_string<_CharT, _Traits, _Alloc>::
171 _S_construct(const _CharT* __s, size_type __n, const _Alloc& __a)
172 {
173 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
174 if (__n == 0 && __a == _Alloc())
175 return _S_empty_rep()._M_refdata();
176 #endif
177 // NB: Not required, but considered best practice.
178 if (__gnu_cxx::__is_null_pointer(__s) && __n)
179 __throw_logic_error(__N("basic_string::_S_construct NULL not valid"));
180
181 // Check for out_of_range and length_error exceptions.
182 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
183 if (__n)
184 _M_copy(__r->_M_refdata(), __s, __n);
185
186 __r->_M_set_length_and_sharable(__n);
187 return __r->_M_refdata();
188 }
189
190 template<typename _CharT, typename _Traits, typename _Alloc>
191 basic_string<_CharT, _Traits, _Alloc>::
192 basic_string(const basic_string& __str)
193 : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
194 __str.get_allocator()),
195 __str.get_allocator())
196 { }
197
198 template<typename _CharT, typename _Traits, typename _Alloc>
199 basic_string<_CharT, _Traits, _Alloc>::
200 basic_string(const _Alloc& __a)
201 : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
202 { }
203
204 template<typename _CharT, typename _Traits, typename _Alloc>
205 basic_string<_CharT, _Traits, _Alloc>::
206 basic_string(const basic_string& __str, size_type __pos, size_type __n)
207 : _M_dataplus(_S_construct(__str._M_data()
208 + __str._M_check(__pos,
209 "basic_string::basic_string"),
210 __str._M_limit(__pos, __n), _Alloc()), _Alloc())
211 { }
212
213 template<typename _CharT, typename _Traits, typename _Alloc>
214 basic_string<_CharT, _Traits, _Alloc>::
215 basic_string(const basic_string& __str, size_type __pos,
216 size_type __n, const _Alloc& __a)
217 : _M_dataplus(_S_construct(__str._M_data()
218 + __str._M_check(__pos,
219 "basic_string::basic_string"),
220 __str._M_limit(__pos, __n), __a), __a)
221 { }
222
223 // TBD: DPG annotate
224 template<typename _CharT, typename _Traits, typename _Alloc>
225 basic_string<_CharT, _Traits, _Alloc>::
226 basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
227 : _M_dataplus(_S_construct(__s, __n, __a), __a)
228 { }
229
230 // TBD: DPG annotate
231 template<typename _CharT, typename _Traits, typename _Alloc>
232 basic_string<_CharT, _Traits, _Alloc>::
233 basic_string(const _CharT* __s, const _Alloc& __a)
234 : _M_dataplus(_S_construct(__s, __s ? traits_type::length(__s) : npos,
235 __a), __a)
236 { }
237
238 template<typename _CharT, typename _Traits, typename _Alloc>
239 basic_string<_CharT, _Traits, _Alloc>::
240 basic_string(size_type __n, _CharT __c, const _Alloc& __a)
241 : _M_dataplus(_S_construct(__n, __c, __a), __a)
242 { }
243
244 // TBD: DPG annotate
245 template<typename _CharT, typename _Traits, typename _Alloc>
246 template<typename _InputIterator>
247 basic_string<_CharT, _Traits, _Alloc>::
248 basic_string(_InputIterator __beg, _InputIterator __end,
249 const _Alloc& __a)
250 : _M_dataplus(_S_construct(__beg, __end, __a), __a)
251 { }
252
253 #ifdef __GXX_EXPERIMENTAL_CXX0X__
254 template<typename _CharT, typename _Traits, typename _Alloc>
255 basic_string<_CharT, _Traits, _Alloc>::
256 basic_string(initializer_list<_CharT> __l, const _Alloc& __a)
257 : _M_dataplus(_S_construct(__l.begin(), __l.size(), __a), __a)
258 { }
259 #endif
260
261 template<typename _CharT, typename _Traits, typename _Alloc>
262 basic_string<_CharT, _Traits, _Alloc>&
263 basic_string<_CharT, _Traits, _Alloc>::
264 assign(const basic_string& __str)
265 {
266 if (_M_rep() != __str._M_rep())
267 {
268 // XXX MT
269 const allocator_type __a = this->get_allocator();
270 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
271 _M_rep()->_M_dispose(__a);
272 _M_data(__tmp);
273 }
274 return *this;
275 }
276
277 template<typename _CharT, typename _Traits, typename _Alloc>
278 basic_string<_CharT, _Traits, _Alloc>&
279 basic_string<_CharT, _Traits, _Alloc>::
280 assign(const _CharT* __s, size_type __n)
281 {
282 __glibcxx_requires_string_len(__s, __n);
283 _M_check_length(this->size(), __n, "basic_string::assign");
284 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
285 return _M_replace_safe(size_type(0), this->size(), __s, __n);
286 else
287 {
288 // Work in-place.
289 const size_type __pos = __s - _M_data();
290 if (__pos >= __n)
291 _M_copy(_M_data(), __s, __n);
292 else if (__pos)
293 _M_move(_M_data(), __s, __n);
294 _M_rep()->_M_set_length_and_sharable(__n);
295 return *this;
296 }
297 }
298
299 template<typename _CharT, typename _Traits, typename _Alloc>
300 basic_string<_CharT, _Traits, _Alloc>&
301 basic_string<_CharT, _Traits, _Alloc>::
302 append(size_type __n, _CharT __c)
303 {
304 if (__n)
305 {
306 _M_check_length(size_type(0), __n, "basic_string::append");
307 const size_type __len = __n + this->size();
308 if (__len > this->capacity() || _M_rep()->_M_is_shared())
309 this->reserve(__len);
310 _M_assign(_M_data() + this->size(), __n, __c);
311 _M_rep()->_M_set_length_and_sharable(__len);
312 }
313 return *this;
314 }
315
316 template<typename _CharT, typename _Traits, typename _Alloc>
317 basic_string<_CharT, _Traits, _Alloc>&
318 basic_string<_CharT, _Traits, _Alloc>::
319 append(const _CharT* __s, size_type __n)
320 {
321 __glibcxx_requires_string_len(__s, __n);
322 if (__n)
323 {
324 _M_check_length(size_type(0), __n, "basic_string::append");
325 const size_type __len = __n + this->size();
326 if (__len > this->capacity() || _M_rep()->_M_is_shared())
327 {
328 if (_M_disjunct(__s))
329 this->reserve(__len);
330 else
331 {
332 const size_type __off = __s - _M_data();
333 this->reserve(__len);
334 __s = _M_data() + __off;
335 }
336 }
337 _M_copy(_M_data() + this->size(), __s, __n);
338 _M_rep()->_M_set_length_and_sharable(__len);
339 }
340 return *this;
341 }
342
343 template<typename _CharT, typename _Traits, typename _Alloc>
344 basic_string<_CharT, _Traits, _Alloc>&
345 basic_string<_CharT, _Traits, _Alloc>::
346 append(const basic_string& __str)
347 {
348 const size_type __size = __str.size();
349 if (__size)
350 {
351 const size_type __len = __size + this->size();
352 if (__len > this->capacity() || _M_rep()->_M_is_shared())
353 this->reserve(__len);
354 _M_copy(_M_data() + this->size(), __str._M_data(), __size);
355 _M_rep()->_M_set_length_and_sharable(__len);
356 }
357 return *this;
358 }
359
360 template<typename _CharT, typename _Traits, typename _Alloc>
361 basic_string<_CharT, _Traits, _Alloc>&
362 basic_string<_CharT, _Traits, _Alloc>::
363 append(const basic_string& __str, size_type __pos, size_type __n)
364 {
365 __str._M_check(__pos, "basic_string::append");
366 __n = __str._M_limit(__pos, __n);
367 if (__n)
368 {
369 const size_type __len = __n + this->size();
370 if (__len > this->capacity() || _M_rep()->_M_is_shared())
371 this->reserve(__len);
372 _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
373 _M_rep()->_M_set_length_and_sharable(__len);
374 }
375 return *this;
376 }
377
378 template<typename _CharT, typename _Traits, typename _Alloc>
379 basic_string<_CharT, _Traits, _Alloc>&
380 basic_string<_CharT, _Traits, _Alloc>::
381 insert(size_type __pos, const _CharT* __s, size_type __n)
382 {
383 __glibcxx_requires_string_len(__s, __n);
384 _M_check(__pos, "basic_string::insert");
385 _M_check_length(size_type(0), __n, "basic_string::insert");
386 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
387 return _M_replace_safe(__pos, size_type(0), __s, __n);
388 else
389 {
390 // Work in-place.
391 const size_type __off = __s - _M_data();
392 _M_mutate(__pos, 0, __n);
393 __s = _M_data() + __off;
394 _CharT* __p = _M_data() + __pos;
395 if (__s + __n <= __p)
396 _M_copy(__p, __s, __n);
397 else if (__s >= __p)
398 _M_copy(__p, __s + __n, __n);
399 else
400 {
401 const size_type __nleft = __p - __s;
402 _M_copy(__p, __s, __nleft);
403 _M_copy(__p + __nleft, __p + __n, __n - __nleft);
404 }
405 return *this;
406 }
407 }
408
409 template<typename _CharT, typename _Traits, typename _Alloc>
410 typename basic_string<_CharT, _Traits, _Alloc>::iterator
411 basic_string<_CharT, _Traits, _Alloc>::
412 erase(iterator __first, iterator __last)
413 {
414 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
415 && __last <= _M_iend());
416
417 // NB: This isn't just an optimization (bail out early when
418 // there is nothing to do, really), it's also a correctness
419 // issue vs MT, see libstdc++/40518.
420 const size_type __size = __last - __first;
421 if (__size)
422 {
423 const size_type __pos = __first - _M_ibegin();
424 _M_mutate(__pos, __size, size_type(0));
425 _M_rep()->_M_set_leaked();
426 return iterator(_M_data() + __pos);
427 }
428 else
429 return __first;
430 }
431
432 template<typename _CharT, typename _Traits, typename _Alloc>
433 basic_string<_CharT, _Traits, _Alloc>&
434 basic_string<_CharT, _Traits, _Alloc>::
435 replace(size_type __pos, size_type __n1, const _CharT* __s,
436 size_type __n2)
437 {
438 __glibcxx_requires_string_len(__s, __n2);
439 _M_check(__pos, "basic_string::replace");
440 __n1 = _M_limit(__pos, __n1);
441 _M_check_length(__n1, __n2, "basic_string::replace");
442 bool __left;
443 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
444 return _M_replace_safe(__pos, __n1, __s, __n2);
445 else if ((__left = __s + __n2 <= _M_data() + __pos)
446 || _M_data() + __pos + __n1 <= __s)
447 {
448 // Work in-place: non-overlapping case.
449 size_type __off = __s - _M_data();
450 __left ? __off : (__off += __n2 - __n1);
451 _M_mutate(__pos, __n1, __n2);
452 _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
453 return *this;
454 }
455 else
456 {
457 // Todo: overlapping case.
458 const basic_string __tmp(__s, __n2);
459 return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
460 }
461 }
462
463 template<typename _CharT, typename _Traits, typename _Alloc>
464 void
465 basic_string<_CharT, _Traits, _Alloc>::_Rep::
466 _M_destroy(const _Alloc& __a) throw ()
467 {
468 const size_type __size = sizeof(_Rep_base) +
469 (this->_M_capacity + 1) * sizeof(_CharT);
470 _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
471 }
472
473 template<typename _CharT, typename _Traits, typename _Alloc>
474 void
475 basic_string<_CharT, _Traits, _Alloc>::
476 _M_leak_hard()
477 {
478 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
479 if (_M_rep() == &_S_empty_rep())
480 return;
481 #endif
482 if (_M_rep()->_M_is_shared())
483 _M_mutate(0, 0, 0);
484 _M_rep()->_M_set_leaked();
485 }
486
487 template<typename _CharT, typename _Traits, typename _Alloc>
488 void
489 basic_string<_CharT, _Traits, _Alloc>::
490 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
491 {
492 const size_type __old_size = this->size();
493 const size_type __new_size = __old_size + __len2 - __len1;
494 const size_type __how_much = __old_size - __pos - __len1;
495
496 if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
497 {
498 // Must reallocate.
499 const allocator_type __a = get_allocator();
500 _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
501
502 if (__pos)
503 _M_copy(__r->_M_refdata(), _M_data(), __pos);
504 if (__how_much)
505 _M_copy(__r->_M_refdata() + __pos + __len2,
506 _M_data() + __pos + __len1, __how_much);
507
508 _M_rep()->_M_dispose(__a);
509 _M_data(__r->_M_refdata());
510 }
511 else if (__how_much && __len1 != __len2)
512 {
513 // Work in-place.
514 _M_move(_M_data() + __pos + __len2,
515 _M_data() + __pos + __len1, __how_much);
516 }
517 _M_rep()->_M_set_length_and_sharable(__new_size);
518 }
519
520 template<typename _CharT, typename _Traits, typename _Alloc>
521 void
522 basic_string<_CharT, _Traits, _Alloc>::
523 reserve(size_type __res)
524 {
525 if (__res != this->capacity() || _M_rep()->_M_is_shared())
526 {
527 // Make sure we don't shrink below the current size
528 if (__res < this->size())
529 __res = this->size();
530 const allocator_type __a = get_allocator();
531 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
532 _M_rep()->_M_dispose(__a);
533 _M_data(__tmp);
534 }
535 }
536
537 template<typename _CharT, typename _Traits, typename _Alloc>
538 void
539 basic_string<_CharT, _Traits, _Alloc>::
540 swap(basic_string& __s)
541 {
542 if (_M_rep()->_M_is_leaked())
543 _M_rep()->_M_set_sharable();
544 if (__s._M_rep()->_M_is_leaked())
545 __s._M_rep()->_M_set_sharable();
546 if (this->get_allocator() == __s.get_allocator())
547 {
548 _CharT* __tmp = _M_data();
549 _M_data(__s._M_data());
550 __s._M_data(__tmp);
551 }
552 // The code below can usually be optimized away.
553 else
554 {
555 const basic_string __tmp1(_M_ibegin(), _M_iend(),
556 __s.get_allocator());
557 const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
558 this->get_allocator());
559 *this = __tmp2;
560 __s = __tmp1;
561 }
562 }
563
564 template<typename _CharT, typename _Traits, typename _Alloc>
565 typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
566 basic_string<_CharT, _Traits, _Alloc>::_Rep::
567 _S_create(size_type __capacity, size_type __old_capacity,
568 const _Alloc& __alloc)
569 {
570 // _GLIBCXX_RESOLVE_LIB_DEFECTS
571 // 83. String::npos vs. string::max_size()
572 if (__capacity > _S_max_size)
573 __throw_length_error(__N("basic_string::_S_create"));
574
575 // The standard places no restriction on allocating more memory
576 // than is strictly needed within this layer at the moment or as
577 // requested by an explicit application call to reserve().
578
579 // Many malloc implementations perform quite poorly when an
580 // application attempts to allocate memory in a stepwise fashion
581 // growing each allocation size by only 1 char. Additionally,
582 // it makes little sense to allocate less linear memory than the
583 // natural blocking size of the malloc implementation.
584 // Unfortunately, we would need a somewhat low-level calculation
585 // with tuned parameters to get this perfect for any particular
586 // malloc implementation. Fortunately, generalizations about
587 // common features seen among implementations seems to suffice.
588
589 // __pagesize need not match the actual VM page size for good
590 // results in practice, thus we pick a common value on the low
591 // side. __malloc_header_size is an estimate of the amount of
592 // overhead per memory allocation (in practice seen N * sizeof
593 // (void*) where N is 0, 2 or 4). According to folklore,
594 // picking this value on the high side is better than
595 // low-balling it (especially when this algorithm is used with
596 // malloc implementations that allocate memory blocks rounded up
597 // to a size which is a power of 2).
598 const size_type __pagesize = 4096;
599 const size_type __malloc_header_size = 4 * sizeof(void*);
600
601 // The below implements an exponential growth policy, necessary to
602 // meet amortized linear time requirements of the library: see
603 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
604 // It's active for allocations requiring an amount of memory above
605 // system pagesize. This is consistent with the requirements of the
606 // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
607 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
608 __capacity = 2 * __old_capacity;
609
610 // NB: Need an array of char_type[__capacity], plus a terminating
611 // null char_type() element, plus enough for the _Rep data structure.
612 // Whew. Seemingly so needy, yet so elemental.
613 size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
614
615 const size_type __adj_size = __size + __malloc_header_size;
616 if (__adj_size > __pagesize && __capacity > __old_capacity)
617 {
618 const size_type __extra = __pagesize - __adj_size % __pagesize;
619 __capacity += __extra / sizeof(_CharT);
620 // Never allocate a string bigger than _S_max_size.
621 if (__capacity > _S_max_size)
622 __capacity = _S_max_size;
623 __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
624 }
625
626 // NB: Might throw, but no worries about a leak, mate: _Rep()
627 // does not throw.
628 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
629 _Rep *__p = new (__place) _Rep;
630 __p->_M_capacity = __capacity;
631 // ABI compatibility - 3.4.x set in _S_create both
632 // _M_refcount and _M_length. All callers of _S_create
633 // in basic_string.tcc then set just _M_length.
634 // In 4.0.x and later both _M_refcount and _M_length
635 // are initialized in the callers, unfortunately we can
636 // have 3.4.x compiled code with _S_create callers inlined
637 // calling 4.0.x+ _S_create.
638 __p->_M_set_sharable();
639 return __p;
640 }
641
642 template<typename _CharT, typename _Traits, typename _Alloc>
643 _CharT*
644 basic_string<_CharT, _Traits, _Alloc>::_Rep::
645 _M_clone(const _Alloc& __alloc, size_type __res)
646 {
647 // Requested capacity of the clone.
648 const size_type __requested_cap = this->_M_length + __res;
649 _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
650 __alloc);
651 if (this->_M_length)
652 _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
653
654 __r->_M_set_length_and_sharable(this->_M_length);
655 return __r->_M_refdata();
656 }
657
658 template<typename _CharT, typename _Traits, typename _Alloc>
659 void
660 basic_string<_CharT, _Traits, _Alloc>::
661 resize(size_type __n, _CharT __c)
662 {
663 const size_type __size = this->size();
664 _M_check_length(__size, __n, "basic_string::resize");
665 if (__size < __n)
666 this->append(__n - __size, __c);
667 else if (__n < __size)
668 this->erase(__n);
669 // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
670 }
671
672 template<typename _CharT, typename _Traits, typename _Alloc>
673 template<typename _InputIterator>
674 basic_string<_CharT, _Traits, _Alloc>&
675 basic_string<_CharT, _Traits, _Alloc>::
676 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
677 _InputIterator __k2, __false_type)
678 {
679 const basic_string __s(__k1, __k2);
680 const size_type __n1 = __i2 - __i1;
681 _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
682 return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
683 __s.size());
684 }
685
686 template<typename _CharT, typename _Traits, typename _Alloc>
687 basic_string<_CharT, _Traits, _Alloc>&
688 basic_string<_CharT, _Traits, _Alloc>::
689 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
690 _CharT __c)
691 {
692 _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
693 _M_mutate(__pos1, __n1, __n2);
694 if (__n2)
695 _M_assign(_M_data() + __pos1, __n2, __c);
696 return *this;
697 }
698
699 template<typename _CharT, typename _Traits, typename _Alloc>
700 basic_string<_CharT, _Traits, _Alloc>&
701 basic_string<_CharT, _Traits, _Alloc>::
702 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
703 size_type __n2)
704 {
705 _M_mutate(__pos1, __n1, __n2);
706 if (__n2)
707 _M_copy(_M_data() + __pos1, __s, __n2);
708 return *this;
709 }
710
711 template<typename _CharT, typename _Traits, typename _Alloc>
712 basic_string<_CharT, _Traits, _Alloc>
713 operator+(const _CharT* __lhs,
714 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
715 {
716 __glibcxx_requires_string(__lhs);
717 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
718 typedef typename __string_type::size_type __size_type;
719 const __size_type __len = _Traits::length(__lhs);
720 __string_type __str;
721 __str.reserve(__len + __rhs.size());
722 __str.append(__lhs, __len);
723 __str.append(__rhs);
724 return __str;
725 }
726
727 template<typename _CharT, typename _Traits, typename _Alloc>
728 basic_string<_CharT, _Traits, _Alloc>
729 operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
730 {
731 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
732 typedef typename __string_type::size_type __size_type;
733 __string_type __str;
734 const __size_type __len = __rhs.size();
735 __str.reserve(__len + 1);
736 __str.append(__size_type(1), __lhs);
737 __str.append(__rhs);
738 return __str;
739 }
740
741 template<typename _CharT, typename _Traits, typename _Alloc>
742 typename basic_string<_CharT, _Traits, _Alloc>::size_type
743 basic_string<_CharT, _Traits, _Alloc>::
744 copy(_CharT* __s, size_type __n, size_type __pos) const
745 {
746 _M_check(__pos, "basic_string::copy");
747 __n = _M_limit(__pos, __n);
748 __glibcxx_requires_string_len(__s, __n);
749 if (__n)
750 _M_copy(__s, _M_data() + __pos, __n);
751 // 21.3.5.7 par 3: do not append null. (good.)
752 return __n;
753 }
754
755 template<typename _CharT, typename _Traits, typename _Alloc>
756 typename basic_string<_CharT, _Traits, _Alloc>::size_type
757 basic_string<_CharT, _Traits, _Alloc>::
758 find(const _CharT* __s, size_type __pos, size_type __n) const
759 {
760 __glibcxx_requires_string_len(__s, __n);
761 const size_type __size = this->size();
762 const _CharT* __data = _M_data();
763
764 if (__n == 0)
765 return __pos <= __size ? __pos : npos;
766
767 if (__n <= __size)
768 {
769 for (; __pos <= __size - __n; ++__pos)
770 if (traits_type::eq(__data[__pos], __s[0])
771 && traits_type::compare(__data + __pos + 1,
772 __s + 1, __n - 1) == 0)
773 return __pos;
774 }
775 return npos;
776 }
777
778 template<typename _CharT, typename _Traits, typename _Alloc>
779 typename basic_string<_CharT, _Traits, _Alloc>::size_type
780 basic_string<_CharT, _Traits, _Alloc>::
781 find(_CharT __c, size_type __pos) const
782 {
783 size_type __ret = npos;
784 const size_type __size = this->size();
785 if (__pos < __size)
786 {
787 const _CharT* __data = _M_data();
788 const size_type __n = __size - __pos;
789 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
790 if (__p)
791 __ret = __p - __data;
792 }
793 return __ret;
794 }
795
796 template<typename _CharT, typename _Traits, typename _Alloc>
797 typename basic_string<_CharT, _Traits, _Alloc>::size_type
798 basic_string<_CharT, _Traits, _Alloc>::
799 rfind(const _CharT* __s, size_type __pos, size_type __n) const
800 {
801 __glibcxx_requires_string_len(__s, __n);
802 const size_type __size = this->size();
803 if (__n <= __size)
804 {
805 __pos = std::min(size_type(__size - __n), __pos);
806 const _CharT* __data = _M_data();
807 do
808 {
809 if (traits_type::compare(__data + __pos, __s, __n) == 0)
810 return __pos;
811 }
812 while (__pos-- > 0);
813 }
814 return npos;
815 }
816
817 template<typename _CharT, typename _Traits, typename _Alloc>
818 typename basic_string<_CharT, _Traits, _Alloc>::size_type
819 basic_string<_CharT, _Traits, _Alloc>::
820 rfind(_CharT __c, size_type __pos) const
821 {
822 size_type __size = this->size();
823 if (__size)
824 {
825 if (--__size > __pos)
826 __size = __pos;
827 for (++__size; __size-- > 0; )
828 if (traits_type::eq(_M_data()[__size], __c))
829 return __size;
830 }
831 return npos;
832 }
833
834 template<typename _CharT, typename _Traits, typename _Alloc>
835 typename basic_string<_CharT, _Traits, _Alloc>::size_type
836 basic_string<_CharT, _Traits, _Alloc>::
837 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
838 {
839 __glibcxx_requires_string_len(__s, __n);
840 for (; __n && __pos < this->size(); ++__pos)
841 {
842 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
843 if (__p)
844 return __pos;
845 }
846 return npos;
847 }
848
849 template<typename _CharT, typename _Traits, typename _Alloc>
850 typename basic_string<_CharT, _Traits, _Alloc>::size_type
851 basic_string<_CharT, _Traits, _Alloc>::
852 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
853 {
854 __glibcxx_requires_string_len(__s, __n);
855 size_type __size = this->size();
856 if (__size && __n)
857 {
858 if (--__size > __pos)
859 __size = __pos;
860 do
861 {
862 if (traits_type::find(__s, __n, _M_data()[__size]))
863 return __size;
864 }
865 while (__size-- != 0);
866 }
867 return npos;
868 }
869
870 template<typename _CharT, typename _Traits, typename _Alloc>
871 typename basic_string<_CharT, _Traits, _Alloc>::size_type
872 basic_string<_CharT, _Traits, _Alloc>::
873 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
874 {
875 __glibcxx_requires_string_len(__s, __n);
876 for (; __pos < this->size(); ++__pos)
877 if (!traits_type::find(__s, __n, _M_data()[__pos]))
878 return __pos;
879 return npos;
880 }
881
882 template<typename _CharT, typename _Traits, typename _Alloc>
883 typename basic_string<_CharT, _Traits, _Alloc>::size_type
884 basic_string<_CharT, _Traits, _Alloc>::
885 find_first_not_of(_CharT __c, size_type __pos) const
886 {
887 for (; __pos < this->size(); ++__pos)
888 if (!traits_type::eq(_M_data()[__pos], __c))
889 return __pos;
890 return npos;
891 }
892
893 template<typename _CharT, typename _Traits, typename _Alloc>
894 typename basic_string<_CharT, _Traits, _Alloc>::size_type
895 basic_string<_CharT, _Traits, _Alloc>::
896 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
897 {
898 __glibcxx_requires_string_len(__s, __n);
899 size_type __size = this->size();
900 if (__size)
901 {
902 if (--__size > __pos)
903 __size = __pos;
904 do
905 {
906 if (!traits_type::find(__s, __n, _M_data()[__size]))
907 return __size;
908 }
909 while (__size--);
910 }
911 return npos;
912 }
913
914 template<typename _CharT, typename _Traits, typename _Alloc>
915 typename basic_string<_CharT, _Traits, _Alloc>::size_type
916 basic_string<_CharT, _Traits, _Alloc>::
917 find_last_not_of(_CharT __c, size_type __pos) const
918 {
919 size_type __size = this->size();
920 if (__size)
921 {
922 if (--__size > __pos)
923 __size = __pos;
924 do
925 {
926 if (!traits_type::eq(_M_data()[__size], __c))
927 return __size;
928 }
929 while (__size--);
930 }
931 return npos;
932 }
933
934 template<typename _CharT, typename _Traits, typename _Alloc>
935 int
936 basic_string<_CharT, _Traits, _Alloc>::
937 compare(size_type __pos, size_type __n, const basic_string& __str) const
938 {
939 _M_check(__pos, "basic_string::compare");
940 __n = _M_limit(__pos, __n);
941 const size_type __osize = __str.size();
942 const size_type __len = std::min(__n, __osize);
943 int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
944 if (!__r)
945 __r = _S_compare(__n, __osize);
946 return __r;
947 }
948
949 template<typename _CharT, typename _Traits, typename _Alloc>
950 int
951 basic_string<_CharT, _Traits, _Alloc>::
952 compare(size_type __pos1, size_type __n1, const basic_string& __str,
953 size_type __pos2, size_type __n2) const
954 {
955 _M_check(__pos1, "basic_string::compare");
956 __str._M_check(__pos2, "basic_string::compare");
957 __n1 = _M_limit(__pos1, __n1);
958 __n2 = __str._M_limit(__pos2, __n2);
959 const size_type __len = std::min(__n1, __n2);
960 int __r = traits_type::compare(_M_data() + __pos1,
961 __str.data() + __pos2, __len);
962 if (!__r)
963 __r = _S_compare(__n1, __n2);
964 return __r;
965 }
966
967 template<typename _CharT, typename _Traits, typename _Alloc>
968 int
969 basic_string<_CharT, _Traits, _Alloc>::
970 compare(const _CharT* __s) const
971 {
972 __glibcxx_requires_string(__s);
973 const size_type __size = this->size();
974 const size_type __osize = traits_type::length(__s);
975 const size_type __len = std::min(__size, __osize);
976 int __r = traits_type::compare(_M_data(), __s, __len);
977 if (!__r)
978 __r = _S_compare(__size, __osize);
979 return __r;
980 }
981
982 template<typename _CharT, typename _Traits, typename _Alloc>
983 int
984 basic_string <_CharT, _Traits, _Alloc>::
985 compare(size_type __pos, size_type __n1, const _CharT* __s) const
986 {
987 __glibcxx_requires_string(__s);
988 _M_check(__pos, "basic_string::compare");
989 __n1 = _M_limit(__pos, __n1);
990 const size_type __osize = traits_type::length(__s);
991 const size_type __len = std::min(__n1, __osize);
992 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
993 if (!__r)
994 __r = _S_compare(__n1, __osize);
995 return __r;
996 }
997
998 template<typename _CharT, typename _Traits, typename _Alloc>
999 int
1000 basic_string <_CharT, _Traits, _Alloc>::
1001 compare(size_type __pos, size_type __n1, const _CharT* __s,
1002 size_type __n2) const
1003 {
1004 __glibcxx_requires_string_len(__s, __n2);
1005 _M_check(__pos, "basic_string::compare");
1006 __n1 = _M_limit(__pos, __n1);
1007 const size_type __len = std::min(__n1, __n2);
1008 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1009 if (!__r)
1010 __r = _S_compare(__n1, __n2);
1011 return __r;
1012 }
1013
1014 // 21.3.7.9 basic_string::getline and operators
1015 template<typename _CharT, typename _Traits, typename _Alloc>
1016 basic_istream<_CharT, _Traits>&
1017 operator>>(basic_istream<_CharT, _Traits>& __in,
1018 basic_string<_CharT, _Traits, _Alloc>& __str)
1019 {
1020 typedef basic_istream<_CharT, _Traits> __istream_type;
1021 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1022 typedef typename __istream_type::ios_base __ios_base;
1023 typedef typename __istream_type::int_type __int_type;
1024 typedef typename __string_type::size_type __size_type;
1025 typedef ctype<_CharT> __ctype_type;
1026 typedef typename __ctype_type::ctype_base __ctype_base;
1027
1028 __size_type __extracted = 0;
1029 typename __ios_base::iostate __err = __ios_base::goodbit;
1030 typename __istream_type::sentry __cerb(__in, false);
1031 if (__cerb)
1032 {
1033 __try
1034 {
1035 // Avoid reallocation for common case.
1036 __str.erase();
1037 _CharT __buf[128];
1038 __size_type __len = 0;
1039 const streamsize __w = __in.width();
1040 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
1041 : __str.max_size();
1042 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1043 const __int_type __eof = _Traits::eof();
1044 __int_type __c = __in.rdbuf()->sgetc();
1045
1046 while (__extracted < __n
1047 && !_Traits::eq_int_type(__c, __eof)
1048 && !__ct.is(__ctype_base::space,
1049 _Traits::to_char_type(__c)))
1050 {
1051 if (__len == sizeof(__buf) / sizeof(_CharT))
1052 {
1053 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
1054 __len = 0;
1055 }
1056 __buf[__len++] = _Traits::to_char_type(__c);
1057 ++__extracted;
1058 __c = __in.rdbuf()->snextc();
1059 }
1060 __str.append(__buf, __len);
1061
1062 if (_Traits::eq_int_type(__c, __eof))
1063 __err |= __ios_base::eofbit;
1064 __in.width(0);
1065 }
1066 __catch(__cxxabiv1::__forced_unwind&)
1067 {
1068 __in._M_setstate(__ios_base::badbit);
1069 __throw_exception_again;
1070 }
1071 __catch(...)
1072 {
1073 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1074 // 91. Description of operator>> and getline() for string<>
1075 // might cause endless loop
1076 __in._M_setstate(__ios_base::badbit);
1077 }
1078 }
1079 // 211. operator>>(istream&, string&) doesn't set failbit
1080 if (!__extracted)
1081 __err |= __ios_base::failbit;
1082 if (__err)
1083 __in.setstate(__err);
1084 return __in;
1085 }
1086
1087 template<typename _CharT, typename _Traits, typename _Alloc>
1088 basic_istream<_CharT, _Traits>&
1089 getline(basic_istream<_CharT, _Traits>& __in,
1090 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
1091 {
1092 typedef basic_istream<_CharT, _Traits> __istream_type;
1093 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1094 typedef typename __istream_type::ios_base __ios_base;
1095 typedef typename __istream_type::int_type __int_type;
1096 typedef typename __string_type::size_type __size_type;
1097
1098 __size_type __extracted = 0;
1099 const __size_type __n = __str.max_size();
1100 typename __ios_base::iostate __err = __ios_base::goodbit;
1101 typename __istream_type::sentry __cerb(__in, true);
1102 if (__cerb)
1103 {
1104 __try
1105 {
1106 __str.erase();
1107 const __int_type __idelim = _Traits::to_int_type(__delim);
1108 const __int_type __eof = _Traits::eof();
1109 __int_type __c = __in.rdbuf()->sgetc();
1110
1111 while (__extracted < __n
1112 && !_Traits::eq_int_type(__c, __eof)
1113 && !_Traits::eq_int_type(__c, __idelim))
1114 {
1115 __str += _Traits::to_char_type(__c);
1116 ++__extracted;
1117 __c = __in.rdbuf()->snextc();
1118 }
1119
1120 if (_Traits::eq_int_type(__c, __eof))
1121 __err |= __ios_base::eofbit;
1122 else if (_Traits::eq_int_type(__c, __idelim))
1123 {
1124 ++__extracted;
1125 __in.rdbuf()->sbumpc();
1126 }
1127 else
1128 __err |= __ios_base::failbit;
1129 }
1130 __catch(__cxxabiv1::__forced_unwind&)
1131 {
1132 __in._M_setstate(__ios_base::badbit);
1133 __throw_exception_again;
1134 }
1135 __catch(...)
1136 {
1137 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1138 // 91. Description of operator>> and getline() for string<>
1139 // might cause endless loop
1140 __in._M_setstate(__ios_base::badbit);
1141 }
1142 }
1143 if (!__extracted)
1144 __err |= __ios_base::failbit;
1145 if (__err)
1146 __in.setstate(__err);
1147 return __in;
1148 }
1149
1150 // Inhibit implicit instantiations for required instantiations,
1151 // which are defined via explicit instantiations elsewhere.
1152 // NB: This syntax is a GNU extension.
1153 #if _GLIBCXX_EXTERN_TEMPLATE > 0
1154 extern template class basic_string<char>;
1155 extern template
1156 basic_istream<char>&
1157 operator>>(basic_istream<char>&, string&);
1158 extern template
1159 basic_ostream<char>&
1160 operator<<(basic_ostream<char>&, const string&);
1161 extern template
1162 basic_istream<char>&
1163 getline(basic_istream<char>&, string&, char);
1164 extern template
1165 basic_istream<char>&
1166 getline(basic_istream<char>&, string&);
1167
1168 #ifdef _GLIBCXX_USE_WCHAR_T
1169 extern template class basic_string<wchar_t>;
1170 extern template
1171 basic_istream<wchar_t>&
1172 operator>>(basic_istream<wchar_t>&, wstring&);
1173 extern template
1174 basic_ostream<wchar_t>&
1175 operator<<(basic_ostream<wchar_t>&, const wstring&);
1176 extern template
1177 basic_istream<wchar_t>&
1178 getline(basic_istream<wchar_t>&, wstring&, wchar_t);
1179 extern template
1180 basic_istream<wchar_t>&
1181 getline(basic_istream<wchar_t>&, wstring&);
1182 #endif
1183 #endif
1184
1185 _GLIBCXX_END_NAMESPACE
1186
1187 #endif