1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
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.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 21 Strings library
35 // This file is included by <string>. It is not meant to be included
38 // Written by Jason Merrill based upon the specification by Takanori Adachi
39 // in ANSI X3J16/94-0013R2. Rewritten by Nathan Myers to ISO-14882.
41 #ifndef _BASIC_STRING_TCC
42 #define _BASIC_STRING_TCC 1
44 #pragma GCC system_header
48 template<typename _Type>
50 __is_null_pointer(_Type* __ptr)
51 { return __ptr == 0; }
53 template<typename _Type>
55 __is_null_pointer(const _Type&)
58 template<typename _CharT, typename _Traits, typename _Alloc>
59 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
60 basic_string<_CharT, _Traits, _Alloc>::
61 _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
63 template<typename _CharT, typename _Traits, typename _Alloc>
65 basic_string<_CharT, _Traits, _Alloc>::
66 _Rep::_S_terminal = _CharT();
68 template<typename _CharT, typename _Traits, typename _Alloc>
69 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
70 basic_string<_CharT, _Traits, _Alloc>::npos;
72 // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
73 // at static init time (before static ctors are run).
74 template<typename _CharT, typename _Traits, typename _Alloc>
75 typename basic_string<_CharT, _Traits, _Alloc>::size_type
76 basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
77 (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
80 // NB: This is the special case for Input Iterators, used in
81 // istreambuf_iterators, etc.
82 // Input Iterators have a cost structure very different from
83 // pointers, calling for a different coding style.
84 template<typename _CharT, typename _Traits, typename _Alloc>
85 template<typename _InIterator>
87 basic_string<_CharT, _Traits, _Alloc>::
88 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
91 if (__beg == __end && __a == _Alloc())
92 return _S_empty_rep()._M_refdata();
93 // Avoid reallocation for common case.
96 while (__beg != __end && __i < sizeof(__buf) / sizeof(_CharT))
98 __buf[__i++] = *__beg;
101 _Rep* __r = _Rep::_S_create(__i, __a);
102 traits_type::copy(__r->_M_refdata(), __buf, __i);
103 __r->_M_length = __i;
106 // NB: this loop looks precisely this way because
107 // it avoids comparing __beg != __end any more
108 // than strictly necessary; != might be expensive!
111 _CharT* __p = __r->_M_refdata() + __r->_M_length;
112 _CharT* __last = __r->_M_refdata() + __r->_M_capacity;
117 __r->_M_length = __p - __r->_M_refdata();
118 *__p = _Rep::_S_terminal; // grrr.
119 return __r->_M_refdata();
126 // Allocate more space.
127 const size_type __len = __p - __r->_M_refdata();
128 _Rep* __another = _Rep::_S_create(__len + 1, __a);
129 traits_type::copy(__another->_M_refdata(),
130 __r->_M_refdata(), __len);
131 __r->_M_destroy(__a);
133 __r->_M_length = __len;
138 __r->_M_destroy(__a);
139 __throw_exception_again;
144 template<typename _CharT, typename _Traits, typename _Alloc>
145 template <class _InIterator>
147 basic_string<_CharT, _Traits, _Alloc>::
148 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
149 forward_iterator_tag)
151 if (__beg == __end && __a == _Alloc())
152 return _S_empty_rep()._M_refdata();
154 // NB: Not required, but considered best practice.
155 if (__builtin_expect(__is_null_pointer(__beg), 0))
156 __throw_logic_error("basic_string::_S_construct NULL not valid");
158 const size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
160 // Check for out_of_range and length_error exceptions.
161 _Rep* __r = _Rep::_S_create(__dnew, __a);
163 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
166 __r->_M_destroy(__a);
167 __throw_exception_again;
169 __r->_M_length = __dnew;
171 __r->_M_refdata()[__dnew] = _Rep::_S_terminal; // grrr.
172 return __r->_M_refdata();
175 template<typename _CharT, typename _Traits, typename _Alloc>
177 basic_string<_CharT, _Traits, _Alloc>::
178 _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
180 if (__n == 0 && __a == _Alloc())
181 return _S_empty_rep()._M_refdata();
183 // Check for out_of_range and length_error exceptions.
184 _Rep* __r = _Rep::_S_create(__n, __a);
188 traits_type::assign(__r->_M_refdata(), __n, __c);
192 __r->_M_destroy(__a);
193 __throw_exception_again;
195 __r->_M_length = __n;
196 __r->_M_refdata()[__n] = _Rep::_S_terminal; // grrr
197 return __r->_M_refdata();
200 template<typename _CharT, typename _Traits, typename _Alloc>
201 basic_string<_CharT, _Traits, _Alloc>::
202 basic_string(const basic_string& __str)
203 : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(), __str.get_allocator()),
204 __str.get_allocator())
207 template<typename _CharT, typename _Traits, typename _Alloc>
208 basic_string<_CharT, _Traits, _Alloc>::
209 basic_string(const _Alloc& __a)
210 : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
213 template<typename _CharT, typename _Traits, typename _Alloc>
214 basic_string<_CharT, _Traits, _Alloc>::
215 basic_string(const basic_string& __str, size_type __pos, size_type __n)
216 : _M_dataplus(_S_construct(__str._M_check(__pos),
217 __str._M_fold(__pos, __n), _Alloc()), _Alloc())
220 template<typename _CharT, typename _Traits, typename _Alloc>
221 basic_string<_CharT, _Traits, _Alloc>::
222 basic_string(const basic_string& __str, size_type __pos,
223 size_type __n, const _Alloc& __a)
224 : _M_dataplus(_S_construct(__str._M_check(__pos),
225 __str._M_fold(__pos, __n), __a), __a)
229 template<typename _CharT, typename _Traits, typename _Alloc>
230 basic_string<_CharT, _Traits, _Alloc>::
231 basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
232 : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
236 template<typename _CharT, typename _Traits, typename _Alloc>
237 basic_string<_CharT, _Traits, _Alloc>::
238 basic_string(const _CharT* __s, const _Alloc& __a)
239 : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
240 __s + npos, __a), __a)
243 template<typename _CharT, typename _Traits, typename _Alloc>
244 basic_string<_CharT, _Traits, _Alloc>::
245 basic_string(size_type __n, _CharT __c, const _Alloc& __a)
246 : _M_dataplus(_S_construct(__n, __c, __a), __a)
250 template<typename _CharT, typename _Traits, typename _Alloc>
251 template<typename _InputIterator>
252 basic_string<_CharT, _Traits, _Alloc>::
253 basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a)
254 : _M_dataplus(_S_construct(__beg, __end, __a), __a)
257 template<typename _CharT, typename _Traits, typename _Alloc>
258 basic_string<_CharT, _Traits, _Alloc>&
259 basic_string<_CharT, _Traits, _Alloc>::
260 assign(const basic_string& __str)
262 if (_M_rep() != __str._M_rep())
265 allocator_type __a = this->get_allocator();
266 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
267 _M_rep()->_M_dispose(__a);
273 template<typename _CharT, typename _Traits, typename _Alloc>
274 basic_string<_CharT, _Traits, _Alloc>&
275 basic_string<_CharT, _Traits, _Alloc>::
276 assign(const basic_string& __str, size_type __pos, size_type __n)
278 const size_type __strsize = __str.size();
279 if (__pos > __strsize)
280 __throw_out_of_range("basic_string::assign");
281 const bool __testn = __n < __strsize - __pos;
282 const size_type __newsize = __testn ? __n : __strsize - __pos;
283 return this->assign(__str._M_data() + __pos, __newsize);
286 template<typename _CharT, typename _Traits, typename _Alloc>
287 basic_string<_CharT, _Traits, _Alloc>&
288 basic_string<_CharT, _Traits, _Alloc>::
289 assign(const _CharT* __s, size_type __n)
291 __glibcxx_requires_string_len(__s, __n);
292 if (__n > this->max_size())
293 __throw_length_error("basic_string::assign");
294 if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data())
295 || less<const _CharT*>()(_M_data() + this->size(), __s))
296 return _M_replace_safe(_M_ibegin(), _M_iend(), __s, __s + __n);
300 const size_type __pos = __s - _M_data();
302 traits_type::copy(_M_data(), __s, __n);
304 traits_type::move(_M_data(), __s, __n);
305 _M_rep()->_M_length = __n;
306 _M_data()[__n] = _Rep::_S_terminal; // grr.
311 template<typename _CharT, typename _Traits, typename _Alloc>
312 basic_string<_CharT, _Traits, _Alloc>&
313 basic_string<_CharT, _Traits, _Alloc>::
314 insert(size_type __pos1, const basic_string& __str,
315 size_type __pos2, size_type __n)
317 const size_type __strsize = __str.size();
318 if (__pos2 > __strsize)
319 __throw_out_of_range("basic_string::insert");
320 const bool __testn = __n < __strsize - __pos2;
321 const size_type __newsize = __testn ? __n : __strsize - __pos2;
322 return this->insert(__pos1, __str._M_data() + __pos2, __newsize);
325 template<typename _CharT, typename _Traits, typename _Alloc>
326 basic_string<_CharT, _Traits, _Alloc>&
327 basic_string<_CharT, _Traits, _Alloc>::
328 insert(size_type __pos, const _CharT* __s, size_type __n)
330 __glibcxx_requires_string_len(__s, __n);
331 const size_type __size = this->size();
333 __throw_out_of_range("basic_string::insert");
334 if (__size > this->max_size() - __n)
335 __throw_length_error("basic_string::insert");
336 if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data())
337 || less<const _CharT*>()(_M_data() + __size, __s))
338 return _M_replace_safe(_M_ibegin() + __pos, _M_ibegin() + __pos,
342 // Work in-place. If _M_mutate reallocates the string, __s
343 // does not point anymore to valid data, therefore we save its
344 // offset, then we restore it.
345 const size_type __off = __s - _M_data();
346 _M_mutate(__pos, 0, __n);
347 __s = _M_data() + __off;
348 _CharT* __p = _M_data() + __pos;
349 if (__s + __n <= __p)
350 traits_type::copy(__p, __s, __n);
352 traits_type::copy(__p, __s + __n, __n);
355 traits_type::copy(__p, __s, __p - __s);
356 traits_type::copy(__p + (__p-__s), __p + __n, __n - (__p-__s));
362 template<typename _CharT, typename _Traits, typename _Alloc>
363 basic_string<_CharT, _Traits, _Alloc>&
364 basic_string<_CharT, _Traits, _Alloc>::
365 replace(size_type __pos, size_type __n1, const _CharT* __s,
368 __glibcxx_requires_string_len(__s, __n2);
369 const size_type __size = this->size();
371 __throw_out_of_range("basic_string::replace");
372 const bool __testn1 = __n1 < __size - __pos;
373 const size_type __foldn1 = __testn1 ? __n1 : __size - __pos;
374 if (__size - __foldn1 > this->max_size() - __n2)
375 __throw_length_error("basic_string::replace");
376 if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data())
377 || less<const _CharT*>()(_M_data() + __size, __s))
378 return _M_replace_safe(_M_ibegin() + __pos,
379 _M_ibegin() + __pos + __foldn1, __s, __s + __n2);
380 // Todo: optimized in-place replace.
382 return _M_replace(_M_ibegin() + __pos, _M_ibegin() + __pos + __foldn1,
384 typename iterator_traits<const _CharT*>::iterator_category());
387 template<typename _CharT, typename _Traits, typename _Alloc>
389 basic_string<_CharT, _Traits, _Alloc>::_Rep::
390 _M_destroy(const _Alloc& __a) throw ()
392 if (this == &_S_empty_rep())
394 const size_type __size = sizeof(_Rep_base) +
395 (this->_M_capacity + 1) * sizeof(_CharT);
396 _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
399 template<typename _CharT, typename _Traits, typename _Alloc>
401 basic_string<_CharT, _Traits, _Alloc>::_M_leak_hard()
403 if (_M_rep() == &_S_empty_rep())
405 if (_M_rep()->_M_is_shared())
407 _M_rep()->_M_set_leaked();
410 // _M_mutate and, below, _M_clone, include, in the same form, an exponential
411 // growth policy, necessary to meet amortized linear time requirements of
412 // the library: see http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
413 // The policy is active for allocations requiring an amount of memory above
414 // system pagesize. This is consistent with the requirements of the standard:
415 // see, f.i., http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
416 template<typename _CharT, typename _Traits, typename _Alloc>
418 basic_string<_CharT, _Traits, _Alloc>::
419 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
421 const size_type __old_size = this->size();
422 const size_type __new_size = __old_size + __len2 - __len1;
423 const _CharT* __src = _M_data() + __pos + __len1;
424 const size_type __how_much = __old_size - __pos - __len1;
426 if (_M_rep() == &_S_empty_rep()
427 || _M_rep()->_M_is_shared() || __new_size > capacity())
430 allocator_type __a = get_allocator();
431 // See below (_S_create) for the meaning and value of these
433 const size_type __pagesize = 4096;
434 const size_type __malloc_header_size = 4 * sizeof (void*);
435 // The biggest string which fits in a memory page
436 const size_type __page_capacity = (__pagesize - __malloc_header_size
437 - sizeof(_Rep) - sizeof(_CharT))
440 if (__new_size > capacity() && __new_size > __page_capacity)
441 // Growing exponentially.
442 __r = _Rep::_S_create(__new_size > 2*capacity() ?
443 __new_size : 2*capacity(), __a);
445 __r = _Rep::_S_create(__new_size, __a);
449 traits_type::copy(__r->_M_refdata(), _M_data(), __pos);
451 traits_type::copy(__r->_M_refdata() + __pos + __len2,
456 __r->_M_dispose(get_allocator());
457 __throw_exception_again;
459 _M_rep()->_M_dispose(__a);
460 _M_data(__r->_M_refdata());
462 else if (__how_much && __len1 != __len2)
465 traits_type::move(_M_data() + __pos + __len2, __src, __how_much);
467 _M_rep()->_M_set_sharable();
468 _M_rep()->_M_length = __new_size;
469 _M_data()[__new_size] = _Rep::_S_terminal; // grrr. (per 21.3.4)
470 // You cannot leave those LWG people alone for a second.
473 template<typename _CharT, typename _Traits, typename _Alloc>
475 basic_string<_CharT, _Traits, _Alloc>::reserve(size_type __res)
477 if (__res > this->capacity() || _M_rep()->_M_is_shared())
479 if (__res > this->max_size())
480 __throw_length_error("basic_string::reserve");
481 // Make sure we don't shrink below the current size
482 if (__res < this->size())
483 __res = this->size();
484 allocator_type __a = get_allocator();
485 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
486 _M_rep()->_M_dispose(__a);
491 template<typename _CharT, typename _Traits, typename _Alloc>
492 void basic_string<_CharT, _Traits, _Alloc>::swap(basic_string& __s)
494 if (_M_rep()->_M_is_leaked())
495 _M_rep()->_M_set_sharable();
496 if (__s._M_rep()->_M_is_leaked())
497 __s._M_rep()->_M_set_sharable();
498 if (this->get_allocator() == __s.get_allocator())
500 _CharT* __tmp = _M_data();
501 _M_data(__s._M_data());
504 // The code below can usually be optimized away.
507 basic_string __tmp1(_M_ibegin(), _M_iend(), __s.get_allocator());
508 basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
509 this->get_allocator());
515 template<typename _CharT, typename _Traits, typename _Alloc>
516 typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
517 basic_string<_CharT, _Traits, _Alloc>::_Rep::
518 _S_create(size_t __capacity, const _Alloc& __alloc)
520 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
521 // _GLIBCXX_RESOLVE_LIB_DEFECTS
522 // 83. String::npos vs. string::max_size()
523 if (__capacity > _S_max_size)
524 __throw_length_error("basic_string::_S_create");
526 // NB: Need an array of char_type[__capacity], plus a
527 // terminating null char_type() element, plus enough for the
528 // _Rep data structure. Whew. Seemingly so needy, yet so elemental.
529 size_t __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep_base);
531 // The standard places no restriction on allocating more memory
532 // than is strictly needed within this layer at the moment or as
533 // requested by an explicit application call to reserve(). Many
534 // malloc implementations perform quite poorly when an
535 // application attempts to allocate memory in a stepwise fashion
536 // growing each allocation size by only 1 char. Additionally,
537 // it makes little sense to allocate less linear memory than the
538 // natural blocking size of the malloc implementation.
539 // Unfortunately, we would need a somewhat low-level calculation
540 // with tuned parameters to get this perfect for any particular
541 // malloc implementation. Fortunately, generalizations about
542 // common features seen among implementations seems to suffice.
544 // __pagesize need not match the actual VM page size for good
545 // results in practice, thus we pick a common value on the low
546 // side. __malloc_header_size is an estimate of the amount of
547 // overhead per memory allocation (in practice seen N * sizeof
548 // (void*) where N is 0, 2 or 4). According to folklore,
549 // picking this value on the high side is better than
550 // low-balling it (especially when this algorithm is used with
551 // malloc implementations that allocate memory blocks rounded up
552 // to a size which is a power of 2).
553 const size_t __pagesize = 4096; // must be 2^i * __subpagesize
554 const size_t __subpagesize = 128; // should be >> __malloc_header_size
555 const size_t __malloc_header_size = 4 * sizeof (void*);
556 if ((__size + __malloc_header_size) > __pagesize)
558 const size_t __extra =
559 (__pagesize - ((__size + __malloc_header_size) % __pagesize))
561 __capacity += __extra / sizeof(_CharT);
562 __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep_base);
564 else if (__size > __subpagesize)
566 const size_t __extra =
567 (__subpagesize - ((__size + __malloc_header_size) % __subpagesize))
569 __capacity += __extra / sizeof(_CharT);
570 __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep_base);
573 // NB: Might throw, but no worries about a leak, mate: _Rep()
575 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
576 _Rep *__p = new (__place) _Rep;
577 __p->_M_capacity = __capacity;
578 __p->_M_set_sharable(); // One reference.
583 template<typename _CharT, typename _Traits, typename _Alloc>
585 basic_string<_CharT, _Traits, _Alloc>::_Rep::
586 _M_clone(const _Alloc& __alloc, size_type __res)
588 // Requested capacity of the clone.
589 const size_type __requested_cap = this->_M_length + __res;
590 // See above (_S_create) for the meaning and value of these constants.
591 const size_type __pagesize = 4096;
592 const size_type __malloc_header_size = 4 * sizeof (void*);
593 // The biggest string which fits in a memory page.
594 const size_type __page_capacity =
595 (__pagesize - __malloc_header_size - sizeof(_Rep_base) - sizeof(_CharT))
598 if (__requested_cap > this->_M_capacity
599 && __requested_cap > __page_capacity)
600 // Growing exponentially.
601 __r = _Rep::_S_create(__requested_cap > 2*this->_M_capacity ?
602 __requested_cap : 2*this->_M_capacity, __alloc);
604 __r = _Rep::_S_create(__requested_cap, __alloc);
610 traits_type::copy(__r->_M_refdata(), _M_refdata(),
615 __r->_M_destroy(__alloc);
616 __throw_exception_again;
619 __r->_M_length = this->_M_length;
620 return __r->_M_refdata();
623 template<typename _CharT, typename _Traits, typename _Alloc>
625 basic_string<_CharT, _Traits, _Alloc>::resize(size_type __n, _CharT __c)
627 if (__n > max_size())
628 __throw_length_error("basic_string::resize");
629 const size_type __size = this->size();
631 this->append(__n - __size, __c);
632 else if (__n < __size)
634 // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
637 template<typename _CharT, typename _Traits, typename _Alloc>
638 basic_string<_CharT, _Traits, _Alloc>&
639 basic_string<_CharT, _Traits, _Alloc>::
640 _M_replace_aux(iterator __i1, iterator __i2, size_type __n2, _CharT __c)
642 const size_type __n1 = __i2 - __i1;
643 const size_type __off1 = __i1 - _M_ibegin();
644 if (max_size() - (this->size() - __n1) <= __n2)
645 __throw_length_error("basic_string::replace");
646 _M_mutate (__off1, __n1, __n2);
647 // Invalidated __i1, __i2
649 traits_type::assign(_M_data() + __off1, __n2, __c);
653 // This is the general replace helper, which currently gets instantiated both
654 // for input iterators and reverse iterators. It buffers internally and then
655 // calls _M_replace_safe.
656 template<typename _CharT, typename _Traits, typename _Alloc>
657 template<typename _InputIterator>
658 basic_string<_CharT, _Traits, _Alloc>&
659 basic_string<_CharT, _Traits, _Alloc>::
660 _M_replace(iterator __i1, iterator __i2, _InputIterator __k1,
661 _InputIterator __k2, input_iterator_tag)
663 // Save concerned source string data in a temporary.
664 const basic_string __s(__k1, __k2);
665 return _M_replace_safe(__i1, __i2, __s._M_ibegin(), __s._M_iend());
668 // This is a special replace helper, which does not buffer internally
669 // and can be used in "safe" situations involving forward iterators,
670 // i.e., when source and destination ranges are known to not overlap.
671 template<typename _CharT, typename _Traits, typename _Alloc>
672 template<typename _ForwardIterator>
673 basic_string<_CharT, _Traits, _Alloc>&
674 basic_string<_CharT, _Traits, _Alloc>::
675 _M_replace_safe(iterator __i1, iterator __i2, _ForwardIterator __k1,
676 _ForwardIterator __k2)
678 const size_type __dnew = static_cast<size_type>(std::distance(__k1, __k2));
679 const size_type __dold = __i2 - __i1;
680 const size_type __dmax = this->max_size();
682 if (__dmax <= __dnew)
683 __throw_length_error("basic_string::_M_replace");
684 const size_type __off = __i1 - _M_ibegin();
685 _M_mutate(__off, __dold, __dnew);
687 // Invalidated __i1, __i2
689 _S_copy_chars(_M_data() + __off, __k1, __k2);
694 template<typename _CharT, typename _Traits, typename _Alloc>
695 basic_string<_CharT, _Traits, _Alloc>&
696 basic_string<_CharT, _Traits, _Alloc>::
697 replace(size_type __pos1, size_type __n1, const basic_string& __str,
698 size_type __pos2, size_type __n2)
700 const size_type __strsize = __str.size();
701 if (__pos2 > __strsize)
702 __throw_out_of_range("basic_string::replace");
703 const bool __testn2 = __n2 < __strsize - __pos2;
704 const size_type __foldn2 = __testn2 ? __n2 : __strsize - __pos2;
705 return this->replace(__pos1, __n1,
706 __str._M_data() + __pos2, __foldn2);
709 template<typename _CharT, typename _Traits, typename _Alloc>
710 basic_string<_CharT, _Traits, _Alloc>&
711 basic_string<_CharT, _Traits, _Alloc>::
712 append(const basic_string& __str)
714 // Iff appending itself, string needs to pre-reserve the
715 // correct size so that _M_mutate does not clobber the
716 // iterators formed here.
717 const size_type __size = __str.size();
718 const size_type __len = __size + this->size();
719 if (__len > this->capacity())
720 this->reserve(__len);
721 return _M_replace_safe(_M_iend(), _M_iend(), __str._M_ibegin(),
725 template<typename _CharT, typename _Traits, typename _Alloc>
726 basic_string<_CharT, _Traits, _Alloc>&
727 basic_string<_CharT, _Traits, _Alloc>::
728 append(const basic_string& __str, size_type __pos, size_type __n)
730 // Iff appending itself, string needs to pre-reserve the
731 // correct size so that _M_mutate does not clobber the
732 // iterators formed here.
733 const size_type __len = std::min(size_type(__str.size() - __pos),
735 if (__len > this->capacity())
736 this->reserve(__len);
737 return _M_replace_safe(_M_iend(), _M_iend(), __str._M_check(__pos),
738 __str._M_fold(__pos, __n));
741 template<typename _CharT, typename _Traits, typename _Alloc>
742 basic_string<_CharT, _Traits, _Alloc>&
743 basic_string<_CharT, _Traits, _Alloc>::
744 append(const _CharT* __s, size_type __n)
746 __glibcxx_requires_string_len(__s, __n);
747 const size_type __len = __n + this->size();
748 if (__len > this->capacity())
749 this->reserve(__len);
750 return _M_replace_safe(_M_iend(), _M_iend(), __s, __s + __n);
753 template<typename _CharT, typename _Traits, typename _Alloc>
754 basic_string<_CharT, _Traits, _Alloc>&
755 basic_string<_CharT, _Traits, _Alloc>::
756 append(size_type __n, _CharT __c)
758 const size_type __len = __n + this->size();
759 if (__len > this->capacity())
760 this->reserve(__len);
761 return this->replace(_M_iend(), _M_iend(), __n, __c);
764 template<typename _CharT, typename _Traits, typename _Alloc>
765 basic_string<_CharT, _Traits, _Alloc>
766 operator+(const _CharT* __lhs,
767 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
769 __glibcxx_requires_string(__lhs);
770 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
771 typedef typename __string_type::size_type __size_type;
772 const __size_type __len = _Traits::length(__lhs);
774 __str.reserve(__len + __rhs.size());
775 __str.append(__lhs, __lhs + __len);
780 template<typename _CharT, typename _Traits, typename _Alloc>
781 basic_string<_CharT, _Traits, _Alloc>
782 operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
784 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
785 typedef typename __string_type::size_type __size_type;
787 const __size_type __len = __rhs.size();
788 __str.reserve(__len + 1);
789 __str.append(__size_type(1), __lhs);
794 template<typename _CharT, typename _Traits, typename _Alloc>
795 typename basic_string<_CharT, _Traits, _Alloc>::size_type
796 basic_string<_CharT, _Traits, _Alloc>::
797 copy(_CharT* __s, size_type __n, size_type __pos) const
799 if (__pos > this->size())
800 __throw_out_of_range("basic_string::copy");
802 if (__n > this->size() - __pos)
803 __n = this->size() - __pos;
805 __glibcxx_requires_string_len(__s, __n);
807 traits_type::copy(__s, _M_data() + __pos, __n);
808 // 21.3.5.7 par 3: do not append null. (good.)
812 template<typename _CharT, typename _Traits, typename _Alloc>
813 typename basic_string<_CharT, _Traits, _Alloc>::size_type
814 basic_string<_CharT, _Traits, _Alloc>::
815 find(const _CharT* __s, size_type __pos, size_type __n) const
817 __glibcxx_requires_string_len(__s, __n);
819 const size_type __size = this->size();
820 size_t __xpos = __pos;
821 const _CharT* __data = _M_data();
822 for (; __xpos + __n <= __size; ++__xpos)
823 if (traits_type::compare(__data + __xpos, __s, __n) == 0)
828 template<typename _CharT, typename _Traits, typename _Alloc>
829 typename basic_string<_CharT, _Traits, _Alloc>::size_type
830 basic_string<_CharT, _Traits, _Alloc>::
831 find(_CharT __c, size_type __pos) const
833 const size_type __size = this->size();
834 size_type __ret = npos;
837 const _CharT* __data = _M_data();
838 const size_type __n = __size - __pos;
839 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
841 __ret = __p - __data;
847 template<typename _CharT, typename _Traits, typename _Alloc>
848 typename basic_string<_CharT, _Traits, _Alloc>::size_type
849 basic_string<_CharT, _Traits, _Alloc>::
850 rfind(const _CharT* __s, size_type __pos, size_type __n) const
852 __glibcxx_requires_string_len(__s, __n);
854 const size_type __size = this->size();
857 __pos = std::min(size_type(__size - __n), __pos);
858 const _CharT* __data = _M_data();
861 if (traits_type::compare(__data + __pos, __s, __n) == 0)
869 template<typename _CharT, typename _Traits, typename _Alloc>
870 typename basic_string<_CharT, _Traits, _Alloc>::size_type
871 basic_string<_CharT, _Traits, _Alloc>::
872 rfind(_CharT __c, size_type __pos) const
874 const size_type __size = this->size();
877 size_t __xpos = __size - 1;
881 for (++__xpos; __xpos-- > 0; )
882 if (traits_type::eq(_M_data()[__xpos], __c))
888 template<typename _CharT, typename _Traits, typename _Alloc>
889 typename basic_string<_CharT, _Traits, _Alloc>::size_type
890 basic_string<_CharT, _Traits, _Alloc>::
891 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
893 __glibcxx_requires_string_len(__s, __n);
895 for (; __n && __pos < this->size(); ++__pos)
897 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
904 template<typename _CharT, typename _Traits, typename _Alloc>
905 typename basic_string<_CharT, _Traits, _Alloc>::size_type
906 basic_string<_CharT, _Traits, _Alloc>::
907 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
909 __glibcxx_requires_string_len(__s, __n);
911 size_type __size = this->size();
914 if (--__size > __pos)
918 if (traits_type::find(__s, __n, _M_data()[__size]))
921 while (__size-- != 0);
926 template<typename _CharT, typename _Traits, typename _Alloc>
927 typename basic_string<_CharT, _Traits, _Alloc>::size_type
928 basic_string<_CharT, _Traits, _Alloc>::
929 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
931 __glibcxx_requires_string_len(__s, __n);
933 size_t __xpos = __pos;
934 for (; __xpos < this->size(); ++__xpos)
935 if (!traits_type::find(__s, __n, _M_data()[__xpos]))
940 template<typename _CharT, typename _Traits, typename _Alloc>
941 typename basic_string<_CharT, _Traits, _Alloc>::size_type
942 basic_string<_CharT, _Traits, _Alloc>::
943 find_first_not_of(_CharT __c, size_type __pos) const
945 size_t __xpos = __pos;
946 for (; __xpos < this->size(); ++__xpos)
947 if (!traits_type::eq(_M_data()[__xpos], __c))
952 template<typename _CharT, typename _Traits, typename _Alloc>
953 typename basic_string<_CharT, _Traits, _Alloc>::size_type
954 basic_string<_CharT, _Traits, _Alloc>::
955 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
957 __glibcxx_requires_string_len(__s, __n);
959 size_type __size = this->size();
962 if (--__size > __pos)
966 if (!traits_type::find(__s, __n, _M_data()[__size]))
974 template<typename _CharT, typename _Traits, typename _Alloc>
975 typename basic_string<_CharT, _Traits, _Alloc>::size_type
976 basic_string<_CharT, _Traits, _Alloc>::
977 find_last_not_of(_CharT __c, size_type __pos) const
979 size_type __size = this->size();
982 if (--__size > __pos)
986 if (!traits_type::eq(_M_data()[__size], __c))
994 template<typename _CharT, typename _Traits, typename _Alloc>
996 basic_string<_CharT, _Traits, _Alloc>::
997 compare(size_type __pos, size_type __n, const basic_string& __str) const
999 const size_type __size = this->size();
1000 const size_type __osize = __str.size();
1002 __throw_out_of_range("basic_string::compare");
1004 const size_type __rsize= std::min(size_type(__size - __pos), __n);
1005 const size_type __len = std::min(__rsize, __osize);
1006 int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
1008 __r = __rsize - __osize;
1012 template<typename _CharT, typename _Traits, typename _Alloc>
1014 basic_string<_CharT, _Traits, _Alloc>::
1015 compare(size_type __pos1, size_type __n1, const basic_string& __str,
1016 size_type __pos2, size_type __n2) const
1018 const size_type __size = this->size();
1019 const size_type __osize = __str.size();
1020 if (__pos1 > __size || __pos2 > __osize)
1021 __throw_out_of_range("basic_string::compare");
1023 const size_type __rsize = std::min(size_type(__size - __pos1), __n1);
1024 const size_type __rosize = std::min(size_type(__osize - __pos2), __n2);
1025 const size_type __len = std::min(__rsize, __rosize);
1026 int __r = traits_type::compare(_M_data() + __pos1,
1027 __str.data() + __pos2, __len);
1029 __r = __rsize - __rosize;
1034 template<typename _CharT, typename _Traits, typename _Alloc>
1036 basic_string<_CharT, _Traits, _Alloc>::
1037 compare(const _CharT* __s) const
1039 __glibcxx_requires_string(__s);
1041 const size_type __size = this->size();
1042 const size_type __osize = traits_type::length(__s);
1043 const size_type __len = std::min(__size, __osize);
1044 int __r = traits_type::compare(_M_data(), __s, __len);
1046 __r = __size - __osize;
1051 template<typename _CharT, typename _Traits, typename _Alloc>
1053 basic_string <_CharT, _Traits, _Alloc>::
1054 compare(size_type __pos, size_type __n1, const _CharT* __s) const
1056 __glibcxx_requires_string(__s);
1058 const size_type __size = this->size();
1060 __throw_out_of_range("basic_string::compare");
1062 const size_type __osize = traits_type::length(__s);
1063 const size_type __rsize = std::min(size_type(__size - __pos), __n1);
1064 const size_type __len = std::min(__rsize, __osize);
1065 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1067 __r = __rsize - __osize;
1071 template<typename _CharT, typename _Traits, typename _Alloc>
1073 basic_string <_CharT, _Traits, _Alloc>::
1074 compare(size_type __pos, size_type __n1, const _CharT* __s,
1075 size_type __n2) const
1077 __glibcxx_requires_string_len(__s, __n2);
1079 const size_type __size = this->size();
1081 __throw_out_of_range("basic_string::compare");
1083 const size_type __osize = std::min(traits_type::length(__s), __n2);
1084 const size_type __rsize = std::min(size_type(__size - __pos), __n1);
1085 const size_type __len = std::min(__rsize, __osize);
1086 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1088 __r = __rsize - __osize;
1092 // Inhibit implicit instantiations for required instantiations,
1093 // which are defined via explicit instantiations elsewhere.
1094 // NB: This syntax is a GNU extension.
1095 #if _GLIBCXX_EXTERN_TEMPLATE
1096 extern template class basic_string<char>;
1098 basic_istream<char>&
1099 operator>>(basic_istream<char>&, string&);
1101 basic_ostream<char>&
1102 operator<<(basic_ostream<char>&, const string&);
1104 basic_istream<char>&
1105 getline(basic_istream<char>&, string&, char);
1107 basic_istream<char>&
1108 getline(basic_istream<char>&, string&);
1110 #ifdef _GLIBCXX_USE_WCHAR_T
1111 extern template class basic_string<wchar_t>;
1113 basic_istream<wchar_t>&
1114 operator>>(basic_istream<wchar_t>&, wstring&);
1116 basic_ostream<wchar_t>&
1117 operator<<(basic_ostream<wchar_t>&, const wstring&);
1119 basic_istream<wchar_t>&
1120 getline(basic_istream<wchar_t>&, wstring&, wchar_t);
1122 basic_istream<wchar_t>&
1123 getline(basic_istream<wchar_t>&, wstring&);