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