1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // ISO C++ 14882: 21 Strings library
34 // This file is included by <string>. It is not meant to be included
37 // Written by Jason Merrill based upon the specification by Takanori Adachi
38 // in ANSI X3J16/94-0013R2. Rewritten by Nathan Myers to ISO-14882.
40 #ifndef _CPP_BITS_STRING_TCC
41 #define _CPP_BITS_STRING_TCC 1
45 template<typename _CharT, typename _Traits, typename _Alloc>
47 basic_string<_CharT, _Traits, _Alloc>::
48 _Rep::_S_terminal = _CharT();
50 template<typename _CharT, typename _Traits, typename _Alloc>
51 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
52 basic_string<_CharT, _Traits, _Alloc>::
53 _Rep::_S_max_size = (((npos - sizeof(_Rep))/sizeof(_CharT)) - 1) / 4;
55 template<typename _CharT, typename _Traits, typename _Alloc>
56 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
57 basic_string<_CharT, _Traits, _Alloc>::npos;
59 // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
60 // at static init time (before static ctors are run).
61 template<typename _CharT, typename _Traits, typename _Alloc>
62 typename basic_string<_CharT, _Traits, _Alloc>::size_type
63 basic_string<_CharT, _Traits, _Alloc>::_S_empty_rep_storage[
64 (sizeof(_Rep) + sizeof(_CharT) + sizeof(size_type) - 1)/sizeof(size_type)];
66 // NB: This is the special case for Input Iterators, used in
67 // istreambuf_iterators, etc.
68 // Input Iterators have a cost structure very different from
69 // pointers, calling for a different coding style.
70 template<typename _CharT, typename _Traits, typename _Alloc>
71 template<typename _InIter>
73 basic_string<_CharT, _Traits, _Alloc>::
74 _S_construct(_InIter __beg, _InIter __end, const _Alloc& __a,
77 if (__beg == __end && __a == _Alloc())
78 return _S_empty_rep()._M_refcopy();
79 // Avoid reallocation for common case.
82 while (__beg != __end && __i < sizeof(__buf) / sizeof(_CharT))
84 __buf[__i++] = *__beg;
87 _Rep* __r = _Rep::_S_create(__i, __a);
88 traits_type::copy(__r->_M_refdata(), __buf, __i);
92 // NB: this loop looks precisely this way because
93 // it avoids comparing __beg != __end any more
94 // than strictly necessary; != might be expensive!
97 _CharT* __p = __r->_M_refdata() + __r->_M_length;
98 _CharT* __last = __r->_M_refdata() + __r->_M_capacity;
103 __r->_M_length = __p - __r->_M_refdata();
104 *__p = _Rep::_S_terminal; // grrr.
105 return __r->_M_refdata();
112 // Allocate more space.
113 size_type __len = __p - __r->_M_refdata();
114 _Rep* __another = _Rep::_S_create(__len + 1, __a);
115 traits_type::copy(__another->_M_refdata(),
116 __r->_M_refdata(), __len);
117 __r->_M_destroy(__a);
119 __r->_M_length = __len;
124 __r->_M_destroy(__a);
125 __throw_exception_again;
130 template<typename _CharT, typename _Traits, typename _Alloc>
131 template <class _InIter>
133 basic_string<_CharT, _Traits, _Alloc>::
134 _S_construct(_InIter __beg, _InIter __end, const _Alloc& __a,
135 forward_iterator_tag)
137 size_type __dnew = static_cast<size_type>(distance(__beg, __end));
139 if (__beg == __end && __a == _Alloc())
140 return _S_empty_rep()._M_refcopy();
142 // Check for out_of_range and length_error exceptions.
143 _Rep* __r = _Rep::_S_create(__dnew, __a);
145 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
148 __r->_M_destroy(__a);
149 __throw_exception_again;
151 __r->_M_length = __dnew;
153 __r->_M_refdata()[__dnew] = _Rep::_S_terminal; // grrr.
154 return __r->_M_refdata();
157 template<typename _CharT, typename _Traits, typename _Alloc>
159 basic_string<_CharT, _Traits, _Alloc>::
160 _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
162 if (__n == 0 && __a == _Alloc())
163 return _S_empty_rep()._M_refcopy();
165 // Check for out_of_range and length_error exceptions.
166 _Rep* __r = _Rep::_S_create(__n, __a);
170 traits_type::assign(__r->_M_refdata(), __n, __c);
174 __r->_M_destroy(__a);
175 __throw_exception_again;
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())
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_check(__pos),
199 __str._M_fold(__pos, __n), _Alloc()), _Alloc())
202 template<typename _CharT, typename _Traits, typename _Alloc>
203 basic_string<_CharT, _Traits, _Alloc>::
204 basic_string(const basic_string& __str, size_type __pos,
205 size_type __n, const _Alloc& __a)
206 : _M_dataplus(_S_construct(__str._M_check(__pos),
207 __str._M_fold(__pos, __n), __a), __a)
210 template<typename _CharT, typename _Traits, typename _Alloc>
211 basic_string<_CharT, _Traits, _Alloc>::
212 basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
213 : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
216 template<typename _CharT, typename _Traits, typename _Alloc>
217 basic_string<_CharT, _Traits, _Alloc>::
218 basic_string(const _CharT* __s, const _Alloc& __a)
219 : _M_dataplus(_S_construct(__s, __s + traits_type::length(__s), __a), __a)
222 template<typename _CharT, typename _Traits, typename _Alloc>
223 basic_string<_CharT, _Traits, _Alloc>::
224 basic_string(size_type __n, _CharT __c, const _Alloc& __a)
225 : _M_dataplus(_S_construct(__n, __c, __a), __a)
228 template<typename _CharT, typename _Traits, typename _Alloc>
229 template<typename _InputIter>
230 basic_string<_CharT, _Traits, _Alloc>::
231 basic_string(_InputIter __beg, _InputIter __end, const _Alloc& __a)
232 : _M_dataplus(_S_construct(__beg, __end, __a), __a)
235 template<typename _CharT, typename _Traits, typename _Alloc>
236 basic_string<_CharT, _Traits, _Alloc>&
237 basic_string<_CharT, _Traits, _Alloc>::assign(const basic_string& __str)
239 if (_M_rep() != __str._M_rep())
242 allocator_type __a = this->get_allocator();
243 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
244 _M_rep()->_M_dispose(__a);
250 template<typename _CharT, typename _Traits, typename _Alloc>
252 basic_string<_CharT, _Traits, _Alloc>::_Rep::
253 _M_destroy(const _Alloc& __a) throw ()
255 size_type __size = sizeof(_Rep) + (_M_capacity + 1) * sizeof(_CharT);
256 _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
259 template<typename _CharT, typename _Traits, typename _Alloc>
261 basic_string<_CharT, _Traits, _Alloc>::_M_leak_hard()
263 if (_M_rep()->_M_is_shared())
265 _M_rep()->_M_set_leaked();
268 template<typename _CharT, typename _Traits, typename _Alloc>
270 basic_string<_CharT, _Traits, _Alloc>::
271 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
273 size_type __old_size = this->size();
274 const size_type __new_size = __old_size + __len2 - __len1;
275 const _CharT* __src = _M_data() + __pos + __len1;
276 const size_type __how_much = __old_size - __pos - __len1;
278 if (_M_rep()->_M_is_shared() || __new_size > capacity())
281 allocator_type __a = get_allocator();
282 _Rep* __r = _Rep::_S_create(__new_size, __a);
286 traits_type::copy(__r->_M_refdata(), _M_data(), __pos);
288 traits_type::copy(__r->_M_refdata() + __pos + __len2,
293 __r->_M_dispose(get_allocator());
294 __throw_exception_again;
296 _M_rep()->_M_dispose(__a);
297 _M_data(__r->_M_refdata());
299 else if (__how_much && __len1 != __len2)
302 traits_type::move(_M_data() + __pos + __len2, __src, __how_much);
304 _M_rep()->_M_set_sharable();
305 _M_rep()->_M_length = __new_size;
306 _M_data()[__new_size] = _Rep::_S_terminal; // grrr. (per 21.3.4)
307 // You cannot leave those LWG people alone for a second.
310 template<typename _CharT, typename _Traits, typename _Alloc>
312 basic_string<_CharT, _Traits, _Alloc>::reserve(size_type __res)
314 if (__res > this->capacity() || _M_rep()->_M_is_shared())
316 if (__res > this->max_size())
317 __throw_length_error("basic_string::reserve");
318 // Make sure we don't shrink below the current size
319 if (__res < this->size())
320 __res = this->size();
321 allocator_type __a = get_allocator();
322 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
323 _M_rep()->_M_dispose(__a);
328 template<typename _CharT, typename _Traits, typename _Alloc>
329 void basic_string<_CharT, _Traits, _Alloc>::swap(basic_string& __s)
331 if (_M_rep()->_M_is_leaked())
332 _M_rep()->_M_set_sharable();
333 if (__s._M_rep()->_M_is_leaked())
334 __s._M_rep()->_M_set_sharable();
335 if (this->get_allocator() == __s.get_allocator())
337 _CharT* __tmp = _M_data();
338 _M_data(__s._M_data());
341 // The code below can usually be optimized away.
344 basic_string __tmp1(_M_ibegin(), _M_iend(), __s.get_allocator());
345 basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
346 this->get_allocator());
352 #ifdef _GLIBCPP_ALLOC_CONTROL
353 template<typename _CharT, typename _Traits, typename _Alloc>
354 bool (*basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_excess_slop)
356 basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_default_excess;
359 template<typename _CharT, typename _Traits, typename _Alloc>
360 typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
361 basic_string<_CharT, _Traits, _Alloc>::_Rep::
362 _S_create(size_t __capacity, const _Alloc& __alloc)
364 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
365 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
366 // 83. String::npos vs. string::max_size()
367 if (__capacity > _S_max_size)
369 if (__capacity == npos)
371 __throw_length_error("basic_string::_S_create");
373 // NB: Need an array of char_type[__capacity], plus a
374 // terminating null char_type() element, plus enough for the
375 // _Rep data structure. Whew. Seemingly so needy, yet so elemental.
376 size_t __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
378 // The standard places no restriction on allocating more memory
379 // than is strictly needed within this layer at the moment or as
380 // requested by an explicit application call to reserve(). Many
381 // malloc implementations perform quite poorly when an
382 // application attempts to allocate memory in a stepwise fashion
383 // growing each allocation size by only 1 char. Additionally,
384 // it makes little sense to allocate less linear memory than the
385 // natural blocking size of the malloc implementation.
386 // Unfortunately, we would need a somewhat low-level calculation
387 // with tuned parameters to get this perfect for any particular
388 // malloc implementation. Fortunately, generalizations about
389 // common features seen among implementations seems to suffice.
390 // This algorithm does not replace the need for an exponential
391 // growth shaper to meet library specification. Note: THIS IS
392 // NOT THE CORRECT LOCATION FOR AN EXPONENTIAL GROWTH SHAPER
393 // (since this code affect initial allocation as well as
396 // __pagesize need not match the actual VM page size for good
397 // results in practice, thus we pick a common value on the low
398 // side. __malloc_header_size is an estimate of the amount of
399 // overhead per memory allocation (in practice seen N * sizeof
400 // (void*) where N is 0, 2 or 4). According to folklore,
401 // picking this value on the high side is better than
402 // low-balling it (especially when this algorithm is used with
403 // malloc implementations that allocate memory blocks rounded up
404 // to a size which is a power of 2).
405 const size_t __pagesize = 4096; // must be 2^i * __subpagesize
406 const size_t __subpagesize = 128; // should be >> __malloc_header_size
407 const size_t __malloc_header_size = 4 * sizeof (void*);
408 if ((__size + __malloc_header_size) > __pagesize)
411 (__pagesize - ((__size + __malloc_header_size) % __pagesize))
413 __capacity += __extra / sizeof(_CharT);
414 __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
416 else if (__size > __subpagesize)
419 (__subpagesize - ((__size + __malloc_header_size) % __subpagesize))
421 __capacity += __extra / sizeof(_CharT);
422 __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
425 // NB: Might throw, but no worries about a leak, mate: _Rep()
427 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
428 _Rep *__p = new (__place) _Rep;
429 __p->_M_capacity = __capacity;
430 __p->_M_set_sharable(); // one reference
435 template<typename _CharT, typename _Traits, typename _Alloc>
437 basic_string<_CharT, _Traits, _Alloc>::_Rep::
438 _M_clone(const _Alloc& __alloc, size_type __res)
440 _Rep* __r = _Rep::_S_create(_M_length + __res, __alloc);
444 { traits_type::copy(__r->_M_refdata(), _M_refdata(), _M_length); }
447 __r->_M_destroy(__alloc);
448 __throw_exception_again;
451 __r->_M_length = _M_length;
452 return __r->_M_refdata();
455 template<typename _CharT, typename _Traits, typename _Alloc>
457 #ifdef _GLIBCPP_ALLOC_CONTROL
458 basic_string<_CharT, _Traits, _Alloc>::_Rep::
459 _S_default_excess(size_t __s, size_t __r)
461 basic_string<_CharT, _Traits, _Alloc>::_Rep::
462 _S_excess_slop(size_t __s, size_t __r)
465 return 2 * (__s <= 16 ? 16 : __s) < __r;
468 template<typename _CharT, typename _Traits, typename _Alloc>
470 basic_string<_CharT, _Traits, _Alloc>::resize(size_type __n, _CharT __c)
472 if (__n > max_size())
473 __throw_length_error("basic_string::resize");
474 size_type __size = this->size();
476 this->append(__n - __size, __c);
477 else if (__n < __size)
479 // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
482 template<typename _CharT, typename _Traits, typename _Alloc>
483 template<typename _InputIter>
484 basic_string<_CharT, _Traits, _Alloc>&
485 basic_string<_CharT, _Traits, _Alloc>::
486 _M_replace(iterator __i1, iterator __i2, _InputIter __k1,
487 _InputIter __k2, input_iterator_tag)
489 basic_string __s(__k1, __k2);
490 return this->replace(__i1, __i2, __s._M_ibegin(), __s._M_iend());
493 template<typename _CharT, typename _Traits, typename _Alloc>
494 template<typename _ForwardIter>
495 basic_string<_CharT, _Traits, _Alloc>&
496 basic_string<_CharT, _Traits, _Alloc>::
497 _M_replace(iterator __i1, iterator __i2, _ForwardIter __k1,
498 _ForwardIter __k2, forward_iterator_tag)
500 size_type __dnew = static_cast<size_type>(distance(__k1, __k2));
501 size_type __dold = __i2 - __i1;
502 size_type __dmax = this->max_size();
504 if (__dmax <= __dnew)
505 __throw_length_error("basic_string::_M_replace");
506 size_type __off = __i1 - _M_ibegin();
508 // Save concerned source string data in a temporary.
509 basic_string __temp(__k1, __k2);
510 _M_mutate(__off, __dold, __dnew);
512 // Invalidated __i1, __i2 (and clobbered original source string
513 // data when destination string == source string and the string
516 _S_copy_chars(_M_data() + __off, __temp.begin(), __temp.end());
521 template<typename _CharT, typename _Traits, typename _Alloc>
522 basic_string<_CharT, _Traits, _Alloc>&
523 basic_string<_CharT, _Traits, _Alloc>::
524 replace(size_type __pos1, size_type __n1, const basic_string& __str,
525 size_type __pos2, size_type __n2)
527 return this->replace(_M_check(__pos1), _M_fold(__pos1, __n1),
528 __str._M_check(__pos2),
529 __str._M_fold(__pos2, __n2));
532 template<typename _CharT, typename _Traits, typename _Alloc>
533 basic_string<_CharT, _Traits, _Alloc>&
534 basic_string<_CharT, _Traits, _Alloc>::
535 append(const basic_string& __str)
537 // Iff appending itself, string needs to pre-reserve the
538 // correct size so that _M_mutate does not clobber the
539 // iterators formed here.
540 size_type __size = __str.size();
541 size_type __len = __size + this->size();
542 if (__len > this->capacity())
543 this->reserve(__len);
544 return this->replace(_M_iend(), _M_iend(), __str._M_ibegin(),
548 template<typename _CharT, typename _Traits, typename _Alloc>
549 basic_string<_CharT, _Traits, _Alloc>&
550 basic_string<_CharT, _Traits, _Alloc>::
551 append(const basic_string& __str, size_type __pos, size_type __n)
553 // Iff appending itself, string needs to pre-reserve the
554 // correct size so that _M_mutate does not clobber the
555 // iterators formed here.
556 size_type __len = min(__str.size() - __pos, __n) + this->size();
557 if (__len > this->capacity())
558 this->reserve(__len);
559 return this->replace(_M_iend(), _M_iend(), __str._M_check(__pos),
560 __str._M_fold(__pos, __n));
563 template<typename _CharT, typename _Traits, typename _Alloc>
564 basic_string<_CharT, _Traits, _Alloc>&
565 basic_string<_CharT, _Traits, _Alloc>::
566 append(const _CharT* __s, size_type __n)
568 size_type __len = __n + this->size();
569 if (__len > this->capacity())
570 this->reserve(__len);
571 return this->replace(_M_iend(), _M_iend(), __s, __s + __n);
574 template<typename _CharT, typename _Traits, typename _Alloc>
575 basic_string<_CharT, _Traits, _Alloc>&
576 basic_string<_CharT, _Traits, _Alloc>::
577 append(size_type __n, _CharT __c)
579 size_type __len = __n + this->size();
580 if (__len > this->capacity())
581 this->reserve(__len);
582 return this->replace(_M_iend(), _M_iend(), __n, __c);
585 template<typename _CharT, typename _Traits, typename _Alloc>
586 basic_string<_CharT, _Traits, _Alloc>
587 operator+(const _CharT* __lhs,
588 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
590 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
591 typedef typename __string_type::size_type __size_type;
592 __size_type __len = _Traits::length(__lhs);
594 __str.reserve(__len + __rhs.size());
595 __str.append(__lhs, __lhs + __len);
600 template<typename _CharT, typename _Traits, typename _Alloc>
601 basic_string<_CharT, _Traits, _Alloc>
602 operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
604 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
605 typedef typename __string_type::size_type __size_type;
607 __size_type __len = __rhs.size();
608 __str.reserve(__len + 1);
609 __str.append(__size_type(1), __lhs);
614 template<typename _CharT, typename _Traits, typename _Alloc>
615 basic_string<_CharT, _Traits, _Alloc>&
616 basic_string<_CharT, _Traits, _Alloc>::
617 replace(iterator __i1, iterator __i2, size_type __n2, _CharT __c)
619 size_type __n1 = __i2 - __i1;
620 size_type __off1 = __i1 - _M_ibegin();
621 if (max_size() - (this->size() - __n1) <= __n2)
622 __throw_length_error("basic_string::replace");
623 _M_mutate (__off1, __n1, __n2);
624 // Invalidated __i1, __i2
626 traits_type::assign(_M_data() + __off1, __n2, __c);
630 template<typename _CharT, typename _Traits, typename _Alloc>
631 typename basic_string<_CharT, _Traits, _Alloc>::size_type
632 basic_string<_CharT, _Traits, _Alloc>::
633 copy(_CharT* __s, size_type __n, size_type __pos) const
635 if (__pos > this->size())
636 __throw_out_of_range("basic_string::copy");
638 if (__n > this->size() - __pos)
639 __n = this->size() - __pos;
641 traits_type::copy(__s, _M_data() + __pos, __n);
642 // 21.3.5.7 par 3: do not append null. (good.)
646 template<typename _CharT, typename _Traits, typename _Alloc>
647 typename basic_string<_CharT, _Traits, _Alloc>::size_type
648 basic_string<_CharT, _Traits, _Alloc>::
649 find(const _CharT* __s, size_type __pos, size_type __n) const
651 size_type __size = this->size();
652 size_t __xpos = __pos;
653 const _CharT* __data = _M_data();
654 for (; __xpos + __n <= __size; ++__xpos)
655 if (traits_type::compare(__data + __xpos, __s, __n) == 0)
660 template<typename _CharT, typename _Traits, typename _Alloc>
661 typename basic_string<_CharT, _Traits, _Alloc>::size_type
662 basic_string<_CharT, _Traits, _Alloc>::
663 find(_CharT __c, size_type __pos) const
665 size_type __size = this->size();
666 size_type __ret = npos;
669 const _CharT* __data = _M_data();
670 size_type __n = __size - __pos;
671 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
673 __ret = __p - __data;
679 template<typename _CharT, typename _Traits, typename _Alloc>
680 typename basic_string<_CharT, _Traits, _Alloc>::size_type
681 basic_string<_CharT, _Traits, _Alloc>::
682 rfind(const _CharT* __s, size_type __pos, size_type __n) const
684 size_type __size = this->size();
687 __pos = std::min(__size - __n, __pos);
688 const _CharT* __data = _M_data();
691 if (traits_type::compare(__data + __pos, __s, __n) == 0)
699 template<typename _CharT, typename _Traits, typename _Alloc>
700 typename basic_string<_CharT, _Traits, _Alloc>::size_type
701 basic_string<_CharT, _Traits, _Alloc>::
702 rfind(_CharT __c, size_type __pos) const
704 size_type __size = this->size();
707 size_t __xpos = __size - 1;
711 for (++__xpos; __xpos-- > 0; )
712 if (traits_type::eq(_M_data()[__xpos], __c))
718 template<typename _CharT, typename _Traits, typename _Alloc>
719 typename basic_string<_CharT, _Traits, _Alloc>::size_type
720 basic_string<_CharT, _Traits, _Alloc>::
721 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
723 for (; __n && __pos < this->size(); ++__pos)
725 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
732 template<typename _CharT, typename _Traits, typename _Alloc>
733 typename basic_string<_CharT, _Traits, _Alloc>::size_type
734 basic_string<_CharT, _Traits, _Alloc>::
735 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
737 size_type __size = this->size();
740 if (--__size > __pos)
744 if (traits_type::find(__s, __n, _M_data()[__size]))
747 while (__size-- != 0);
752 template<typename _CharT, typename _Traits, typename _Alloc>
753 typename basic_string<_CharT, _Traits, _Alloc>::size_type
754 basic_string<_CharT, _Traits, _Alloc>::
755 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
757 size_t __xpos = __pos;
758 for (; __xpos < this->size(); ++__xpos)
759 if (!traits_type::find(__s, __n, _M_data()[__xpos]))
764 template<typename _CharT, typename _Traits, typename _Alloc>
765 typename basic_string<_CharT, _Traits, _Alloc>::size_type
766 basic_string<_CharT, _Traits, _Alloc>::
767 find_first_not_of(_CharT __c, size_type __pos) const
769 size_t __xpos = __pos;
770 for (; __xpos < this->size(); ++__xpos)
771 if (!traits_type::eq(_M_data()[__xpos], __c))
776 template<typename _CharT, typename _Traits, typename _Alloc>
777 typename basic_string<_CharT, _Traits, _Alloc>::size_type
778 basic_string<_CharT, _Traits, _Alloc>::
779 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
781 size_type __size = this->size();
784 if (--__size > __pos)
788 if (!traits_type::find(__s, __n, _M_data()[__size]))
796 template<typename _CharT, typename _Traits, typename _Alloc>
797 typename basic_string<_CharT, _Traits, _Alloc>::size_type
798 basic_string<_CharT, _Traits, _Alloc>::
799 find_last_not_of(_CharT __c, size_type __pos) const
801 size_type __size = this->size();
804 if (--__size > __pos)
808 if (!traits_type::eq(_M_data()[__size], __c))
816 template<typename _CharT, typename _Traits, typename _Alloc>
818 basic_string<_CharT, _Traits, _Alloc>::
819 compare(size_type __pos, size_type __n, const basic_string& __str) const
821 size_type __size = this->size();
822 size_type __osize = __str.size();
824 __throw_out_of_range("basic_string::compare");
826 size_type __rsize= min(__size - __pos, __n);
827 size_type __len = min(__rsize, __osize);
828 int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
830 __r = __rsize - __osize;
834 template<typename _CharT, typename _Traits, typename _Alloc>
836 basic_string<_CharT, _Traits, _Alloc>::
837 compare(size_type __pos1, size_type __n1, const basic_string& __str,
838 size_type __pos2, size_type __n2) const
840 size_type __size = this->size();
841 size_type __osize = __str.size();
842 if (__pos1 > __size || __pos2 > __osize)
843 __throw_out_of_range("basic_string::compare");
845 size_type __rsize = min(__size - __pos1, __n1);
846 size_type __rosize = min(__osize - __pos2, __n2);
847 size_type __len = min(__rsize, __rosize);
848 int __r = traits_type::compare(_M_data() + __pos1,
849 __str.data() + __pos2, __len);
851 __r = __rsize - __rosize;
856 template<typename _CharT, typename _Traits, typename _Alloc>
858 basic_string<_CharT, _Traits, _Alloc>::
859 compare(const _CharT* __s) const
861 size_type __size = this->size();
862 int __r = traits_type::compare(_M_data(), __s, __size);
864 __r = __size - traits_type::length(__s);
869 template<typename _CharT, typename _Traits, typename _Alloc>
871 basic_string <_CharT, _Traits, _Alloc>::
872 compare(size_type __pos, size_type __n1, const _CharT* __s) const
874 size_type __size = this->size();
876 __throw_out_of_range("basic_string::compare");
878 size_type __osize = traits_type::length(__s);
879 size_type __rsize = min(__size - __pos, __n1);
880 size_type __len = min(__rsize, __osize);
881 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
883 __r = __rsize - __osize;
887 template<typename _CharT, typename _Traits, typename _Alloc>
889 basic_string <_CharT, _Traits, _Alloc>::
890 compare(size_type __pos, size_type __n1, const _CharT* __s,
891 size_type __n2) const
893 size_type __size = this->size();
895 __throw_out_of_range("basic_string::compare");
897 size_type __osize = min(traits_type::length(__s), __n2);
898 size_type __rsize = min(__size - __pos, __n1);
899 size_type __len = min(__rsize, __osize);
900 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
902 __r = __rsize - __osize;
906 template <class _CharT, class _Traits, class _Alloc>
908 _S_string_copy(const basic_string<_CharT, _Traits, _Alloc>& __str,
909 _CharT* __buf, typename _Alloc::size_type __bufsiz)
911 typedef typename _Alloc::size_type size_type;
912 size_type __strsize = __str.size();
913 size_type __bytes = min(__strsize, __bufsiz - 1);
914 _Traits::copy(__buf, __str.data(), __bytes);
915 __buf[__bytes] = _CharT();