basic_string.h (basic_string<>:: basic_string(basic_string&&), [...]): Add.
[gcc.git] / libstdc++-v3 / include / bits / basic_string.h
1 // Components for manipulating sequences of characters -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
21
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
26
27 /** @file basic_string.h
28 * This is an internal header file, included by other library headers.
29 * You should not attempt to use it directly.
30 */
31
32 //
33 // ISO C++ 14882: 21 Strings library
34 //
35
36 #ifndef _BASIC_STRING_H
37 #define _BASIC_STRING_H 1
38
39 #pragma GCC system_header
40
41 #include <ext/atomicity.h>
42 #include <debug/debug.h>
43 #include <initializer_list>
44
45 _GLIBCXX_BEGIN_NAMESPACE(std)
46
47 /**
48 * @class basic_string basic_string.h <string>
49 * @brief Managing sequences of characters and character-like objects.
50 *
51 * @ingroup sequences
52 *
53 * Meets the requirements of a <a href="tables.html#65">container</a>, a
54 * <a href="tables.html#66">reversible container</a>, and a
55 * <a href="tables.html#67">sequence</a>. Of the
56 * <a href="tables.html#68">optional sequence requirements</a>, only
57 * @c push_back, @c at, and array access are supported.
58 *
59 * @doctodo
60 *
61 *
62 * Documentation? What's that?
63 * Nathan Myers <ncm@cantrip.org>.
64 *
65 * A string looks like this:
66 *
67 * @code
68 * [_Rep]
69 * _M_length
70 * [basic_string<char_type>] _M_capacity
71 * _M_dataplus _M_refcount
72 * _M_p ----------------> unnamed array of char_type
73 * @endcode
74 *
75 * Where the _M_p points to the first character in the string, and
76 * you cast it to a pointer-to-_Rep and subtract 1 to get a
77 * pointer to the header.
78 *
79 * This approach has the enormous advantage that a string object
80 * requires only one allocation. All the ugliness is confined
81 * within a single pair of inline functions, which each compile to
82 * a single "add" instruction: _Rep::_M_data(), and
83 * string::_M_rep(); and the allocation function which gets a
84 * block of raw bytes and with room enough and constructs a _Rep
85 * object at the front.
86 *
87 * The reason you want _M_data pointing to the character array and
88 * not the _Rep is so that the debugger can see the string
89 * contents. (Probably we should add a non-inline member to get
90 * the _Rep for the debugger to use, so users can check the actual
91 * string length.)
92 *
93 * Note that the _Rep object is a POD so that you can have a
94 * static "empty string" _Rep object already "constructed" before
95 * static constructors have run. The reference-count encoding is
96 * chosen so that a 0 indicates one reference, so you never try to
97 * destroy the empty-string _Rep object.
98 *
99 * All but the last paragraph is considered pretty conventional
100 * for a C++ string implementation.
101 */
102 // 21.3 Template class basic_string
103 template<typename _CharT, typename _Traits, typename _Alloc>
104 class basic_string
105 {
106 typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
107
108 // Types:
109 public:
110 typedef _Traits traits_type;
111 typedef typename _Traits::char_type value_type;
112 typedef _Alloc allocator_type;
113 typedef typename _CharT_alloc_type::size_type size_type;
114 typedef typename _CharT_alloc_type::difference_type difference_type;
115 typedef typename _CharT_alloc_type::reference reference;
116 typedef typename _CharT_alloc_type::const_reference const_reference;
117 typedef typename _CharT_alloc_type::pointer pointer;
118 typedef typename _CharT_alloc_type::const_pointer const_pointer;
119 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
120 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
121 const_iterator;
122 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
123 typedef std::reverse_iterator<iterator> reverse_iterator;
124
125 private:
126 // _Rep: string representation
127 // Invariants:
128 // 1. String really contains _M_length + 1 characters: due to 21.3.4
129 // must be kept null-terminated.
130 // 2. _M_capacity >= _M_length
131 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
132 // 3. _M_refcount has three states:
133 // -1: leaked, one reference, no ref-copies allowed, non-const.
134 // 0: one reference, non-const.
135 // n>0: n + 1 references, operations require a lock, const.
136 // 4. All fields==0 is an empty string, given the extra storage
137 // beyond-the-end for a null terminator; thus, the shared
138 // empty string representation needs no constructor.
139
140 struct _Rep_base
141 {
142 size_type _M_length;
143 size_type _M_capacity;
144 _Atomic_word _M_refcount;
145 };
146
147 struct _Rep : _Rep_base
148 {
149 // Types:
150 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
151
152 // (Public) Data members:
153
154 // The maximum number of individual char_type elements of an
155 // individual string is determined by _S_max_size. This is the
156 // value that will be returned by max_size(). (Whereas npos
157 // is the maximum number of bytes the allocator can allocate.)
158 // If one was to divvy up the theoretical largest size string,
159 // with a terminating character and m _CharT elements, it'd
160 // look like this:
161 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
162 // Solving for m:
163 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
164 // In addition, this implementation quarters this amount.
165 static const size_type _S_max_size;
166 static const _CharT _S_terminal;
167
168 // The following storage is init'd to 0 by the linker, resulting
169 // (carefully) in an empty string with one reference.
170 static size_type _S_empty_rep_storage[];
171
172 static _Rep&
173 _S_empty_rep()
174 {
175 // NB: Mild hack to avoid strict-aliasing warnings. Note that
176 // _S_empty_rep_storage is never modified and the punning should
177 // be reasonably safe in this case.
178 void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
179 return *reinterpret_cast<_Rep*>(__p);
180 }
181
182 bool
183 _M_is_leaked() const
184 { return this->_M_refcount < 0; }
185
186 bool
187 _M_is_shared() const
188 { return this->_M_refcount > 0; }
189
190 void
191 _M_set_leaked()
192 { this->_M_refcount = -1; }
193
194 void
195 _M_set_sharable()
196 { this->_M_refcount = 0; }
197
198 void
199 _M_set_length_and_sharable(size_type __n)
200 {
201 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
202 if (__builtin_expect(this != &_S_empty_rep(), false))
203 #endif
204 {
205 this->_M_set_sharable(); // One reference.
206 this->_M_length = __n;
207 traits_type::assign(this->_M_refdata()[__n], _S_terminal);
208 // grrr. (per 21.3.4)
209 // You cannot leave those LWG people alone for a second.
210 }
211 }
212
213 _CharT*
214 _M_refdata() throw()
215 { return reinterpret_cast<_CharT*>(this + 1); }
216
217 _CharT*
218 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
219 {
220 return (!_M_is_leaked() && __alloc1 == __alloc2)
221 ? _M_refcopy() : _M_clone(__alloc1);
222 }
223
224 // Create & Destroy
225 static _Rep*
226 _S_create(size_type, size_type, const _Alloc&);
227
228 void
229 _M_dispose(const _Alloc& __a)
230 {
231 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
232 if (__builtin_expect(this != &_S_empty_rep(), false))
233 #endif
234 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
235 -1) <= 0)
236 _M_destroy(__a);
237 } // XXX MT
238
239 void
240 _M_destroy(const _Alloc&) throw();
241
242 _CharT*
243 _M_refcopy() throw()
244 {
245 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
246 if (__builtin_expect(this != &_S_empty_rep(), false))
247 #endif
248 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
249 return _M_refdata();
250 } // XXX MT
251
252 _CharT*
253 _M_clone(const _Alloc&, size_type __res = 0);
254 };
255
256 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
257 struct _Alloc_hider : _Alloc
258 {
259 _Alloc_hider(_CharT* __dat, const _Alloc& __a)
260 : _Alloc(__a), _M_p(__dat) { }
261
262 _CharT* _M_p; // The actual data.
263 };
264
265 public:
266 // Data Members (public):
267 // NB: This is an unsigned type, and thus represents the maximum
268 // size that the allocator can hold.
269 /// Value returned by various member functions when they fail.
270 static const size_type npos = static_cast<size_type>(-1);
271
272 private:
273 // Data Members (private):
274 mutable _Alloc_hider _M_dataplus;
275
276 _CharT*
277 _M_data() const
278 { return _M_dataplus._M_p; }
279
280 _CharT*
281 _M_data(_CharT* __p)
282 { return (_M_dataplus._M_p = __p); }
283
284 _Rep*
285 _M_rep() const
286 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
287
288 // For the internal use we have functions similar to `begin'/`end'
289 // but they do not call _M_leak.
290 iterator
291 _M_ibegin() const
292 { return iterator(_M_data()); }
293
294 iterator
295 _M_iend() const
296 { return iterator(_M_data() + this->size()); }
297
298 void
299 _M_leak() // for use in begin() & non-const op[]
300 {
301 if (!_M_rep()->_M_is_leaked())
302 _M_leak_hard();
303 }
304
305 size_type
306 _M_check(size_type __pos, const char* __s) const
307 {
308 if (__pos > this->size())
309 __throw_out_of_range(__N(__s));
310 return __pos;
311 }
312
313 void
314 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
315 {
316 if (this->max_size() - (this->size() - __n1) < __n2)
317 __throw_length_error(__N(__s));
318 }
319
320 // NB: _M_limit doesn't check for a bad __pos value.
321 size_type
322 _M_limit(size_type __pos, size_type __off) const
323 {
324 const bool __testoff = __off < this->size() - __pos;
325 return __testoff ? __off : this->size() - __pos;
326 }
327
328 // True if _Rep and source do not overlap.
329 bool
330 _M_disjunct(const _CharT* __s) const
331 {
332 return (less<const _CharT*>()(__s, _M_data())
333 || less<const _CharT*>()(_M_data() + this->size(), __s));
334 }
335
336 // When __n = 1 way faster than the general multichar
337 // traits_type::copy/move/assign.
338 static void
339 _M_copy(_CharT* __d, const _CharT* __s, size_type __n)
340 {
341 if (__n == 1)
342 traits_type::assign(*__d, *__s);
343 else
344 traits_type::copy(__d, __s, __n);
345 }
346
347 static void
348 _M_move(_CharT* __d, const _CharT* __s, size_type __n)
349 {
350 if (__n == 1)
351 traits_type::assign(*__d, *__s);
352 else
353 traits_type::move(__d, __s, __n);
354 }
355
356 static void
357 _M_assign(_CharT* __d, size_type __n, _CharT __c)
358 {
359 if (__n == 1)
360 traits_type::assign(*__d, __c);
361 else
362 traits_type::assign(__d, __n, __c);
363 }
364
365 // _S_copy_chars is a separate template to permit specialization
366 // to optimize for the common case of pointers as iterators.
367 template<class _Iterator>
368 static void
369 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
370 {
371 for (; __k1 != __k2; ++__k1, ++__p)
372 traits_type::assign(*__p, *__k1); // These types are off.
373 }
374
375 static void
376 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
377 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
378
379 static void
380 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
381 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
382
383 static void
384 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
385 { _M_copy(__p, __k1, __k2 - __k1); }
386
387 static void
388 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
389 { _M_copy(__p, __k1, __k2 - __k1); }
390
391 static int
392 _S_compare(size_type __n1, size_type __n2)
393 {
394 const difference_type __d = difference_type(__n1 - __n2);
395
396 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
397 return __gnu_cxx::__numeric_traits<int>::__max;
398 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
399 return __gnu_cxx::__numeric_traits<int>::__min;
400 else
401 return int(__d);
402 }
403
404 void
405 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
406
407 void
408 _M_leak_hard();
409
410 static _Rep&
411 _S_empty_rep()
412 { return _Rep::_S_empty_rep(); }
413
414 public:
415 // Construct/copy/destroy:
416 // NB: We overload ctors in some cases instead of using default
417 // arguments, per 17.4.4.4 para. 2 item 2.
418
419 /**
420 * @brief Default constructor creates an empty string.
421 */
422 basic_string()
423 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
424 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
425 #else
426 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
427 #endif
428
429 /**
430 * @brief Construct an empty string using allocator @a a.
431 */
432 explicit
433 basic_string(const _Alloc& __a);
434
435 // NB: per LWG issue 42, semantics different from IS:
436 /**
437 * @brief Construct string with copy of value of @a str.
438 * @param str Source string.
439 */
440 basic_string(const basic_string& __str);
441 /**
442 * @brief Construct string as copy of a substring.
443 * @param str Source string.
444 * @param pos Index of first character to copy from.
445 * @param n Number of characters to copy (default remainder).
446 */
447 basic_string(const basic_string& __str, size_type __pos,
448 size_type __n = npos);
449 /**
450 * @brief Construct string as copy of a substring.
451 * @param str Source string.
452 * @param pos Index of first character to copy from.
453 * @param n Number of characters to copy.
454 * @param a Allocator to use.
455 */
456 basic_string(const basic_string& __str, size_type __pos,
457 size_type __n, const _Alloc& __a);
458
459 /**
460 * @brief Construct string initialized by a character array.
461 * @param s Source character array.
462 * @param n Number of characters to copy.
463 * @param a Allocator to use (default is default allocator).
464 *
465 * NB: @a s must have at least @a n characters, '\\0' has no special
466 * meaning.
467 */
468 basic_string(const _CharT* __s, size_type __n,
469 const _Alloc& __a = _Alloc());
470 /**
471 * @brief Construct string as copy of a C string.
472 * @param s Source C string.
473 * @param a Allocator to use (default is default allocator).
474 */
475 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
476 /**
477 * @brief Construct string as multiple characters.
478 * @param n Number of characters.
479 * @param c Character to use.
480 * @param a Allocator to use (default is default allocator).
481 */
482 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
483
484 #ifdef __GXX_EXPERIMENTAL_CXX0X__
485 /**
486 * @brief Move construct string.
487 * @param str Source string.
488 *
489 * The newly-created string contains the exact contents of @a str.
490 * @a str is a valid, but unspecified string.
491 **/
492 basic_string(basic_string&& __str)
493 : _M_dataplus(__str._M_dataplus)
494 {
495 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
496 __str._M_data(_S_empty_rep()._M_refdata());
497 #else
498 __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
499 #endif
500 }
501
502 /**
503 * @brief Construct string from an initializer list.
504 * @param l std::initializer_list of characters.
505 * @param a Allocator to use (default is default allocator).
506 */
507 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
508 #endif // __GXX_EXPERIMENTAL_CXX0X__
509
510 /**
511 * @brief Construct string as copy of a range.
512 * @param beg Start of range.
513 * @param end End of range.
514 * @param a Allocator to use (default is default allocator).
515 */
516 template<class _InputIterator>
517 basic_string(_InputIterator __beg, _InputIterator __end,
518 const _Alloc& __a = _Alloc());
519
520 /**
521 * @brief Destroy the string instance.
522 */
523 ~basic_string()
524 { _M_rep()->_M_dispose(this->get_allocator()); }
525
526 /**
527 * @brief Assign the value of @a str to this string.
528 * @param str Source string.
529 */
530 basic_string&
531 operator=(const basic_string& __str)
532 { return this->assign(__str); }
533
534 /**
535 * @brief Copy contents of @a s into this string.
536 * @param s Source null-terminated string.
537 */
538 basic_string&
539 operator=(const _CharT* __s)
540 { return this->assign(__s); }
541
542 /**
543 * @brief Set value to string of length 1.
544 * @param c Source character.
545 *
546 * Assigning to a character makes this string length 1 and
547 * (*this)[0] == @a c.
548 */
549 basic_string&
550 operator=(_CharT __c)
551 {
552 this->assign(1, __c);
553 return *this;
554 }
555
556 #ifdef __GXX_EXPERIMENTAL_CXX0X__
557 /**
558 * @brief Move assign the value of @a str to this string.
559 * @param str Source string.
560 *
561 * The contents of @a str are moved into this string (without copying).
562 * @a str is a valid, but unspecified string.
563 **/
564 basic_string&
565 operator=(basic_string&& __str)
566 {
567 // NB: DR 1204.
568 this->swap(__str);
569 return *this;
570 }
571
572 /**
573 * @brief Set value to string constructed from initializer list.
574 * @param l std::initializer_list.
575 */
576 basic_string&
577 operator=(initializer_list<_CharT> __l)
578 {
579 this->assign(__l.begin(), __l.size());
580 return *this;
581 }
582 #endif // __GXX_EXPERIMENTAL_CXX0X__
583
584 // Iterators:
585 /**
586 * Returns a read/write iterator that points to the first character in
587 * the %string. Unshares the string.
588 */
589 iterator
590 begin()
591 {
592 _M_leak();
593 return iterator(_M_data());
594 }
595
596 /**
597 * Returns a read-only (constant) iterator that points to the first
598 * character in the %string.
599 */
600 const_iterator
601 begin() const
602 { return const_iterator(_M_data()); }
603
604 /**
605 * Returns a read/write iterator that points one past the last
606 * character in the %string. Unshares the string.
607 */
608 iterator
609 end()
610 {
611 _M_leak();
612 return iterator(_M_data() + this->size());
613 }
614
615 /**
616 * Returns a read-only (constant) iterator that points one past the
617 * last character in the %string.
618 */
619 const_iterator
620 end() const
621 { return const_iterator(_M_data() + this->size()); }
622
623 /**
624 * Returns a read/write reverse iterator that points to the last
625 * character in the %string. Iteration is done in reverse element
626 * order. Unshares the string.
627 */
628 reverse_iterator
629 rbegin()
630 { return reverse_iterator(this->end()); }
631
632 /**
633 * Returns a read-only (constant) reverse iterator that points
634 * to the last character in the %string. Iteration is done in
635 * reverse element order.
636 */
637 const_reverse_iterator
638 rbegin() const
639 { return const_reverse_iterator(this->end()); }
640
641 /**
642 * Returns a read/write reverse iterator that points to one before the
643 * first character in the %string. Iteration is done in reverse
644 * element order. Unshares the string.
645 */
646 reverse_iterator
647 rend()
648 { return reverse_iterator(this->begin()); }
649
650 /**
651 * Returns a read-only (constant) reverse iterator that points
652 * to one before the first character in the %string. Iteration
653 * is done in reverse element order.
654 */
655 const_reverse_iterator
656 rend() const
657 { return const_reverse_iterator(this->begin()); }
658
659 #ifdef __GXX_EXPERIMENTAL_CXX0X__
660 /**
661 * Returns a read-only (constant) iterator that points to the first
662 * character in the %string.
663 */
664 const_iterator
665 cbegin() const
666 { return const_iterator(this->_M_data()); }
667
668 /**
669 * Returns a read-only (constant) iterator that points one past the
670 * last character in the %string.
671 */
672 const_iterator
673 cend() const
674 { return const_iterator(this->_M_data() + this->size()); }
675
676 /**
677 * Returns a read-only (constant) reverse iterator that points
678 * to the last character in the %string. Iteration is done in
679 * reverse element order.
680 */
681 const_reverse_iterator
682 crbegin() const
683 { return const_reverse_iterator(this->end()); }
684
685 /**
686 * Returns a read-only (constant) reverse iterator that points
687 * to one before the first character in the %string. Iteration
688 * is done in reverse element order.
689 */
690 const_reverse_iterator
691 crend() const
692 { return const_reverse_iterator(this->begin()); }
693 #endif
694
695 public:
696 // Capacity:
697 /// Returns the number of characters in the string, not including any
698 /// null-termination.
699 size_type
700 size() const
701 { return _M_rep()->_M_length; }
702
703 /// Returns the number of characters in the string, not including any
704 /// null-termination.
705 size_type
706 length() const
707 { return _M_rep()->_M_length; }
708
709 /// Returns the size() of the largest possible %string.
710 size_type
711 max_size() const
712 { return _Rep::_S_max_size; }
713
714 /**
715 * @brief Resizes the %string to the specified number of characters.
716 * @param n Number of characters the %string should contain.
717 * @param c Character to fill any new elements.
718 *
719 * This function will %resize the %string to the specified
720 * number of characters. If the number is smaller than the
721 * %string's current size the %string is truncated, otherwise
722 * the %string is extended and new elements are set to @a c.
723 */
724 void
725 resize(size_type __n, _CharT __c);
726
727 /**
728 * @brief Resizes the %string to the specified number of characters.
729 * @param n Number of characters the %string should contain.
730 *
731 * This function will resize the %string to the specified length. If
732 * the new size is smaller than the %string's current size the %string
733 * is truncated, otherwise the %string is extended and new characters
734 * are default-constructed. For basic types such as char, this means
735 * setting them to 0.
736 */
737 void
738 resize(size_type __n)
739 { this->resize(__n, _CharT()); }
740
741 #ifdef __GXX_EXPERIMENTAL_CXX0X__
742 /// A non-binding request to reduce capacity() to size().
743 void
744 shrink_to_fit()
745 {
746 try
747 { reserve(0); }
748 catch(...)
749 { }
750 }
751 #endif
752
753 /**
754 * Returns the total number of characters that the %string can hold
755 * before needing to allocate more memory.
756 */
757 size_type
758 capacity() const
759 { return _M_rep()->_M_capacity; }
760
761 /**
762 * @brief Attempt to preallocate enough memory for specified number of
763 * characters.
764 * @param res_arg Number of characters required.
765 * @throw std::length_error If @a res_arg exceeds @c max_size().
766 *
767 * This function attempts to reserve enough memory for the
768 * %string to hold the specified number of characters. If the
769 * number requested is more than max_size(), length_error is
770 * thrown.
771 *
772 * The advantage of this function is that if optimal code is a
773 * necessity and the user can determine the string length that will be
774 * required, the user can reserve the memory in %advance, and thus
775 * prevent a possible reallocation of memory and copying of %string
776 * data.
777 */
778 void
779 reserve(size_type __res_arg = 0);
780
781 /**
782 * Erases the string, making it empty.
783 */
784 void
785 clear()
786 { _M_mutate(0, this->size(), 0); }
787
788 /**
789 * Returns true if the %string is empty. Equivalent to *this == "".
790 */
791 bool
792 empty() const
793 { return this->size() == 0; }
794
795 // Element access:
796 /**
797 * @brief Subscript access to the data contained in the %string.
798 * @param pos The index of the character to access.
799 * @return Read-only (constant) reference to the character.
800 *
801 * This operator allows for easy, array-style, data access.
802 * Note that data access with this operator is unchecked and
803 * out_of_range lookups are not defined. (For checked lookups
804 * see at().)
805 */
806 const_reference
807 operator[] (size_type __pos) const
808 {
809 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
810 return _M_data()[__pos];
811 }
812
813 /**
814 * @brief Subscript access to the data contained in the %string.
815 * @param pos The index of the character to access.
816 * @return Read/write reference to the character.
817 *
818 * This operator allows for easy, array-style, data access.
819 * Note that data access with this operator is unchecked and
820 * out_of_range lookups are not defined. (For checked lookups
821 * see at().) Unshares the string.
822 */
823 reference
824 operator[](size_type __pos)
825 {
826 // allow pos == size() as v3 extension:
827 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
828 // but be strict in pedantic mode:
829 _GLIBCXX_DEBUG_PEDASSERT(__pos < size());
830 _M_leak();
831 return _M_data()[__pos];
832 }
833
834 /**
835 * @brief Provides access to the data contained in the %string.
836 * @param n The index of the character to access.
837 * @return Read-only (const) reference to the character.
838 * @throw std::out_of_range If @a n is an invalid index.
839 *
840 * This function provides for safer data access. The parameter is
841 * first checked that it is in the range of the string. The function
842 * throws out_of_range if the check fails.
843 */
844 const_reference
845 at(size_type __n) const
846 {
847 if (__n >= this->size())
848 __throw_out_of_range(__N("basic_string::at"));
849 return _M_data()[__n];
850 }
851
852 /**
853 * @brief Provides access to the data contained in the %string.
854 * @param n The index of the character to access.
855 * @return Read/write reference to the character.
856 * @throw std::out_of_range If @a n is an invalid index.
857 *
858 * This function provides for safer data access. The parameter is
859 * first checked that it is in the range of the string. The function
860 * throws out_of_range if the check fails. Success results in
861 * unsharing the string.
862 */
863 reference
864 at(size_type __n)
865 {
866 if (__n >= size())
867 __throw_out_of_range(__N("basic_string::at"));
868 _M_leak();
869 return _M_data()[__n];
870 }
871
872 // Modifiers:
873 /**
874 * @brief Append a string to this string.
875 * @param str The string to append.
876 * @return Reference to this string.
877 */
878 basic_string&
879 operator+=(const basic_string& __str)
880 { return this->append(__str); }
881
882 /**
883 * @brief Append a C string.
884 * @param s The C string to append.
885 * @return Reference to this string.
886 */
887 basic_string&
888 operator+=(const _CharT* __s)
889 { return this->append(__s); }
890
891 /**
892 * @brief Append a character.
893 * @param c The character to append.
894 * @return Reference to this string.
895 */
896 basic_string&
897 operator+=(_CharT __c)
898 {
899 this->push_back(__c);
900 return *this;
901 }
902
903 #ifdef __GXX_EXPERIMENTAL_CXX0X__
904 /**
905 * @brief Append an initializer_list of characters.
906 * @param l The initializer_list of characters to be appended.
907 * @return Reference to this string.
908 */
909 basic_string&
910 operator+=(initializer_list<_CharT> __l)
911 { return this->append(__l.begin(), __l.size()); }
912 #endif // __GXX_EXPERIMENTAL_CXX0X__
913
914 /**
915 * @brief Append a string to this string.
916 * @param str The string to append.
917 * @return Reference to this string.
918 */
919 basic_string&
920 append(const basic_string& __str);
921
922 /**
923 * @brief Append a substring.
924 * @param str The string to append.
925 * @param pos Index of the first character of str to append.
926 * @param n The number of characters to append.
927 * @return Reference to this string.
928 * @throw std::out_of_range if @a pos is not a valid index.
929 *
930 * This function appends @a n characters from @a str starting at @a pos
931 * to this string. If @a n is is larger than the number of available
932 * characters in @a str, the remainder of @a str is appended.
933 */
934 basic_string&
935 append(const basic_string& __str, size_type __pos, size_type __n);
936
937 /**
938 * @brief Append a C substring.
939 * @param s The C string to append.
940 * @param n The number of characters to append.
941 * @return Reference to this string.
942 */
943 basic_string&
944 append(const _CharT* __s, size_type __n);
945
946 /**
947 * @brief Append a C string.
948 * @param s The C string to append.
949 * @return Reference to this string.
950 */
951 basic_string&
952 append(const _CharT* __s)
953 {
954 __glibcxx_requires_string(__s);
955 return this->append(__s, traits_type::length(__s));
956 }
957
958 /**
959 * @brief Append multiple characters.
960 * @param n The number of characters to append.
961 * @param c The character to use.
962 * @return Reference to this string.
963 *
964 * Appends n copies of c to this string.
965 */
966 basic_string&
967 append(size_type __n, _CharT __c);
968
969 #ifdef __GXX_EXPERIMENTAL_CXX0X__
970 /**
971 * @brief Append an initializer_list of characters.
972 * @param l The initializer_list of characters to append.
973 * @return Reference to this string.
974 */
975 basic_string&
976 append(initializer_list<_CharT> __l)
977 { return this->append(__l.begin(), __l.size()); }
978 #endif // __GXX_EXPERIMENTAL_CXX0X__
979
980 /**
981 * @brief Append a range of characters.
982 * @param first Iterator referencing the first character to append.
983 * @param last Iterator marking the end of the range.
984 * @return Reference to this string.
985 *
986 * Appends characters in the range [first,last) to this string.
987 */
988 template<class _InputIterator>
989 basic_string&
990 append(_InputIterator __first, _InputIterator __last)
991 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
992
993 /**
994 * @brief Append a single character.
995 * @param c Character to append.
996 */
997 void
998 push_back(_CharT __c)
999 {
1000 const size_type __len = 1 + this->size();
1001 if (__len > this->capacity() || _M_rep()->_M_is_shared())
1002 this->reserve(__len);
1003 traits_type::assign(_M_data()[this->size()], __c);
1004 _M_rep()->_M_set_length_and_sharable(__len);
1005 }
1006
1007 /**
1008 * @brief Set value to contents of another string.
1009 * @param str Source string to use.
1010 * @return Reference to this string.
1011 */
1012 basic_string&
1013 assign(const basic_string& __str);
1014
1015 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1016 /**
1017 * @brief Set value to contents of another string.
1018 * @param str Source string to use.
1019 * @return Reference to this string.
1020 *
1021 * This function sets this string to the exact contents of @a str.
1022 * @a str is a valid, but unspecified string.
1023 */
1024 basic_string&
1025 assign(basic_string&& __str)
1026 {
1027 this->swap(__str);
1028 return *this;
1029 }
1030 #endif // __GXX_EXPERIMENTAL_CXX0X__
1031
1032 /**
1033 * @brief Set value to a substring of a string.
1034 * @param str The string to use.
1035 * @param pos Index of the first character of str.
1036 * @param n Number of characters to use.
1037 * @return Reference to this string.
1038 * @throw std::out_of_range if @a pos is not a valid index.
1039 *
1040 * This function sets this string to the substring of @a str consisting
1041 * of @a n characters at @a pos. If @a n is is larger than the number
1042 * of available characters in @a str, the remainder of @a str is used.
1043 */
1044 basic_string&
1045 assign(const basic_string& __str, size_type __pos, size_type __n)
1046 { return this->assign(__str._M_data()
1047 + __str._M_check(__pos, "basic_string::assign"),
1048 __str._M_limit(__pos, __n)); }
1049
1050 /**
1051 * @brief Set value to a C substring.
1052 * @param s The C string to use.
1053 * @param n Number of characters to use.
1054 * @return Reference to this string.
1055 *
1056 * This function sets the value of this string to the first @a n
1057 * characters of @a s. If @a n is is larger than the number of
1058 * available characters in @a s, the remainder of @a s is used.
1059 */
1060 basic_string&
1061 assign(const _CharT* __s, size_type __n);
1062
1063 /**
1064 * @brief Set value to contents of a C string.
1065 * @param s The C string to use.
1066 * @return Reference to this string.
1067 *
1068 * This function sets the value of this string to the value of @a s.
1069 * The data is copied, so there is no dependence on @a s once the
1070 * function returns.
1071 */
1072 basic_string&
1073 assign(const _CharT* __s)
1074 {
1075 __glibcxx_requires_string(__s);
1076 return this->assign(__s, traits_type::length(__s));
1077 }
1078
1079 /**
1080 * @brief Set value to multiple characters.
1081 * @param n Length of the resulting string.
1082 * @param c The character to use.
1083 * @return Reference to this string.
1084 *
1085 * This function sets the value of this string to @a n copies of
1086 * character @a c.
1087 */
1088 basic_string&
1089 assign(size_type __n, _CharT __c)
1090 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1091
1092 /**
1093 * @brief Set value to a range of characters.
1094 * @param first Iterator referencing the first character to append.
1095 * @param last Iterator marking the end of the range.
1096 * @return Reference to this string.
1097 *
1098 * Sets value of string to characters in the range [first,last).
1099 */
1100 template<class _InputIterator>
1101 basic_string&
1102 assign(_InputIterator __first, _InputIterator __last)
1103 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
1104
1105 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1106 /**
1107 * @brief Set value to an initializer_list of characters.
1108 * @param l The initializer_list of characters to assign.
1109 * @return Reference to this string.
1110 */
1111 basic_string&
1112 assign(initializer_list<_CharT> __l)
1113 { return this->assign(__l.begin(), __l.size()); }
1114 #endif // __GXX_EXPERIMENTAL_CXX0X__
1115
1116 /**
1117 * @brief Insert multiple characters.
1118 * @param p Iterator referencing location in string to insert at.
1119 * @param n Number of characters to insert
1120 * @param c The character to insert.
1121 * @throw std::length_error If new length exceeds @c max_size().
1122 *
1123 * Inserts @a n copies of character @a c starting at the position
1124 * referenced by iterator @a p. If adding characters causes the length
1125 * to exceed max_size(), length_error is thrown. The value of the
1126 * string doesn't change if an error is thrown.
1127 */
1128 void
1129 insert(iterator __p, size_type __n, _CharT __c)
1130 { this->replace(__p, __p, __n, __c); }
1131
1132 /**
1133 * @brief Insert a range of characters.
1134 * @param p Iterator referencing location in string to insert at.
1135 * @param beg Start of range.
1136 * @param end End of range.
1137 * @throw std::length_error If new length exceeds @c max_size().
1138 *
1139 * Inserts characters in range [beg,end). If adding characters causes
1140 * the length to exceed max_size(), length_error is thrown. The value
1141 * of the string doesn't change if an error is thrown.
1142 */
1143 template<class _InputIterator>
1144 void
1145 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1146 { this->replace(__p, __p, __beg, __end); }
1147
1148 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1149 /**
1150 * @brief Insert an initializer_list of characters.
1151 * @param p Iterator referencing location in string to insert at.
1152 * @param l The initializer_list of characters to insert.
1153 * @throw std::length_error If new length exceeds @c max_size().
1154 */
1155 void
1156 insert(iterator __p, initializer_list<_CharT> __l)
1157 {
1158 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1159 this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
1160 }
1161 #endif // __GXX_EXPERIMENTAL_CXX0X__
1162
1163 /**
1164 * @brief Insert value of a string.
1165 * @param pos1 Iterator referencing location in string to insert at.
1166 * @param str The string to insert.
1167 * @return Reference to this string.
1168 * @throw std::length_error If new length exceeds @c max_size().
1169 *
1170 * Inserts value of @a str starting at @a pos1. If adding characters
1171 * causes the length to exceed max_size(), length_error is thrown. The
1172 * value of the string doesn't change if an error is thrown.
1173 */
1174 basic_string&
1175 insert(size_type __pos1, const basic_string& __str)
1176 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1177
1178 /**
1179 * @brief Insert a substring.
1180 * @param pos1 Iterator referencing location in string to insert at.
1181 * @param str The string to insert.
1182 * @param pos2 Start of characters in str to insert.
1183 * @param n Number of characters to insert.
1184 * @return Reference to this string.
1185 * @throw std::length_error If new length exceeds @c max_size().
1186 * @throw std::out_of_range If @a pos1 > size() or
1187 * @a pos2 > @a str.size().
1188 *
1189 * Starting at @a pos1, insert @a n character of @a str beginning with
1190 * @a pos2. If adding characters causes the length to exceed
1191 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
1192 * this string or @a pos2 is beyond the end of @a str, out_of_range is
1193 * thrown. The value of the string doesn't change if an error is
1194 * thrown.
1195 */
1196 basic_string&
1197 insert(size_type __pos1, const basic_string& __str,
1198 size_type __pos2, size_type __n)
1199 { return this->insert(__pos1, __str._M_data()
1200 + __str._M_check(__pos2, "basic_string::insert"),
1201 __str._M_limit(__pos2, __n)); }
1202
1203 /**
1204 * @brief Insert a C substring.
1205 * @param pos Iterator referencing location in string to insert at.
1206 * @param s The C string to insert.
1207 * @param n The number of characters to insert.
1208 * @return Reference to this string.
1209 * @throw std::length_error If new length exceeds @c max_size().
1210 * @throw std::out_of_range If @a pos is beyond the end of this
1211 * string.
1212 *
1213 * Inserts the first @a n characters of @a s starting at @a pos. If
1214 * adding characters causes the length to exceed max_size(),
1215 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1216 * thrown. The value of the string doesn't change if an error is
1217 * thrown.
1218 */
1219 basic_string&
1220 insert(size_type __pos, const _CharT* __s, size_type __n);
1221
1222 /**
1223 * @brief Insert a C string.
1224 * @param pos Iterator referencing location in string to insert at.
1225 * @param s The C string to insert.
1226 * @return Reference to this string.
1227 * @throw std::length_error If new length exceeds @c max_size().
1228 * @throw std::out_of_range If @a pos is beyond the end of this
1229 * string.
1230 *
1231 * Inserts the first @a n characters of @a s starting at @a pos. If
1232 * adding characters causes the length to exceed max_size(),
1233 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1234 * thrown. The value of the string doesn't change if an error is
1235 * thrown.
1236 */
1237 basic_string&
1238 insert(size_type __pos, const _CharT* __s)
1239 {
1240 __glibcxx_requires_string(__s);
1241 return this->insert(__pos, __s, traits_type::length(__s));
1242 }
1243
1244 /**
1245 * @brief Insert multiple characters.
1246 * @param pos Index in string to insert at.
1247 * @param n Number of characters to insert
1248 * @param c The character to insert.
1249 * @return Reference to this string.
1250 * @throw std::length_error If new length exceeds @c max_size().
1251 * @throw std::out_of_range If @a pos is beyond the end of this
1252 * string.
1253 *
1254 * Inserts @a n copies of character @a c starting at index @a pos. If
1255 * adding characters causes the length to exceed max_size(),
1256 * length_error is thrown. If @a pos > length(), out_of_range is
1257 * thrown. The value of the string doesn't change if an error is
1258 * thrown.
1259 */
1260 basic_string&
1261 insert(size_type __pos, size_type __n, _CharT __c)
1262 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1263 size_type(0), __n, __c); }
1264
1265 /**
1266 * @brief Insert one character.
1267 * @param p Iterator referencing position in string to insert at.
1268 * @param c The character to insert.
1269 * @return Iterator referencing newly inserted char.
1270 * @throw std::length_error If new length exceeds @c max_size().
1271 *
1272 * Inserts character @a c at position referenced by @a p. If adding
1273 * character causes the length to exceed max_size(), length_error is
1274 * thrown. If @a p is beyond end of string, out_of_range is thrown.
1275 * The value of the string doesn't change if an error is thrown.
1276 */
1277 iterator
1278 insert(iterator __p, _CharT __c)
1279 {
1280 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1281 const size_type __pos = __p - _M_ibegin();
1282 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1283 _M_rep()->_M_set_leaked();
1284 return iterator(_M_data() + __pos);
1285 }
1286
1287 /**
1288 * @brief Remove characters.
1289 * @param pos Index of first character to remove (default 0).
1290 * @param n Number of characters to remove (default remainder).
1291 * @return Reference to this string.
1292 * @throw std::out_of_range If @a pos is beyond the end of this
1293 * string.
1294 *
1295 * Removes @a n characters from this string starting at @a pos. The
1296 * length of the string is reduced by @a n. If there are < @a n
1297 * characters to remove, the remainder of the string is truncated. If
1298 * @a p is beyond end of string, out_of_range is thrown. The value of
1299 * the string doesn't change if an error is thrown.
1300 */
1301 basic_string&
1302 erase(size_type __pos = 0, size_type __n = npos)
1303 {
1304 _M_mutate(_M_check(__pos, "basic_string::erase"),
1305 _M_limit(__pos, __n), size_type(0));
1306 return *this;
1307 }
1308
1309 /**
1310 * @brief Remove one character.
1311 * @param position Iterator referencing the character to remove.
1312 * @return iterator referencing same location after removal.
1313 *
1314 * Removes the character at @a position from this string. The value
1315 * of the string doesn't change if an error is thrown.
1316 */
1317 iterator
1318 erase(iterator __position)
1319 {
1320 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1321 && __position < _M_iend());
1322 const size_type __pos = __position - _M_ibegin();
1323 _M_mutate(__pos, size_type(1), size_type(0));
1324 _M_rep()->_M_set_leaked();
1325 return iterator(_M_data() + __pos);
1326 }
1327
1328 /**
1329 * @brief Remove a range of characters.
1330 * @param first Iterator referencing the first character to remove.
1331 * @param last Iterator referencing the end of the range.
1332 * @return Iterator referencing location of first after removal.
1333 *
1334 * Removes the characters in the range [first,last) from this string.
1335 * The value of the string doesn't change if an error is thrown.
1336 */
1337 iterator
1338 erase(iterator __first, iterator __last);
1339
1340 /**
1341 * @brief Replace characters with value from another string.
1342 * @param pos Index of first character to replace.
1343 * @param n Number of characters to be replaced.
1344 * @param str String to insert.
1345 * @return Reference to this string.
1346 * @throw std::out_of_range If @a pos is beyond the end of this
1347 * string.
1348 * @throw std::length_error If new length exceeds @c max_size().
1349 *
1350 * Removes the characters in the range [pos,pos+n) from this string.
1351 * In place, the value of @a str is inserted. If @a pos is beyond end
1352 * of string, out_of_range is thrown. If the length of the result
1353 * exceeds max_size(), length_error is thrown. The value of the string
1354 * doesn't change if an error is thrown.
1355 */
1356 basic_string&
1357 replace(size_type __pos, size_type __n, const basic_string& __str)
1358 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1359
1360 /**
1361 * @brief Replace characters with value from another string.
1362 * @param pos1 Index of first character to replace.
1363 * @param n1 Number of characters to be replaced.
1364 * @param str String to insert.
1365 * @param pos2 Index of first character of str to use.
1366 * @param n2 Number of characters from str to use.
1367 * @return Reference to this string.
1368 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1369 * str.size().
1370 * @throw std::length_error If new length exceeds @c max_size().
1371 *
1372 * Removes the characters in the range [pos1,pos1 + n) from this
1373 * string. In place, the value of @a str is inserted. If @a pos is
1374 * beyond end of string, out_of_range is thrown. If the length of the
1375 * result exceeds max_size(), length_error is thrown. The value of the
1376 * string doesn't change if an error is thrown.
1377 */
1378 basic_string&
1379 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1380 size_type __pos2, size_type __n2)
1381 { return this->replace(__pos1, __n1, __str._M_data()
1382 + __str._M_check(__pos2, "basic_string::replace"),
1383 __str._M_limit(__pos2, __n2)); }
1384
1385 /**
1386 * @brief Replace characters with value of a C substring.
1387 * @param pos Index of first character to replace.
1388 * @param n1 Number of characters to be replaced.
1389 * @param s C string to insert.
1390 * @param n2 Number of characters from @a s to use.
1391 * @return Reference to this string.
1392 * @throw std::out_of_range If @a pos1 > size().
1393 * @throw std::length_error If new length exceeds @c max_size().
1394 *
1395 * Removes the characters in the range [pos,pos + n1) from this string.
1396 * In place, the first @a n2 characters of @a s are inserted, or all
1397 * of @a s if @a n2 is too large. If @a pos is beyond end of string,
1398 * out_of_range is thrown. If the length of result exceeds max_size(),
1399 * length_error is thrown. The value of the string doesn't change if
1400 * an error is thrown.
1401 */
1402 basic_string&
1403 replace(size_type __pos, size_type __n1, const _CharT* __s,
1404 size_type __n2);
1405
1406 /**
1407 * @brief Replace characters with value of a C string.
1408 * @param pos Index of first character to replace.
1409 * @param n1 Number of characters to be replaced.
1410 * @param s C string to insert.
1411 * @return Reference to this string.
1412 * @throw std::out_of_range If @a pos > size().
1413 * @throw std::length_error If new length exceeds @c max_size().
1414 *
1415 * Removes the characters in the range [pos,pos + n1) from this string.
1416 * In place, the first @a n characters of @a s are inserted. If @a
1417 * pos is beyond end of string, out_of_range is thrown. If the length
1418 * of result exceeds max_size(), length_error is thrown. The value of
1419 * the string doesn't change if an error is thrown.
1420 */
1421 basic_string&
1422 replace(size_type __pos, size_type __n1, const _CharT* __s)
1423 {
1424 __glibcxx_requires_string(__s);
1425 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1426 }
1427
1428 /**
1429 * @brief Replace characters with multiple characters.
1430 * @param pos Index of first character to replace.
1431 * @param n1 Number of characters to be replaced.
1432 * @param n2 Number of characters to insert.
1433 * @param c Character to insert.
1434 * @return Reference to this string.
1435 * @throw std::out_of_range If @a pos > size().
1436 * @throw std::length_error If new length exceeds @c max_size().
1437 *
1438 * Removes the characters in the range [pos,pos + n1) from this string.
1439 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1440 * end of string, out_of_range is thrown. If the length of result
1441 * exceeds max_size(), length_error is thrown. The value of the string
1442 * doesn't change if an error is thrown.
1443 */
1444 basic_string&
1445 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1446 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1447 _M_limit(__pos, __n1), __n2, __c); }
1448
1449 /**
1450 * @brief Replace range of characters with string.
1451 * @param i1 Iterator referencing start of range to replace.
1452 * @param i2 Iterator referencing end of range to replace.
1453 * @param str String value to insert.
1454 * @return Reference to this string.
1455 * @throw std::length_error If new length exceeds @c max_size().
1456 *
1457 * Removes the characters in the range [i1,i2). In place, the value of
1458 * @a str is inserted. If the length of result exceeds max_size(),
1459 * length_error is thrown. The value of the string doesn't change if
1460 * an error is thrown.
1461 */
1462 basic_string&
1463 replace(iterator __i1, iterator __i2, const basic_string& __str)
1464 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1465
1466 /**
1467 * @brief Replace range of characters with C substring.
1468 * @param i1 Iterator referencing start of range to replace.
1469 * @param i2 Iterator referencing end of range to replace.
1470 * @param s C string value to insert.
1471 * @param n Number of characters from s to insert.
1472 * @return Reference to this string.
1473 * @throw std::length_error If new length exceeds @c max_size().
1474 *
1475 * Removes the characters in the range [i1,i2). In place, the first @a
1476 * n characters of @a s are inserted. If the length of result exceeds
1477 * max_size(), length_error is thrown. The value of the string doesn't
1478 * change if an error is thrown.
1479 */
1480 basic_string&
1481 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1482 {
1483 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1484 && __i2 <= _M_iend());
1485 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1486 }
1487
1488 /**
1489 * @brief Replace range of characters with C string.
1490 * @param i1 Iterator referencing start of range to replace.
1491 * @param i2 Iterator referencing end of range to replace.
1492 * @param s C string value to insert.
1493 * @return Reference to this string.
1494 * @throw std::length_error If new length exceeds @c max_size().
1495 *
1496 * Removes the characters in the range [i1,i2). In place, the
1497 * characters of @a s are inserted. If the length of result exceeds
1498 * max_size(), length_error is thrown. The value of the string doesn't
1499 * change if an error is thrown.
1500 */
1501 basic_string&
1502 replace(iterator __i1, iterator __i2, const _CharT* __s)
1503 {
1504 __glibcxx_requires_string(__s);
1505 return this->replace(__i1, __i2, __s, traits_type::length(__s));
1506 }
1507
1508 /**
1509 * @brief Replace range of characters with multiple characters
1510 * @param i1 Iterator referencing start of range to replace.
1511 * @param i2 Iterator referencing end of range to replace.
1512 * @param n Number of characters to insert.
1513 * @param c Character to insert.
1514 * @return Reference to this string.
1515 * @throw std::length_error If new length exceeds @c max_size().
1516 *
1517 * Removes the characters in the range [i1,i2). In place, @a n copies
1518 * of @a c are inserted. If the length of result exceeds max_size(),
1519 * length_error is thrown. The value of the string doesn't change if
1520 * an error is thrown.
1521 */
1522 basic_string&
1523 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1524 {
1525 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1526 && __i2 <= _M_iend());
1527 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1528 }
1529
1530 /**
1531 * @brief Replace range of characters with range.
1532 * @param i1 Iterator referencing start of range to replace.
1533 * @param i2 Iterator referencing end of range to replace.
1534 * @param k1 Iterator referencing start of range to insert.
1535 * @param k2 Iterator referencing end of range to insert.
1536 * @return Reference to this string.
1537 * @throw std::length_error If new length exceeds @c max_size().
1538 *
1539 * Removes the characters in the range [i1,i2). In place, characters
1540 * in the range [k1,k2) are inserted. If the length of result exceeds
1541 * max_size(), length_error is thrown. The value of the string doesn't
1542 * change if an error is thrown.
1543 */
1544 template<class _InputIterator>
1545 basic_string&
1546 replace(iterator __i1, iterator __i2,
1547 _InputIterator __k1, _InputIterator __k2)
1548 {
1549 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1550 && __i2 <= _M_iend());
1551 __glibcxx_requires_valid_range(__k1, __k2);
1552 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1553 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1554 }
1555
1556 // Specializations for the common case of pointer and iterator:
1557 // useful to avoid the overhead of temporary buffering in _M_replace.
1558 basic_string&
1559 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1560 {
1561 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1562 && __i2 <= _M_iend());
1563 __glibcxx_requires_valid_range(__k1, __k2);
1564 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1565 __k1, __k2 - __k1);
1566 }
1567
1568 basic_string&
1569 replace(iterator __i1, iterator __i2,
1570 const _CharT* __k1, const _CharT* __k2)
1571 {
1572 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1573 && __i2 <= _M_iend());
1574 __glibcxx_requires_valid_range(__k1, __k2);
1575 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1576 __k1, __k2 - __k1);
1577 }
1578
1579 basic_string&
1580 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1581 {
1582 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1583 && __i2 <= _M_iend());
1584 __glibcxx_requires_valid_range(__k1, __k2);
1585 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1586 __k1.base(), __k2 - __k1);
1587 }
1588
1589 basic_string&
1590 replace(iterator __i1, iterator __i2,
1591 const_iterator __k1, const_iterator __k2)
1592 {
1593 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1594 && __i2 <= _M_iend());
1595 __glibcxx_requires_valid_range(__k1, __k2);
1596 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1597 __k1.base(), __k2 - __k1);
1598 }
1599
1600 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1601 /**
1602 * @brief Replace range of characters with initializer_list.
1603 * @param i1 Iterator referencing start of range to replace.
1604 * @param i2 Iterator referencing end of range to replace.
1605 * @param l The initializer_list of characters to insert.
1606 * @return Reference to this string.
1607 * @throw std::length_error If new length exceeds @c max_size().
1608 *
1609 * Removes the characters in the range [i1,i2). In place, characters
1610 * in the range [k1,k2) are inserted. If the length of result exceeds
1611 * max_size(), length_error is thrown. The value of the string doesn't
1612 * change if an error is thrown.
1613 */
1614 basic_string& replace(iterator __i1, iterator __i2,
1615 initializer_list<_CharT> __l)
1616 { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
1617 #endif // __GXX_EXPERIMENTAL_CXX0X__
1618
1619 private:
1620 template<class _Integer>
1621 basic_string&
1622 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1623 _Integer __val, __true_type)
1624 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1625
1626 template<class _InputIterator>
1627 basic_string&
1628 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1629 _InputIterator __k2, __false_type);
1630
1631 basic_string&
1632 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1633 _CharT __c);
1634
1635 basic_string&
1636 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1637 size_type __n2);
1638
1639 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1640 // requires special behaviour if _InIter is an integral type
1641 template<class _InIterator>
1642 static _CharT*
1643 _S_construct_aux(_InIterator __beg, _InIterator __end,
1644 const _Alloc& __a, __false_type)
1645 {
1646 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1647 return _S_construct(__beg, __end, __a, _Tag());
1648 }
1649
1650 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1651 // 438. Ambiguity in the "do the right thing" clause
1652 template<class _Integer>
1653 static _CharT*
1654 _S_construct_aux(_Integer __beg, _Integer __end,
1655 const _Alloc& __a, __true_type)
1656 { return _S_construct_aux_2(static_cast<size_type>(__beg),
1657 __end, __a); }
1658
1659 static _CharT*
1660 _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
1661 { return _S_construct(__req, __c, __a); }
1662
1663 template<class _InIterator>
1664 static _CharT*
1665 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1666 {
1667 typedef typename std::__is_integer<_InIterator>::__type _Integral;
1668 return _S_construct_aux(__beg, __end, __a, _Integral());
1669 }
1670
1671 // For Input Iterators, used in istreambuf_iterators, etc.
1672 template<class _InIterator>
1673 static _CharT*
1674 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1675 input_iterator_tag);
1676
1677 // For forward_iterators up to random_access_iterators, used for
1678 // string::iterator, _CharT*, etc.
1679 template<class _FwdIterator>
1680 static _CharT*
1681 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1682 forward_iterator_tag);
1683
1684 static _CharT*
1685 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1686
1687 public:
1688
1689 /**
1690 * @brief Copy substring into C string.
1691 * @param s C string to copy value into.
1692 * @param n Number of characters to copy.
1693 * @param pos Index of first character to copy.
1694 * @return Number of characters actually copied
1695 * @throw std::out_of_range If pos > size().
1696 *
1697 * Copies up to @a n characters starting at @a pos into the C string @a
1698 * s. If @a pos is greater than size(), out_of_range is thrown.
1699 */
1700 size_type
1701 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1702
1703 /**
1704 * @brief Swap contents with another string.
1705 * @param s String to swap with.
1706 *
1707 * Exchanges the contents of this string with that of @a s in constant
1708 * time.
1709 */
1710 void
1711 swap(basic_string& __s);
1712
1713 // String operations:
1714 /**
1715 * @brief Return const pointer to null-terminated contents.
1716 *
1717 * This is a handle to internal data. Do not modify or dire things may
1718 * happen.
1719 */
1720 const _CharT*
1721 c_str() const
1722 { return _M_data(); }
1723
1724 /**
1725 * @brief Return const pointer to contents.
1726 *
1727 * This is a handle to internal data. Do not modify or dire things may
1728 * happen.
1729 */
1730 const _CharT*
1731 data() const
1732 { return _M_data(); }
1733
1734 /**
1735 * @brief Return copy of allocator used to construct this string.
1736 */
1737 allocator_type
1738 get_allocator() const
1739 { return _M_dataplus; }
1740
1741 /**
1742 * @brief Find position of a C substring.
1743 * @param s C string to locate.
1744 * @param pos Index of character to search from.
1745 * @param n Number of characters from @a s to search for.
1746 * @return Index of start of first occurrence.
1747 *
1748 * Starting from @a pos, searches forward for the first @a n characters
1749 * in @a s within this string. If found, returns the index where it
1750 * begins. If not found, returns npos.
1751 */
1752 size_type
1753 find(const _CharT* __s, size_type __pos, size_type __n) const;
1754
1755 /**
1756 * @brief Find position of a string.
1757 * @param str String to locate.
1758 * @param pos Index of character to search from (default 0).
1759 * @return Index of start of first occurrence.
1760 *
1761 * Starting from @a pos, searches forward for value of @a str within
1762 * this string. If found, returns the index where it begins. If not
1763 * found, returns npos.
1764 */
1765 size_type
1766 find(const basic_string& __str, size_type __pos = 0) const
1767 { return this->find(__str.data(), __pos, __str.size()); }
1768
1769 /**
1770 * @brief Find position of a C string.
1771 * @param s C string to locate.
1772 * @param pos Index of character to search from (default 0).
1773 * @return Index of start of first occurrence.
1774 *
1775 * Starting from @a pos, searches forward for the value of @a s within
1776 * this string. If found, returns the index where it begins. If not
1777 * found, returns npos.
1778 */
1779 size_type
1780 find(const _CharT* __s, size_type __pos = 0) const
1781 {
1782 __glibcxx_requires_string(__s);
1783 return this->find(__s, __pos, traits_type::length(__s));
1784 }
1785
1786 /**
1787 * @brief Find position of a character.
1788 * @param c Character to locate.
1789 * @param pos Index of character to search from (default 0).
1790 * @return Index of first occurrence.
1791 *
1792 * Starting from @a pos, searches forward for @a c within this string.
1793 * If found, returns the index where it was found. If not found,
1794 * returns npos.
1795 */
1796 size_type
1797 find(_CharT __c, size_type __pos = 0) const;
1798
1799 /**
1800 * @brief Find last position of a string.
1801 * @param str String to locate.
1802 * @param pos Index of character to search back from (default end).
1803 * @return Index of start of last occurrence.
1804 *
1805 * Starting from @a pos, searches backward for value of @a str within
1806 * this string. If found, returns the index where it begins. If not
1807 * found, returns npos.
1808 */
1809 size_type
1810 rfind(const basic_string& __str, size_type __pos = npos) const
1811 { return this->rfind(__str.data(), __pos, __str.size()); }
1812
1813 /**
1814 * @brief Find last position of a C substring.
1815 * @param s C string to locate.
1816 * @param pos Index of character to search back from.
1817 * @param n Number of characters from s to search for.
1818 * @return Index of start of last occurrence.
1819 *
1820 * Starting from @a pos, searches backward for the first @a n
1821 * characters in @a s within this string. If found, returns the index
1822 * where it begins. If not found, returns npos.
1823 */
1824 size_type
1825 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1826
1827 /**
1828 * @brief Find last position of a C string.
1829 * @param s C string to locate.
1830 * @param pos Index of character to start search at (default end).
1831 * @return Index of start of last occurrence.
1832 *
1833 * Starting from @a pos, searches backward for the value of @a s within
1834 * this string. If found, returns the index where it begins. If not
1835 * found, returns npos.
1836 */
1837 size_type
1838 rfind(const _CharT* __s, size_type __pos = npos) const
1839 {
1840 __glibcxx_requires_string(__s);
1841 return this->rfind(__s, __pos, traits_type::length(__s));
1842 }
1843
1844 /**
1845 * @brief Find last position of a character.
1846 * @param c Character to locate.
1847 * @param pos Index of character to search back from (default end).
1848 * @return Index of last occurrence.
1849 *
1850 * Starting from @a pos, searches backward for @a c within this string.
1851 * If found, returns the index where it was found. If not found,
1852 * returns npos.
1853 */
1854 size_type
1855 rfind(_CharT __c, size_type __pos = npos) const;
1856
1857 /**
1858 * @brief Find position of a character of string.
1859 * @param str String containing characters to locate.
1860 * @param pos Index of character to search from (default 0).
1861 * @return Index of first occurrence.
1862 *
1863 * Starting from @a pos, searches forward for one of the characters of
1864 * @a str within this string. If found, returns the index where it was
1865 * found. If not found, returns npos.
1866 */
1867 size_type
1868 find_first_of(const basic_string& __str, size_type __pos = 0) const
1869 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1870
1871 /**
1872 * @brief Find position of a character of C substring.
1873 * @param s String containing characters to locate.
1874 * @param pos Index of character to search from.
1875 * @param n Number of characters from s to search for.
1876 * @return Index of first occurrence.
1877 *
1878 * Starting from @a pos, searches forward for one of the first @a n
1879 * characters of @a s within this string. If found, returns the index
1880 * where it was found. If not found, returns npos.
1881 */
1882 size_type
1883 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1884
1885 /**
1886 * @brief Find position of a character of C string.
1887 * @param s String containing characters to locate.
1888 * @param pos Index of character to search from (default 0).
1889 * @return Index of first occurrence.
1890 *
1891 * Starting from @a pos, searches forward for one of the characters of
1892 * @a s within this string. If found, returns the index where it was
1893 * found. If not found, returns npos.
1894 */
1895 size_type
1896 find_first_of(const _CharT* __s, size_type __pos = 0) const
1897 {
1898 __glibcxx_requires_string(__s);
1899 return this->find_first_of(__s, __pos, traits_type::length(__s));
1900 }
1901
1902 /**
1903 * @brief Find position of a character.
1904 * @param c Character to locate.
1905 * @param pos Index of character to search from (default 0).
1906 * @return Index of first occurrence.
1907 *
1908 * Starting from @a pos, searches forward for the character @a c within
1909 * this string. If found, returns the index where it was found. If
1910 * not found, returns npos.
1911 *
1912 * Note: equivalent to find(c, pos).
1913 */
1914 size_type
1915 find_first_of(_CharT __c, size_type __pos = 0) const
1916 { return this->find(__c, __pos); }
1917
1918 /**
1919 * @brief Find last position of a character of string.
1920 * @param str String containing characters to locate.
1921 * @param pos Index of character to search back from (default end).
1922 * @return Index of last occurrence.
1923 *
1924 * Starting from @a pos, searches backward for one of the characters of
1925 * @a str within this string. If found, returns the index where it was
1926 * found. If not found, returns npos.
1927 */
1928 size_type
1929 find_last_of(const basic_string& __str, size_type __pos = npos) const
1930 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1931
1932 /**
1933 * @brief Find last position of a character of C substring.
1934 * @param s C string containing characters to locate.
1935 * @param pos Index of character to search back from.
1936 * @param n Number of characters from s to search for.
1937 * @return Index of last occurrence.
1938 *
1939 * Starting from @a pos, searches backward for one of the first @a n
1940 * characters of @a s within this string. If found, returns the index
1941 * where it was found. If not found, returns npos.
1942 */
1943 size_type
1944 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1945
1946 /**
1947 * @brief Find last position of a character of C string.
1948 * @param s C string containing characters to locate.
1949 * @param pos Index of character to search back from (default end).
1950 * @return Index of last occurrence.
1951 *
1952 * Starting from @a pos, searches backward for one of the characters of
1953 * @a s within this string. If found, returns the index where it was
1954 * found. If not found, returns npos.
1955 */
1956 size_type
1957 find_last_of(const _CharT* __s, size_type __pos = npos) const
1958 {
1959 __glibcxx_requires_string(__s);
1960 return this->find_last_of(__s, __pos, traits_type::length(__s));
1961 }
1962
1963 /**
1964 * @brief Find last position of a character.
1965 * @param c Character to locate.
1966 * @param pos Index of character to search back from (default end).
1967 * @return Index of last occurrence.
1968 *
1969 * Starting from @a pos, searches backward for @a c within this string.
1970 * If found, returns the index where it was found. If not found,
1971 * returns npos.
1972 *
1973 * Note: equivalent to rfind(c, pos).
1974 */
1975 size_type
1976 find_last_of(_CharT __c, size_type __pos = npos) const
1977 { return this->rfind(__c, __pos); }
1978
1979 /**
1980 * @brief Find position of a character not in string.
1981 * @param str String containing characters to avoid.
1982 * @param pos Index of character to search from (default 0).
1983 * @return Index of first occurrence.
1984 *
1985 * Starting from @a pos, searches forward for a character not contained
1986 * in @a str within this string. If found, returns the index where it
1987 * was found. If not found, returns npos.
1988 */
1989 size_type
1990 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1991 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1992
1993 /**
1994 * @brief Find position of a character not in C substring.
1995 * @param s C string containing characters to avoid.
1996 * @param pos Index of character to search from.
1997 * @param n Number of characters from s to consider.
1998 * @return Index of first occurrence.
1999 *
2000 * Starting from @a pos, searches forward for a character not contained
2001 * in the first @a n characters of @a s within this string. If found,
2002 * returns the index where it was found. If not found, returns npos.
2003 */
2004 size_type
2005 find_first_not_of(const _CharT* __s, size_type __pos,
2006 size_type __n) const;
2007
2008 /**
2009 * @brief Find position of a character not in C string.
2010 * @param s C string containing characters to avoid.
2011 * @param pos Index of character to search from (default 0).
2012 * @return Index of first occurrence.
2013 *
2014 * Starting from @a pos, searches forward for a character not contained
2015 * in @a s within this string. If found, returns the index where it
2016 * was found. If not found, returns npos.
2017 */
2018 size_type
2019 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
2020 {
2021 __glibcxx_requires_string(__s);
2022 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
2023 }
2024
2025 /**
2026 * @brief Find position of a different character.
2027 * @param c Character to avoid.
2028 * @param pos Index of character to search from (default 0).
2029 * @return Index of first occurrence.
2030 *
2031 * Starting from @a pos, searches forward for a character other than @a c
2032 * within this string. If found, returns the index where it was found.
2033 * If not found, returns npos.
2034 */
2035 size_type
2036 find_first_not_of(_CharT __c, size_type __pos = 0) const;
2037
2038 /**
2039 * @brief Find last position of a character not in string.
2040 * @param str String containing characters to avoid.
2041 * @param pos Index of character to search back from (default end).
2042 * @return Index of last occurrence.
2043 *
2044 * Starting from @a pos, searches backward for a character not
2045 * contained in @a str within this string. If found, returns the index
2046 * where it was found. If not found, returns npos.
2047 */
2048 size_type
2049 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
2050 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
2051
2052 /**
2053 * @brief Find last position of a character not in C substring.
2054 * @param s C string containing characters to avoid.
2055 * @param pos Index of character to search back from.
2056 * @param n Number of characters from s to consider.
2057 * @return Index of last occurrence.
2058 *
2059 * Starting from @a pos, searches backward for a character not
2060 * contained in the first @a n characters of @a s within this string.
2061 * If found, returns the index where it was found. If not found,
2062 * returns npos.
2063 */
2064 size_type
2065 find_last_not_of(const _CharT* __s, size_type __pos,
2066 size_type __n) const;
2067 /**
2068 * @brief Find last position of a character not in C string.
2069 * @param s C string containing characters to avoid.
2070 * @param pos Index of character to search back from (default end).
2071 * @return Index of last occurrence.
2072 *
2073 * Starting from @a pos, searches backward for a character not
2074 * contained in @a s within this string. If found, returns the index
2075 * where it was found. If not found, returns npos.
2076 */
2077 size_type
2078 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
2079 {
2080 __glibcxx_requires_string(__s);
2081 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
2082 }
2083
2084 /**
2085 * @brief Find last position of a different character.
2086 * @param c Character to avoid.
2087 * @param pos Index of character to search back from (default end).
2088 * @return Index of last occurrence.
2089 *
2090 * Starting from @a pos, searches backward for a character other than
2091 * @a c within this string. If found, returns the index where it was
2092 * found. If not found, returns npos.
2093 */
2094 size_type
2095 find_last_not_of(_CharT __c, size_type __pos = npos) const;
2096
2097 /**
2098 * @brief Get a substring.
2099 * @param pos Index of first character (default 0).
2100 * @param n Number of characters in substring (default remainder).
2101 * @return The new string.
2102 * @throw std::out_of_range If pos > size().
2103 *
2104 * Construct and return a new string using the @a n characters starting
2105 * at @a pos. If the string is too short, use the remainder of the
2106 * characters. If @a pos is beyond the end of the string, out_of_range
2107 * is thrown.
2108 */
2109 basic_string
2110 substr(size_type __pos = 0, size_type __n = npos) const
2111 { return basic_string(*this,
2112 _M_check(__pos, "basic_string::substr"), __n); }
2113
2114 /**
2115 * @brief Compare to a string.
2116 * @param str String to compare against.
2117 * @return Integer < 0, 0, or > 0.
2118 *
2119 * Returns an integer < 0 if this string is ordered before @a str, 0 if
2120 * their values are equivalent, or > 0 if this string is ordered after
2121 * @a str. Determines the effective length rlen of the strings to
2122 * compare as the smallest of size() and str.size(). The function
2123 * then compares the two strings by calling traits::compare(data(),
2124 * str.data(),rlen). If the result of the comparison is nonzero returns
2125 * it, otherwise the shorter one is ordered first.
2126 */
2127 int
2128 compare(const basic_string& __str) const
2129 {
2130 const size_type __size = this->size();
2131 const size_type __osize = __str.size();
2132 const size_type __len = std::min(__size, __osize);
2133
2134 int __r = traits_type::compare(_M_data(), __str.data(), __len);
2135 if (!__r)
2136 __r = _S_compare(__size, __osize);
2137 return __r;
2138 }
2139
2140 /**
2141 * @brief Compare substring to a string.
2142 * @param pos Index of first character of substring.
2143 * @param n Number of characters in substring.
2144 * @param str String to compare against.
2145 * @return Integer < 0, 0, or > 0.
2146 *
2147 * Form the substring of this string from the @a n characters starting
2148 * at @a pos. Returns an integer < 0 if the substring is ordered
2149 * before @a str, 0 if their values are equivalent, or > 0 if the
2150 * substring is ordered after @a str. Determines the effective length
2151 * rlen of the strings to compare as the smallest of the length of the
2152 * substring and @a str.size(). The function then compares the two
2153 * strings by calling traits::compare(substring.data(),str.data(),rlen).
2154 * If the result of the comparison is nonzero returns it, otherwise the
2155 * shorter one is ordered first.
2156 */
2157 int
2158 compare(size_type __pos, size_type __n, const basic_string& __str) const;
2159
2160 /**
2161 * @brief Compare substring to a substring.
2162 * @param pos1 Index of first character of substring.
2163 * @param n1 Number of characters in substring.
2164 * @param str String to compare against.
2165 * @param pos2 Index of first character of substring of str.
2166 * @param n2 Number of characters in substring of str.
2167 * @return Integer < 0, 0, or > 0.
2168 *
2169 * Form the substring of this string from the @a n1 characters starting
2170 * at @a pos1. Form the substring of @a str from the @a n2 characters
2171 * starting at @a pos2. Returns an integer < 0 if this substring is
2172 * ordered before the substring of @a str, 0 if their values are
2173 * equivalent, or > 0 if this substring is ordered after the substring
2174 * of @a str. Determines the effective length rlen of the strings
2175 * to compare as the smallest of the lengths of the substrings. The
2176 * function then compares the two strings by calling
2177 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2178 * If the result of the comparison is nonzero returns it, otherwise the
2179 * shorter one is ordered first.
2180 */
2181 int
2182 compare(size_type __pos1, size_type __n1, const basic_string& __str,
2183 size_type __pos2, size_type __n2) const;
2184
2185 /**
2186 * @brief Compare to a C string.
2187 * @param s C string to compare against.
2188 * @return Integer < 0, 0, or > 0.
2189 *
2190 * Returns an integer < 0 if this string is ordered before @a s, 0 if
2191 * their values are equivalent, or > 0 if this string is ordered after
2192 * @a s. Determines the effective length rlen of the strings to
2193 * compare as the smallest of size() and the length of a string
2194 * constructed from @a s. The function then compares the two strings
2195 * by calling traits::compare(data(),s,rlen). If the result of the
2196 * comparison is nonzero returns it, otherwise the shorter one is
2197 * ordered first.
2198 */
2199 int
2200 compare(const _CharT* __s) const;
2201
2202 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2203 // 5 String::compare specification questionable
2204 /**
2205 * @brief Compare substring to a C string.
2206 * @param pos Index of first character of substring.
2207 * @param n1 Number of characters in substring.
2208 * @param s C string to compare against.
2209 * @return Integer < 0, 0, or > 0.
2210 *
2211 * Form the substring of this string from the @a n1 characters starting
2212 * at @a pos. Returns an integer < 0 if the substring is ordered
2213 * before @a s, 0 if their values are equivalent, or > 0 if the
2214 * substring is ordered after @a s. Determines the effective length
2215 * rlen of the strings to compare as the smallest of the length of the
2216 * substring and the length of a string constructed from @a s. The
2217 * function then compares the two string by calling
2218 * traits::compare(substring.data(),s,rlen). If the result of the
2219 * comparison is nonzero returns it, otherwise the shorter one is
2220 * ordered first.
2221 */
2222 int
2223 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2224
2225 /**
2226 * @brief Compare substring against a character array.
2227 * @param pos1 Index of first character of substring.
2228 * @param n1 Number of characters in substring.
2229 * @param s character array to compare against.
2230 * @param n2 Number of characters of s.
2231 * @return Integer < 0, 0, or > 0.
2232 *
2233 * Form the substring of this string from the @a n1 characters starting
2234 * at @a pos1. Form a string from the first @a n2 characters of @a s.
2235 * Returns an integer < 0 if this substring is ordered before the string
2236 * from @a s, 0 if their values are equivalent, or > 0 if this substring
2237 * is ordered after the string from @a s. Determines the effective
2238 * length rlen of the strings to compare as the smallest of the length
2239 * of the substring and @a n2. The function then compares the two
2240 * strings by calling traits::compare(substring.data(),s,rlen). If the
2241 * result of the comparison is nonzero returns it, otherwise the shorter
2242 * one is ordered first.
2243 *
2244 * NB: s must have at least n2 characters, '\\0' has no special
2245 * meaning.
2246 */
2247 int
2248 compare(size_type __pos, size_type __n1, const _CharT* __s,
2249 size_type __n2) const;
2250 };
2251
2252 // operator+
2253 /**
2254 * @brief Concatenate two strings.
2255 * @param lhs First string.
2256 * @param rhs Last string.
2257 * @return New string with value of @a lhs followed by @a rhs.
2258 */
2259 template<typename _CharT, typename _Traits, typename _Alloc>
2260 basic_string<_CharT, _Traits, _Alloc>
2261 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2262 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2263 {
2264 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2265 __str.append(__rhs);
2266 return __str;
2267 }
2268
2269 /**
2270 * @brief Concatenate C string and string.
2271 * @param lhs First string.
2272 * @param rhs Last string.
2273 * @return New string with value of @a lhs followed by @a rhs.
2274 */
2275 template<typename _CharT, typename _Traits, typename _Alloc>
2276 basic_string<_CharT,_Traits,_Alloc>
2277 operator+(const _CharT* __lhs,
2278 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2279
2280 /**
2281 * @brief Concatenate character and string.
2282 * @param lhs First string.
2283 * @param rhs Last string.
2284 * @return New string with @a lhs followed by @a rhs.
2285 */
2286 template<typename _CharT, typename _Traits, typename _Alloc>
2287 basic_string<_CharT,_Traits,_Alloc>
2288 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2289
2290 /**
2291 * @brief Concatenate string and C string.
2292 * @param lhs First string.
2293 * @param rhs Last string.
2294 * @return New string with @a lhs followed by @a rhs.
2295 */
2296 template<typename _CharT, typename _Traits, typename _Alloc>
2297 inline basic_string<_CharT, _Traits, _Alloc>
2298 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2299 const _CharT* __rhs)
2300 {
2301 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2302 __str.append(__rhs);
2303 return __str;
2304 }
2305
2306 /**
2307 * @brief Concatenate string and character.
2308 * @param lhs First string.
2309 * @param rhs Last string.
2310 * @return New string with @a lhs followed by @a rhs.
2311 */
2312 template<typename _CharT, typename _Traits, typename _Alloc>
2313 inline basic_string<_CharT, _Traits, _Alloc>
2314 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2315 {
2316 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
2317 typedef typename __string_type::size_type __size_type;
2318 __string_type __str(__lhs);
2319 __str.append(__size_type(1), __rhs);
2320 return __str;
2321 }
2322
2323 // operator ==
2324 /**
2325 * @brief Test equivalence of two strings.
2326 * @param lhs First string.
2327 * @param rhs Second string.
2328 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2329 */
2330 template<typename _CharT, typename _Traits, typename _Alloc>
2331 inline bool
2332 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2333 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2334 { return __lhs.compare(__rhs) == 0; }
2335
2336 template<typename _CharT>
2337 inline
2338 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
2339 operator==(const basic_string<_CharT>& __lhs,
2340 const basic_string<_CharT>& __rhs)
2341 { return (__lhs.size() == __rhs.size()
2342 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
2343 __lhs.size())); }
2344
2345 /**
2346 * @brief Test equivalence of C string and string.
2347 * @param lhs C string.
2348 * @param rhs String.
2349 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
2350 */
2351 template<typename _CharT, typename _Traits, typename _Alloc>
2352 inline bool
2353 operator==(const _CharT* __lhs,
2354 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2355 { return __rhs.compare(__lhs) == 0; }
2356
2357 /**
2358 * @brief Test equivalence of string and C string.
2359 * @param lhs String.
2360 * @param rhs C string.
2361 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2362 */
2363 template<typename _CharT, typename _Traits, typename _Alloc>
2364 inline bool
2365 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2366 const _CharT* __rhs)
2367 { return __lhs.compare(__rhs) == 0; }
2368
2369 // operator !=
2370 /**
2371 * @brief Test difference of two strings.
2372 * @param lhs First string.
2373 * @param rhs Second string.
2374 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2375 */
2376 template<typename _CharT, typename _Traits, typename _Alloc>
2377 inline bool
2378 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2379 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2380 { return !(__lhs == __rhs); }
2381
2382 /**
2383 * @brief Test difference of C string and string.
2384 * @param lhs C string.
2385 * @param rhs String.
2386 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2387 */
2388 template<typename _CharT, typename _Traits, typename _Alloc>
2389 inline bool
2390 operator!=(const _CharT* __lhs,
2391 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2392 { return !(__lhs == __rhs); }
2393
2394 /**
2395 * @brief Test difference of string and C string.
2396 * @param lhs String.
2397 * @param rhs C string.
2398 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2399 */
2400 template<typename _CharT, typename _Traits, typename _Alloc>
2401 inline bool
2402 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2403 const _CharT* __rhs)
2404 { return !(__lhs == __rhs); }
2405
2406 // operator <
2407 /**
2408 * @brief Test if string precedes string.
2409 * @param lhs First string.
2410 * @param rhs Second string.
2411 * @return True if @a lhs precedes @a rhs. False otherwise.
2412 */
2413 template<typename _CharT, typename _Traits, typename _Alloc>
2414 inline bool
2415 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2416 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2417 { return __lhs.compare(__rhs) < 0; }
2418
2419 /**
2420 * @brief Test if string precedes C string.
2421 * @param lhs String.
2422 * @param rhs C string.
2423 * @return True if @a lhs precedes @a rhs. False otherwise.
2424 */
2425 template<typename _CharT, typename _Traits, typename _Alloc>
2426 inline bool
2427 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2428 const _CharT* __rhs)
2429 { return __lhs.compare(__rhs) < 0; }
2430
2431 /**
2432 * @brief Test if C string precedes string.
2433 * @param lhs C string.
2434 * @param rhs String.
2435 * @return True if @a lhs precedes @a rhs. False otherwise.
2436 */
2437 template<typename _CharT, typename _Traits, typename _Alloc>
2438 inline bool
2439 operator<(const _CharT* __lhs,
2440 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2441 { return __rhs.compare(__lhs) > 0; }
2442
2443 // operator >
2444 /**
2445 * @brief Test if string follows string.
2446 * @param lhs First string.
2447 * @param rhs Second string.
2448 * @return True if @a lhs follows @a rhs. False otherwise.
2449 */
2450 template<typename _CharT, typename _Traits, typename _Alloc>
2451 inline bool
2452 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2453 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2454 { return __lhs.compare(__rhs) > 0; }
2455
2456 /**
2457 * @brief Test if string follows C string.
2458 * @param lhs String.
2459 * @param rhs C string.
2460 * @return True if @a lhs follows @a rhs. False otherwise.
2461 */
2462 template<typename _CharT, typename _Traits, typename _Alloc>
2463 inline bool
2464 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2465 const _CharT* __rhs)
2466 { return __lhs.compare(__rhs) > 0; }
2467
2468 /**
2469 * @brief Test if C string follows string.
2470 * @param lhs C string.
2471 * @param rhs String.
2472 * @return True if @a lhs follows @a rhs. False otherwise.
2473 */
2474 template<typename _CharT, typename _Traits, typename _Alloc>
2475 inline bool
2476 operator>(const _CharT* __lhs,
2477 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2478 { return __rhs.compare(__lhs) < 0; }
2479
2480 // operator <=
2481 /**
2482 * @brief Test if string doesn't follow string.
2483 * @param lhs First string.
2484 * @param rhs Second string.
2485 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2486 */
2487 template<typename _CharT, typename _Traits, typename _Alloc>
2488 inline bool
2489 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2490 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2491 { return __lhs.compare(__rhs) <= 0; }
2492
2493 /**
2494 * @brief Test if string doesn't follow C string.
2495 * @param lhs String.
2496 * @param rhs C string.
2497 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2498 */
2499 template<typename _CharT, typename _Traits, typename _Alloc>
2500 inline bool
2501 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2502 const _CharT* __rhs)
2503 { return __lhs.compare(__rhs) <= 0; }
2504
2505 /**
2506 * @brief Test if C string doesn't follow string.
2507 * @param lhs C string.
2508 * @param rhs String.
2509 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2510 */
2511 template<typename _CharT, typename _Traits, typename _Alloc>
2512 inline bool
2513 operator<=(const _CharT* __lhs,
2514 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2515 { return __rhs.compare(__lhs) >= 0; }
2516
2517 // operator >=
2518 /**
2519 * @brief Test if string doesn't precede string.
2520 * @param lhs First string.
2521 * @param rhs Second string.
2522 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2523 */
2524 template<typename _CharT, typename _Traits, typename _Alloc>
2525 inline bool
2526 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2527 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2528 { return __lhs.compare(__rhs) >= 0; }
2529
2530 /**
2531 * @brief Test if string doesn't precede C string.
2532 * @param lhs String.
2533 * @param rhs C string.
2534 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2535 */
2536 template<typename _CharT, typename _Traits, typename _Alloc>
2537 inline bool
2538 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2539 const _CharT* __rhs)
2540 { return __lhs.compare(__rhs) >= 0; }
2541
2542 /**
2543 * @brief Test if C string doesn't precede string.
2544 * @param lhs C string.
2545 * @param rhs String.
2546 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2547 */
2548 template<typename _CharT, typename _Traits, typename _Alloc>
2549 inline bool
2550 operator>=(const _CharT* __lhs,
2551 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2552 { return __rhs.compare(__lhs) <= 0; }
2553
2554 /**
2555 * @brief Swap contents of two strings.
2556 * @param lhs First string.
2557 * @param rhs Second string.
2558 *
2559 * Exchanges the contents of @a lhs and @a rhs in constant time.
2560 */
2561 template<typename _CharT, typename _Traits, typename _Alloc>
2562 inline void
2563 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2564 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2565 { __lhs.swap(__rhs); }
2566
2567 /**
2568 * @brief Read stream into a string.
2569 * @param is Input stream.
2570 * @param str Buffer to store into.
2571 * @return Reference to the input stream.
2572 *
2573 * Stores characters from @a is into @a str until whitespace is found, the
2574 * end of the stream is encountered, or str.max_size() is reached. If
2575 * is.width() is non-zero, that is the limit on the number of characters
2576 * stored into @a str. Any previous contents of @a str are erased.
2577 */
2578 template<typename _CharT, typename _Traits, typename _Alloc>
2579 basic_istream<_CharT, _Traits>&
2580 operator>>(basic_istream<_CharT, _Traits>& __is,
2581 basic_string<_CharT, _Traits, _Alloc>& __str);
2582
2583 template<>
2584 basic_istream<char>&
2585 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
2586
2587 /**
2588 * @brief Write string to a stream.
2589 * @param os Output stream.
2590 * @param str String to write out.
2591 * @return Reference to the output stream.
2592 *
2593 * Output characters of @a str into os following the same rules as for
2594 * writing a C string.
2595 */
2596 template<typename _CharT, typename _Traits, typename _Alloc>
2597 inline basic_ostream<_CharT, _Traits>&
2598 operator<<(basic_ostream<_CharT, _Traits>& __os,
2599 const basic_string<_CharT, _Traits, _Alloc>& __str)
2600 {
2601 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2602 // 586. string inserter not a formatted function
2603 return __ostream_insert(__os, __str.data(), __str.size());
2604 }
2605
2606 /**
2607 * @brief Read a line from stream into a string.
2608 * @param is Input stream.
2609 * @param str Buffer to store into.
2610 * @param delim Character marking end of line.
2611 * @return Reference to the input stream.
2612 *
2613 * Stores characters from @a is into @a str until @a delim is found, the
2614 * end of the stream is encountered, or str.max_size() is reached. If
2615 * is.width() is non-zero, that is the limit on the number of characters
2616 * stored into @a str. Any previous contents of @a str are erased. If @a
2617 * delim was encountered, it is extracted but not stored into @a str.
2618 */
2619 template<typename _CharT, typename _Traits, typename _Alloc>
2620 basic_istream<_CharT, _Traits>&
2621 getline(basic_istream<_CharT, _Traits>& __is,
2622 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2623
2624 /**
2625 * @brief Read a line from stream into a string.
2626 * @param is Input stream.
2627 * @param str Buffer to store into.
2628 * @return Reference to the input stream.
2629 *
2630 * Stores characters from is into @a str until '\n' is found, the end of
2631 * the stream is encountered, or str.max_size() is reached. If is.width()
2632 * is non-zero, that is the limit on the number of characters stored into
2633 * @a str. Any previous contents of @a str are erased. If end of line was
2634 * encountered, it is extracted but not stored into @a str.
2635 */
2636 template<typename _CharT, typename _Traits, typename _Alloc>
2637 inline basic_istream<_CharT, _Traits>&
2638 getline(basic_istream<_CharT, _Traits>& __is,
2639 basic_string<_CharT, _Traits, _Alloc>& __str)
2640 { return getline(__is, __str, __is.widen('\n')); }
2641
2642 template<>
2643 basic_istream<char>&
2644 getline(basic_istream<char>& __in, basic_string<char>& __str,
2645 char __delim);
2646
2647 #ifdef _GLIBCXX_USE_WCHAR_T
2648 template<>
2649 basic_istream<wchar_t>&
2650 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
2651 wchar_t __delim);
2652 #endif
2653
2654 _GLIBCXX_END_NAMESPACE
2655
2656 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99) \
2657 && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
2658
2659 #include <ext/string_conversions.h>
2660
2661 _GLIBCXX_BEGIN_NAMESPACE(std)
2662
2663 // 21.4 Numeric Conversions [string.conversions].
2664 inline int
2665 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
2666 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
2667 __idx, __base); }
2668
2669 inline long
2670 stol(const string& __str, size_t* __idx = 0, int __base = 10)
2671 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
2672 __idx, __base); }
2673
2674 inline unsigned long
2675 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
2676 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
2677 __idx, __base); }
2678
2679 inline long long
2680 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
2681 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
2682 __idx, __base); }
2683
2684 inline unsigned long long
2685 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
2686 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
2687 __idx, __base); }
2688
2689 // NB: strtof vs strtod.
2690 inline float
2691 stof(const string& __str, size_t* __idx = 0)
2692 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
2693
2694 inline double
2695 stod(const string& __str, size_t* __idx = 0)
2696 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
2697
2698 inline long double
2699 stold(const string& __str, size_t* __idx = 0)
2700 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
2701
2702 // NB: (v)snprintf vs sprintf.
2703
2704 // DR 1261.
2705 inline string
2706 to_string(int __val)
2707 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
2708 "%d", __val); }
2709
2710 inline string
2711 to_string(unsigned __val)
2712 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2713 4 * sizeof(unsigned),
2714 "%u", __val); }
2715
2716 inline string
2717 to_string(long __val)
2718 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
2719 "%ld", __val); }
2720
2721 inline string
2722 to_string(unsigned long __val)
2723 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2724 4 * sizeof(unsigned long),
2725 "%lu", __val); }
2726
2727 inline string
2728 to_string(long long __val)
2729 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2730 4 * sizeof(long long),
2731 "%lld", __val); }
2732
2733 inline string
2734 to_string(unsigned long long __val)
2735 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2736 4 * sizeof(unsigned long long),
2737 "%llu", __val); }
2738
2739 inline string
2740 to_string(float __val)
2741 {
2742 const int __n =
2743 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
2744 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2745 "%f", __val);
2746 }
2747
2748 inline string
2749 to_string(double __val)
2750 {
2751 const int __n =
2752 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
2753 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2754 "%f", __val);
2755 }
2756
2757 inline string
2758 to_string(long double __val)
2759 {
2760 const int __n =
2761 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
2762 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2763 "%Lf", __val);
2764 }
2765
2766 #ifdef _GLIBCXX_USE_WCHAR_T
2767 inline int
2768 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
2769 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
2770 __idx, __base); }
2771
2772 inline long
2773 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
2774 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
2775 __idx, __base); }
2776
2777 inline unsigned long
2778 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
2779 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
2780 __idx, __base); }
2781
2782 inline long long
2783 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
2784 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
2785 __idx, __base); }
2786
2787 inline unsigned long long
2788 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
2789 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
2790 __idx, __base); }
2791
2792 // NB: wcstof vs wcstod.
2793 inline float
2794 stof(const wstring& __str, size_t* __idx = 0)
2795 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
2796
2797 inline double
2798 stod(const wstring& __str, size_t* __idx = 0)
2799 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
2800
2801 inline long double
2802 stold(const wstring& __str, size_t* __idx = 0)
2803 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
2804
2805 // DR 1261.
2806 inline wstring
2807 to_wstring(int __val)
2808 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
2809 L"%d", __val); }
2810
2811 inline wstring
2812 to_wstring(unsigned __val)
2813 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2814 4 * sizeof(unsigned),
2815 L"%u", __val); }
2816
2817 inline wstring
2818 to_wstring(long __val)
2819 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
2820 L"%ld", __val); }
2821
2822 inline wstring
2823 to_wstring(unsigned long __val)
2824 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2825 4 * sizeof(unsigned long),
2826 L"%lu", __val); }
2827
2828 inline wstring
2829 to_wstring(long long __val)
2830 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2831 4 * sizeof(long long),
2832 L"%lld", __val); }
2833
2834 inline wstring
2835 to_wstring(unsigned long long __val)
2836 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2837 4 * sizeof(unsigned long long),
2838 L"%llu", __val); }
2839
2840 inline wstring
2841 to_wstring(float __val)
2842 {
2843 const int __n =
2844 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
2845 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
2846 L"%f", __val);
2847 }
2848
2849 inline wstring
2850 to_wstring(double __val)
2851 {
2852 const int __n =
2853 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
2854 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
2855 L"%f", __val);
2856 }
2857
2858 inline wstring
2859 to_wstring(long double __val)
2860 {
2861 const int __n =
2862 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
2863 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
2864 L"%Lf", __val);
2865 }
2866 #endif
2867
2868 _GLIBCXX_END_NAMESPACE
2869
2870 #endif
2871
2872 #endif /* _BASIC_STRING_H */