1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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(_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 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
92 if (__beg == __end && __a == _Alloc())
93 return _S_empty_rep()._M_refdata();
95 // Avoid reallocation for common case.
98 while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
100 __buf[__len++] = *__beg;
103 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
104 traits_type::copy(__r->_M_refdata(), __buf, __len);
107 while (__beg != __end)
109 if (__len == __r->_M_capacity)
111 // Allocate more space.
112 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
113 traits_type::copy(__another->_M_refdata(),
114 __r->_M_refdata(), __len);
115 __r->_M_destroy(__a);
118 __r->_M_refdata()[__len++] = *__beg;
124 __r->_M_destroy(__a);
125 __throw_exception_again;
127 __r->_M_length = __len;
128 __r->_M_refdata()[__len] = _Rep::_S_terminal; // grrr.
129 return __r->_M_refdata();
132 template<typename _CharT, typename _Traits, typename _Alloc>
133 template <typename _InIterator>
135 basic_string<_CharT, _Traits, _Alloc>::
136 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
137 forward_iterator_tag)
139 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
140 if (__beg == __end && __a == _Alloc())
141 return _S_empty_rep()._M_refdata();
143 // NB: Not required, but considered best practice.
144 if (__builtin_expect(__is_null_pointer(__beg) && __beg != __end, 0))
145 __throw_logic_error(__N("basic_string::_S_construct NULL not valid"));
147 const size_type __dnew = static_cast<size_type>(std::distance(__beg,
149 // Check for out_of_range and length_error exceptions.
150 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
152 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
155 __r->_M_destroy(__a);
156 __throw_exception_again;
158 __r->_M_length = __dnew;
159 __r->_M_refdata()[__dnew] = _Rep::_S_terminal; // grrr.
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 traits_type::assign(__r->_M_refdata(), __n, __c);
177 __r->_M_length = __n;
178 __r->_M_refdata()[__n] = _Rep::_S_terminal; // grrr
179 return __r->_M_refdata();
182 template<typename _CharT, typename _Traits, typename _Alloc>
183 basic_string<_CharT, _Traits, _Alloc>::
184 basic_string(const basic_string& __str)
185 : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
186 __str.get_allocator()),
187 __str.get_allocator())
190 template<typename _CharT, typename _Traits, typename _Alloc>
191 basic_string<_CharT, _Traits, _Alloc>::
192 basic_string(const _Alloc& __a)
193 : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
196 template<typename _CharT, typename _Traits, typename _Alloc>
197 basic_string<_CharT, _Traits, _Alloc>::
198 basic_string(const basic_string& __str, size_type __pos, size_type __n)
199 : _M_dataplus(_S_construct(__str._M_data()
200 + __str._M_check(__pos,
201 "basic_string::basic_string"),
202 __str._M_data() + __str._M_limit(__pos, __n)
203 + __pos, _Alloc()), _Alloc())
206 template<typename _CharT, typename _Traits, typename _Alloc>
207 basic_string<_CharT, _Traits, _Alloc>::
208 basic_string(const basic_string& __str, size_type __pos,
209 size_type __n, const _Alloc& __a)
210 : _M_dataplus(_S_construct(__str._M_data()
211 + __str._M_check(__pos,
212 "basic_string::basic_string"),
213 __str._M_data() + __str._M_limit(__pos, __n)
218 template<typename _CharT, typename _Traits, typename _Alloc>
219 basic_string<_CharT, _Traits, _Alloc>::
220 basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
221 : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
225 template<typename _CharT, typename _Traits, typename _Alloc>
226 basic_string<_CharT, _Traits, _Alloc>::
227 basic_string(const _CharT* __s, const _Alloc& __a)
228 : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
229 __s + npos, __a), __a)
232 template<typename _CharT, typename _Traits, typename _Alloc>
233 basic_string<_CharT, _Traits, _Alloc>::
234 basic_string(size_type __n, _CharT __c, const _Alloc& __a)
235 : _M_dataplus(_S_construct(__n, __c, __a), __a)
239 template<typename _CharT, typename _Traits, typename _Alloc>
240 template<typename _InputIterator>
241 basic_string<_CharT, _Traits, _Alloc>::
242 basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a)
243 : _M_dataplus(_S_construct(__beg, __end, __a), __a)
246 template<typename _CharT, typename _Traits, typename _Alloc>
247 basic_string<_CharT, _Traits, _Alloc>&
248 basic_string<_CharT, _Traits, _Alloc>::
249 assign(const basic_string& __str)
251 if (_M_rep() != __str._M_rep())
254 const allocator_type __a = this->get_allocator();
255 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
256 _M_rep()->_M_dispose(__a);
262 template<typename _CharT, typename _Traits, typename _Alloc>
263 basic_string<_CharT, _Traits, _Alloc>&
264 basic_string<_CharT, _Traits, _Alloc>::
265 assign(const _CharT* __s, size_type __n)
267 __glibcxx_requires_string_len(__s, __n);
268 if (__n > this->max_size())
269 __throw_length_error(__N("basic_string::assign"));
270 if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data())
271 || less<const _CharT*>()(_M_data() + this->size(), __s))
272 return _M_replace_safe(size_type(0), this->size(), __s, __n);
276 const size_type __pos = __s - _M_data();
278 traits_type::copy(_M_data(), __s, __n);
280 traits_type::move(_M_data(), __s, __n);
281 _M_rep()->_M_set_sharable();
282 _M_rep()->_M_length = __n;
283 _M_data()[__n] = _Rep::_S_terminal; // grr.
288 template<typename _CharT, typename _Traits, typename _Alloc>
289 basic_string<_CharT, _Traits, _Alloc>&
290 basic_string<_CharT, _Traits, _Alloc>::
291 insert(size_type __pos, const _CharT* __s, size_type __n)
293 __glibcxx_requires_string_len(__s, __n);
294 _M_check(__pos, "basic_string::insert");
295 if (this->max_size() - this->size() < __n)
296 __throw_length_error(__N("basic_string::insert"));
297 if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data())
298 || less<const _CharT*>()(_M_data() + this->size(), __s))
299 return _M_replace_safe(__pos, size_type(0), __s, __n);
302 // Work in-place. If _M_mutate reallocates the string, __s
303 // does not point anymore to valid data, therefore we save its
304 // offset, then we restore it.
305 const size_type __off = __s - _M_data();
306 _M_mutate(__pos, 0, __n);
307 __s = _M_data() + __off;
308 _CharT* __p = _M_data() + __pos;
309 if (__s + __n <= __p)
310 traits_type::copy(__p, __s, __n);
312 traits_type::copy(__p, __s + __n, __n);
315 const size_type __nleft = __p - __s;
316 traits_type::copy(__p, __s, __nleft);
317 traits_type::copy(__p + __nleft, __p + __n, __n - __nleft);
323 template<typename _CharT, typename _Traits, typename _Alloc>
324 basic_string<_CharT, _Traits, _Alloc>&
325 basic_string<_CharT, _Traits, _Alloc>::
326 replace(size_type __pos, size_type __n1, const _CharT* __s,
329 __glibcxx_requires_string_len(__s, __n2);
330 _M_check(__pos, "basic_string::replace");
331 __n1 = _M_limit(__pos, __n1);
332 if (this->max_size() - (this->size() - __n1) < __n2)
333 __throw_length_error(__N("basic_string::replace"));
335 if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data())
336 || less<const _CharT*>()(_M_data() + this->size(), __s))
337 return _M_replace_safe(__pos, __n1, __s, __n2);
338 else if ((__left = __s + __n2 <= _M_data() + __pos)
339 || _M_data() + __pos + __n1 <= __s)
341 // Work in-place: non-overlapping case.
342 const size_type __off = __s - _M_data();
343 _M_mutate(__pos, __n1, __n2);
345 traits_type::copy(_M_data() + __pos,
346 _M_data() + __off, __n2);
348 traits_type::copy(_M_data() + __pos,
349 _M_data() + __off + __n2 - __n1, __n2);
354 // Todo: overlapping case.
355 const basic_string __tmp(__s, __n2);
356 return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
360 template<typename _CharT, typename _Traits, typename _Alloc>
362 basic_string<_CharT, _Traits, _Alloc>::_Rep::
363 _M_destroy(const _Alloc& __a) throw ()
365 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
366 if (this == &_S_empty_rep())
369 const size_type __size = sizeof(_Rep_base) +
370 (this->_M_capacity + 1) * sizeof(_CharT);
371 _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
374 template<typename _CharT, typename _Traits, typename _Alloc>
376 basic_string<_CharT, _Traits, _Alloc>::_M_leak_hard()
378 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
379 if (_M_rep() == &_S_empty_rep())
382 if (_M_rep()->_M_is_shared())
384 _M_rep()->_M_set_leaked();
387 template<typename _CharT, typename _Traits, typename _Alloc>
389 basic_string<_CharT, _Traits, _Alloc>::
390 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
392 const size_type __old_size = this->size();
393 const size_type __new_size = __old_size + __len2 - __len1;
394 const size_type __how_much = __old_size - __pos - __len1;
396 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
397 if (_M_rep() == &_S_empty_rep()
398 || _M_rep()->_M_is_shared() || __new_size > capacity())
400 if (_M_rep()->_M_is_shared() || __new_size > capacity())
404 const allocator_type __a = get_allocator();
405 _Rep* __r = _Rep::_S_create(__new_size, capacity(), __a);
408 traits_type::copy(__r->_M_refdata(), _M_data(), __pos);
410 traits_type::copy(__r->_M_refdata() + __pos + __len2,
411 _M_data() + __pos + __len1, __how_much);
413 _M_rep()->_M_dispose(__a);
414 _M_data(__r->_M_refdata());
416 else if (__how_much && __len1 != __len2)
419 traits_type::move(_M_data() + __pos + __len2,
420 _M_data() + __pos + __len1, __how_much);
422 _M_rep()->_M_set_sharable();
423 _M_rep()->_M_length = __new_size;
424 _M_data()[__new_size] = _Rep::_S_terminal; // grrr. (per 21.3.4)
425 // You cannot leave those LWG people alone for a second.
428 template<typename _CharT, typename _Traits, typename _Alloc>
430 basic_string<_CharT, _Traits, _Alloc>::reserve(size_type __res)
432 if (__res != this->capacity() || _M_rep()->_M_is_shared())
434 if (__res > this->max_size())
435 __throw_length_error(__N("basic_string::reserve"));
436 // Make sure we don't shrink below the current size
437 if (__res < this->size())
438 __res = this->size();
439 const allocator_type __a = get_allocator();
440 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
441 _M_rep()->_M_dispose(__a);
446 template<typename _CharT, typename _Traits, typename _Alloc>
447 void basic_string<_CharT, _Traits, _Alloc>::swap(basic_string& __s)
449 if (_M_rep()->_M_is_leaked())
450 _M_rep()->_M_set_sharable();
451 if (__s._M_rep()->_M_is_leaked())
452 __s._M_rep()->_M_set_sharable();
453 if (this->get_allocator() == __s.get_allocator())
455 _CharT* __tmp = _M_data();
456 _M_data(__s._M_data());
459 // The code below can usually be optimized away.
462 const basic_string __tmp1(_M_ibegin(), _M_iend(),
463 __s.get_allocator());
464 const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
465 this->get_allocator());
471 template<typename _CharT, typename _Traits, typename _Alloc>
472 typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
473 basic_string<_CharT, _Traits, _Alloc>::_Rep::
474 _S_create(size_type __capacity, size_type __old_capacity,
475 const _Alloc& __alloc)
477 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
478 // _GLIBCXX_RESOLVE_LIB_DEFECTS
479 // 83. String::npos vs. string::max_size()
480 if (__capacity > _S_max_size)
481 __throw_length_error(__N("basic_string::_S_create"));
483 // The standard places no restriction on allocating more memory
484 // than is strictly needed within this layer at the moment or as
485 // requested by an explicit application call to reserve().
487 // Many malloc implementations perform quite poorly when an
488 // application attempts to allocate memory in a stepwise fashion
489 // growing each allocation size by only 1 char. Additionally,
490 // it makes little sense to allocate less linear memory than the
491 // natural blocking size of the malloc implementation.
492 // Unfortunately, we would need a somewhat low-level calculation
493 // with tuned parameters to get this perfect for any particular
494 // malloc implementation. Fortunately, generalizations about
495 // common features seen among implementations seems to suffice.
497 // __pagesize need not match the actual VM page size for good
498 // results in practice, thus we pick a common value on the low
499 // side. __malloc_header_size is an estimate of the amount of
500 // overhead per memory allocation (in practice seen N * sizeof
501 // (void*) where N is 0, 2 or 4). According to folklore,
502 // picking this value on the high side is better than
503 // low-balling it (especially when this algorithm is used with
504 // malloc implementations that allocate memory blocks rounded up
505 // to a size which is a power of 2).
506 const size_type __pagesize = 4096;
507 const size_type __malloc_header_size = 4 * sizeof(void*);
509 // The below implements an exponential growth policy, necessary to
510 // meet amortized linear time requirements of the library: see
511 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
512 // It's active for allocations requiring an amount of memory above
513 // system pagesize. This is consistent with the requirements of the
514 // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
515 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
516 __capacity = 2 * __old_capacity;
518 // NB: Need an array of char_type[__capacity], plus a terminating
519 // null char_type() element, plus enough for the _Rep data structure.
520 // Whew. Seemingly so needy, yet so elemental.
521 size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
523 const size_type __adj_size = __size + __malloc_header_size;
524 if (__adj_size > __pagesize)
526 const size_type __extra = __pagesize - __adj_size % __pagesize;
527 __capacity += __extra / sizeof(_CharT);
528 // Never allocate a string bigger than _S_max_size.
529 if (__capacity > _S_max_size)
530 __capacity = _S_max_size;
531 __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
534 // NB: Might throw, but no worries about a leak, mate: _Rep()
536 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
537 _Rep *__p = new (__place) _Rep;
538 __p->_M_capacity = __capacity;
539 __p->_M_set_sharable(); // One reference.
544 template<typename _CharT, typename _Traits, typename _Alloc>
546 basic_string<_CharT, _Traits, _Alloc>::_Rep::
547 _M_clone(const _Alloc& __alloc, size_type __res)
549 // Requested capacity of the clone.
550 const size_type __requested_cap = this->_M_length + __res;
551 _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
554 traits_type::copy(__r->_M_refdata(), _M_refdata(),
557 __r->_M_length = this->_M_length;
558 __r->_M_refdata()[this->_M_length] = _Rep::_S_terminal;
559 return __r->_M_refdata();
562 template<typename _CharT, typename _Traits, typename _Alloc>
564 basic_string<_CharT, _Traits, _Alloc>::resize(size_type __n, _CharT __c)
566 if (__n > max_size())
567 __throw_length_error(__N("basic_string::resize"));
568 const size_type __size = this->size();
570 this->append(__n - __size, __c);
571 else if (__n < __size)
573 // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
576 template<typename _CharT, typename _Traits, typename _Alloc>
577 template<typename _InputIterator>
578 basic_string<_CharT, _Traits, _Alloc>&
579 basic_string<_CharT, _Traits, _Alloc>::
580 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
581 _InputIterator __k2, __false_type)
583 const basic_string __s(__k1, __k2);
584 const size_type __n1 = __i2 - __i1;
585 if (this->max_size() - (this->size() - __n1) < __s.size())
586 __throw_length_error(__N("basic_string::_M_replace_dispatch"));
587 return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
591 template<typename _CharT, typename _Traits, typename _Alloc>
592 basic_string<_CharT, _Traits, _Alloc>&
593 basic_string<_CharT, _Traits, _Alloc>::
594 append(const basic_string& __str)
596 // Iff appending itself, string needs to pre-reserve the
597 // correct size so that _M_mutate does not clobber the
598 // pointer __str._M_data() formed here.
599 const size_type __size = __str.size();
600 const size_type __len = __size + this->size();
601 if (__len > this->capacity())
602 this->reserve(__len);
603 return _M_replace_safe(this->size(), size_type(0), __str._M_data(),
607 template<typename _CharT, typename _Traits, typename _Alloc>
608 basic_string<_CharT, _Traits, _Alloc>&
609 basic_string<_CharT, _Traits, _Alloc>::
610 append(const basic_string& __str, size_type __pos, size_type __n)
612 // Iff appending itself, string needs to pre-reserve the
613 // correct size so that _M_mutate does not clobber the
614 // pointer __str._M_data() formed here.
615 __str._M_check(__pos, "basic_string::append");
616 __n = __str._M_limit(__pos, __n);
617 const size_type __len = __n + this->size();
618 if (__len > this->capacity())
619 this->reserve(__len);
620 return _M_replace_safe(this->size(), size_type(0), __str._M_data()
624 template<typename _CharT, typename _Traits, typename _Alloc>
625 basic_string<_CharT, _Traits, _Alloc>&
626 basic_string<_CharT, _Traits, _Alloc>::
627 append(const _CharT* __s, size_type __n)
629 __glibcxx_requires_string_len(__s, __n);
630 const size_type __len = __n + this->size();
631 if (__len > this->capacity())
632 this->reserve(__len);
633 return _M_replace_safe(this->size(), size_type(0), __s, __n);
636 template<typename _CharT, typename _Traits, typename _Alloc>
637 basic_string<_CharT, _Traits, _Alloc>
638 operator+(const _CharT* __lhs,
639 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
641 __glibcxx_requires_string(__lhs);
642 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
643 typedef typename __string_type::size_type __size_type;
644 const __size_type __len = _Traits::length(__lhs);
646 __str.reserve(__len + __rhs.size());
647 __str.append(__lhs, __len);
652 template<typename _CharT, typename _Traits, typename _Alloc>
653 basic_string<_CharT, _Traits, _Alloc>
654 operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
656 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
657 typedef typename __string_type::size_type __size_type;
659 const __size_type __len = __rhs.size();
660 __str.reserve(__len + 1);
661 __str.append(__size_type(1), __lhs);
666 template<typename _CharT, typename _Traits, typename _Alloc>
667 typename basic_string<_CharT, _Traits, _Alloc>::size_type
668 basic_string<_CharT, _Traits, _Alloc>::
669 copy(_CharT* __s, size_type __n, size_type __pos) const
671 _M_check(__pos, "basic_string::copy");
672 __n = _M_limit(__pos, __n);
673 __glibcxx_requires_string_len(__s, __n);
675 traits_type::copy(__s, _M_data() + __pos, __n);
676 // 21.3.5.7 par 3: do not append null. (good.)
680 template<typename _CharT, typename _Traits, typename _Alloc>
681 typename basic_string<_CharT, _Traits, _Alloc>::size_type
682 basic_string<_CharT, _Traits, _Alloc>::
683 find(const _CharT* __s, size_type __pos, size_type __n) const
685 __glibcxx_requires_string_len(__s, __n);
686 size_type __ret = npos;
687 const size_type __size = this->size();
688 if (__pos + __n <= __size)
690 const _CharT* __data = _M_data();
691 const _CharT* __p = std::search(__data + __pos, __data + __size,
692 __s, __s + __n, traits_type::eq);
693 if (__p != __data + __size || __n == 0)
694 __ret = __p - __data;
699 template<typename _CharT, typename _Traits, typename _Alloc>
700 typename basic_string<_CharT, _Traits, _Alloc>::size_type
701 basic_string<_CharT, _Traits, _Alloc>::
702 find(_CharT __c, size_type __pos) const
704 size_type __ret = npos;
705 const size_type __size = this->size();
708 const _CharT* __data = _M_data();
709 const size_type __n = __size - __pos;
710 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
712 __ret = __p - __data;
717 template<typename _CharT, typename _Traits, typename _Alloc>
718 typename basic_string<_CharT, _Traits, _Alloc>::size_type
719 basic_string<_CharT, _Traits, _Alloc>::
720 rfind(const _CharT* __s, size_type __pos, size_type __n) const
722 __glibcxx_requires_string_len(__s, __n);
723 const size_type __size = this->size();
726 __pos = std::min(size_type(__size - __n), __pos);
727 const _CharT* __data = _M_data();
730 if (traits_type::compare(__data + __pos, __s, __n) == 0)
738 template<typename _CharT, typename _Traits, typename _Alloc>
739 typename basic_string<_CharT, _Traits, _Alloc>::size_type
740 basic_string<_CharT, _Traits, _Alloc>::
741 rfind(_CharT __c, size_type __pos) const
743 size_type __size = this->size();
746 if (--__size > __pos)
748 for (++__size; __size-- > 0; )
749 if (traits_type::eq(_M_data()[__size], __c))
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_first_of(const _CharT* __s, size_type __pos, size_type __n) const
760 __glibcxx_requires_string_len(__s, __n);
761 for (; __n && __pos < this->size(); ++__pos)
763 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
770 template<typename _CharT, typename _Traits, typename _Alloc>
771 typename basic_string<_CharT, _Traits, _Alloc>::size_type
772 basic_string<_CharT, _Traits, _Alloc>::
773 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
775 __glibcxx_requires_string_len(__s, __n);
776 size_type __size = this->size();
779 if (--__size > __pos)
783 if (traits_type::find(__s, __n, _M_data()[__size]))
786 while (__size-- != 0);
791 template<typename _CharT, typename _Traits, typename _Alloc>
792 typename basic_string<_CharT, _Traits, _Alloc>::size_type
793 basic_string<_CharT, _Traits, _Alloc>::
794 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
796 __glibcxx_requires_string_len(__s, __n);
797 for (; __pos < this->size(); ++__pos)
798 if (!traits_type::find(__s, __n, _M_data()[__pos]))
803 template<typename _CharT, typename _Traits, typename _Alloc>
804 typename basic_string<_CharT, _Traits, _Alloc>::size_type
805 basic_string<_CharT, _Traits, _Alloc>::
806 find_first_not_of(_CharT __c, size_type __pos) const
808 for (; __pos < this->size(); ++__pos)
809 if (!traits_type::eq(_M_data()[__pos], __c))
814 template<typename _CharT, typename _Traits, typename _Alloc>
815 typename basic_string<_CharT, _Traits, _Alloc>::size_type
816 basic_string<_CharT, _Traits, _Alloc>::
817 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
819 __glibcxx_requires_string_len(__s, __n);
820 size_type __size = this->size();
823 if (--__size > __pos)
827 if (!traits_type::find(__s, __n, _M_data()[__size]))
835 template<typename _CharT, typename _Traits, typename _Alloc>
836 typename basic_string<_CharT, _Traits, _Alloc>::size_type
837 basic_string<_CharT, _Traits, _Alloc>::
838 find_last_not_of(_CharT __c, size_type __pos) const
840 size_type __size = this->size();
843 if (--__size > __pos)
847 if (!traits_type::eq(_M_data()[__size], __c))
855 template<typename _CharT, typename _Traits, typename _Alloc>
857 basic_string<_CharT, _Traits, _Alloc>::
858 compare(size_type __pos, size_type __n, const basic_string& __str) const
860 _M_check(__pos, "basic_string::compare");
861 __n = _M_limit(__pos, __n);
862 const size_type __osize = __str.size();
863 const size_type __len = std::min(__n, __osize);
864 int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
870 template<typename _CharT, typename _Traits, typename _Alloc>
872 basic_string<_CharT, _Traits, _Alloc>::
873 compare(size_type __pos1, size_type __n1, const basic_string& __str,
874 size_type __pos2, size_type __n2) const
876 _M_check(__pos1, "basic_string::compare");
877 __str._M_check(__pos2, "basic_string::compare");
878 __n1 = _M_limit(__pos1, __n1);
879 __n2 = __str._M_limit(__pos2, __n2);
880 const size_type __len = std::min(__n1, __n2);
881 int __r = traits_type::compare(_M_data() + __pos1,
882 __str.data() + __pos2, __len);
888 template<typename _CharT, typename _Traits, typename _Alloc>
890 basic_string<_CharT, _Traits, _Alloc>::
891 compare(const _CharT* __s) const
893 __glibcxx_requires_string(__s);
894 const size_type __size = this->size();
895 const size_type __osize = traits_type::length(__s);
896 const size_type __len = std::min(__size, __osize);
897 int __r = traits_type::compare(_M_data(), __s, __len);
899 __r = __size - __osize;
903 template<typename _CharT, typename _Traits, typename _Alloc>
905 basic_string <_CharT, _Traits, _Alloc>::
906 compare(size_type __pos, size_type __n1, const _CharT* __s) const
908 __glibcxx_requires_string(__s);
909 _M_check(__pos, "basic_string::compare");
910 __n1 = _M_limit(__pos, __n1);
911 const size_type __osize = traits_type::length(__s);
912 const size_type __len = std::min(__n1, __osize);
913 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
915 __r = __n1 - __osize;
919 template<typename _CharT, typename _Traits, typename _Alloc>
921 basic_string <_CharT, _Traits, _Alloc>::
922 compare(size_type __pos, size_type __n1, const _CharT* __s,
923 size_type __n2) const
925 __glibcxx_requires_string_len(__s, __n2);
926 _M_check(__pos, "basic_string::compare");
927 __n1 = _M_limit(__pos, __n1);
928 const size_type __len = std::min(__n1, __n2);
929 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
935 // Inhibit implicit instantiations for required instantiations,
936 // which are defined via explicit instantiations elsewhere.
937 // NB: This syntax is a GNU extension.
938 #if _GLIBCXX_EXTERN_TEMPLATE
939 extern template class basic_string<char>;
942 operator>>(basic_istream<char>&, string&);
945 operator<<(basic_ostream<char>&, const string&);
948 getline(basic_istream<char>&, string&, char);
951 getline(basic_istream<char>&, string&);
953 #ifdef _GLIBCXX_USE_WCHAR_T
954 extern template class basic_string<wchar_t>;
956 basic_istream<wchar_t>&
957 operator>>(basic_istream<wchar_t>&, wstring&);
959 basic_ostream<wchar_t>&
960 operator<<(basic_ostream<wchar_t>&, const wstring&);
962 basic_istream<wchar_t>&
963 getline(basic_istream<wchar_t>&, wstring&, wchar_t);
965 basic_istream<wchar_t>&
966 getline(basic_istream<wchar_t>&, wstring&);