OSDN Git Service

2001-11-27 Loren J. Rittle <ljrittle@acm.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / basic_string.tcc
1 // Components for manipulating sequences of characters -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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,
19 // USA.
20
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.
29
30 //
31 // ISO C++ 14882: 21  Strings library
32 //
33
34 // This file is included by <string>.  It is not meant to be included
35 // separately.
36
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.
39
40 #ifndef _CPP_BITS_STRING_TCC
41 #define _CPP_BITS_STRING_TCC 1
42
43 namespace std
44 {
45   template<typename _CharT, typename _Traits, typename _Alloc>
46     const _CharT 
47     basic_string<_CharT, _Traits, _Alloc>::
48     _Rep::_S_terminal = _CharT();
49
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;
54
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;
58
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)];
65
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>
72       _CharT*
73       basic_string<_CharT, _Traits, _Alloc>::
74       _S_construct(_InIter __beg, _InIter __end, const _Alloc& __a,
75                    input_iterator_tag)
76       {
77         if (__beg == __end && __a == _Alloc())
78           return _S_empty_rep()._M_refcopy();
79         // Avoid reallocation for common case.
80         _CharT __buf[100];
81         size_type __i = 0;
82         while (__beg != __end && __i < sizeof(__buf) / sizeof(_CharT))
83           { 
84             __buf[__i++] = *__beg; 
85             ++__beg; 
86           }
87         _Rep* __r = _Rep::_S_create(__i, __a);
88         traits_type::copy(__r->_M_refdata(), __buf, __i);
89         __r->_M_length = __i;
90         try 
91           {
92             // NB: this loop looks precisely this way because
93             // it avoids comparing __beg != __end any more
94             // than strictly necessary; != might be expensive!
95             for (;;)
96               {
97                 _CharT* __p = __r->_M_refdata() + __r->_M_length;
98                 _CharT* __last = __r->_M_refdata() + __r->_M_capacity;
99                 for (;;)
100                   {
101                     if (__beg == __end)
102                       {
103                         __r->_M_length = __p - __r->_M_refdata();
104                         *__p = _Rep::_S_terminal;       // grrr.
105                         return __r->_M_refdata();
106                       }
107                     if (__p == __last)
108                       break;
109                     *__p++ = *__beg; 
110                     ++__beg;
111                   }
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);
118                 __r = __another;
119                 __r->_M_length = __len;
120               }
121           }
122         catch(...) 
123           {
124             __r->_M_destroy(__a); 
125             __throw_exception_again;
126           }
127         return 0;
128       }
129   
130   template<typename _CharT, typename _Traits, typename _Alloc>
131     template <class _InIter>
132       _CharT*
133       basic_string<_CharT, _Traits, _Alloc>::
134       _S_construct(_InIter __beg, _InIter __end, const _Alloc& __a, 
135                    forward_iterator_tag)
136       {
137         size_type __dnew = static_cast<size_type>(distance(__beg, __end));
138
139         if (__beg == __end && __a == _Alloc())
140           return _S_empty_rep()._M_refcopy();
141
142         // Check for out_of_range and length_error exceptions.
143         _Rep* __r = _Rep::_S_create(__dnew, __a);
144         try 
145           { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
146         catch(...) 
147           { 
148             __r->_M_destroy(__a); 
149             __throw_exception_again;
150           }
151         __r->_M_length = __dnew;
152
153         __r->_M_refdata()[__dnew] = _Rep::_S_terminal;  // grrr.
154         return __r->_M_refdata();
155       }
156
157   template<typename _CharT, typename _Traits, typename _Alloc>
158     _CharT*
159     basic_string<_CharT, _Traits, _Alloc>::
160     _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
161     {
162       if (__n == 0 && __a == _Alloc())
163         return _S_empty_rep()._M_refcopy();
164
165       // Check for out_of_range and length_error exceptions.
166       _Rep* __r = _Rep::_S_create(__n, __a);
167       try 
168         { 
169           if (__n) 
170             traits_type::assign(__r->_M_refdata(), __n, __c); 
171         }
172       catch(...) 
173         { 
174           __r->_M_destroy(__a); 
175           __throw_exception_again;
176         }
177       __r->_M_length = __n;
178       __r->_M_refdata()[__n] = _Rep::_S_terminal;  // grrr
179       return __r->_M_refdata();
180     }
181
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     { }
188
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)
193     { }
194  
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())
200     { }
201
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)
208     { }
209
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)
214     { }
215
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)
220     { }
221
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)
226     { }
227  
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)
233     { }
234
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)
238     {
239       if (_M_rep() != __str._M_rep())
240         {
241           // XXX MT
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);
245           _M_data(__tmp);
246         }
247       return *this;
248     }
249   
250   template<typename _CharT, typename _Traits, typename _Alloc>
251     void
252     basic_string<_CharT, _Traits, _Alloc>::_Rep::
253     _M_destroy(const _Alloc& __a) throw ()
254     {
255       size_type __size = sizeof(_Rep) + (_M_capacity + 1) * sizeof(_CharT);
256       _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
257     }
258
259   template<typename _CharT, typename _Traits, typename _Alloc>
260     void
261     basic_string<_CharT, _Traits, _Alloc>::_M_leak_hard()
262     {
263       if (_M_rep()->_M_is_shared()) 
264         _M_mutate(0, 0, 0);
265       _M_rep()->_M_set_leaked();
266     }
267
268   template<typename _CharT, typename _Traits, typename _Alloc>
269     void
270     basic_string<_CharT, _Traits, _Alloc>::
271     _M_mutate(size_type __pos, size_type __len1, size_type __len2)
272     {
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;
277       
278       if (_M_rep()->_M_is_shared() || __new_size > capacity())
279         {
280           // Must reallocate.
281           allocator_type __a = get_allocator();
282           _Rep* __r = _Rep::_S_create(__new_size, __a);
283           try 
284             {
285               if (__pos)
286                 traits_type::copy(__r->_M_refdata(), _M_data(), __pos);
287               if (__how_much)
288                 traits_type::copy(__r->_M_refdata() + __pos + __len2, 
289                                   __src, __how_much);
290             }
291           catch(...) 
292             { 
293               __r->_M_dispose(get_allocator()); 
294               __throw_exception_again;
295             }
296           _M_rep()->_M_dispose(__a);
297           _M_data(__r->_M_refdata());
298       }
299       else if (__how_much && __len1 != __len2)
300         {
301           // Work in-place
302           traits_type::move(_M_data() + __pos + __len2, __src, __how_much);
303         }
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.
308     }
309   
310   template<typename _CharT, typename _Traits, typename _Alloc>
311     void
312     basic_string<_CharT, _Traits, _Alloc>::reserve(size_type __res)
313     {
314       if (__res > this->capacity() || _M_rep()->_M_is_shared())
315         {
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);
324           _M_data(__tmp);
325         }
326     }
327   
328   template<typename _CharT, typename _Traits, typename _Alloc>
329     void basic_string<_CharT, _Traits, _Alloc>::swap(basic_string& __s)
330     {
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())
336         {
337           _CharT* __tmp = _M_data();
338           _M_data(__s._M_data());
339           __s._M_data(__tmp);
340         }
341       // The code below can usually be optimized away.
342       else 
343         {
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());
347           *this = __tmp2;
348           __s = __tmp1;
349         }
350     }
351
352 #ifdef _GLIBCPP_ALLOC_CONTROL
353   template<typename _CharT, typename _Traits, typename _Alloc>
354     bool (*basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_excess_slop) 
355     (size_t, size_t) = 
356     basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_default_excess;
357 #endif
358
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)
363     {
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)
368 #else
369       if (__capacity == npos)
370 #endif
371         __throw_length_error("basic_string::_S_create");
372
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);
377
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
394       // reallocation).
395
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)
409         {
410           size_t __extra =
411             (__pagesize - ((__size + __malloc_header_size) % __pagesize))
412             % __pagesize;
413           __capacity += __extra / sizeof(_CharT);
414           __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
415         }
416       else if (__size > __subpagesize)
417         {
418           size_t __extra =
419             (__subpagesize - ((__size + __malloc_header_size) % __subpagesize))
420             % __subpagesize;
421           __capacity += __extra / sizeof(_CharT);
422           __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
423         }
424
425       // NB: Might throw, but no worries about a leak, mate: _Rep()
426       // does not throw.
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
431       __p->_M_length = 0;
432       return __p;
433     }
434
435   template<typename _CharT, typename _Traits, typename _Alloc>
436     _CharT*
437     basic_string<_CharT, _Traits, _Alloc>::_Rep::
438     _M_clone(const _Alloc& __alloc, size_type __res)
439     {
440       _Rep* __r = _Rep::_S_create(_M_length + __res, __alloc);
441       if (_M_length)
442         {
443           try 
444             { traits_type::copy(__r->_M_refdata(), _M_refdata(), _M_length); }
445           catch(...)  
446             { 
447               __r->_M_destroy(__alloc); 
448               __throw_exception_again;
449             }
450         }
451       __r->_M_length = _M_length;
452       return __r->_M_refdata();
453     }
454   
455   template<typename _CharT, typename _Traits, typename _Alloc>
456   inline bool
457 #ifdef _GLIBCPP_ALLOC_CONTROL
458     basic_string<_CharT, _Traits, _Alloc>::_Rep::
459     _S_default_excess(size_t __s, size_t __r)
460 #else
461     basic_string<_CharT, _Traits, _Alloc>::_Rep::
462     _S_excess_slop(size_t __s, size_t __r)
463 #endif
464     {
465       return 2 * (__s <= 16 ? 16 : __s) < __r;
466     }
467   
468   template<typename _CharT, typename _Traits, typename _Alloc>
469     void
470     basic_string<_CharT, _Traits, _Alloc>::resize(size_type __n, _CharT __c)
471     {
472       if (__n > max_size())
473         __throw_length_error("basic_string::resize");
474       size_type __size = this->size();
475       if (__size < __n)
476         this->append(__n - __size, __c);
477       else if (__n < __size)
478         this->erase(__n);
479       // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
480     }
481   
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)
488       {
489         basic_string __s(__k1, __k2);
490         return this->replace(__i1, __i2, __s._M_ibegin(), __s._M_iend());
491       }
492
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)
499       {
500         size_type __dnew = static_cast<size_type>(distance(__k1, __k2));
501         size_type __dold = __i2 - __i1;
502         size_type __dmax = this->max_size();
503
504         if (__dmax <= __dnew)
505           __throw_length_error("basic_string::_M_replace");
506         size_type __off = __i1 - _M_ibegin();
507
508         // Save concerned source string data in a temporary.
509         basic_string __temp(__k1, __k2);
510         _M_mutate(__off, __dold, __dnew);
511         
512         // Invalidated __i1, __i2 (and clobbered original source string
513         // data when destination string == source string and the string
514         // is unshared).
515         if (__dnew)
516           _S_copy_chars(_M_data() + __off, __temp.begin(), __temp.end());
517
518         return *this;
519       }
520
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)
526     {
527       return this->replace(_M_check(__pos1), _M_fold(__pos1, __n1),
528                            __str._M_check(__pos2), 
529                            __str._M_fold(__pos2, __n2));
530     }
531
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)
536     {
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(),
545                            __str._M_iend());
546     }
547
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)
552     {
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));
561     }
562
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)
567     {
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);
572     }
573
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)
578     {
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);
583     }
584
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)
589     {
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);
593       __string_type __str;
594       __str.reserve(__len + __rhs.size());
595       __str.append(__lhs, __lhs + __len);
596       __str.append(__rhs);
597       return __str;
598     }
599
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)
603     {
604       typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
605       typedef typename __string_type::size_type   __size_type;
606       __string_type __str;
607       __size_type __len = __rhs.size();
608       __str.reserve(__len + 1);
609       __str.append(__size_type(1), __lhs);
610       __str.append(__rhs);
611       return __str;
612     }
613
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)
618     {
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
625       if (__n2)
626         traits_type::assign(_M_data() + __off1, __n2, __c);
627       return *this;
628     }
629   
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
634     {
635       if (__pos > this->size())
636         __throw_out_of_range("basic_string::copy");
637       
638       if (__n > this->size() - __pos)
639         __n = this->size() - __pos;
640       
641       traits_type::copy(__s, _M_data() + __pos, __n);
642       // 21.3.5.7 par 3: do not append null.  (good.)
643       return __n;
644     }
645
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
650     {
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)
656           return __xpos;
657       return npos;
658     }
659
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
664     {
665       size_type __size = this->size();
666       size_type __ret = npos;
667       if (__pos < __size)
668         {
669           const _CharT* __data = _M_data();
670           size_type __n = __size - __pos;
671           const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
672           if (__p)
673             __ret = __p - __data;
674         }
675       return __ret;
676     }
677
678
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
683     {
684       size_type __size = this->size();
685       if (__n <= __size)
686         {
687           __pos = std::min(__size - __n, __pos);
688           const _CharT* __data = _M_data();
689           do 
690             {
691               if (traits_type::compare(__data + __pos, __s, __n) == 0)
692                 return __pos;
693             } 
694           while (__pos-- > 0);
695         }
696       return npos;
697     }
698   
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
703     {
704       size_type __size = this->size();
705       if (__size)
706         {
707           size_t __xpos = __size - 1;
708           if (__xpos > __pos)
709             __xpos = __pos;
710       
711           for (++__xpos; __xpos-- > 0; )
712             if (traits_type::eq(_M_data()[__xpos], __c))
713               return __xpos;
714         }
715       return npos;
716     }
717   
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
722     {
723       for (; __n && __pos < this->size(); ++__pos)
724         {
725           const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
726           if (__p)
727             return __pos;
728         }
729       return npos;
730     }
731  
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
736     {
737       size_type __size = this->size();
738       if (__size && __n)
739         { 
740           if (--__size > __pos) 
741             __size = __pos;
742           do
743             {
744               if (traits_type::find(__s, __n, _M_data()[__size]))
745                 return __size;
746             } 
747           while (__size-- != 0);
748         }
749       return npos;
750     }
751   
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
756     {
757       size_t __xpos = __pos;
758       for (; __xpos < this->size(); ++__xpos)
759         if (!traits_type::find(__s, __n, _M_data()[__xpos]))
760           return __xpos;
761       return npos;
762     }
763
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
768     {
769       size_t __xpos = __pos;
770       for (; __xpos < this->size(); ++__xpos)
771         if (!traits_type::eq(_M_data()[__xpos], __c))
772           return __xpos;
773       return npos;
774     }
775
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
780     {
781       size_type __size = this->size();
782       if (__size)
783         { 
784           if (--__size > __pos) 
785             __size = __pos;
786           do
787             {
788               if (!traits_type::find(__s, __n, _M_data()[__size]))
789                 return __size;
790             } 
791           while (__size--);
792         }
793       return npos;
794     }
795
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
800     {
801       size_type __size = this->size();
802       if (__size)
803         { 
804           if (--__size > __pos) 
805             __size = __pos;
806           do
807             {
808               if (!traits_type::eq(_M_data()[__size], __c))
809                 return __size;
810             } 
811           while (__size--);
812         }
813       return npos;
814     }
815   
816   template<typename _CharT, typename _Traits, typename _Alloc>
817     int
818     basic_string<_CharT, _Traits, _Alloc>::
819     compare(size_type __pos, size_type __n, const basic_string& __str) const
820     {
821       size_type __size = this->size();
822       size_type __osize = __str.size();
823       if (__pos > __size)
824         __throw_out_of_range("basic_string::compare");
825       
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);
829       if (!__r)
830         __r = __rsize - __osize;
831       return __r;
832     }
833
834   template<typename _CharT, typename _Traits, typename _Alloc>
835     int
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
839     {
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");
844       
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);
850       if (!__r)
851         __r = __rsize - __rosize;
852       return __r;
853     }
854
855
856   template<typename _CharT, typename _Traits, typename _Alloc>
857     int
858     basic_string<_CharT, _Traits, _Alloc>::
859     compare(const _CharT* __s) const
860     {
861       size_type __size = this->size();
862       int __r = traits_type::compare(_M_data(), __s, __size);
863       if (!__r)
864         __r = __size - traits_type::length(__s);
865       return __r;
866     }
867
868
869   template<typename _CharT, typename _Traits, typename _Alloc>
870     int
871     basic_string <_CharT, _Traits, _Alloc>::
872     compare(size_type __pos, size_type __n1, const _CharT* __s) const
873     {
874       size_type __size = this->size();
875       if (__pos > __size)
876         __throw_out_of_range("basic_string::compare");
877       
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);
882       if (!__r)
883         __r = __rsize - __osize;
884       return __r;
885     }
886
887   template<typename _CharT, typename _Traits, typename _Alloc>
888     int
889     basic_string <_CharT, _Traits, _Alloc>::
890     compare(size_type __pos, size_type __n1, const _CharT* __s, 
891             size_type __n2) const
892     {
893       size_type __size = this->size();
894       if (__pos > __size)
895         __throw_out_of_range("basic_string::compare");
896       
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);
901       if (!__r)
902         __r = __rsize - __osize;
903       return __r;
904     }
905
906   template <class _CharT, class _Traits, class _Alloc>
907     void
908     _S_string_copy(const basic_string<_CharT, _Traits, _Alloc>& __str,
909                    _CharT* __buf, typename _Alloc::size_type __bufsiz)
910     {
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();
916     }
917 } // namespace std
918
919 #endif