1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009
5 // Free Software Foundation, Inc.
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)
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.
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.
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/>.
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.
33 // ISO C++ 14882: 21 Strings library
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.
39 #ifndef _BASIC_STRING_TCC
40 #define _BASIC_STRING_TCC 1
42 #pragma GCC system_header
44 #include <cxxabi-forced.h>
46 _GLIBCXX_BEGIN_NAMESPACE(std)
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;
53 template<typename _CharT, typename _Traits, typename _Alloc>
55 basic_string<_CharT, _Traits, _Alloc>::
56 _Rep::_S_terminal = _CharT();
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;
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) /
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>
77 basic_string<_CharT, _Traits, _Alloc>::
78 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
81 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
82 if (__beg == __end && __a == _Alloc())
83 return _S_empty_rep()._M_refdata();
85 // Avoid reallocation for common case.
88 while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
90 __buf[__len++] = *__beg;
93 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
94 _M_copy(__r->_M_refdata(), __buf, __len);
97 while (__beg != __end)
99 if (__len == __r->_M_capacity)
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);
107 __r->_M_refdata()[__len++] = *__beg;
113 __r->_M_destroy(__a);
114 __throw_exception_again;
116 __r->_M_set_length_and_sharable(__len);
117 return __r->_M_refdata();
120 template<typename _CharT, typename _Traits, typename _Alloc>
121 template <typename _FwdIterator>
123 basic_string<_CharT, _Traits, _Alloc>::
124 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
125 forward_iterator_tag)
127 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
128 if (__beg == __end && __a == _Alloc())
129 return _S_empty_rep()._M_refdata();
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"));
135 const size_type __dnew = static_cast<size_type>(std::distance(__beg,
137 // Check for out_of_range and length_error exceptions.
138 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
140 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
143 __r->_M_destroy(__a);
144 __throw_exception_again;
146 __r->_M_set_length_and_sharable(__dnew);
147 return __r->_M_refdata();
150 template<typename _CharT, typename _Traits, typename _Alloc>
152 basic_string<_CharT, _Traits, _Alloc>::
153 _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
155 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
156 if (__n == 0 && __a == _Alloc())
157 return _S_empty_rep()._M_refdata();
159 // Check for out_of_range and length_error exceptions.
160 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
162 _M_assign(__r->_M_refdata(), __n, __c);
164 __r->_M_set_length_and_sharable(__n);
165 return __r->_M_refdata();
168 template<typename _CharT, typename _Traits, typename _Alloc>
170 basic_string<_CharT, _Traits, _Alloc>::
171 _S_construct(const _CharT* __s, size_type __n, const _Alloc& __a)
173 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
174 if (__n == 0 && __a == _Alloc())
175 return _S_empty_rep()._M_refdata();
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"));
181 // Check for out_of_range and length_error exceptions.
182 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
184 _M_copy(__r->_M_refdata(), __s, __n);
186 __r->_M_set_length_and_sharable(__n);
187 return __r->_M_refdata();
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())
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)
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())
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)
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)
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,
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)
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,
250 : _M_dataplus(_S_construct(__beg, __end, __a), __a)
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)
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)
266 if (_M_rep() != __str._M_rep())
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);
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)
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);
289 const size_type __pos = __s - _M_data();
291 _M_copy(_M_data(), __s, __n);
293 _M_move(_M_data(), __s, __n);
294 _M_rep()->_M_set_length_and_sharable(__n);
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)
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);
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)
321 __glibcxx_requires_string_len(__s, __n);
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())
328 if (_M_disjunct(__s))
329 this->reserve(__len);
332 const size_type __off = __s - _M_data();
333 this->reserve(__len);
334 __s = _M_data() + __off;
337 _M_copy(_M_data() + this->size(), __s, __n);
338 _M_rep()->_M_set_length_and_sharable(__len);
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)
348 const size_type __size = __str.size();
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);
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)
365 __str._M_check(__pos, "basic_string::append");
366 __n = __str._M_limit(__pos, __n);
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);
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)
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);
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);
398 _M_copy(__p, __s + __n, __n);
401 const size_type __nleft = __p - __s;
402 _M_copy(__p, __s, __nleft);
403 _M_copy(__p + __nleft, __p + __n, __n - __nleft);
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)
414 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
415 && __last <= _M_iend());
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;
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);
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,
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");
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)
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);
457 // Todo: overlapping case.
458 const basic_string __tmp(__s, __n2);
459 return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
463 template<typename _CharT, typename _Traits, typename _Alloc>
465 basic_string<_CharT, _Traits, _Alloc>::_Rep::
466 _M_destroy(const _Alloc& __a) throw ()
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);
473 template<typename _CharT, typename _Traits, typename _Alloc>
475 basic_string<_CharT, _Traits, _Alloc>::
478 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
479 if (_M_rep() == &_S_empty_rep())
482 if (_M_rep()->_M_is_shared())
484 _M_rep()->_M_set_leaked();
487 template<typename _CharT, typename _Traits, typename _Alloc>
489 basic_string<_CharT, _Traits, _Alloc>::
490 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
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;
496 if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
499 const allocator_type __a = get_allocator();
500 _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
503 _M_copy(__r->_M_refdata(), _M_data(), __pos);
505 _M_copy(__r->_M_refdata() + __pos + __len2,
506 _M_data() + __pos + __len1, __how_much);
508 _M_rep()->_M_dispose(__a);
509 _M_data(__r->_M_refdata());
511 else if (__how_much && __len1 != __len2)
514 _M_move(_M_data() + __pos + __len2,
515 _M_data() + __pos + __len1, __how_much);
517 _M_rep()->_M_set_length_and_sharable(__new_size);
520 template<typename _CharT, typename _Traits, typename _Alloc>
522 basic_string<_CharT, _Traits, _Alloc>::
523 reserve(size_type __res)
525 if (__res != this->capacity() || _M_rep()->_M_is_shared())
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);
537 template<typename _CharT, typename _Traits, typename _Alloc>
539 basic_string<_CharT, _Traits, _Alloc>::
540 swap(basic_string& __s)
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())
548 _CharT* __tmp = _M_data();
549 _M_data(__s._M_data());
552 // The code below can usually be optimized away.
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());
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)
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"));
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().
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.
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*);
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;
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);
615 const size_type __adj_size = __size + __malloc_header_size;
616 if (__adj_size > __pagesize && __capacity > __old_capacity)
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);
626 // NB: Might throw, but no worries about a leak, mate: _Rep()
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();
642 template<typename _CharT, typename _Traits, typename _Alloc>
644 basic_string<_CharT, _Traits, _Alloc>::_Rep::
645 _M_clone(const _Alloc& __alloc, size_type __res)
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,
652 _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
654 __r->_M_set_length_and_sharable(this->_M_length);
655 return __r->_M_refdata();
658 template<typename _CharT, typename _Traits, typename _Alloc>
660 basic_string<_CharT, _Traits, _Alloc>::
661 resize(size_type __n, _CharT __c)
663 const size_type __size = this->size();
664 _M_check_length(__size, __n, "basic_string::resize");
666 this->append(__n - __size, __c);
667 else if (__n < __size)
669 // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
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)
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(),
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,
692 _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
693 _M_mutate(__pos1, __n1, __n2);
695 _M_assign(_M_data() + __pos1, __n2, __c);
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,
705 _M_mutate(__pos1, __n1, __n2);
707 _M_copy(_M_data() + __pos1, __s, __n2);
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)
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);
721 __str.reserve(__len + __rhs.size());
722 __str.append(__lhs, __len);
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)
731 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
732 typedef typename __string_type::size_type __size_type;
734 const __size_type __len = __rhs.size();
735 __str.reserve(__len + 1);
736 __str.append(__size_type(1), __lhs);
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
746 _M_check(__pos, "basic_string::copy");
747 __n = _M_limit(__pos, __n);
748 __glibcxx_requires_string_len(__s, __n);
750 _M_copy(__s, _M_data() + __pos, __n);
751 // 21.3.5.7 par 3: do not append null. (good.)
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
760 __glibcxx_requires_string_len(__s, __n);
761 const size_type __size = this->size();
762 const _CharT* __data = _M_data();
765 return __pos <= __size ? __pos : npos;
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)
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
783 size_type __ret = npos;
784 const size_type __size = this->size();
787 const _CharT* __data = _M_data();
788 const size_type __n = __size - __pos;
789 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
791 __ret = __p - __data;
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
801 __glibcxx_requires_string_len(__s, __n);
802 const size_type __size = this->size();
805 __pos = std::min(size_type(__size - __n), __pos);
806 const _CharT* __data = _M_data();
809 if (traits_type::compare(__data + __pos, __s, __n) == 0)
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
822 size_type __size = this->size();
825 if (--__size > __pos)
827 for (++__size; __size-- > 0; )
828 if (traits_type::eq(_M_data()[__size], __c))
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
839 __glibcxx_requires_string_len(__s, __n);
840 for (; __n && __pos < this->size(); ++__pos)
842 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
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
854 __glibcxx_requires_string_len(__s, __n);
855 size_type __size = this->size();
858 if (--__size > __pos)
862 if (traits_type::find(__s, __n, _M_data()[__size]))
865 while (__size-- != 0);
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
875 __glibcxx_requires_string_len(__s, __n);
876 for (; __pos < this->size(); ++__pos)
877 if (!traits_type::find(__s, __n, _M_data()[__pos]))
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
887 for (; __pos < this->size(); ++__pos)
888 if (!traits_type::eq(_M_data()[__pos], __c))
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
898 __glibcxx_requires_string_len(__s, __n);
899 size_type __size = this->size();
902 if (--__size > __pos)
906 if (!traits_type::find(__s, __n, _M_data()[__size]))
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
919 size_type __size = this->size();
922 if (--__size > __pos)
926 if (!traits_type::eq(_M_data()[__size], __c))
934 template<typename _CharT, typename _Traits, typename _Alloc>
936 basic_string<_CharT, _Traits, _Alloc>::
937 compare(size_type __pos, size_type __n, const basic_string& __str) const
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);
945 __r = _S_compare(__n, __osize);
949 template<typename _CharT, typename _Traits, typename _Alloc>
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
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);
963 __r = _S_compare(__n1, __n2);
967 template<typename _CharT, typename _Traits, typename _Alloc>
969 basic_string<_CharT, _Traits, _Alloc>::
970 compare(const _CharT* __s) const
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);
978 __r = _S_compare(__size, __osize);
982 template<typename _CharT, typename _Traits, typename _Alloc>
984 basic_string <_CharT, _Traits, _Alloc>::
985 compare(size_type __pos, size_type __n1, const _CharT* __s) const
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);
994 __r = _S_compare(__n1, __osize);
998 template<typename _CharT, typename _Traits, typename _Alloc>
1000 basic_string <_CharT, _Traits, _Alloc>::
1001 compare(size_type __pos, size_type __n1, const _CharT* __s,
1002 size_type __n2) const
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);
1010 __r = _S_compare(__n1, __n2);
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)
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;
1028 __size_type __extracted = 0;
1029 typename __ios_base::iostate __err = __ios_base::goodbit;
1030 typename __istream_type::sentry __cerb(__in, false);
1035 // Avoid reallocation for common case.
1038 __size_type __len = 0;
1039 const streamsize __w = __in.width();
1040 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
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();
1046 while (__extracted < __n
1047 && !_Traits::eq_int_type(__c, __eof)
1048 && !__ct.is(__ctype_base::space,
1049 _Traits::to_char_type(__c)))
1051 if (__len == sizeof(__buf) / sizeof(_CharT))
1053 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
1056 __buf[__len++] = _Traits::to_char_type(__c);
1058 __c = __in.rdbuf()->snextc();
1060 __str.append(__buf, __len);
1062 if (_Traits::eq_int_type(__c, __eof))
1063 __err |= __ios_base::eofbit;
1066 __catch(__cxxabiv1::__forced_unwind&)
1068 __in._M_setstate(__ios_base::badbit);
1069 __throw_exception_again;
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);
1079 // 211. operator>>(istream&, string&) doesn't set failbit
1081 __err |= __ios_base::failbit;
1083 __in.setstate(__err);
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)
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;
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);
1107 const __int_type __idelim = _Traits::to_int_type(__delim);
1108 const __int_type __eof = _Traits::eof();
1109 __int_type __c = __in.rdbuf()->sgetc();
1111 while (__extracted < __n
1112 && !_Traits::eq_int_type(__c, __eof)
1113 && !_Traits::eq_int_type(__c, __idelim))
1115 __str += _Traits::to_char_type(__c);
1117 __c = __in.rdbuf()->snextc();
1120 if (_Traits::eq_int_type(__c, __eof))
1121 __err |= __ios_base::eofbit;
1122 else if (_Traits::eq_int_type(__c, __idelim))
1125 __in.rdbuf()->sbumpc();
1128 __err |= __ios_base::failbit;
1130 __catch(__cxxabiv1::__forced_unwind&)
1132 __in._M_setstate(__ios_base::badbit);
1133 __throw_exception_again;
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);
1144 __err |= __ios_base::failbit;
1146 __in.setstate(__err);
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>;
1156 basic_istream<char>&
1157 operator>>(basic_istream<char>&, string&);
1159 basic_ostream<char>&
1160 operator<<(basic_ostream<char>&, const string&);
1162 basic_istream<char>&
1163 getline(basic_istream<char>&, string&, char);
1165 basic_istream<char>&
1166 getline(basic_istream<char>&, string&);
1168 #ifdef _GLIBCXX_USE_WCHAR_T
1169 extern template class basic_string<wchar_t>;
1171 basic_istream<wchar_t>&
1172 operator>>(basic_istream<wchar_t>&, wstring&);
1174 basic_ostream<wchar_t>&
1175 operator<<(basic_ostream<wchar_t>&, const wstring&);
1177 basic_istream<wchar_t>&
1178 getline(basic_istream<wchar_t>&, wstring&, wchar_t);
1180 basic_istream<wchar_t>&
1181 getline(basic_istream<wchar_t>&, wstring&);
1185 _GLIBCXX_END_NAMESPACE