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);
91 // NB: this loop looks precisely this way because
92 // it avoids comparing __beg != __end any more
93 // than strictly necessary; != might be expensive!
96 _CharT* __p = __r->_M_refdata() + __r->_M_length;
97 _CharT* __last = __r->_M_refdata() + __r->_M_capacity;
102 __r->_M_length = __p - __r->_M_refdata();
103 *__p = _Rep::_S_terminal; // grrr.
104 return __r->_M_refdata();
111 // Allocate more space.
112 size_type __len = __p - __r->_M_refdata();
113 _Rep* __another = _Rep::_S_create(__len + 1, __a);
114 traits_type::copy(__another->_M_refdata(),
115 __r->_M_refdata(), __len);
116 __r->_M_destroy(__a);
118 __r->_M_length = __len;
122 __r->_M_destroy(__a);
128 template<typename _CharT, typename _Traits, typename _Alloc>
129 template <class _InIter>
131 basic_string<_CharT,_Traits,_Alloc>::
132 _S_construct(_InIter __beg, _InIter __end, const _Alloc& __a,
133 forward_iterator_tag)
135 size_type __dnew = static_cast<size_type>(distance(__beg, __end));
137 if (__beg == __end && __a == _Alloc())
138 return _S_empty_rep()._M_refcopy();
140 // Check for out_of_range and length_error exceptions.
141 _Rep* __r = _Rep::_S_create(__dnew, __a);
143 _S_copy_chars(__r->_M_refdata(), __beg, __end);
146 __r->_M_destroy(__a);
149 __r->_M_length = __dnew;
151 __r->_M_refdata()[__dnew] = _Rep::_S_terminal; // grrr.
152 return __r->_M_refdata();
155 template<typename _CharT, typename _Traits, typename _Alloc>
157 basic_string<_CharT,_Traits, _Alloc>::
158 _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
160 if (__n == 0 && __a == _Alloc())
161 return _S_empty_rep()._M_refcopy();
163 // Check for out_of_range and length_error exceptions.
164 _Rep* __r = _Rep::_S_create(__n, __a);
167 traits_type::assign(__r->_M_refdata(), __n, __c);
170 __r->_M_destroy(__a);
173 __r->_M_length = __n;
174 __r->_M_refdata()[__n] = _Rep::_S_terminal; // grrr
175 return __r->_M_refdata();
178 template<typename _CharT, typename _Traits, typename _Alloc>
179 basic_string<_CharT, _Traits, _Alloc>::
180 basic_string(const basic_string& __str)
181 : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(), __str.get_allocator()),
182 __str.get_allocator())
185 template<typename _CharT, typename _Traits, typename _Alloc>
186 basic_string<_CharT, _Traits, _Alloc>::
187 basic_string(const _Alloc& __a)
188 : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
191 template<typename _CharT, typename _Traits, typename _Alloc>
192 basic_string<_CharT, _Traits, _Alloc>::
193 basic_string(const basic_string& __str, size_type __pos, size_type __n)
194 : _M_dataplus(_S_construct(__str._M_check(__pos),
195 __str._M_fold(__pos, __n), _Alloc()), _Alloc())
198 template<typename _CharT, typename _Traits, typename _Alloc>
199 basic_string<_CharT, _Traits, _Alloc>::
200 basic_string(const basic_string& __str, size_type __pos,
201 size_type __n, const _Alloc& __a)
202 : _M_dataplus(_S_construct(__str._M_check(__pos),
203 __str._M_fold(__pos, __n), __a), __a)
206 template<typename _CharT, typename _Traits, typename _Alloc>
207 basic_string<_CharT, _Traits, _Alloc>::
208 basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
209 : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
212 template<typename _CharT, typename _Traits, typename _Alloc>
213 basic_string<_CharT, _Traits, _Alloc>::
214 basic_string(const _CharT* __s, const _Alloc& __a)
215 : _M_dataplus(_S_construct(__s, __s + traits_type::length(__s), __a), __a)
218 template<typename _CharT, typename _Traits, typename _Alloc>
219 basic_string<_CharT, _Traits, _Alloc>::
220 basic_string(size_type __n, _CharT __c, const _Alloc& __a)
221 : _M_dataplus(_S_construct(__n, __c, __a), __a)
224 template<typename _CharT, typename _Traits, typename _Alloc>
225 template<typename _InputIter>
226 basic_string<_CharT, _Traits, _Alloc>::
227 basic_string(_InputIter __beg, _InputIter __end, const _Alloc& __a)
228 : _M_dataplus(_S_construct(__beg, __end, __a), __a)
231 template<typename _CharT, typename _Traits, typename _Alloc>
232 basic_string<_CharT, _Traits, _Alloc>&
233 basic_string<_CharT, _Traits, _Alloc>::assign(const basic_string& __str)
235 if (_M_rep() != __str._M_rep())
238 allocator_type __a = this->get_allocator();
239 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
240 _M_rep()->_M_dispose(__a);
246 template<typename _CharT, typename _Traits, typename _Alloc>
248 basic_string<_CharT, _Traits, _Alloc>::_Rep::
249 _M_destroy(const _Alloc& __a) throw ()
251 size_type __size = sizeof(_Rep) + (_M_capacity + 1) * sizeof(_CharT);
252 _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
255 template<typename _CharT, typename _Traits, typename _Alloc>
257 basic_string<_CharT, _Traits, _Alloc>::_M_leak_hard()
259 if (_M_rep()->_M_is_shared())
261 _M_rep()->_M_set_leaked();
264 template<typename _CharT, typename _Traits, typename _Alloc>
266 basic_string<_CharT, _Traits, _Alloc>::
267 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
269 size_type __old_size = this->size();
270 const size_type __new_size = __old_size + __len2 - __len1;
271 const _CharT* __src = _M_data() + __pos + __len1;
272 const size_type __how_much = __old_size - __pos - __len1;
274 if (_M_rep()->_M_is_shared() || __new_size > capacity())
277 allocator_type __a = get_allocator();
278 _Rep* __r = _Rep::_S_create(__new_size, __a);
281 traits_type::copy(__r->_M_refdata(), _M_data(), __pos);
283 traits_type::copy(__r->_M_refdata() + __pos + __len2,
287 __r->_M_dispose(get_allocator());
290 _M_rep()->_M_dispose(__a);
291 _M_data(__r->_M_refdata());
293 else if (__how_much && __len1 != __len2)
296 traits_type::move(_M_data() + __pos + __len2, __src, __how_much);
298 _M_rep()->_M_set_sharable();
299 _M_rep()->_M_length = __new_size;
300 _M_data()[__new_size] = _Rep::_S_terminal; // grrr. (per 21.3.4)
301 // You cannot leave those LWG people alone for a second.
304 template<typename _CharT, typename _Traits, typename _Alloc>
306 basic_string<_CharT, _Traits, _Alloc>::reserve(size_type __res)
308 if (__res > this->capacity() || _M_rep()->_M_is_shared())
310 __LENGTHERROR(__res > this->max_size());
311 allocator_type __a = get_allocator();
312 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
313 _M_rep()->_M_dispose(__a);
318 template<typename _CharT, typename _Traits, typename _Alloc>
319 void basic_string<_CharT, _Traits, _Alloc>::swap(basic_string& __s)
321 if (_M_rep()->_M_is_leaked())
322 _M_rep()->_M_set_sharable();
323 if (__s._M_rep()->_M_is_leaked())
324 __s._M_rep()->_M_set_sharable();
325 if (this->get_allocator() == __s.get_allocator())
327 _CharT* __tmp = _M_data();
328 _M_data(__s._M_data());
331 // The code below can usually be optimized away.
334 basic_string __tmp1(_M_ibegin(), _M_iend(), __s.get_allocator());
335 basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
336 this->get_allocator());
342 #ifdef _GLIBCPP_ALLOC_CONTROL
343 template<typename _CharT, typename _Traits, typename _Alloc>
344 bool (*basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_excess_slop)
346 basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_default_excess;
349 template<typename _CharT, typename _Traits, typename _Alloc>
350 basic_string<_CharT, _Traits, _Alloc>::_Rep*
351 basic_string<_CharT, _Traits, _Alloc>::_Rep::
352 _S_create(size_t __capacity, const _Alloc& __alloc)
354 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
355 // 83. String::npos vs. string::max_size()
356 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
357 __LENGTHERROR(__capacity > _S_max_size);
359 __LENGTHERROR(__capacity == npos);
362 // NB: Need an array of char_type[__capacity], plus a
363 // terminating null char_type() element, plus enough for the
364 // _Rep data structure. Whew. Seemingly so needy, yet so elemental.
365 size_t __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
366 // NB: Might throw, but no worries about a leak, mate: _Rep()
368 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
369 _Rep *__p = new (__place) _Rep;
370 __p->_M_capacity = __capacity;
371 __p->_M_set_sharable(); // one reference
376 template<typename _CharT, typename _Traits, typename _Alloc>
378 basic_string<_CharT, _Traits, _Alloc>::_Rep::
379 _M_clone(const _Alloc& __alloc, size_type __res)
381 _Rep* __r = _Rep::_S_create(_M_length + __res, __alloc);
385 traits_type::copy(__r->_M_refdata(), _M_refdata(), _M_length);
388 __r->_M_destroy(__alloc);
392 __r->_M_length = _M_length;
393 return __r->_M_refdata();
396 template<typename _CharT, typename _Traits, typename _Alloc>
398 #ifdef _GLIBCPP_ALLOC_CONTROL
399 basic_string<_CharT, _Traits, _Alloc>::_Rep::
400 _S_default_excess(size_t __s, size_t __r)
402 basic_string<_CharT, _Traits, _Alloc>::_Rep::
403 _S_excess_slop(size_t __s, size_t __r)
406 return 2 * (__s <= 16 ? 16 : __s) < __r;
409 template<typename _CharT, typename _Traits, typename _Alloc>
411 basic_string<_CharT, _Traits, _Alloc>::resize(size_type __n, _CharT __c)
413 __LENGTHERROR(__n > max_size());
414 size_type __size = this->size();
416 this->append(__n - __size, __c);
417 else if (__n < __size)
419 // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
422 template<typename _CharT, typename _Traits, typename _Alloc>
423 template<typename _InputIter>
424 basic_string<_CharT, _Traits, _Alloc>&
425 basic_string<_CharT, _Traits, _Alloc>::
426 _M_replace(iterator __i1, iterator __i2, _InputIter __k1,
427 _InputIter __k2, input_iterator_tag)
429 basic_string __s(__k1, __k2);
430 return this->replace(__i1, __i2, __s._M_ibegin(), __s._M_iend());
433 template<typename _CharT, typename _Traits, typename _Alloc>
434 template<typename _ForwardIter>
435 basic_string<_CharT, _Traits, _Alloc>&
436 basic_string<_CharT, _Traits, _Alloc>::
437 _M_replace(iterator __i1, iterator __i2, _ForwardIter __k1,
438 _ForwardIter __k2, forward_iterator_tag)
440 size_type __dold = __i2 - __i1;
441 size_type __dmax = this->max_size();
442 size_type __dnew = static_cast<size_type>(distance(__k1, __k2));
444 __LENGTHERROR(__dmax <= __dnew);
445 size_type __off = __i1 - _M_ibegin();
446 _M_mutate(__off, __dold, __dnew);
447 // Invalidated __i1, __i2
449 _S_copy_chars(_M_data() + __off, __k1, __k2);
454 template<typename _CharT, typename _Traits, typename _Alloc>
455 basic_string<_CharT, _Traits, _Alloc>&
456 basic_string<_CharT, _Traits, _Alloc>::
457 replace(size_type __pos1, size_type __n1, const basic_string& __str,
458 size_type __pos2, size_type __n2)
460 return this->replace(_M_check(__pos1), _M_fold(__pos1, __n1),
461 __str._M_check(__pos2),
462 __str._M_fold(__pos2, __n2));
465 template<typename _CharT, typename _Traits, typename _Alloc>
466 basic_string<_CharT,_Traits,_Alloc>&
467 basic_string<_CharT,_Traits,_Alloc>::
468 append(const basic_string& __str)
470 // Iff appending itself, string needs to pre-reserve the
471 // correct size so that _M_mutate does not clobber the
472 // iterators formed here.
473 size_type __size = __str.size();
474 size_type __len = __size + this->size();
475 if (__len > this->capacity())
476 this->reserve(__len);
477 return this->replace(_M_iend(), _M_iend(), __str._M_ibegin(),
481 template<typename _CharT, typename _Traits, typename _Alloc>
482 basic_string<_CharT,_Traits,_Alloc>&
483 basic_string<_CharT,_Traits,_Alloc>::
484 append(const basic_string& __str, size_type __pos, size_type __n)
486 // Iff appending itself, string needs to pre-reserve the
487 // correct size so that _M_mutate does not clobber the
488 // iterators formed here.
489 size_type __len = min(__str.size() - __pos, __n) + this->size();
490 if (__len > this->capacity())
491 this->reserve(__len);
492 return this->replace(_M_iend(), _M_iend(), __str._M_check(__pos),
493 __str._M_fold(__pos, __n));
496 template<typename _CharT, typename _Traits, typename _Alloc>
497 basic_string<_CharT,_Traits,_Alloc>&
498 basic_string<_CharT,_Traits,_Alloc>::
499 append(const _CharT* __s, size_type __n)
501 size_type __len = __n + this->size();
502 if (__len > this->capacity())
503 this->reserve(__len);
504 return this->replace(_M_iend(), _M_iend(), __s, __s + __n);
507 template<typename _CharT, typename _Traits, typename _Alloc>
508 basic_string<_CharT,_Traits,_Alloc>&
509 basic_string<_CharT,_Traits,_Alloc>::
510 append(size_type __n, _CharT __c)
512 size_type __len = __n + this->size();
513 if (__len > this->capacity())
514 this->reserve(__len);
515 return this->replace(_M_iend(), _M_iend(), __n, __c);
518 template<typename _CharT, typename _Traits, typename _Alloc>
519 basic_string<_CharT,_Traits,_Alloc>
520 operator+(const _CharT* __lhs,
521 const basic_string<_CharT,_Traits,_Alloc>& __rhs)
523 typedef basic_string<_CharT,_Traits,_Alloc> __string_type;
524 typedef typename __string_type::size_type __size_type;
525 __size_type __len = _Traits::length(__lhs);
527 __str.reserve(__len + __rhs.size());
528 __str.append(__lhs, __lhs + __len);
533 template<typename _CharT, typename _Traits, typename _Alloc>
534 basic_string<_CharT,_Traits,_Alloc>
535 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs)
537 typedef basic_string<_CharT,_Traits,_Alloc> __string_type;
538 typedef typename __string_type::size_type __size_type;
540 __size_type __len = __rhs.size();
541 __str.reserve(__len + 1);
542 __str.append(__string_type::size_type(1), __lhs);
547 template<typename _CharT, typename _Traits, typename _Alloc>
548 basic_string<_CharT, _Traits, _Alloc>&
549 basic_string<_CharT, _Traits, _Alloc>::
550 replace(iterator __i1, iterator __i2, size_type __n2, _CharT __c)
552 size_type __n1 = __i2 - __i1;
553 size_type __off1 = __i1 - _M_ibegin();
554 __LENGTHERROR(max_size() - (this->size() - __n1) <= __n2);
555 _M_mutate (__off1, __n1, __n2);
556 // Invalidated __i1, __i2
558 traits_type::assign(_M_data() + __off1, __n2, __c);
562 template<typename _CharT, typename _Traits, typename _Alloc>
563 basic_string<_CharT, _Traits, _Alloc>::size_type
564 basic_string<_CharT, _Traits, _Alloc>::
565 copy(_CharT* __s, size_type __n, size_type __pos) const
567 __OUTOFRANGE(__pos > this->size());
569 if (__n > this->size() - __pos)
570 __n = this->size() - __pos;
572 traits_type::copy(__s, _M_data() + __pos, __n);
573 // 21.3.5.7 par 3: do not append null. (good.)
577 template<typename _CharT, typename _Traits, typename _Alloc>
578 basic_string<_CharT, _Traits, _Alloc>::size_type
579 basic_string<_CharT, _Traits, _Alloc>::
580 find(const _CharT* __s, size_type __pos, size_type __n) const
582 size_type __size = this->size();
583 size_t __xpos = __pos;
584 const _CharT* __data = _M_data();
585 for (; __xpos + __n <= __size; ++__xpos)
586 if (traits_type::compare(__data + __xpos, __s, __n) == 0)
591 template<typename _CharT, typename _Traits, typename _Alloc>
592 basic_string<_CharT, _Traits, _Alloc>::size_type
593 basic_string<_CharT, _Traits, _Alloc>::
594 find(_CharT __c, size_type __pos) const
596 size_type __size = this->size();
597 size_type __ret = npos;
600 const _CharT* __data = _M_data();
601 size_type __n = __size - __pos;
602 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
604 __ret = __p - __data;
610 template<typename _CharT, typename _Traits, typename _Alloc>
611 basic_string<_CharT, _Traits, _Alloc>::size_type
612 basic_string<_CharT, _Traits, _Alloc>::
613 rfind(const _CharT* __s, size_type __pos, size_type __n) const
615 size_type __size = this->size();
618 __pos = std::min(__size - __n ,__pos);
619 const _CharT* __data = _M_data();
622 if (traits_type::compare(__data + __pos, __s, __n) == 0)
630 template<typename _CharT, typename _Traits, typename _Alloc>
631 basic_string<_CharT, _Traits, _Alloc>::size_type
632 basic_string<_CharT, _Traits, _Alloc>::
633 rfind(_CharT __c, size_type __pos) const
635 size_type __size = this->size();
638 size_t __xpos = __size - 1;
642 for (++__xpos; __xpos-- > 0; )
643 if (traits_type::eq(_M_data()[__xpos], __c))
649 template<typename _CharT, typename _Traits, typename _Alloc>
650 basic_string<_CharT, _Traits, _Alloc>::size_type
651 basic_string<_CharT, _Traits, _Alloc>::
652 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
654 for (; __n && __pos < this->size(); ++__pos)
656 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
663 template<typename _CharT, typename _Traits, typename _Alloc>
664 basic_string<_CharT, _Traits, _Alloc>::size_type
665 basic_string<_CharT, _Traits, _Alloc>::
666 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
668 size_type __size = this->size();
671 if (--__size > __pos)
675 if (traits_type::find(__s, __n, _M_data()[__size]))
678 while (__size-- != 0);
683 template<typename _CharT, typename _Traits, typename _Alloc>
684 basic_string<_CharT, _Traits, _Alloc>::size_type
685 basic_string<_CharT, _Traits, _Alloc>::
686 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
688 size_t __xpos = __pos;
689 for (; __n && __xpos < this->size(); ++__xpos)
690 if (!traits_type::find(__s, __n, _M_data()[__xpos]))
695 template<typename _CharT, typename _Traits, typename _Alloc>
696 basic_string<_CharT, _Traits, _Alloc>::size_type
697 basic_string<_CharT, _Traits, _Alloc>::
698 find_first_not_of(_CharT __c, size_type __pos) const
700 size_t __xpos = __pos;
701 for (; __xpos < this->size(); ++__xpos)
702 if (!traits_type::eq(_M_data()[__xpos], __c))
707 template<typename _CharT, typename _Traits, typename _Alloc>
708 basic_string<_CharT, _Traits, _Alloc>::size_type
709 basic_string<_CharT, _Traits, _Alloc>::
710 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
712 size_type __size = this->size();
715 if (--__size > __pos)
719 if (!traits_type::find(__s, __n, _M_data()[__size]))
727 template<typename _CharT, typename _Traits, typename _Alloc>
728 basic_string<_CharT, _Traits, _Alloc>::size_type
729 basic_string<_CharT, _Traits, _Alloc>::
730 find_last_not_of(_CharT __c, size_type __pos) const
732 size_type __size = this->size();
735 if (--__size > __pos)
739 if (!traits_type::eq(_M_data()[__size], __c))
747 template<typename _CharT, typename _Traits, typename _Alloc>
749 basic_string<_CharT, _Traits, _Alloc>::
750 compare(size_type __pos, size_type __n, const basic_string& __str) const
752 size_type __size = this->size();
753 size_type __osize = __str.size();
754 __OUTOFRANGE(__pos > __size);
756 size_type __rsize= min(__size - __pos, __n);
757 size_type __len = min(__rsize, __osize);
758 int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
760 __r = __rsize - __osize;
764 template<typename _CharT, typename _Traits, typename _Alloc>
766 basic_string<_CharT, _Traits, _Alloc>::
767 compare(size_type __pos1, size_type __n1, const basic_string& __str,
768 size_type __pos2, size_type __n2) const
770 size_type __size = this->size();
771 size_type __osize = __str.size();
772 __OUTOFRANGE(__pos1 > __size);
773 __OUTOFRANGE(__pos2 > __osize);
775 size_type __rsize = min(__size - __pos1, __n1);
776 size_type __rosize = min(__osize - __pos2, __n2);
777 size_type __len = min(__rsize, __rosize);
778 int __r = traits_type::compare(_M_data() + __pos1,
779 __str.data() + __pos2, __len);
781 __r = __rsize - __rosize;
786 template<typename _CharT, typename _Traits, typename _Alloc>
788 basic_string<_CharT, _Traits, _Alloc>::
789 compare(const _CharT* __s) const
791 size_type __size = this->size();
792 int __r = traits_type::compare(_M_data(), __s, __size);
794 __r = __size - traits_type::length(__s);
799 template<typename _CharT, typename _Traits, typename _Alloc>
801 basic_string <_CharT,_Traits,_Alloc>::
802 compare(size_type __pos, size_type __n1, const _CharT* __s,
803 size_type __n2) const
805 size_type __size = this->size();
806 __OUTOFRANGE(__pos > __size);
808 size_type __osize = min(traits_type::length(__s), __n2);
809 size_type __rsize = min(__size - __pos, __n1);
810 size_type __len = min(__rsize, __osize);
811 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
813 __r = __rsize - __osize;
817 template <class _CharT, class _Traits, class _Alloc>
819 _S_string_copy(const basic_string<_CharT, _Traits, _Alloc>& __str,
820 _CharT* __buf, typename _Alloc::size_type __bufsiz)
822 typedef typename _Alloc::size_type size_type;
823 size_type __strsize = __str.size();
824 size_type __bytes = min(__strsize, __bufsiz - 1);
825 _Traits::copy(__buf, __str.data(), __bytes);
826 __buf[__bytes] = _CharT();
831 #endif /* _CPP_BITS_STRING_TCC */