xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/bits/basic_string.tcc (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // Components for manipulating sequences of characters -*- C++ -*-
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4*404b540aSrobert // 2006, 2007
5*404b540aSrobert // Free Software Foundation, Inc.
6*404b540aSrobert //
7*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
8*404b540aSrobert // software; you can redistribute it and/or modify it under the
9*404b540aSrobert // terms of the GNU General Public License as published by the
10*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
11*404b540aSrobert // any later version.
12*404b540aSrobert 
13*404b540aSrobert // This library is distributed in the hope that it will be useful,
14*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*404b540aSrobert // GNU General Public License for more details.
17*404b540aSrobert 
18*404b540aSrobert // You should have received a copy of the GNU General Public License along
19*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
20*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21*404b540aSrobert // USA.
22*404b540aSrobert 
23*404b540aSrobert // As a special exception, you may use this file as part of a free software
24*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
25*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
26*404b540aSrobert // this file and link it with other files to produce an executable, this
27*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
28*404b540aSrobert // the GNU General Public License.  This exception does not however
29*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
30*404b540aSrobert // the GNU General Public License.
31*404b540aSrobert 
32*404b540aSrobert /** @file basic_string.tcc
33*404b540aSrobert  *  This is an internal header file, included by other library headers.
34*404b540aSrobert  *  You should not attempt to use it directly.
35*404b540aSrobert  */
36*404b540aSrobert 
37*404b540aSrobert //
38*404b540aSrobert // ISO C++ 14882: 21  Strings library
39*404b540aSrobert //
40*404b540aSrobert 
41*404b540aSrobert // Written by Jason Merrill based upon the specification by Takanori Adachi
42*404b540aSrobert // in ANSI X3J16/94-0013R2.  Rewritten by Nathan Myers to ISO-14882.
43*404b540aSrobert 
44*404b540aSrobert #ifndef _BASIC_STRING_TCC
45*404b540aSrobert #define _BASIC_STRING_TCC 1
46*404b540aSrobert 
47*404b540aSrobert #pragma GCC system_header
48*404b540aSrobert 
_GLIBCXX_BEGIN_NAMESPACE(std)49*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
50*404b540aSrobert 
51*404b540aSrobert   template<typename _Type>
52*404b540aSrobert     inline bool
53*404b540aSrobert     __is_null_pointer(_Type* __ptr)
54*404b540aSrobert     { return __ptr == 0; }
55*404b540aSrobert 
56*404b540aSrobert   template<typename _Type>
57*404b540aSrobert     inline bool
__is_null_pointer(_Type)58*404b540aSrobert     __is_null_pointer(_Type)
59*404b540aSrobert     { return false; }
60*404b540aSrobert 
61*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
62*404b540aSrobert     const typename basic_string<_CharT, _Traits, _Alloc>::size_type
63*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
64*404b540aSrobert     _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
65*404b540aSrobert 
66*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
67*404b540aSrobert     const _CharT
68*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
69*404b540aSrobert     _Rep::_S_terminal = _CharT();
70*404b540aSrobert 
71*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
72*404b540aSrobert     const typename basic_string<_CharT, _Traits, _Alloc>::size_type
73*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::npos;
74*404b540aSrobert 
75*404b540aSrobert   // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
76*404b540aSrobert   // at static init time (before static ctors are run).
77*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
78*404b540aSrobert     typename basic_string<_CharT, _Traits, _Alloc>::size_type
79*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
80*404b540aSrobert     (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
81*404b540aSrobert       sizeof(size_type)];
82*404b540aSrobert 
83*404b540aSrobert   // NB: This is the special case for Input Iterators, used in
84*404b540aSrobert   // istreambuf_iterators, etc.
85*404b540aSrobert   // Input Iterators have a cost structure very different from
86*404b540aSrobert   // pointers, calling for a different coding style.
87*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
88*404b540aSrobert     template<typename _InIterator>
89*404b540aSrobert       _CharT*
90*404b540aSrobert       basic_string<_CharT, _Traits, _Alloc>::
_S_construct(_InIterator __beg,_InIterator __end,const _Alloc & __a,input_iterator_tag)91*404b540aSrobert       _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
92*404b540aSrobert 		   input_iterator_tag)
93*404b540aSrobert       {
94*404b540aSrobert #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
95*404b540aSrobert 	if (__beg == __end && __a == _Alloc())
96*404b540aSrobert 	  return _S_empty_rep()._M_refdata();
97*404b540aSrobert #endif
98*404b540aSrobert 	// Avoid reallocation for common case.
99*404b540aSrobert 	_CharT __buf[128];
100*404b540aSrobert 	size_type __len = 0;
101*404b540aSrobert 	while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
102*404b540aSrobert 	  {
103*404b540aSrobert 	    __buf[__len++] = *__beg;
104*404b540aSrobert 	    ++__beg;
105*404b540aSrobert 	  }
106*404b540aSrobert 	_Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
107*404b540aSrobert 	_M_copy(__r->_M_refdata(), __buf, __len);
108*404b540aSrobert 	try
109*404b540aSrobert 	  {
110*404b540aSrobert 	    while (__beg != __end)
111*404b540aSrobert 	      {
112*404b540aSrobert 		if (__len == __r->_M_capacity)
113*404b540aSrobert 		  {
114*404b540aSrobert 		    // Allocate more space.
115*404b540aSrobert 		    _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
116*404b540aSrobert 		    _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
117*404b540aSrobert 		    __r->_M_destroy(__a);
118*404b540aSrobert 		    __r = __another;
119*404b540aSrobert 		  }
120*404b540aSrobert 		__r->_M_refdata()[__len++] = *__beg;
121*404b540aSrobert 		++__beg;
122*404b540aSrobert 	      }
123*404b540aSrobert 	  }
124*404b540aSrobert 	catch(...)
125*404b540aSrobert 	  {
126*404b540aSrobert 	    __r->_M_destroy(__a);
127*404b540aSrobert 	    __throw_exception_again;
128*404b540aSrobert 	  }
129*404b540aSrobert 	__r->_M_set_length_and_sharable(__len);
130*404b540aSrobert 	return __r->_M_refdata();
131*404b540aSrobert       }
132*404b540aSrobert 
133*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
134*404b540aSrobert     template <typename _InIterator>
135*404b540aSrobert       _CharT*
136*404b540aSrobert       basic_string<_CharT, _Traits, _Alloc>::
_S_construct(_InIterator __beg,_InIterator __end,const _Alloc & __a,forward_iterator_tag)137*404b540aSrobert       _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
138*404b540aSrobert 		   forward_iterator_tag)
139*404b540aSrobert       {
140*404b540aSrobert #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
141*404b540aSrobert 	if (__beg == __end && __a == _Alloc())
142*404b540aSrobert 	  return _S_empty_rep()._M_refdata();
143*404b540aSrobert #endif
144*404b540aSrobert 	// NB: Not required, but considered best practice.
145*404b540aSrobert 	if (__builtin_expect(__is_null_pointer(__beg) && __beg != __end, 0))
146*404b540aSrobert 	  __throw_logic_error(__N("basic_string::_S_construct NULL not valid"));
147*404b540aSrobert 
148*404b540aSrobert 	const size_type __dnew = static_cast<size_type>(std::distance(__beg,
149*404b540aSrobert 								      __end));
150*404b540aSrobert 	// Check for out_of_range and length_error exceptions.
151*404b540aSrobert 	_Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
152*404b540aSrobert 	try
153*404b540aSrobert 	  { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
154*404b540aSrobert 	catch(...)
155*404b540aSrobert 	  {
156*404b540aSrobert 	    __r->_M_destroy(__a);
157*404b540aSrobert 	    __throw_exception_again;
158*404b540aSrobert 	  }
159*404b540aSrobert 	__r->_M_set_length_and_sharable(__dnew);
160*404b540aSrobert 	return __r->_M_refdata();
161*404b540aSrobert       }
162*404b540aSrobert 
163*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
164*404b540aSrobert     _CharT*
165*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
_S_construct(size_type __n,_CharT __c,const _Alloc & __a)166*404b540aSrobert     _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
167*404b540aSrobert     {
168*404b540aSrobert #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
169*404b540aSrobert       if (__n == 0 && __a == _Alloc())
170*404b540aSrobert 	return _S_empty_rep()._M_refdata();
171*404b540aSrobert #endif
172*404b540aSrobert       // Check for out_of_range and length_error exceptions.
173*404b540aSrobert       _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
174*404b540aSrobert       if (__n)
175*404b540aSrobert 	_M_assign(__r->_M_refdata(), __n, __c);
176*404b540aSrobert 
177*404b540aSrobert       __r->_M_set_length_and_sharable(__n);
178*404b540aSrobert       return __r->_M_refdata();
179*404b540aSrobert     }
180*404b540aSrobert 
181*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
182*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
basic_string(const basic_string & __str)183*404b540aSrobert     basic_string(const basic_string& __str)
184*404b540aSrobert     : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
185*404b540aSrobert 					  __str.get_allocator()),
186*404b540aSrobert 		  __str.get_allocator())
187*404b540aSrobert     { }
188*404b540aSrobert 
189*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
190*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
basic_string(const _Alloc & __a)191*404b540aSrobert     basic_string(const _Alloc& __a)
192*404b540aSrobert     : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
193*404b540aSrobert     { }
194*404b540aSrobert 
195*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
196*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
basic_string(const basic_string & __str,size_type __pos,size_type __n)197*404b540aSrobert     basic_string(const basic_string& __str, size_type __pos, size_type __n)
198*404b540aSrobert     : _M_dataplus(_S_construct(__str._M_data()
199*404b540aSrobert 			       + __str._M_check(__pos,
200*404b540aSrobert 						"basic_string::basic_string"),
201*404b540aSrobert 			       __str._M_data() + __str._M_limit(__pos, __n)
202*404b540aSrobert 			       + __pos, _Alloc()), _Alloc())
203*404b540aSrobert     { }
204*404b540aSrobert 
205*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
206*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
basic_string(const basic_string & __str,size_type __pos,size_type __n,const _Alloc & __a)207*404b540aSrobert     basic_string(const basic_string& __str, size_type __pos,
208*404b540aSrobert 		 size_type __n, const _Alloc& __a)
209*404b540aSrobert     : _M_dataplus(_S_construct(__str._M_data()
210*404b540aSrobert 			       + __str._M_check(__pos,
211*404b540aSrobert 						"basic_string::basic_string"),
212*404b540aSrobert 			       __str._M_data() + __str._M_limit(__pos, __n)
213*404b540aSrobert 			       + __pos, __a), __a)
214*404b540aSrobert     { }
215*404b540aSrobert 
216*404b540aSrobert   // TBD: DPG annotate
217*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
218*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
basic_string(const _CharT * __s,size_type __n,const _Alloc & __a)219*404b540aSrobert     basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
220*404b540aSrobert     : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
221*404b540aSrobert     { }
222*404b540aSrobert 
223*404b540aSrobert   // TBD: DPG annotate
224*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
225*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
basic_string(const _CharT * __s,const _Alloc & __a)226*404b540aSrobert     basic_string(const _CharT* __s, const _Alloc& __a)
227*404b540aSrobert     : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
228*404b540aSrobert 			       __s + npos, __a), __a)
229*404b540aSrobert     { }
230*404b540aSrobert 
231*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
232*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
basic_string(size_type __n,_CharT __c,const _Alloc & __a)233*404b540aSrobert     basic_string(size_type __n, _CharT __c, const _Alloc& __a)
234*404b540aSrobert     : _M_dataplus(_S_construct(__n, __c, __a), __a)
235*404b540aSrobert     { }
236*404b540aSrobert 
237*404b540aSrobert   // TBD: DPG annotate
238*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
239*404b540aSrobert     template<typename _InputIterator>
240*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
basic_string(_InputIterator __beg,_InputIterator __end,const _Alloc & __a)241*404b540aSrobert     basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a)
242*404b540aSrobert     : _M_dataplus(_S_construct(__beg, __end, __a), __a)
243*404b540aSrobert     { }
244*404b540aSrobert 
245*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
246*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>&
247*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
assign(const basic_string & __str)248*404b540aSrobert     assign(const basic_string& __str)
249*404b540aSrobert     {
250*404b540aSrobert       if (_M_rep() != __str._M_rep())
251*404b540aSrobert 	{
252*404b540aSrobert 	  // XXX MT
253*404b540aSrobert 	  const allocator_type __a = this->get_allocator();
254*404b540aSrobert 	  _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
255*404b540aSrobert 	  _M_rep()->_M_dispose(__a);
256*404b540aSrobert 	  _M_data(__tmp);
257*404b540aSrobert 	}
258*404b540aSrobert       return *this;
259*404b540aSrobert     }
260*404b540aSrobert 
261*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
262*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>&
263*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
assign(const _CharT * __s,size_type __n)264*404b540aSrobert     assign(const _CharT* __s, size_type __n)
265*404b540aSrobert     {
266*404b540aSrobert       __glibcxx_requires_string_len(__s, __n);
267*404b540aSrobert       _M_check_length(this->size(), __n, "basic_string::assign");
268*404b540aSrobert       if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
269*404b540aSrobert 	return _M_replace_safe(size_type(0), this->size(), __s, __n);
270*404b540aSrobert       else
271*404b540aSrobert 	{
272*404b540aSrobert 	  // Work in-place.
273*404b540aSrobert 	  const size_type __pos = __s - _M_data();
274*404b540aSrobert 	  if (__pos >= __n)
275*404b540aSrobert 	    _M_copy(_M_data(), __s, __n);
276*404b540aSrobert 	  else if (__pos)
277*404b540aSrobert 	    _M_move(_M_data(), __s, __n);
278*404b540aSrobert 	  _M_rep()->_M_set_length_and_sharable(__n);
279*404b540aSrobert 	  return *this;
280*404b540aSrobert 	}
281*404b540aSrobert      }
282*404b540aSrobert 
283*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
284*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>&
285*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
append(size_type __n,_CharT __c)286*404b540aSrobert     append(size_type __n, _CharT __c)
287*404b540aSrobert     {
288*404b540aSrobert       if (__n)
289*404b540aSrobert 	{
290*404b540aSrobert 	  _M_check_length(size_type(0), __n, "basic_string::append");
291*404b540aSrobert 	  const size_type __len = __n + this->size();
292*404b540aSrobert 	  if (__len > this->capacity() || _M_rep()->_M_is_shared())
293*404b540aSrobert 	    this->reserve(__len);
294*404b540aSrobert 	  _M_assign(_M_data() + this->size(), __n, __c);
295*404b540aSrobert 	  _M_rep()->_M_set_length_and_sharable(__len);
296*404b540aSrobert 	}
297*404b540aSrobert       return *this;
298*404b540aSrobert     }
299*404b540aSrobert 
300*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
301*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>&
302*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
append(const _CharT * __s,size_type __n)303*404b540aSrobert     append(const _CharT* __s, size_type __n)
304*404b540aSrobert     {
305*404b540aSrobert       __glibcxx_requires_string_len(__s, __n);
306*404b540aSrobert       if (__n)
307*404b540aSrobert 	{
308*404b540aSrobert 	  _M_check_length(size_type(0), __n, "basic_string::append");
309*404b540aSrobert 	  const size_type __len = __n + this->size();
310*404b540aSrobert 	  if (__len > this->capacity() || _M_rep()->_M_is_shared())
311*404b540aSrobert 	    {
312*404b540aSrobert 	      if (_M_disjunct(__s))
313*404b540aSrobert 		this->reserve(__len);
314*404b540aSrobert 	      else
315*404b540aSrobert 		{
316*404b540aSrobert 		  const size_type __off = __s - _M_data();
317*404b540aSrobert 		  this->reserve(__len);
318*404b540aSrobert 		  __s = _M_data() + __off;
319*404b540aSrobert 		}
320*404b540aSrobert 	    }
321*404b540aSrobert 	  _M_copy(_M_data() + this->size(), __s, __n);
322*404b540aSrobert 	  _M_rep()->_M_set_length_and_sharable(__len);
323*404b540aSrobert 	}
324*404b540aSrobert       return *this;
325*404b540aSrobert     }
326*404b540aSrobert 
327*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
328*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>&
329*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
append(const basic_string & __str)330*404b540aSrobert     append(const basic_string& __str)
331*404b540aSrobert     {
332*404b540aSrobert       const size_type __size = __str.size();
333*404b540aSrobert       if (__size)
334*404b540aSrobert 	{
335*404b540aSrobert 	  const size_type __len = __size + this->size();
336*404b540aSrobert 	  if (__len > this->capacity() || _M_rep()->_M_is_shared())
337*404b540aSrobert 	    this->reserve(__len);
338*404b540aSrobert 	  _M_copy(_M_data() + this->size(), __str._M_data(), __size);
339*404b540aSrobert 	  _M_rep()->_M_set_length_and_sharable(__len);
340*404b540aSrobert 	}
341*404b540aSrobert       return *this;
342*404b540aSrobert     }
343*404b540aSrobert 
344*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
345*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>&
346*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
append(const basic_string & __str,size_type __pos,size_type __n)347*404b540aSrobert     append(const basic_string& __str, size_type __pos, size_type __n)
348*404b540aSrobert     {
349*404b540aSrobert       __str._M_check(__pos, "basic_string::append");
350*404b540aSrobert       __n = __str._M_limit(__pos, __n);
351*404b540aSrobert       if (__n)
352*404b540aSrobert 	{
353*404b540aSrobert 	  const size_type __len = __n + this->size();
354*404b540aSrobert 	  if (__len > this->capacity() || _M_rep()->_M_is_shared())
355*404b540aSrobert 	    this->reserve(__len);
356*404b540aSrobert 	  _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
357*404b540aSrobert 	  _M_rep()->_M_set_length_and_sharable(__len);
358*404b540aSrobert 	}
359*404b540aSrobert       return *this;
360*404b540aSrobert     }
361*404b540aSrobert 
362*404b540aSrobert    template<typename _CharT, typename _Traits, typename _Alloc>
363*404b540aSrobert      basic_string<_CharT, _Traits, _Alloc>&
364*404b540aSrobert      basic_string<_CharT, _Traits, _Alloc>::
insert(size_type __pos,const _CharT * __s,size_type __n)365*404b540aSrobert      insert(size_type __pos, const _CharT* __s, size_type __n)
366*404b540aSrobert      {
367*404b540aSrobert        __glibcxx_requires_string_len(__s, __n);
368*404b540aSrobert        _M_check(__pos, "basic_string::insert");
369*404b540aSrobert        _M_check_length(size_type(0), __n, "basic_string::insert");
370*404b540aSrobert        if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
371*404b540aSrobert          return _M_replace_safe(__pos, size_type(0), __s, __n);
372*404b540aSrobert        else
373*404b540aSrobert          {
374*404b540aSrobert            // Work in-place.
375*404b540aSrobert            const size_type __off = __s - _M_data();
376*404b540aSrobert            _M_mutate(__pos, 0, __n);
377*404b540aSrobert            __s = _M_data() + __off;
378*404b540aSrobert            _CharT* __p = _M_data() + __pos;
379*404b540aSrobert            if (__s  + __n <= __p)
380*404b540aSrobert              _M_copy(__p, __s, __n);
381*404b540aSrobert            else if (__s >= __p)
382*404b540aSrobert              _M_copy(__p, __s + __n, __n);
383*404b540aSrobert            else
384*404b540aSrobert              {
385*404b540aSrobert 	       const size_type __nleft = __p - __s;
386*404b540aSrobert                _M_copy(__p, __s, __nleft);
387*404b540aSrobert                _M_copy(__p + __nleft, __p + __n, __n - __nleft);
388*404b540aSrobert              }
389*404b540aSrobert            return *this;
390*404b540aSrobert          }
391*404b540aSrobert      }
392*404b540aSrobert 
393*404b540aSrobert    template<typename _CharT, typename _Traits, typename _Alloc>
394*404b540aSrobert      basic_string<_CharT, _Traits, _Alloc>&
395*404b540aSrobert      basic_string<_CharT, _Traits, _Alloc>::
replace(size_type __pos,size_type __n1,const _CharT * __s,size_type __n2)396*404b540aSrobert      replace(size_type __pos, size_type __n1, const _CharT* __s,
397*404b540aSrobert 	     size_type __n2)
398*404b540aSrobert      {
399*404b540aSrobert        __glibcxx_requires_string_len(__s, __n2);
400*404b540aSrobert        _M_check(__pos, "basic_string::replace");
401*404b540aSrobert        __n1 = _M_limit(__pos, __n1);
402*404b540aSrobert        _M_check_length(__n1, __n2, "basic_string::replace");
403*404b540aSrobert        bool __left;
404*404b540aSrobert        if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
405*404b540aSrobert          return _M_replace_safe(__pos, __n1, __s, __n2);
406*404b540aSrobert        else if ((__left = __s + __n2 <= _M_data() + __pos)
407*404b540aSrobert 		|| _M_data() + __pos + __n1 <= __s)
408*404b540aSrobert 	 {
409*404b540aSrobert 	   // Work in-place: non-overlapping case.
410*404b540aSrobert 	   size_type __off = __s - _M_data();
411*404b540aSrobert 	   __left ? __off : (__off += __n2 - __n1);
412*404b540aSrobert 	   _M_mutate(__pos, __n1, __n2);
413*404b540aSrobert 	   _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
414*404b540aSrobert 	   return *this;
415*404b540aSrobert 	 }
416*404b540aSrobert        else
417*404b540aSrobert 	 {
418*404b540aSrobert 	   // Todo: overlapping case.
419*404b540aSrobert 	   const basic_string __tmp(__s, __n2);
420*404b540aSrobert 	   return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
421*404b540aSrobert 	 }
422*404b540aSrobert      }
423*404b540aSrobert 
424*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
425*404b540aSrobert     void
426*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::_Rep::
_M_destroy(const _Alloc & __a)427*404b540aSrobert     _M_destroy(const _Alloc& __a) throw ()
428*404b540aSrobert     {
429*404b540aSrobert       const size_type __size = sizeof(_Rep_base) +
430*404b540aSrobert 	                       (this->_M_capacity + 1) * sizeof(_CharT);
431*404b540aSrobert       _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
432*404b540aSrobert     }
433*404b540aSrobert 
434*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
435*404b540aSrobert     void
436*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
_M_leak_hard()437*404b540aSrobert     _M_leak_hard()
438*404b540aSrobert     {
439*404b540aSrobert #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
440*404b540aSrobert       if (_M_rep() == &_S_empty_rep())
441*404b540aSrobert 	return;
442*404b540aSrobert #endif
443*404b540aSrobert       if (_M_rep()->_M_is_shared())
444*404b540aSrobert 	_M_mutate(0, 0, 0);
445*404b540aSrobert       _M_rep()->_M_set_leaked();
446*404b540aSrobert     }
447*404b540aSrobert 
448*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
449*404b540aSrobert     void
450*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
_M_mutate(size_type __pos,size_type __len1,size_type __len2)451*404b540aSrobert     _M_mutate(size_type __pos, size_type __len1, size_type __len2)
452*404b540aSrobert     {
453*404b540aSrobert       const size_type __old_size = this->size();
454*404b540aSrobert       const size_type __new_size = __old_size + __len2 - __len1;
455*404b540aSrobert       const size_type __how_much = __old_size - __pos - __len1;
456*404b540aSrobert 
457*404b540aSrobert       if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
458*404b540aSrobert 	{
459*404b540aSrobert 	  // Must reallocate.
460*404b540aSrobert 	  const allocator_type __a = get_allocator();
461*404b540aSrobert 	  _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
462*404b540aSrobert 
463*404b540aSrobert 	  if (__pos)
464*404b540aSrobert 	    _M_copy(__r->_M_refdata(), _M_data(), __pos);
465*404b540aSrobert 	  if (__how_much)
466*404b540aSrobert 	    _M_copy(__r->_M_refdata() + __pos + __len2,
467*404b540aSrobert 		    _M_data() + __pos + __len1, __how_much);
468*404b540aSrobert 
469*404b540aSrobert 	  _M_rep()->_M_dispose(__a);
470*404b540aSrobert 	  _M_data(__r->_M_refdata());
471*404b540aSrobert 	}
472*404b540aSrobert       else if (__how_much && __len1 != __len2)
473*404b540aSrobert 	{
474*404b540aSrobert 	  // Work in-place.
475*404b540aSrobert 	  _M_move(_M_data() + __pos + __len2,
476*404b540aSrobert 		  _M_data() + __pos + __len1, __how_much);
477*404b540aSrobert 	}
478*404b540aSrobert       _M_rep()->_M_set_length_and_sharable(__new_size);
479*404b540aSrobert     }
480*404b540aSrobert 
481*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
482*404b540aSrobert     void
483*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
reserve(size_type __res)484*404b540aSrobert     reserve(size_type __res)
485*404b540aSrobert     {
486*404b540aSrobert       if (__res != this->capacity() || _M_rep()->_M_is_shared())
487*404b540aSrobert         {
488*404b540aSrobert 	  // Make sure we don't shrink below the current size
489*404b540aSrobert 	  if (__res < this->size())
490*404b540aSrobert 	    __res = this->size();
491*404b540aSrobert 	  const allocator_type __a = get_allocator();
492*404b540aSrobert 	  _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
493*404b540aSrobert 	  _M_rep()->_M_dispose(__a);
494*404b540aSrobert 	  _M_data(__tmp);
495*404b540aSrobert         }
496*404b540aSrobert     }
497*404b540aSrobert 
498*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
499*404b540aSrobert     void
500*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
swap(basic_string & __s)501*404b540aSrobert     swap(basic_string& __s)
502*404b540aSrobert     {
503*404b540aSrobert       if (_M_rep()->_M_is_leaked())
504*404b540aSrobert 	_M_rep()->_M_set_sharable();
505*404b540aSrobert       if (__s._M_rep()->_M_is_leaked())
506*404b540aSrobert 	__s._M_rep()->_M_set_sharable();
507*404b540aSrobert       if (this->get_allocator() == __s.get_allocator())
508*404b540aSrobert 	{
509*404b540aSrobert 	  _CharT* __tmp = _M_data();
510*404b540aSrobert 	  _M_data(__s._M_data());
511*404b540aSrobert 	  __s._M_data(__tmp);
512*404b540aSrobert 	}
513*404b540aSrobert       // The code below can usually be optimized away.
514*404b540aSrobert       else
515*404b540aSrobert 	{
516*404b540aSrobert 	  const basic_string __tmp1(_M_ibegin(), _M_iend(),
517*404b540aSrobert 				    __s.get_allocator());
518*404b540aSrobert 	  const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
519*404b540aSrobert 				    this->get_allocator());
520*404b540aSrobert 	  *this = __tmp2;
521*404b540aSrobert 	  __s = __tmp1;
522*404b540aSrobert 	}
523*404b540aSrobert     }
524*404b540aSrobert 
525*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
526*404b540aSrobert     typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
527*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::_Rep::
_S_create(size_type __capacity,size_type __old_capacity,const _Alloc & __alloc)528*404b540aSrobert     _S_create(size_type __capacity, size_type __old_capacity,
529*404b540aSrobert 	      const _Alloc& __alloc)
530*404b540aSrobert     {
531*404b540aSrobert       // _GLIBCXX_RESOLVE_LIB_DEFECTS
532*404b540aSrobert       // 83.  String::npos vs. string::max_size()
533*404b540aSrobert       if (__capacity > _S_max_size)
534*404b540aSrobert 	__throw_length_error(__N("basic_string::_S_create"));
535*404b540aSrobert 
536*404b540aSrobert       // The standard places no restriction on allocating more memory
537*404b540aSrobert       // than is strictly needed within this layer at the moment or as
538*404b540aSrobert       // requested by an explicit application call to reserve().
539*404b540aSrobert 
540*404b540aSrobert       // Many malloc implementations perform quite poorly when an
541*404b540aSrobert       // application attempts to allocate memory in a stepwise fashion
542*404b540aSrobert       // growing each allocation size by only 1 char.  Additionally,
543*404b540aSrobert       // it makes little sense to allocate less linear memory than the
544*404b540aSrobert       // natural blocking size of the malloc implementation.
545*404b540aSrobert       // Unfortunately, we would need a somewhat low-level calculation
546*404b540aSrobert       // with tuned parameters to get this perfect for any particular
547*404b540aSrobert       // malloc implementation.  Fortunately, generalizations about
548*404b540aSrobert       // common features seen among implementations seems to suffice.
549*404b540aSrobert 
550*404b540aSrobert       // __pagesize need not match the actual VM page size for good
551*404b540aSrobert       // results in practice, thus we pick a common value on the low
552*404b540aSrobert       // side.  __malloc_header_size is an estimate of the amount of
553*404b540aSrobert       // overhead per memory allocation (in practice seen N * sizeof
554*404b540aSrobert       // (void*) where N is 0, 2 or 4).  According to folklore,
555*404b540aSrobert       // picking this value on the high side is better than
556*404b540aSrobert       // low-balling it (especially when this algorithm is used with
557*404b540aSrobert       // malloc implementations that allocate memory blocks rounded up
558*404b540aSrobert       // to a size which is a power of 2).
559*404b540aSrobert       const size_type __pagesize = 4096;
560*404b540aSrobert       const size_type __malloc_header_size = 4 * sizeof(void*);
561*404b540aSrobert 
562*404b540aSrobert       // The below implements an exponential growth policy, necessary to
563*404b540aSrobert       // meet amortized linear time requirements of the library: see
564*404b540aSrobert       // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
565*404b540aSrobert       // It's active for allocations requiring an amount of memory above
566*404b540aSrobert       // system pagesize. This is consistent with the requirements of the
567*404b540aSrobert       // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
568*404b540aSrobert       if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
569*404b540aSrobert 	__capacity = 2 * __old_capacity;
570*404b540aSrobert 
571*404b540aSrobert       // NB: Need an array of char_type[__capacity], plus a terminating
572*404b540aSrobert       // null char_type() element, plus enough for the _Rep data structure.
573*404b540aSrobert       // Whew. Seemingly so needy, yet so elemental.
574*404b540aSrobert       size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
575*404b540aSrobert 
576*404b540aSrobert       const size_type __adj_size = __size + __malloc_header_size;
577*404b540aSrobert       if (__adj_size > __pagesize && __capacity > __old_capacity)
578*404b540aSrobert 	{
579*404b540aSrobert 	  const size_type __extra = __pagesize - __adj_size % __pagesize;
580*404b540aSrobert 	  __capacity += __extra / sizeof(_CharT);
581*404b540aSrobert 	  // Never allocate a string bigger than _S_max_size.
582*404b540aSrobert 	  if (__capacity > _S_max_size)
583*404b540aSrobert 	    __capacity = _S_max_size;
584*404b540aSrobert 	  __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
585*404b540aSrobert 	}
586*404b540aSrobert 
587*404b540aSrobert       // NB: Might throw, but no worries about a leak, mate: _Rep()
588*404b540aSrobert       // does not throw.
589*404b540aSrobert       void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
590*404b540aSrobert       _Rep *__p = new (__place) _Rep;
591*404b540aSrobert       __p->_M_capacity = __capacity;
592*404b540aSrobert       // ABI compatibility - 3.4.x set in _S_create both
593*404b540aSrobert       // _M_refcount and _M_length.  All callers of _S_create
594*404b540aSrobert       // in basic_string.tcc then set just _M_length.
595*404b540aSrobert       // In 4.0.x and later both _M_refcount and _M_length
596*404b540aSrobert       // are initialized in the callers, unfortunately we can
597*404b540aSrobert       // have 3.4.x compiled code with _S_create callers inlined
598*404b540aSrobert       // calling 4.0.x+ _S_create.
599*404b540aSrobert       __p->_M_set_sharable();
600*404b540aSrobert       return __p;
601*404b540aSrobert     }
602*404b540aSrobert 
603*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
604*404b540aSrobert     _CharT*
605*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::_Rep::
_M_clone(const _Alloc & __alloc,size_type __res)606*404b540aSrobert     _M_clone(const _Alloc& __alloc, size_type __res)
607*404b540aSrobert     {
608*404b540aSrobert       // Requested capacity of the clone.
609*404b540aSrobert       const size_type __requested_cap = this->_M_length + __res;
610*404b540aSrobert       _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
611*404b540aSrobert 				  __alloc);
612*404b540aSrobert       if (this->_M_length)
613*404b540aSrobert 	_M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
614*404b540aSrobert 
615*404b540aSrobert       __r->_M_set_length_and_sharable(this->_M_length);
616*404b540aSrobert       return __r->_M_refdata();
617*404b540aSrobert     }
618*404b540aSrobert 
619*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
620*404b540aSrobert     void
621*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
resize(size_type __n,_CharT __c)622*404b540aSrobert     resize(size_type __n, _CharT __c)
623*404b540aSrobert     {
624*404b540aSrobert       const size_type __size = this->size();
625*404b540aSrobert       _M_check_length(__size, __n, "basic_string::resize");
626*404b540aSrobert       if (__size < __n)
627*404b540aSrobert 	this->append(__n - __size, __c);
628*404b540aSrobert       else if (__n < __size)
629*404b540aSrobert 	this->erase(__n);
630*404b540aSrobert       // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
631*404b540aSrobert     }
632*404b540aSrobert 
633*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
634*404b540aSrobert     template<typename _InputIterator>
635*404b540aSrobert       basic_string<_CharT, _Traits, _Alloc>&
636*404b540aSrobert       basic_string<_CharT, _Traits, _Alloc>::
_M_replace_dispatch(iterator __i1,iterator __i2,_InputIterator __k1,_InputIterator __k2,__false_type)637*404b540aSrobert       _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
638*404b540aSrobert 			  _InputIterator __k2, __false_type)
639*404b540aSrobert       {
640*404b540aSrobert 	const basic_string __s(__k1, __k2);
641*404b540aSrobert 	const size_type __n1 = __i2 - __i1;
642*404b540aSrobert 	_M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
643*404b540aSrobert 	return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
644*404b540aSrobert 			       __s.size());
645*404b540aSrobert       }
646*404b540aSrobert 
647*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
648*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>&
649*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
_M_replace_aux(size_type __pos1,size_type __n1,size_type __n2,_CharT __c)650*404b540aSrobert     _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
651*404b540aSrobert 		   _CharT __c)
652*404b540aSrobert     {
653*404b540aSrobert       _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
654*404b540aSrobert       _M_mutate(__pos1, __n1, __n2);
655*404b540aSrobert       if (__n2)
656*404b540aSrobert 	_M_assign(_M_data() + __pos1, __n2, __c);
657*404b540aSrobert       return *this;
658*404b540aSrobert     }
659*404b540aSrobert 
660*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
661*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>&
662*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
_M_replace_safe(size_type __pos1,size_type __n1,const _CharT * __s,size_type __n2)663*404b540aSrobert     _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
664*404b540aSrobert 		    size_type __n2)
665*404b540aSrobert     {
666*404b540aSrobert       _M_mutate(__pos1, __n1, __n2);
667*404b540aSrobert       if (__n2)
668*404b540aSrobert 	_M_copy(_M_data() + __pos1, __s, __n2);
669*404b540aSrobert       return *this;
670*404b540aSrobert     }
671*404b540aSrobert 
672*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
673*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>
operator +(const _CharT * __lhs,const basic_string<_CharT,_Traits,_Alloc> & __rhs)674*404b540aSrobert     operator+(const _CharT* __lhs,
675*404b540aSrobert 	      const basic_string<_CharT, _Traits, _Alloc>& __rhs)
676*404b540aSrobert     {
677*404b540aSrobert       __glibcxx_requires_string(__lhs);
678*404b540aSrobert       typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
679*404b540aSrobert       typedef typename __string_type::size_type	  __size_type;
680*404b540aSrobert       const __size_type __len = _Traits::length(__lhs);
681*404b540aSrobert       __string_type __str;
682*404b540aSrobert       __str.reserve(__len + __rhs.size());
683*404b540aSrobert       __str.append(__lhs, __len);
684*404b540aSrobert       __str.append(__rhs);
685*404b540aSrobert       return __str;
686*404b540aSrobert     }
687*404b540aSrobert 
688*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
689*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>
operator +(_CharT __lhs,const basic_string<_CharT,_Traits,_Alloc> & __rhs)690*404b540aSrobert     operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
691*404b540aSrobert     {
692*404b540aSrobert       typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
693*404b540aSrobert       typedef typename __string_type::size_type	  __size_type;
694*404b540aSrobert       __string_type __str;
695*404b540aSrobert       const __size_type __len = __rhs.size();
696*404b540aSrobert       __str.reserve(__len + 1);
697*404b540aSrobert       __str.append(__size_type(1), __lhs);
698*404b540aSrobert       __str.append(__rhs);
699*404b540aSrobert       return __str;
700*404b540aSrobert     }
701*404b540aSrobert 
702*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
703*404b540aSrobert     typename basic_string<_CharT, _Traits, _Alloc>::size_type
704*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
copy(_CharT * __s,size_type __n,size_type __pos) const705*404b540aSrobert     copy(_CharT* __s, size_type __n, size_type __pos) const
706*404b540aSrobert     {
707*404b540aSrobert       _M_check(__pos, "basic_string::copy");
708*404b540aSrobert       __n = _M_limit(__pos, __n);
709*404b540aSrobert       __glibcxx_requires_string_len(__s, __n);
710*404b540aSrobert       if (__n)
711*404b540aSrobert 	_M_copy(__s, _M_data() + __pos, __n);
712*404b540aSrobert       // 21.3.5.7 par 3: do not append null.  (good.)
713*404b540aSrobert       return __n;
714*404b540aSrobert     }
715*404b540aSrobert 
716*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
717*404b540aSrobert     typename basic_string<_CharT, _Traits, _Alloc>::size_type
718*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
find(const _CharT * __s,size_type __pos,size_type __n) const719*404b540aSrobert     find(const _CharT* __s, size_type __pos, size_type __n) const
720*404b540aSrobert     {
721*404b540aSrobert       __glibcxx_requires_string_len(__s, __n);
722*404b540aSrobert       const size_type __size = this->size();
723*404b540aSrobert       const _CharT* __data = _M_data();
724*404b540aSrobert 
725*404b540aSrobert       if (__n == 0)
726*404b540aSrobert 	return __pos <= __size ? __pos : npos;
727*404b540aSrobert 
728*404b540aSrobert       if (__n <= __size)
729*404b540aSrobert 	{
730*404b540aSrobert 	  for (; __pos <= __size - __n; ++__pos)
731*404b540aSrobert 	    if (traits_type::eq(__data[__pos], __s[0])
732*404b540aSrobert 		&& traits_type::compare(__data + __pos + 1,
733*404b540aSrobert 					__s + 1, __n - 1) == 0)
734*404b540aSrobert 	      return __pos;
735*404b540aSrobert 	}
736*404b540aSrobert       return npos;
737*404b540aSrobert     }
738*404b540aSrobert 
739*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
740*404b540aSrobert     typename basic_string<_CharT, _Traits, _Alloc>::size_type
741*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
find(_CharT __c,size_type __pos) const742*404b540aSrobert     find(_CharT __c, size_type __pos) const
743*404b540aSrobert     {
744*404b540aSrobert       size_type __ret = npos;
745*404b540aSrobert       const size_type __size = this->size();
746*404b540aSrobert       if (__pos < __size)
747*404b540aSrobert 	{
748*404b540aSrobert 	  const _CharT* __data = _M_data();
749*404b540aSrobert 	  const size_type __n = __size - __pos;
750*404b540aSrobert 	  const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
751*404b540aSrobert 	  if (__p)
752*404b540aSrobert 	    __ret = __p - __data;
753*404b540aSrobert 	}
754*404b540aSrobert       return __ret;
755*404b540aSrobert     }
756*404b540aSrobert 
757*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
758*404b540aSrobert     typename basic_string<_CharT, _Traits, _Alloc>::size_type
759*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
rfind(const _CharT * __s,size_type __pos,size_type __n) const760*404b540aSrobert     rfind(const _CharT* __s, size_type __pos, size_type __n) const
761*404b540aSrobert     {
762*404b540aSrobert       __glibcxx_requires_string_len(__s, __n);
763*404b540aSrobert       const size_type __size = this->size();
764*404b540aSrobert       if (__n <= __size)
765*404b540aSrobert 	{
766*404b540aSrobert 	  __pos = std::min(size_type(__size - __n), __pos);
767*404b540aSrobert 	  const _CharT* __data = _M_data();
768*404b540aSrobert 	  do
769*404b540aSrobert 	    {
770*404b540aSrobert 	      if (traits_type::compare(__data + __pos, __s, __n) == 0)
771*404b540aSrobert 		return __pos;
772*404b540aSrobert 	    }
773*404b540aSrobert 	  while (__pos-- > 0);
774*404b540aSrobert 	}
775*404b540aSrobert       return npos;
776*404b540aSrobert     }
777*404b540aSrobert 
778*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
779*404b540aSrobert     typename basic_string<_CharT, _Traits, _Alloc>::size_type
780*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
rfind(_CharT __c,size_type __pos) const781*404b540aSrobert     rfind(_CharT __c, size_type __pos) const
782*404b540aSrobert     {
783*404b540aSrobert       size_type __size = this->size();
784*404b540aSrobert       if (__size)
785*404b540aSrobert 	{
786*404b540aSrobert 	  if (--__size > __pos)
787*404b540aSrobert 	    __size = __pos;
788*404b540aSrobert 	  for (++__size; __size-- > 0; )
789*404b540aSrobert 	    if (traits_type::eq(_M_data()[__size], __c))
790*404b540aSrobert 	      return __size;
791*404b540aSrobert 	}
792*404b540aSrobert       return npos;
793*404b540aSrobert     }
794*404b540aSrobert 
795*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
796*404b540aSrobert     typename basic_string<_CharT, _Traits, _Alloc>::size_type
797*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
find_first_of(const _CharT * __s,size_type __pos,size_type __n) const798*404b540aSrobert     find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
799*404b540aSrobert     {
800*404b540aSrobert       __glibcxx_requires_string_len(__s, __n);
801*404b540aSrobert       for (; __n && __pos < this->size(); ++__pos)
802*404b540aSrobert 	{
803*404b540aSrobert 	  const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
804*404b540aSrobert 	  if (__p)
805*404b540aSrobert 	    return __pos;
806*404b540aSrobert 	}
807*404b540aSrobert       return npos;
808*404b540aSrobert     }
809*404b540aSrobert 
810*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
811*404b540aSrobert     typename basic_string<_CharT, _Traits, _Alloc>::size_type
812*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
find_last_of(const _CharT * __s,size_type __pos,size_type __n) const813*404b540aSrobert     find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
814*404b540aSrobert     {
815*404b540aSrobert       __glibcxx_requires_string_len(__s, __n);
816*404b540aSrobert       size_type __size = this->size();
817*404b540aSrobert       if (__size && __n)
818*404b540aSrobert 	{
819*404b540aSrobert 	  if (--__size > __pos)
820*404b540aSrobert 	    __size = __pos;
821*404b540aSrobert 	  do
822*404b540aSrobert 	    {
823*404b540aSrobert 	      if (traits_type::find(__s, __n, _M_data()[__size]))
824*404b540aSrobert 		return __size;
825*404b540aSrobert 	    }
826*404b540aSrobert 	  while (__size-- != 0);
827*404b540aSrobert 	}
828*404b540aSrobert       return npos;
829*404b540aSrobert     }
830*404b540aSrobert 
831*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
832*404b540aSrobert     typename basic_string<_CharT, _Traits, _Alloc>::size_type
833*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
find_first_not_of(const _CharT * __s,size_type __pos,size_type __n) const834*404b540aSrobert     find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
835*404b540aSrobert     {
836*404b540aSrobert       __glibcxx_requires_string_len(__s, __n);
837*404b540aSrobert       for (; __pos < this->size(); ++__pos)
838*404b540aSrobert 	if (!traits_type::find(__s, __n, _M_data()[__pos]))
839*404b540aSrobert 	  return __pos;
840*404b540aSrobert       return npos;
841*404b540aSrobert     }
842*404b540aSrobert 
843*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
844*404b540aSrobert     typename basic_string<_CharT, _Traits, _Alloc>::size_type
845*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
find_first_not_of(_CharT __c,size_type __pos) const846*404b540aSrobert     find_first_not_of(_CharT __c, size_type __pos) const
847*404b540aSrobert     {
848*404b540aSrobert       for (; __pos < this->size(); ++__pos)
849*404b540aSrobert 	if (!traits_type::eq(_M_data()[__pos], __c))
850*404b540aSrobert 	  return __pos;
851*404b540aSrobert       return npos;
852*404b540aSrobert     }
853*404b540aSrobert 
854*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
855*404b540aSrobert     typename basic_string<_CharT, _Traits, _Alloc>::size_type
856*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
find_last_not_of(const _CharT * __s,size_type __pos,size_type __n) const857*404b540aSrobert     find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
858*404b540aSrobert     {
859*404b540aSrobert       __glibcxx_requires_string_len(__s, __n);
860*404b540aSrobert       size_type __size = this->size();
861*404b540aSrobert       if (__size)
862*404b540aSrobert 	{
863*404b540aSrobert 	  if (--__size > __pos)
864*404b540aSrobert 	    __size = __pos;
865*404b540aSrobert 	  do
866*404b540aSrobert 	    {
867*404b540aSrobert 	      if (!traits_type::find(__s, __n, _M_data()[__size]))
868*404b540aSrobert 		return __size;
869*404b540aSrobert 	    }
870*404b540aSrobert 	  while (__size--);
871*404b540aSrobert 	}
872*404b540aSrobert       return npos;
873*404b540aSrobert     }
874*404b540aSrobert 
875*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
876*404b540aSrobert     typename basic_string<_CharT, _Traits, _Alloc>::size_type
877*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
find_last_not_of(_CharT __c,size_type __pos) const878*404b540aSrobert     find_last_not_of(_CharT __c, size_type __pos) const
879*404b540aSrobert     {
880*404b540aSrobert       size_type __size = this->size();
881*404b540aSrobert       if (__size)
882*404b540aSrobert 	{
883*404b540aSrobert 	  if (--__size > __pos)
884*404b540aSrobert 	    __size = __pos;
885*404b540aSrobert 	  do
886*404b540aSrobert 	    {
887*404b540aSrobert 	      if (!traits_type::eq(_M_data()[__size], __c))
888*404b540aSrobert 		return __size;
889*404b540aSrobert 	    }
890*404b540aSrobert 	  while (__size--);
891*404b540aSrobert 	}
892*404b540aSrobert       return npos;
893*404b540aSrobert     }
894*404b540aSrobert 
895*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
896*404b540aSrobert     int
897*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
compare(size_type __pos,size_type __n,const basic_string & __str) const898*404b540aSrobert     compare(size_type __pos, size_type __n, const basic_string& __str) const
899*404b540aSrobert     {
900*404b540aSrobert       _M_check(__pos, "basic_string::compare");
901*404b540aSrobert       __n = _M_limit(__pos, __n);
902*404b540aSrobert       const size_type __osize = __str.size();
903*404b540aSrobert       const size_type __len = std::min(__n, __osize);
904*404b540aSrobert       int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
905*404b540aSrobert       if (!__r)
906*404b540aSrobert 	__r = __n - __osize;
907*404b540aSrobert       return __r;
908*404b540aSrobert     }
909*404b540aSrobert 
910*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
911*404b540aSrobert     int
912*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
compare(size_type __pos1,size_type __n1,const basic_string & __str,size_type __pos2,size_type __n2) const913*404b540aSrobert     compare(size_type __pos1, size_type __n1, const basic_string& __str,
914*404b540aSrobert 	    size_type __pos2, size_type __n2) const
915*404b540aSrobert     {
916*404b540aSrobert       _M_check(__pos1, "basic_string::compare");
917*404b540aSrobert       __str._M_check(__pos2, "basic_string::compare");
918*404b540aSrobert       __n1 = _M_limit(__pos1, __n1);
919*404b540aSrobert       __n2 = __str._M_limit(__pos2, __n2);
920*404b540aSrobert       const size_type __len = std::min(__n1, __n2);
921*404b540aSrobert       int __r = traits_type::compare(_M_data() + __pos1,
922*404b540aSrobert 				     __str.data() + __pos2, __len);
923*404b540aSrobert       if (!__r)
924*404b540aSrobert 	__r = __n1 - __n2;
925*404b540aSrobert       return __r;
926*404b540aSrobert     }
927*404b540aSrobert 
928*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
929*404b540aSrobert     int
930*404b540aSrobert     basic_string<_CharT, _Traits, _Alloc>::
compare(const _CharT * __s) const931*404b540aSrobert     compare(const _CharT* __s) const
932*404b540aSrobert     {
933*404b540aSrobert       __glibcxx_requires_string(__s);
934*404b540aSrobert       const size_type __size = this->size();
935*404b540aSrobert       const size_type __osize = traits_type::length(__s);
936*404b540aSrobert       const size_type __len = std::min(__size, __osize);
937*404b540aSrobert       int __r = traits_type::compare(_M_data(), __s, __len);
938*404b540aSrobert       if (!__r)
939*404b540aSrobert 	__r = __size - __osize;
940*404b540aSrobert       return __r;
941*404b540aSrobert     }
942*404b540aSrobert 
943*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
944*404b540aSrobert     int
945*404b540aSrobert     basic_string <_CharT, _Traits, _Alloc>::
compare(size_type __pos,size_type __n1,const _CharT * __s) const946*404b540aSrobert     compare(size_type __pos, size_type __n1, const _CharT* __s) const
947*404b540aSrobert     {
948*404b540aSrobert       __glibcxx_requires_string(__s);
949*404b540aSrobert       _M_check(__pos, "basic_string::compare");
950*404b540aSrobert       __n1 = _M_limit(__pos, __n1);
951*404b540aSrobert       const size_type __osize = traits_type::length(__s);
952*404b540aSrobert       const size_type __len = std::min(__n1, __osize);
953*404b540aSrobert       int __r = traits_type::compare(_M_data() + __pos, __s, __len);
954*404b540aSrobert       if (!__r)
955*404b540aSrobert 	__r = __n1 - __osize;
956*404b540aSrobert       return __r;
957*404b540aSrobert     }
958*404b540aSrobert 
959*404b540aSrobert   template<typename _CharT, typename _Traits, typename _Alloc>
960*404b540aSrobert     int
961*404b540aSrobert     basic_string <_CharT, _Traits, _Alloc>::
compare(size_type __pos,size_type __n1,const _CharT * __s,size_type __n2) const962*404b540aSrobert     compare(size_type __pos, size_type __n1, const _CharT* __s,
963*404b540aSrobert 	    size_type __n2) const
964*404b540aSrobert     {
965*404b540aSrobert       __glibcxx_requires_string_len(__s, __n2);
966*404b540aSrobert       _M_check(__pos, "basic_string::compare");
967*404b540aSrobert       __n1 = _M_limit(__pos, __n1);
968*404b540aSrobert       const size_type __len = std::min(__n1, __n2);
969*404b540aSrobert       int __r = traits_type::compare(_M_data() + __pos, __s, __len);
970*404b540aSrobert       if (!__r)
971*404b540aSrobert 	__r = __n1 - __n2;
972*404b540aSrobert       return __r;
973*404b540aSrobert     }
974*404b540aSrobert 
975*404b540aSrobert   // Inhibit implicit instantiations for required instantiations,
976*404b540aSrobert   // which are defined via explicit instantiations elsewhere.
977*404b540aSrobert   // NB: This syntax is a GNU extension.
978*404b540aSrobert #if _GLIBCXX_EXTERN_TEMPLATE
979*404b540aSrobert   extern template class basic_string<char>;
980*404b540aSrobert   extern template
981*404b540aSrobert     basic_istream<char>&
982*404b540aSrobert     operator>>(basic_istream<char>&, string&);
983*404b540aSrobert   extern template
984*404b540aSrobert     basic_ostream<char>&
985*404b540aSrobert     operator<<(basic_ostream<char>&, const string&);
986*404b540aSrobert   extern template
987*404b540aSrobert     basic_istream<char>&
988*404b540aSrobert     getline(basic_istream<char>&, string&, char);
989*404b540aSrobert   extern template
990*404b540aSrobert     basic_istream<char>&
991*404b540aSrobert     getline(basic_istream<char>&, string&);
992*404b540aSrobert 
993*404b540aSrobert #ifdef _GLIBCXX_USE_WCHAR_T
994*404b540aSrobert   extern template class basic_string<wchar_t>;
995*404b540aSrobert   extern template
996*404b540aSrobert     basic_istream<wchar_t>&
997*404b540aSrobert     operator>>(basic_istream<wchar_t>&, wstring&);
998*404b540aSrobert   extern template
999*404b540aSrobert     basic_ostream<wchar_t>&
1000*404b540aSrobert     operator<<(basic_ostream<wchar_t>&, const wstring&);
1001*404b540aSrobert   extern template
1002*404b540aSrobert     basic_istream<wchar_t>&
1003*404b540aSrobert     getline(basic_istream<wchar_t>&, wstring&, wchar_t);
1004*404b540aSrobert   extern template
1005*404b540aSrobert     basic_istream<wchar_t>&
1006*404b540aSrobert     getline(basic_istream<wchar_t>&, wstring&);
1007*404b540aSrobert #endif
1008*404b540aSrobert #endif
1009*404b540aSrobert 
1010*404b540aSrobert _GLIBCXX_END_NAMESPACE
1011*404b540aSrobert 
1012*404b540aSrobert #endif
1013