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