1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
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 2, 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 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 /** @file basic_string.tcc
33 * This is an internal header file, included by other library headers.
34 * You should not attempt to use it directly.
38 // ISO C++ 14882: 21 Strings library
41 // Written by Jason Merrill based upon the specification by Takanori Adachi
42 // in ANSI X3J16/94-0013R2. Rewritten by Nathan Myers to ISO-14882.
44 #ifndef _BASIC_STRING_TCC
45 #define _BASIC_STRING_TCC 1
47 #pragma GCC system_header
49 _GLIBCXX_BEGIN_NAMESPACE(std)
51 template<typename _Type>
53 __is_null_pointer(_Type* __ptr)
54 { return __ptr == 0; }
56 template<typename _Type>
58 __is_null_pointer(_Type)
61 template<typename _CharT, typename _Traits, typename _Alloc>
62 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
63 basic_string<_CharT, _Traits, _Alloc>::
64 _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
66 template<typename _CharT, typename _Traits, typename _Alloc>
68 basic_string<_CharT, _Traits, _Alloc>::
69 _Rep::_S_terminal = _CharT();
71 template<typename _CharT, typename _Traits, typename _Alloc>
72 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
73 basic_string<_CharT, _Traits, _Alloc>::npos;
75 // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
76 // at static init time (before static ctors are run).
77 template<typename _CharT, typename _Traits, typename _Alloc>
78 typename basic_string<_CharT, _Traits, _Alloc>::size_type
79 basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
80 (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
83 // NB: This is the special case for Input Iterators, used in
84 // istreambuf_iterators, etc.
85 // Input Iterators have a cost structure very different from
86 // pointers, calling for a different coding style.
87 template<typename _CharT, typename _Traits, typename _Alloc>
88 template<typename _InIterator>
90 basic_string<_CharT, _Traits, _Alloc>::
91 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
94 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
95 if (__beg == __end && __a == _Alloc())
96 return _S_empty_rep()._M_refdata();
98 // Avoid reallocation for common case.
101 while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
103 __buf[__len++] = *__beg;
106 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
107 _M_copy(__r->_M_refdata(), __buf, __len);
110 while (__beg != __end)
112 if (__len == __r->_M_capacity)
114 // Allocate more space.
115 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
116 _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
117 __r->_M_destroy(__a);
120 __r->_M_refdata()[__len++] = *__beg;
126 __r->_M_destroy(__a);
127 __throw_exception_again;
129 __r->_M_set_length_and_sharable(__len);
130 return __r->_M_refdata();
133 template<typename _CharT, typename _Traits, typename _Alloc>
134 template <typename _InIterator>
136 basic_string<_CharT, _Traits, _Alloc>::
137 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
138 forward_iterator_tag)
140 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
141 if (__beg == __end && __a == _Alloc())
142 return _S_empty_rep()._M_refdata();
144 // NB: Not required, but considered best practice.
145 if (__builtin_expect(__is_null_pointer(__beg) && __beg != __end, 0))
146 __throw_logic_error(__N("basic_string::_S_construct NULL not valid"));
148 const size_type __dnew = static_cast<size_type>(std::distance(__beg,
150 // Check for out_of_range and length_error exceptions.
151 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
153 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
156 __r->_M_destroy(__a);
157 __throw_exception_again;
159 __r->_M_set_length_and_sharable(__dnew);
160 return __r->_M_refdata();
163 template<typename _CharT, typename _Traits, typename _Alloc>
165 basic_string<_CharT, _Traits, _Alloc>::
166 _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
168 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
169 if (__n == 0 && __a == _Alloc())
170 return _S_empty_rep()._M_refdata();
172 // Check for out_of_range and length_error exceptions.
173 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
175 _M_assign(__r->_M_refdata(), __n, __c);
177 __r->_M_set_length_and_sharable(__n);
178 return __r->_M_refdata();
181 template<typename _CharT, typename _Traits, typename _Alloc>
182 basic_string<_CharT, _Traits, _Alloc>::
183 basic_string(const basic_string& __str)
184 : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
185 __str.get_allocator()),
186 __str.get_allocator())
189 template<typename _CharT, typename _Traits, typename _Alloc>
190 basic_string<_CharT, _Traits, _Alloc>::
191 basic_string(const _Alloc& __a)
192 : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
195 template<typename _CharT, typename _Traits, typename _Alloc>
196 basic_string<_CharT, _Traits, _Alloc>::
197 basic_string(const basic_string& __str, size_type __pos, size_type __n)
198 : _M_dataplus(_S_construct(__str._M_data()
199 + __str._M_check(__pos,
200 "basic_string::basic_string"),
201 __str._M_data() + __str._M_limit(__pos, __n)
202 + __pos, _Alloc()), _Alloc())
205 template<typename _CharT, typename _Traits, typename _Alloc>
206 basic_string<_CharT, _Traits, _Alloc>::
207 basic_string(const basic_string& __str, size_type __pos,
208 size_type __n, const _Alloc& __a)
209 : _M_dataplus(_S_construct(__str._M_data()
210 + __str._M_check(__pos,
211 "basic_string::basic_string"),
212 __str._M_data() + __str._M_limit(__pos, __n)
217 template<typename _CharT, typename _Traits, typename _Alloc>
218 basic_string<_CharT, _Traits, _Alloc>::
219 basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
220 : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
224 template<typename _CharT, typename _Traits, typename _Alloc>
225 basic_string<_CharT, _Traits, _Alloc>::
226 basic_string(const _CharT* __s, const _Alloc& __a)
227 : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
228 __s + npos, __a), __a)
231 template<typename _CharT, typename _Traits, typename _Alloc>
232 basic_string<_CharT, _Traits, _Alloc>::
233 basic_string(size_type __n, _CharT __c, const _Alloc& __a)
234 : _M_dataplus(_S_construct(__n, __c, __a), __a)
238 template<typename _CharT, typename _Traits, typename _Alloc>
239 template<typename _InputIterator>
240 basic_string<_CharT, _Traits, _Alloc>::
241 basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a)
242 : _M_dataplus(_S_construct(__beg, __end, __a), __a)
245 template<typename _CharT, typename _Traits, typename _Alloc>
246 basic_string<_CharT, _Traits, _Alloc>&
247 basic_string<_CharT, _Traits, _Alloc>::
248 assign(const basic_string& __str)
250 if (_M_rep() != __str._M_rep())
253 const allocator_type __a = this->get_allocator();
254 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
255 _M_rep()->_M_dispose(__a);
261 template<typename _CharT, typename _Traits, typename _Alloc>
262 basic_string<_CharT, _Traits, _Alloc>&
263 basic_string<_CharT, _Traits, _Alloc>::
264 assign(const _CharT* __s, size_type __n)
266 __glibcxx_requires_string_len(__s, __n);
267 _M_check_length(this->size(), __n, "basic_string::assign");
268 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
269 return _M_replace_safe(size_type(0), this->size(), __s, __n);
273 const size_type __pos = __s - _M_data();
275 _M_copy(_M_data(), __s, __n);
277 _M_move(_M_data(), __s, __n);
278 _M_rep()->_M_set_length_and_sharable(__n);
283 template<typename _CharT, typename _Traits, typename _Alloc>
284 basic_string<_CharT, _Traits, _Alloc>&
285 basic_string<_CharT, _Traits, _Alloc>::
286 append(size_type __n, _CharT __c)
290 _M_check_length(size_type(0), __n, "basic_string::append");
291 const size_type __len = __n + this->size();
292 if (__len > this->capacity() || _M_rep()->_M_is_shared())
293 this->reserve(__len);
294 _M_assign(_M_data() + this->size(), __n, __c);
295 _M_rep()->_M_set_length_and_sharable(__len);
300 template<typename _CharT, typename _Traits, typename _Alloc>
301 basic_string<_CharT, _Traits, _Alloc>&
302 basic_string<_CharT, _Traits, _Alloc>::
303 append(const _CharT* __s, size_type __n)
305 __glibcxx_requires_string_len(__s, __n);
308 _M_check_length(size_type(0), __n, "basic_string::append");
309 const size_type __len = __n + this->size();
310 if (__len > this->capacity() || _M_rep()->_M_is_shared())
312 if (_M_disjunct(__s))
313 this->reserve(__len);
316 const size_type __off = __s - _M_data();
317 this->reserve(__len);
318 __s = _M_data() + __off;
321 _M_copy(_M_data() + this->size(), __s, __n);
322 _M_rep()->_M_set_length_and_sharable(__len);
327 template<typename _CharT, typename _Traits, typename _Alloc>
328 basic_string<_CharT, _Traits, _Alloc>&
329 basic_string<_CharT, _Traits, _Alloc>::
330 append(const basic_string& __str)
332 const size_type __size = __str.size();
335 const size_type __len = __size + this->size();
336 if (__len > this->capacity() || _M_rep()->_M_is_shared())
337 this->reserve(__len);
338 _M_copy(_M_data() + this->size(), __str._M_data(), __size);
339 _M_rep()->_M_set_length_and_sharable(__len);
344 template<typename _CharT, typename _Traits, typename _Alloc>
345 basic_string<_CharT, _Traits, _Alloc>&
346 basic_string<_CharT, _Traits, _Alloc>::
347 append(const basic_string& __str, size_type __pos, size_type __n)
349 __str._M_check(__pos, "basic_string::append");
350 __n = __str._M_limit(__pos, __n);
353 const size_type __len = __n + this->size();
354 if (__len > this->capacity() || _M_rep()->_M_is_shared())
355 this->reserve(__len);
356 _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
357 _M_rep()->_M_set_length_and_sharable(__len);
362 template<typename _CharT, typename _Traits, typename _Alloc>
363 basic_string<_CharT, _Traits, _Alloc>&
364 basic_string<_CharT, _Traits, _Alloc>::
365 insert(size_type __pos, const _CharT* __s, size_type __n)
367 __glibcxx_requires_string_len(__s, __n);
368 _M_check(__pos, "basic_string::insert");
369 _M_check_length(size_type(0), __n, "basic_string::insert");
370 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
371 return _M_replace_safe(__pos, size_type(0), __s, __n);
375 const size_type __off = __s - _M_data();
376 _M_mutate(__pos, 0, __n);
377 __s = _M_data() + __off;
378 _CharT* __p = _M_data() + __pos;
379 if (__s + __n <= __p)
380 _M_copy(__p, __s, __n);
382 _M_copy(__p, __s + __n, __n);
385 const size_type __nleft = __p - __s;
386 _M_copy(__p, __s, __nleft);
387 _M_copy(__p + __nleft, __p + __n, __n - __nleft);
393 template<typename _CharT, typename _Traits, typename _Alloc>
394 basic_string<_CharT, _Traits, _Alloc>&
395 basic_string<_CharT, _Traits, _Alloc>::
396 replace(size_type __pos, size_type __n1, const _CharT* __s,
399 __glibcxx_requires_string_len(__s, __n2);
400 _M_check(__pos, "basic_string::replace");
401 __n1 = _M_limit(__pos, __n1);
402 _M_check_length(__n1, __n2, "basic_string::replace");
404 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
405 return _M_replace_safe(__pos, __n1, __s, __n2);
406 else if ((__left = __s + __n2 <= _M_data() + __pos)
407 || _M_data() + __pos + __n1 <= __s)
409 // Work in-place: non-overlapping case.
410 size_type __off = __s - _M_data();
411 __left ? __off : (__off += __n2 - __n1);
412 _M_mutate(__pos, __n1, __n2);
413 _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
418 // Todo: overlapping case.
419 const basic_string __tmp(__s, __n2);
420 return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
424 template<typename _CharT, typename _Traits, typename _Alloc>
426 basic_string<_CharT, _Traits, _Alloc>::_Rep::
427 _M_destroy(const _Alloc& __a) throw ()
429 const size_type __size = sizeof(_Rep_base) +
430 (this->_M_capacity + 1) * sizeof(_CharT);
431 _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
434 template<typename _CharT, typename _Traits, typename _Alloc>
436 basic_string<_CharT, _Traits, _Alloc>::
439 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
440 if (_M_rep() == &_S_empty_rep())
443 if (_M_rep()->_M_is_shared())
445 _M_rep()->_M_set_leaked();
448 template<typename _CharT, typename _Traits, typename _Alloc>
450 basic_string<_CharT, _Traits, _Alloc>::
451 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
453 const size_type __old_size = this->size();
454 const size_type __new_size = __old_size + __len2 - __len1;
455 const size_type __how_much = __old_size - __pos - __len1;
457 if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
460 const allocator_type __a = get_allocator();
461 _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
464 _M_copy(__r->_M_refdata(), _M_data(), __pos);
466 _M_copy(__r->_M_refdata() + __pos + __len2,
467 _M_data() + __pos + __len1, __how_much);
469 _M_rep()->_M_dispose(__a);
470 _M_data(__r->_M_refdata());
472 else if (__how_much && __len1 != __len2)
475 _M_move(_M_data() + __pos + __len2,
476 _M_data() + __pos + __len1, __how_much);
478 _M_rep()->_M_set_length_and_sharable(__new_size);
481 template<typename _CharT, typename _Traits, typename _Alloc>
483 basic_string<_CharT, _Traits, _Alloc>::
484 reserve(size_type __res)
486 if (__res != this->capacity() || _M_rep()->_M_is_shared())
488 // Make sure we don't shrink below the current size
489 if (__res < this->size())
490 __res = this->size();
491 const allocator_type __a = get_allocator();
492 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
493 _M_rep()->_M_dispose(__a);
498 template<typename _CharT, typename _Traits, typename _Alloc>
500 basic_string<_CharT, _Traits, _Alloc>::
501 swap(basic_string& __s)
503 if (_M_rep()->_M_is_leaked())
504 _M_rep()->_M_set_sharable();
505 if (__s._M_rep()->_M_is_leaked())
506 __s._M_rep()->_M_set_sharable();
507 if (this->get_allocator() == __s.get_allocator())
509 _CharT* __tmp = _M_data();
510 _M_data(__s._M_data());
513 // The code below can usually be optimized away.
516 const basic_string __tmp1(_M_ibegin(), _M_iend(),
517 __s.get_allocator());
518 const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
519 this->get_allocator());
525 template<typename _CharT, typename _Traits, typename _Alloc>
526 typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
527 basic_string<_CharT, _Traits, _Alloc>::_Rep::
528 _S_create(size_type __capacity, size_type __old_capacity,
529 const _Alloc& __alloc)
531 // _GLIBCXX_RESOLVE_LIB_DEFECTS
532 // 83. String::npos vs. string::max_size()
533 if (__capacity > _S_max_size)
534 __throw_length_error(__N("basic_string::_S_create"));
536 // The standard places no restriction on allocating more memory
537 // than is strictly needed within this layer at the moment or as
538 // requested by an explicit application call to reserve().
540 // Many malloc implementations perform quite poorly when an
541 // application attempts to allocate memory in a stepwise fashion
542 // growing each allocation size by only 1 char. Additionally,
543 // it makes little sense to allocate less linear memory than the
544 // natural blocking size of the malloc implementation.
545 // Unfortunately, we would need a somewhat low-level calculation
546 // with tuned parameters to get this perfect for any particular
547 // malloc implementation. Fortunately, generalizations about
548 // common features seen among implementations seems to suffice.
550 // __pagesize need not match the actual VM page size for good
551 // results in practice, thus we pick a common value on the low
552 // side. __malloc_header_size is an estimate of the amount of
553 // overhead per memory allocation (in practice seen N * sizeof
554 // (void*) where N is 0, 2 or 4). According to folklore,
555 // picking this value on the high side is better than
556 // low-balling it (especially when this algorithm is used with
557 // malloc implementations that allocate memory blocks rounded up
558 // to a size which is a power of 2).
559 const size_type __pagesize = 4096;
560 const size_type __malloc_header_size = 4 * sizeof(void*);
562 // The below implements an exponential growth policy, necessary to
563 // meet amortized linear time requirements of the library: see
564 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
565 // It's active for allocations requiring an amount of memory above
566 // system pagesize. This is consistent with the requirements of the
567 // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
568 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
569 __capacity = 2 * __old_capacity;
571 // NB: Need an array of char_type[__capacity], plus a terminating
572 // null char_type() element, plus enough for the _Rep data structure.
573 // Whew. Seemingly so needy, yet so elemental.
574 size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
576 const size_type __adj_size = __size + __malloc_header_size;
577 if (__adj_size > __pagesize && __capacity > __old_capacity)
579 const size_type __extra = __pagesize - __adj_size % __pagesize;
580 __capacity += __extra / sizeof(_CharT);
581 // Never allocate a string bigger than _S_max_size.
582 if (__capacity > _S_max_size)
583 __capacity = _S_max_size;
584 __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
587 // NB: Might throw, but no worries about a leak, mate: _Rep()
589 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
590 _Rep *__p = new (__place) _Rep;
591 __p->_M_capacity = __capacity;
592 // ABI compatibility - 3.4.x set in _S_create both
593 // _M_refcount and _M_length. All callers of _S_create
594 // in basic_string.tcc then set just _M_length.
595 // In 4.0.x and later both _M_refcount and _M_length
596 // are initialized in the callers, unfortunately we can
597 // have 3.4.x compiled code with _S_create callers inlined
598 // calling 4.0.x+ _S_create.
599 __p->_M_set_sharable();
603 template<typename _CharT, typename _Traits, typename _Alloc>
605 basic_string<_CharT, _Traits, _Alloc>::_Rep::
606 _M_clone(const _Alloc& __alloc, size_type __res)
608 // Requested capacity of the clone.
609 const size_type __requested_cap = this->_M_length + __res;
610 _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
613 _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
615 __r->_M_set_length_and_sharable(this->_M_length);
616 return __r->_M_refdata();
619 template<typename _CharT, typename _Traits, typename _Alloc>
621 basic_string<_CharT, _Traits, _Alloc>::
622 resize(size_type __n, _CharT __c)
624 const size_type __size = this->size();
625 _M_check_length(__size, __n, "basic_string::resize");
627 this->append(__n - __size, __c);
628 else if (__n < __size)
630 // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
633 template<typename _CharT, typename _Traits, typename _Alloc>
634 template<typename _InputIterator>
635 basic_string<_CharT, _Traits, _Alloc>&
636 basic_string<_CharT, _Traits, _Alloc>::
637 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
638 _InputIterator __k2, __false_type)
640 const basic_string __s(__k1, __k2);
641 const size_type __n1 = __i2 - __i1;
642 _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
643 return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
647 template<typename _CharT, typename _Traits, typename _Alloc>
648 basic_string<_CharT, _Traits, _Alloc>&
649 basic_string<_CharT, _Traits, _Alloc>::
650 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
653 _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
654 _M_mutate(__pos1, __n1, __n2);
656 _M_assign(_M_data() + __pos1, __n2, __c);
660 template<typename _CharT, typename _Traits, typename _Alloc>
661 basic_string<_CharT, _Traits, _Alloc>&
662 basic_string<_CharT, _Traits, _Alloc>::
663 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
666 _M_mutate(__pos1, __n1, __n2);
668 _M_copy(_M_data() + __pos1, __s, __n2);
672 template<typename _CharT, typename _Traits, typename _Alloc>
673 basic_string<_CharT, _Traits, _Alloc>
674 operator+(const _CharT* __lhs,
675 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
677 __glibcxx_requires_string(__lhs);
678 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
679 typedef typename __string_type::size_type __size_type;
680 const __size_type __len = _Traits::length(__lhs);
682 __str.reserve(__len + __rhs.size());
683 __str.append(__lhs, __len);
688 template<typename _CharT, typename _Traits, typename _Alloc>
689 basic_string<_CharT, _Traits, _Alloc>
690 operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
692 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
693 typedef typename __string_type::size_type __size_type;
695 const __size_type __len = __rhs.size();
696 __str.reserve(__len + 1);
697 __str.append(__size_type(1), __lhs);
702 template<typename _CharT, typename _Traits, typename _Alloc>
703 typename basic_string<_CharT, _Traits, _Alloc>::size_type
704 basic_string<_CharT, _Traits, _Alloc>::
705 copy(_CharT* __s, size_type __n, size_type __pos) const
707 _M_check(__pos, "basic_string::copy");
708 __n = _M_limit(__pos, __n);
709 __glibcxx_requires_string_len(__s, __n);
711 _M_copy(__s, _M_data() + __pos, __n);
712 // 21.3.5.7 par 3: do not append null. (good.)
716 template<typename _CharT, typename _Traits, typename _Alloc>
717 typename basic_string<_CharT, _Traits, _Alloc>::size_type
718 basic_string<_CharT, _Traits, _Alloc>::
719 find(const _CharT* __s, size_type __pos, size_type __n) const
721 __glibcxx_requires_string_len(__s, __n);
722 const size_type __size = this->size();
723 const _CharT* __data = _M_data();
726 return __pos <= __size ? __pos : npos;
730 for (; __pos <= __size - __n; ++__pos)
731 if (traits_type::eq(__data[__pos], __s[0])
732 && traits_type::compare(__data + __pos + 1,
733 __s + 1, __n - 1) == 0)
739 template<typename _CharT, typename _Traits, typename _Alloc>
740 typename basic_string<_CharT, _Traits, _Alloc>::size_type
741 basic_string<_CharT, _Traits, _Alloc>::
742 find(_CharT __c, size_type __pos) const
744 size_type __ret = npos;
745 const size_type __size = this->size();
748 const _CharT* __data = _M_data();
749 const size_type __n = __size - __pos;
750 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
752 __ret = __p - __data;
757 template<typename _CharT, typename _Traits, typename _Alloc>
758 typename basic_string<_CharT, _Traits, _Alloc>::size_type
759 basic_string<_CharT, _Traits, _Alloc>::
760 rfind(const _CharT* __s, size_type __pos, size_type __n) const
762 __glibcxx_requires_string_len(__s, __n);
763 const size_type __size = this->size();
766 __pos = std::min(size_type(__size - __n), __pos);
767 const _CharT* __data = _M_data();
770 if (traits_type::compare(__data + __pos, __s, __n) == 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 rfind(_CharT __c, size_type __pos) const
783 size_type __size = this->size();
786 if (--__size > __pos)
788 for (++__size; __size-- > 0; )
789 if (traits_type::eq(_M_data()[__size], __c))
795 template<typename _CharT, typename _Traits, typename _Alloc>
796 typename basic_string<_CharT, _Traits, _Alloc>::size_type
797 basic_string<_CharT, _Traits, _Alloc>::
798 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
800 __glibcxx_requires_string_len(__s, __n);
801 for (; __n && __pos < this->size(); ++__pos)
803 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
810 template<typename _CharT, typename _Traits, typename _Alloc>
811 typename basic_string<_CharT, _Traits, _Alloc>::size_type
812 basic_string<_CharT, _Traits, _Alloc>::
813 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
815 __glibcxx_requires_string_len(__s, __n);
816 size_type __size = this->size();
819 if (--__size > __pos)
823 if (traits_type::find(__s, __n, _M_data()[__size]))
826 while (__size-- != 0);
831 template<typename _CharT, typename _Traits, typename _Alloc>
832 typename basic_string<_CharT, _Traits, _Alloc>::size_type
833 basic_string<_CharT, _Traits, _Alloc>::
834 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
836 __glibcxx_requires_string_len(__s, __n);
837 for (; __pos < this->size(); ++__pos)
838 if (!traits_type::find(__s, __n, _M_data()[__pos]))
843 template<typename _CharT, typename _Traits, typename _Alloc>
844 typename basic_string<_CharT, _Traits, _Alloc>::size_type
845 basic_string<_CharT, _Traits, _Alloc>::
846 find_first_not_of(_CharT __c, size_type __pos) const
848 for (; __pos < this->size(); ++__pos)
849 if (!traits_type::eq(_M_data()[__pos], __c))
854 template<typename _CharT, typename _Traits, typename _Alloc>
855 typename basic_string<_CharT, _Traits, _Alloc>::size_type
856 basic_string<_CharT, _Traits, _Alloc>::
857 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
859 __glibcxx_requires_string_len(__s, __n);
860 size_type __size = this->size();
863 if (--__size > __pos)
867 if (!traits_type::find(__s, __n, _M_data()[__size]))
875 template<typename _CharT, typename _Traits, typename _Alloc>
876 typename basic_string<_CharT, _Traits, _Alloc>::size_type
877 basic_string<_CharT, _Traits, _Alloc>::
878 find_last_not_of(_CharT __c, size_type __pos) const
880 size_type __size = this->size();
883 if (--__size > __pos)
887 if (!traits_type::eq(_M_data()[__size], __c))
895 template<typename _CharT, typename _Traits, typename _Alloc>
897 basic_string<_CharT, _Traits, _Alloc>::
898 compare(size_type __pos, size_type __n, const basic_string& __str) const
900 _M_check(__pos, "basic_string::compare");
901 __n = _M_limit(__pos, __n);
902 const size_type __osize = __str.size();
903 const size_type __len = std::min(__n, __osize);
904 int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
910 template<typename _CharT, typename _Traits, typename _Alloc>
912 basic_string<_CharT, _Traits, _Alloc>::
913 compare(size_type __pos1, size_type __n1, const basic_string& __str,
914 size_type __pos2, size_type __n2) const
916 _M_check(__pos1, "basic_string::compare");
917 __str._M_check(__pos2, "basic_string::compare");
918 __n1 = _M_limit(__pos1, __n1);
919 __n2 = __str._M_limit(__pos2, __n2);
920 const size_type __len = std::min(__n1, __n2);
921 int __r = traits_type::compare(_M_data() + __pos1,
922 __str.data() + __pos2, __len);
928 template<typename _CharT, typename _Traits, typename _Alloc>
930 basic_string<_CharT, _Traits, _Alloc>::
931 compare(const _CharT* __s) const
933 __glibcxx_requires_string(__s);
934 const size_type __size = this->size();
935 const size_type __osize = traits_type::length(__s);
936 const size_type __len = std::min(__size, __osize);
937 int __r = traits_type::compare(_M_data(), __s, __len);
939 __r = __size - __osize;
943 template<typename _CharT, typename _Traits, typename _Alloc>
945 basic_string <_CharT, _Traits, _Alloc>::
946 compare(size_type __pos, size_type __n1, const _CharT* __s) const
948 __glibcxx_requires_string(__s);
949 _M_check(__pos, "basic_string::compare");
950 __n1 = _M_limit(__pos, __n1);
951 const size_type __osize = traits_type::length(__s);
952 const size_type __len = std::min(__n1, __osize);
953 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
955 __r = __n1 - __osize;
959 template<typename _CharT, typename _Traits, typename _Alloc>
961 basic_string <_CharT, _Traits, _Alloc>::
962 compare(size_type __pos, size_type __n1, const _CharT* __s,
963 size_type __n2) const
965 __glibcxx_requires_string_len(__s, __n2);
966 _M_check(__pos, "basic_string::compare");
967 __n1 = _M_limit(__pos, __n1);
968 const size_type __len = std::min(__n1, __n2);
969 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
975 // Inhibit implicit instantiations for required instantiations,
976 // which are defined via explicit instantiations elsewhere.
977 // NB: This syntax is a GNU extension.
978 #if _GLIBCXX_EXTERN_TEMPLATE
979 extern template class basic_string<char>;
982 operator>>(basic_istream<char>&, string&);
985 operator<<(basic_ostream<char>&, const string&);
988 getline(basic_istream<char>&, string&, char);
991 getline(basic_istream<char>&, string&);
993 #ifdef _GLIBCXX_USE_WCHAR_T
994 extern template class basic_string<wchar_t>;
996 basic_istream<wchar_t>&
997 operator>>(basic_istream<wchar_t>&, wstring&);
999 basic_ostream<wchar_t>&
1000 operator<<(basic_ostream<wchar_t>&, const wstring&);
1002 basic_istream<wchar_t>&
1003 getline(basic_istream<wchar_t>&, wstring&, wchar_t);
1005 basic_istream<wchar_t>&
1006 getline(basic_istream<wchar_t>&, wstring&);
1010 _GLIBCXX_END_NAMESPACE