e82b8900111a1ff16d439aa4e6f1592ebfe254e7
[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
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 inline
423 basic_string();
424
425 /**
426 * @brief Construct an empty string using allocator @a a.
427 */
428 explicit
429 basic_string(const _Alloc& __a);
430
431 // NB: per LWG issue 42, semantics different from IS:
432 /**
433 * @brief Construct string with copy of value of @a str.
434 * @param str Source string.
435 */
436 basic_string(const basic_string& __str);
437 /**
438 * @brief Construct string as copy of a substring.
439 * @param str Source string.
440 * @param pos Index of first character to copy from.
441 * @param n Number of characters to copy (default remainder).
442 */
443 basic_string(const basic_string& __str, size_type __pos,
444 size_type __n = npos);
445 /**
446 * @brief Construct string as copy of a substring.
447 * @param str Source string.
448 * @param pos Index of first character to copy from.
449 * @param n Number of characters to copy.
450 * @param a Allocator to use.
451 */
452 basic_string(const basic_string& __str, size_type __pos,
453 size_type __n, const _Alloc& __a);
454
455 /**
456 * @brief Construct string initialized by a character array.
457 * @param s Source character array.
458 * @param n Number of characters to copy.
459 * @param a Allocator to use (default is default allocator).
460 *
461 * NB: @a s must have at least @a n characters, '\\0' has no special
462 * meaning.
463 */
464 basic_string(const _CharT* __s, size_type __n,
465 const _Alloc& __a = _Alloc());
466 /**
467 * @brief Construct string as copy of a C string.
468 * @param s Source C string.
469 * @param a Allocator to use (default is default allocator).
470 */
471 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
472 /**
473 * @brief Construct string as multiple characters.
474 * @param n Number of characters.
475 * @param c Character to use.
476 * @param a Allocator to use (default is default allocator).
477 */
478 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
479
480 #ifdef __GXX_EXPERIMENTAL_CXX0X__
481 /**
482 * @brief Construct string from an initializer list.
483 * @param l std::initializer_list of characters.
484 * @param a Allocator to use (default is default allocator).
485 */
486 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
487 #endif // __GXX_EXPERIMENTAL_CXX0X__
488
489 /**
490 * @brief Construct string as copy of a range.
491 * @param beg Start of range.
492 * @param end End of range.
493 * @param a Allocator to use (default is default allocator).
494 */
495 template<class _InputIterator>
496 basic_string(_InputIterator __beg, _InputIterator __end,
497 const _Alloc& __a = _Alloc());
498
499 /**
500 * @brief Destroy the string instance.
501 */
502 ~basic_string()
503 { _M_rep()->_M_dispose(this->get_allocator()); }
504
505 /**
506 * @brief Assign the value of @a str to this string.
507 * @param str Source string.
508 */
509 basic_string&
510 operator=(const basic_string& __str)
511 { return this->assign(__str); }
512
513 /**
514 * @brief Copy contents of @a s into this string.
515 * @param s Source null-terminated string.
516 */
517 basic_string&
518 operator=(const _CharT* __s)
519 { return this->assign(__s); }
520
521 /**
522 * @brief Set value to string of length 1.
523 * @param c Source character.
524 *
525 * Assigning to a character makes this string length 1 and
526 * (*this)[0] == @a c.
527 */
528 basic_string&
529 operator=(_CharT __c)
530 {
531 this->assign(1, __c);
532 return *this;
533 }
534
535 #ifdef __GXX_EXPERIMENTAL_CXX0X__
536 /**
537 * @brief Set value to string constructed from initializer list.
538 * @param l std::initializer_list.
539 */
540 basic_string&
541 operator=(initializer_list<_CharT> __l)
542 {
543 this->assign(__l.begin(), __l.size());
544 return *this;
545 }
546 #endif // __GXX_EXPERIMENTAL_CXX0X__
547
548 // Iterators:
549 /**
550 * Returns a read/write iterator that points to the first character in
551 * the %string. Unshares the string.
552 */
553 iterator
554 begin()
555 {
556 _M_leak();
557 return iterator(_M_data());
558 }
559
560 /**
561 * Returns a read-only (constant) iterator that points to the first
562 * character in the %string.
563 */
564 const_iterator
565 begin() const
566 { return const_iterator(_M_data()); }
567
568 /**
569 * Returns a read/write iterator that points one past the last
570 * character in the %string. Unshares the string.
571 */
572 iterator
573 end()
574 {
575 _M_leak();
576 return iterator(_M_data() + this->size());
577 }
578
579 /**
580 * Returns a read-only (constant) iterator that points one past the
581 * last character in the %string.
582 */
583 const_iterator
584 end() const
585 { return const_iterator(_M_data() + this->size()); }
586
587 /**
588 * Returns a read/write reverse iterator that points to the last
589 * character in the %string. Iteration is done in reverse element
590 * order. Unshares the string.
591 */
592 reverse_iterator
593 rbegin()
594 { return reverse_iterator(this->end()); }
595
596 /**
597 * Returns a read-only (constant) reverse iterator that points
598 * to the last character in the %string. Iteration is done in
599 * reverse element order.
600 */
601 const_reverse_iterator
602 rbegin() const
603 { return const_reverse_iterator(this->end()); }
604
605 /**
606 * Returns a read/write reverse iterator that points to one before the
607 * first character in the %string. Iteration is done in reverse
608 * element order. Unshares the string.
609 */
610 reverse_iterator
611 rend()
612 { return reverse_iterator(this->begin()); }
613
614 /**
615 * Returns a read-only (constant) reverse iterator that points
616 * to one before the first character in the %string. Iteration
617 * is done in reverse element order.
618 */
619 const_reverse_iterator
620 rend() const
621 { return const_reverse_iterator(this->begin()); }
622
623 #ifdef __GXX_EXPERIMENTAL_CXX0X__
624 /**
625 * Returns a read-only (constant) iterator that points to the first
626 * character in the %string.
627 */
628 const_iterator
629 cbegin() const
630 { return const_iterator(this->_M_data()); }
631
632 /**
633 * Returns a read-only (constant) iterator that points one past the
634 * last character in the %string.
635 */
636 const_iterator
637 cend() const
638 { return const_iterator(this->_M_data() + this->size()); }
639
640 /**
641 * Returns a read-only (constant) reverse iterator that points
642 * to the last character in the %string. Iteration is done in
643 * reverse element order.
644 */
645 const_reverse_iterator
646 crbegin() const
647 { return const_reverse_iterator(this->end()); }
648
649 /**
650 * Returns a read-only (constant) reverse iterator that points
651 * to one before the first character in the %string. Iteration
652 * is done in reverse element order.
653 */
654 const_reverse_iterator
655 crend() const
656 { return const_reverse_iterator(this->begin()); }
657 #endif
658
659 public:
660 // Capacity:
661 /// Returns the number of characters in the string, not including any
662 /// null-termination.
663 size_type
664 size() const
665 { return _M_rep()->_M_length; }
666
667 /// Returns the number of characters in the string, not including any
668 /// null-termination.
669 size_type
670 length() const
671 { return _M_rep()->_M_length; }
672
673 /// Returns the size() of the largest possible %string.
674 size_type
675 max_size() const
676 { return _Rep::_S_max_size; }
677
678 /**
679 * @brief Resizes the %string to the specified number of characters.
680 * @param n Number of characters the %string should contain.
681 * @param c Character to fill any new elements.
682 *
683 * This function will %resize the %string to the specified
684 * number of characters. If the number is smaller than the
685 * %string's current size the %string is truncated, otherwise
686 * the %string is extended and new elements are set to @a c.
687 */
688 void
689 resize(size_type __n, _CharT __c);
690
691 /**
692 * @brief Resizes the %string to the specified number of characters.
693 * @param n Number of characters the %string should contain.
694 *
695 * This function will resize the %string to the specified length. If
696 * the new size is smaller than the %string's current size the %string
697 * is truncated, otherwise the %string is extended and new characters
698 * are default-constructed. For basic types such as char, this means
699 * setting them to 0.
700 */
701 void
702 resize(size_type __n)
703 { this->resize(__n, _CharT()); }
704
705 #ifdef __GXX_EXPERIMENTAL_CXX0X__
706 /// A non-binding request to reduce capacity() to size().
707 void
708 shrink_to_fit()
709 {
710 try
711 { reserve(0); }
712 catch(...)
713 { }
714 }
715 #endif
716
717 /**
718 * Returns the total number of characters that the %string can hold
719 * before needing to allocate more memory.
720 */
721 size_type
722 capacity() const
723 { return _M_rep()->_M_capacity; }
724
725 /**
726 * @brief Attempt to preallocate enough memory for specified number of
727 * characters.
728 * @param res_arg Number of characters required.
729 * @throw std::length_error If @a res_arg exceeds @c max_size().
730 *
731 * This function attempts to reserve enough memory for the
732 * %string to hold the specified number of characters. If the
733 * number requested is more than max_size(), length_error is
734 * thrown.
735 *
736 * The advantage of this function is that if optimal code is a
737 * necessity and the user can determine the string length that will be
738 * required, the user can reserve the memory in %advance, and thus
739 * prevent a possible reallocation of memory and copying of %string
740 * data.
741 */
742 void
743 reserve(size_type __res_arg = 0);
744
745 /**
746 * Erases the string, making it empty.
747 */
748 void
749 clear()
750 { _M_mutate(0, this->size(), 0); }
751
752 /**
753 * Returns true if the %string is empty. Equivalent to *this == "".
754 */
755 bool
756 empty() const
757 { return this->size() == 0; }
758
759 // Element access:
760 /**
761 * @brief Subscript access to the data contained in the %string.
762 * @param pos The index of the character to access.
763 * @return Read-only (constant) reference to the character.
764 *
765 * This operator allows for easy, array-style, data access.
766 * Note that data access with this operator is unchecked and
767 * out_of_range lookups are not defined. (For checked lookups
768 * see at().)
769 */
770 const_reference
771 operator[] (size_type __pos) const
772 {
773 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
774 return _M_data()[__pos];
775 }
776
777 /**
778 * @brief Subscript access to the data contained in the %string.
779 * @param pos The index of the character to access.
780 * @return Read/write reference to the character.
781 *
782 * This operator allows for easy, array-style, data access.
783 * Note that data access with this operator is unchecked and
784 * out_of_range lookups are not defined. (For checked lookups
785 * see at().) Unshares the string.
786 */
787 reference
788 operator[](size_type __pos)
789 {
790 // allow pos == size() as v3 extension:
791 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
792 // but be strict in pedantic mode:
793 _GLIBCXX_DEBUG_PEDASSERT(__pos < size());
794 _M_leak();
795 return _M_data()[__pos];
796 }
797
798 /**
799 * @brief Provides access to the data contained in the %string.
800 * @param n The index of the character to access.
801 * @return Read-only (const) reference to the character.
802 * @throw std::out_of_range If @a n is an invalid index.
803 *
804 * This function provides for safer data access. The parameter is
805 * first checked that it is in the range of the string. The function
806 * throws out_of_range if the check fails.
807 */
808 const_reference
809 at(size_type __n) const
810 {
811 if (__n >= this->size())
812 __throw_out_of_range(__N("basic_string::at"));
813 return _M_data()[__n];
814 }
815
816 /**
817 * @brief Provides access to the data contained in the %string.
818 * @param n The index of the character to access.
819 * @return Read/write reference to the character.
820 * @throw std::out_of_range If @a n is an invalid index.
821 *
822 * This function provides for safer data access. The parameter is
823 * first checked that it is in the range of the string. The function
824 * throws out_of_range if the check fails. Success results in
825 * unsharing the string.
826 */
827 reference
828 at(size_type __n)
829 {
830 if (__n >= size())
831 __throw_out_of_range(__N("basic_string::at"));
832 _M_leak();
833 return _M_data()[__n];
834 }
835
836 // Modifiers:
837 /**
838 * @brief Append a string to this string.
839 * @param str The string to append.
840 * @return Reference to this string.
841 */
842 basic_string&
843 operator+=(const basic_string& __str)
844 { return this->append(__str); }
845
846 /**
847 * @brief Append a C string.
848 * @param s The C string to append.
849 * @return Reference to this string.
850 */
851 basic_string&
852 operator+=(const _CharT* __s)
853 { return this->append(__s); }
854
855 /**
856 * @brief Append a character.
857 * @param c The character to append.
858 * @return Reference to this string.
859 */
860 basic_string&
861 operator+=(_CharT __c)
862 {
863 this->push_back(__c);
864 return *this;
865 }
866
867 #ifdef __GXX_EXPERIMENTAL_CXX0X__
868 /**
869 * @brief Append an initializer_list of characters.
870 * @param l The initializer_list of characters to be appended.
871 * @return Reference to this string.
872 */
873 basic_string&
874 operator+=(initializer_list<_CharT> __l)
875 { return this->append(__l.begin(), __l.size()); }
876 #endif // __GXX_EXPERIMENTAL_CXX0X__
877
878 /**
879 * @brief Append a string to this string.
880 * @param str The string to append.
881 * @return Reference to this string.
882 */
883 basic_string&
884 append(const basic_string& __str);
885
886 /**
887 * @brief Append a substring.
888 * @param str The string to append.
889 * @param pos Index of the first character of str to append.
890 * @param n The number of characters to append.
891 * @return Reference to this string.
892 * @throw std::out_of_range if @a pos is not a valid index.
893 *
894 * This function appends @a n characters from @a str starting at @a pos
895 * to this string. If @a n is is larger than the number of available
896 * characters in @a str, the remainder of @a str is appended.
897 */
898 basic_string&
899 append(const basic_string& __str, size_type __pos, size_type __n);
900
901 /**
902 * @brief Append a C substring.
903 * @param s The C string to append.
904 * @param n The number of characters to append.
905 * @return Reference to this string.
906 */
907 basic_string&
908 append(const _CharT* __s, size_type __n);
909
910 /**
911 * @brief Append a C string.
912 * @param s The C string to append.
913 * @return Reference to this string.
914 */
915 basic_string&
916 append(const _CharT* __s)
917 {
918 __glibcxx_requires_string(__s);
919 return this->append(__s, traits_type::length(__s));
920 }
921
922 /**
923 * @brief Append multiple characters.
924 * @param n The number of characters to append.
925 * @param c The character to use.
926 * @return Reference to this string.
927 *
928 * Appends n copies of c to this string.
929 */
930 basic_string&
931 append(size_type __n, _CharT __c);
932
933 #ifdef __GXX_EXPERIMENTAL_CXX0X__
934 /**
935 * @brief Append an initializer_list of characters.
936 * @param l The initializer_list of characters to append.
937 * @return Reference to this string.
938 */
939 basic_string&
940 append(initializer_list<_CharT> __l)
941 { return this->append(__l.begin(), __l.size()); }
942 #endif // __GXX_EXPERIMENTAL_CXX0X__
943
944 /**
945 * @brief Append a range of characters.
946 * @param first Iterator referencing the first character to append.
947 * @param last Iterator marking the end of the range.
948 * @return Reference to this string.
949 *
950 * Appends characters in the range [first,last) to this string.
951 */
952 template<class _InputIterator>
953 basic_string&
954 append(_InputIterator __first, _InputIterator __last)
955 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
956
957 /**
958 * @brief Append a single character.
959 * @param c Character to append.
960 */
961 void
962 push_back(_CharT __c)
963 {
964 const size_type __len = 1 + this->size();
965 if (__len > this->capacity() || _M_rep()->_M_is_shared())
966 this->reserve(__len);
967 traits_type::assign(_M_data()[this->size()], __c);
968 _M_rep()->_M_set_length_and_sharable(__len);
969 }
970
971 /**
972 * @brief Set value to contents of another string.
973 * @param str Source string to use.
974 * @return Reference to this string.
975 */
976 basic_string&
977 assign(const basic_string& __str);
978
979 /**
980 * @brief Set value to a substring of a string.
981 * @param str The string to use.
982 * @param pos Index of the first character of str.
983 * @param n Number of characters to use.
984 * @return Reference to this string.
985 * @throw std::out_of_range if @a pos is not a valid index.
986 *
987 * This function sets this string to the substring of @a str consisting
988 * of @a n characters at @a pos. If @a n is is larger than the number
989 * of available characters in @a str, the remainder of @a str is used.
990 */
991 basic_string&
992 assign(const basic_string& __str, size_type __pos, size_type __n)
993 { return this->assign(__str._M_data()
994 + __str._M_check(__pos, "basic_string::assign"),
995 __str._M_limit(__pos, __n)); }
996
997 /**
998 * @brief Set value to a C substring.
999 * @param s The C string to use.
1000 * @param n Number of characters to use.
1001 * @return Reference to this string.
1002 *
1003 * This function sets the value of this string to the first @a n
1004 * characters of @a s. If @a n is is larger than the number of
1005 * available characters in @a s, the remainder of @a s is used.
1006 */
1007 basic_string&
1008 assign(const _CharT* __s, size_type __n);
1009
1010 /**
1011 * @brief Set value to contents of a C string.
1012 * @param s The C string to use.
1013 * @return Reference to this string.
1014 *
1015 * This function sets the value of this string to the value of @a s.
1016 * The data is copied, so there is no dependence on @a s once the
1017 * function returns.
1018 */
1019 basic_string&
1020 assign(const _CharT* __s)
1021 {
1022 __glibcxx_requires_string(__s);
1023 return this->assign(__s, traits_type::length(__s));
1024 }
1025
1026 /**
1027 * @brief Set value to multiple characters.
1028 * @param n Length of the resulting string.
1029 * @param c The character to use.
1030 * @return Reference to this string.
1031 *
1032 * This function sets the value of this string to @a n copies of
1033 * character @a c.
1034 */
1035 basic_string&
1036 assign(size_type __n, _CharT __c)
1037 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1038
1039 /**
1040 * @brief Set value to a range of characters.
1041 * @param first Iterator referencing the first character to append.
1042 * @param last Iterator marking the end of the range.
1043 * @return Reference to this string.
1044 *
1045 * Sets value of string to characters in the range [first,last).
1046 */
1047 template<class _InputIterator>
1048 basic_string&
1049 assign(_InputIterator __first, _InputIterator __last)
1050 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
1051
1052 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1053 /**
1054 * @brief Set value to an initializer_list of characters.
1055 * @param l The initializer_list of characters to assign.
1056 * @return Reference to this string.
1057 */
1058 basic_string&
1059 assign(initializer_list<_CharT> __l)
1060 { return this->assign(__l.begin(), __l.size()); }
1061 #endif // __GXX_EXPERIMENTAL_CXX0X__
1062
1063 /**
1064 * @brief Insert multiple characters.
1065 * @param p Iterator referencing location in string to insert at.
1066 * @param n Number of characters to insert
1067 * @param c The character to insert.
1068 * @throw std::length_error If new length exceeds @c max_size().
1069 *
1070 * Inserts @a n copies of character @a c starting at the position
1071 * referenced by iterator @a p. If adding characters causes the length
1072 * to exceed max_size(), length_error is thrown. The value of the
1073 * string doesn't change if an error is thrown.
1074 */
1075 void
1076 insert(iterator __p, size_type __n, _CharT __c)
1077 { this->replace(__p, __p, __n, __c); }
1078
1079 /**
1080 * @brief Insert a range of characters.
1081 * @param p Iterator referencing location in string to insert at.
1082 * @param beg Start of range.
1083 * @param end End of range.
1084 * @throw std::length_error If new length exceeds @c max_size().
1085 *
1086 * Inserts characters in range [beg,end). If adding characters causes
1087 * the length to exceed max_size(), length_error is thrown. The value
1088 * of the string doesn't change if an error is thrown.
1089 */
1090 template<class _InputIterator>
1091 void
1092 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1093 { this->replace(__p, __p, __beg, __end); }
1094
1095 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1096 /**
1097 * @brief Insert an initializer_list of characters.
1098 * @param p Iterator referencing location in string to insert at.
1099 * @param l The initializer_list of characters to insert.
1100 * @throw std::length_error If new length exceeds @c max_size().
1101 */
1102 void
1103 insert(iterator __p, initializer_list<_CharT> __l)
1104 {
1105 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1106 this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
1107 }
1108 #endif // __GXX_EXPERIMENTAL_CXX0X__
1109
1110 /**
1111 * @brief Insert value of a string.
1112 * @param pos1 Iterator referencing location in string to insert at.
1113 * @param str The string to insert.
1114 * @return Reference to this string.
1115 * @throw std::length_error If new length exceeds @c max_size().
1116 *
1117 * Inserts value of @a str starting at @a pos1. If adding characters
1118 * causes the length to exceed max_size(), length_error is thrown. The
1119 * value of the string doesn't change if an error is thrown.
1120 */
1121 basic_string&
1122 insert(size_type __pos1, const basic_string& __str)
1123 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1124
1125 /**
1126 * @brief Insert a substring.
1127 * @param pos1 Iterator referencing location in string to insert at.
1128 * @param str The string to insert.
1129 * @param pos2 Start of characters in str to insert.
1130 * @param n Number of characters to insert.
1131 * @return Reference to this string.
1132 * @throw std::length_error If new length exceeds @c max_size().
1133 * @throw std::out_of_range If @a pos1 > size() or
1134 * @a pos2 > @a str.size().
1135 *
1136 * Starting at @a pos1, insert @a n character of @a str beginning with
1137 * @a pos2. If adding characters causes the length to exceed
1138 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
1139 * this string or @a pos2 is beyond the end of @a str, out_of_range is
1140 * thrown. The value of the string doesn't change if an error is
1141 * thrown.
1142 */
1143 basic_string&
1144 insert(size_type __pos1, const basic_string& __str,
1145 size_type __pos2, size_type __n)
1146 { return this->insert(__pos1, __str._M_data()
1147 + __str._M_check(__pos2, "basic_string::insert"),
1148 __str._M_limit(__pos2, __n)); }
1149
1150 /**
1151 * @brief Insert a C substring.
1152 * @param pos Iterator referencing location in string to insert at.
1153 * @param s The C string to insert.
1154 * @param n The number of characters to insert.
1155 * @return Reference to this string.
1156 * @throw std::length_error If new length exceeds @c max_size().
1157 * @throw std::out_of_range If @a pos is beyond the end of this
1158 * string.
1159 *
1160 * Inserts the first @a n characters of @a s starting at @a pos. If
1161 * adding characters causes the length to exceed max_size(),
1162 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1163 * thrown. The value of the string doesn't change if an error is
1164 * thrown.
1165 */
1166 basic_string&
1167 insert(size_type __pos, const _CharT* __s, size_type __n);
1168
1169 /**
1170 * @brief Insert a C string.
1171 * @param pos Iterator referencing location in string to insert at.
1172 * @param s The C string to insert.
1173 * @return Reference to this string.
1174 * @throw std::length_error If new length exceeds @c max_size().
1175 * @throw std::out_of_range If @a pos is beyond the end of this
1176 * string.
1177 *
1178 * Inserts the first @a n characters of @a s starting at @a pos. If
1179 * adding characters causes the length to exceed max_size(),
1180 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1181 * thrown. The value of the string doesn't change if an error is
1182 * thrown.
1183 */
1184 basic_string&
1185 insert(size_type __pos, const _CharT* __s)
1186 {
1187 __glibcxx_requires_string(__s);
1188 return this->insert(__pos, __s, traits_type::length(__s));
1189 }
1190
1191 /**
1192 * @brief Insert multiple characters.
1193 * @param pos Index in string to insert at.
1194 * @param n Number of characters to insert
1195 * @param c The character to insert.
1196 * @return Reference to this string.
1197 * @throw std::length_error If new length exceeds @c max_size().
1198 * @throw std::out_of_range If @a pos is beyond the end of this
1199 * string.
1200 *
1201 * Inserts @a n copies of character @a c starting at index @a pos. If
1202 * adding characters causes the length to exceed max_size(),
1203 * length_error is thrown. If @a pos > length(), out_of_range is
1204 * thrown. The value of the string doesn't change if an error is
1205 * thrown.
1206 */
1207 basic_string&
1208 insert(size_type __pos, size_type __n, _CharT __c)
1209 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1210 size_type(0), __n, __c); }
1211
1212 /**
1213 * @brief Insert one character.
1214 * @param p Iterator referencing position in string to insert at.
1215 * @param c The character to insert.
1216 * @return Iterator referencing newly inserted char.
1217 * @throw std::length_error If new length exceeds @c max_size().
1218 *
1219 * Inserts character @a c at position referenced by @a p. If adding
1220 * character causes the length to exceed max_size(), length_error is
1221 * thrown. If @a p is beyond end of string, out_of_range is thrown.
1222 * The value of the string doesn't change if an error is thrown.
1223 */
1224 iterator
1225 insert(iterator __p, _CharT __c)
1226 {
1227 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1228 const size_type __pos = __p - _M_ibegin();
1229 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1230 _M_rep()->_M_set_leaked();
1231 return iterator(_M_data() + __pos);
1232 }
1233
1234 /**
1235 * @brief Remove characters.
1236 * @param pos Index of first character to remove (default 0).
1237 * @param n Number of characters to remove (default remainder).
1238 * @return Reference to this string.
1239 * @throw std::out_of_range If @a pos is beyond the end of this
1240 * string.
1241 *
1242 * Removes @a n characters from this string starting at @a pos. The
1243 * length of the string is reduced by @a n. If there are < @a n
1244 * characters to remove, the remainder of the string is truncated. If
1245 * @a p is beyond end of string, out_of_range is thrown. The value of
1246 * the string doesn't change if an error is thrown.
1247 */
1248 basic_string&
1249 erase(size_type __pos = 0, size_type __n = npos)
1250 {
1251 _M_mutate(_M_check(__pos, "basic_string::erase"),
1252 _M_limit(__pos, __n), size_type(0));
1253 return *this;
1254 }
1255
1256 /**
1257 * @brief Remove one character.
1258 * @param position Iterator referencing the character to remove.
1259 * @return iterator referencing same location after removal.
1260 *
1261 * Removes the character at @a position from this string. The value
1262 * of the string doesn't change if an error is thrown.
1263 */
1264 iterator
1265 erase(iterator __position)
1266 {
1267 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1268 && __position < _M_iend());
1269 const size_type __pos = __position - _M_ibegin();
1270 _M_mutate(__pos, size_type(1), size_type(0));
1271 _M_rep()->_M_set_leaked();
1272 return iterator(_M_data() + __pos);
1273 }
1274
1275 /**
1276 * @brief Remove a range of characters.
1277 * @param first Iterator referencing the first character to remove.
1278 * @param last Iterator referencing the end of the range.
1279 * @return Iterator referencing location of first after removal.
1280 *
1281 * Removes the characters in the range [first,last) from this string.
1282 * The value of the string doesn't change if an error is thrown.
1283 */
1284 iterator
1285 erase(iterator __first, iterator __last);
1286
1287 /**
1288 * @brief Replace characters with value from another string.
1289 * @param pos Index of first character to replace.
1290 * @param n Number of characters to be replaced.
1291 * @param str String to insert.
1292 * @return Reference to this string.
1293 * @throw std::out_of_range If @a pos is beyond the end of this
1294 * string.
1295 * @throw std::length_error If new length exceeds @c max_size().
1296 *
1297 * Removes the characters in the range [pos,pos+n) from this string.
1298 * In place, the value of @a str is inserted. If @a pos is beyond end
1299 * of string, out_of_range is thrown. If the length of the result
1300 * exceeds max_size(), length_error is thrown. The value of the string
1301 * doesn't change if an error is thrown.
1302 */
1303 basic_string&
1304 replace(size_type __pos, size_type __n, const basic_string& __str)
1305 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1306
1307 /**
1308 * @brief Replace characters with value from another string.
1309 * @param pos1 Index of first character to replace.
1310 * @param n1 Number of characters to be replaced.
1311 * @param str String to insert.
1312 * @param pos2 Index of first character of str to use.
1313 * @param n2 Number of characters from str to use.
1314 * @return Reference to this string.
1315 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1316 * str.size().
1317 * @throw std::length_error If new length exceeds @c max_size().
1318 *
1319 * Removes the characters in the range [pos1,pos1 + n) from this
1320 * string. In place, the value of @a str is inserted. If @a pos is
1321 * beyond end of string, out_of_range is thrown. If the length of the
1322 * result exceeds max_size(), length_error is thrown. The value of the
1323 * string doesn't change if an error is thrown.
1324 */
1325 basic_string&
1326 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1327 size_type __pos2, size_type __n2)
1328 { return this->replace(__pos1, __n1, __str._M_data()
1329 + __str._M_check(__pos2, "basic_string::replace"),
1330 __str._M_limit(__pos2, __n2)); }
1331
1332 /**
1333 * @brief Replace characters with value of a C substring.
1334 * @param pos Index of first character to replace.
1335 * @param n1 Number of characters to be replaced.
1336 * @param s C string to insert.
1337 * @param n2 Number of characters from @a s to use.
1338 * @return Reference to this string.
1339 * @throw std::out_of_range If @a pos1 > size().
1340 * @throw std::length_error If new length exceeds @c max_size().
1341 *
1342 * Removes the characters in the range [pos,pos + n1) from this string.
1343 * In place, the first @a n2 characters of @a s are inserted, or all
1344 * of @a s if @a n2 is too large. If @a pos is beyond end of string,
1345 * out_of_range is thrown. If the length of result exceeds max_size(),
1346 * length_error is thrown. The value of the string doesn't change if
1347 * an error is thrown.
1348 */
1349 basic_string&
1350 replace(size_type __pos, size_type __n1, const _CharT* __s,
1351 size_type __n2);
1352
1353 /**
1354 * @brief Replace characters with value of a C string.
1355 * @param pos Index of first character to replace.
1356 * @param n1 Number of characters to be replaced.
1357 * @param s C string to insert.
1358 * @return Reference to this string.
1359 * @throw std::out_of_range If @a pos > size().
1360 * @throw std::length_error If new length exceeds @c max_size().
1361 *
1362 * Removes the characters in the range [pos,pos + n1) from this string.
1363 * In place, the first @a n characters of @a s are inserted. If @a
1364 * pos is beyond end of string, out_of_range is thrown. If the length
1365 * of result exceeds max_size(), length_error is thrown. The value of
1366 * the string doesn't change if an error is thrown.
1367 */
1368 basic_string&
1369 replace(size_type __pos, size_type __n1, const _CharT* __s)
1370 {
1371 __glibcxx_requires_string(__s);
1372 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1373 }
1374
1375 /**
1376 * @brief Replace characters with multiple characters.
1377 * @param pos Index of first character to replace.
1378 * @param n1 Number of characters to be replaced.
1379 * @param n2 Number of characters to insert.
1380 * @param c Character to insert.
1381 * @return Reference to this string.
1382 * @throw std::out_of_range If @a pos > size().
1383 * @throw std::length_error If new length exceeds @c max_size().
1384 *
1385 * Removes the characters in the range [pos,pos + n1) from this string.
1386 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1387 * end of string, out_of_range is thrown. If the length of result
1388 * exceeds max_size(), length_error is thrown. The value of the string
1389 * doesn't change if an error is thrown.
1390 */
1391 basic_string&
1392 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1393 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1394 _M_limit(__pos, __n1), __n2, __c); }
1395
1396 /**
1397 * @brief Replace range of characters with string.
1398 * @param i1 Iterator referencing start of range to replace.
1399 * @param i2 Iterator referencing end of range to replace.
1400 * @param str String value to insert.
1401 * @return Reference to this string.
1402 * @throw std::length_error If new length exceeds @c max_size().
1403 *
1404 * Removes the characters in the range [i1,i2). In place, the value of
1405 * @a str is inserted. If the length of result exceeds max_size(),
1406 * length_error is thrown. The value of the string doesn't change if
1407 * an error is thrown.
1408 */
1409 basic_string&
1410 replace(iterator __i1, iterator __i2, const basic_string& __str)
1411 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1412
1413 /**
1414 * @brief Replace range of characters with C substring.
1415 * @param i1 Iterator referencing start of range to replace.
1416 * @param i2 Iterator referencing end of range to replace.
1417 * @param s C string value to insert.
1418 * @param n Number of characters from s to insert.
1419 * @return Reference to this string.
1420 * @throw std::length_error If new length exceeds @c max_size().
1421 *
1422 * Removes the characters in the range [i1,i2). In place, the first @a
1423 * n characters of @a s are inserted. If the length of result exceeds
1424 * max_size(), length_error is thrown. The value of the string doesn't
1425 * change if an error is thrown.
1426 */
1427 basic_string&
1428 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1429 {
1430 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1431 && __i2 <= _M_iend());
1432 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1433 }
1434
1435 /**
1436 * @brief Replace range of characters with C string.
1437 * @param i1 Iterator referencing start of range to replace.
1438 * @param i2 Iterator referencing end of range to replace.
1439 * @param s C string value to insert.
1440 * @return Reference to this string.
1441 * @throw std::length_error If new length exceeds @c max_size().
1442 *
1443 * Removes the characters in the range [i1,i2). In place, the
1444 * characters of @a s are inserted. If the length of result exceeds
1445 * max_size(), length_error is thrown. The value of the string doesn't
1446 * change if an error is thrown.
1447 */
1448 basic_string&
1449 replace(iterator __i1, iterator __i2, const _CharT* __s)
1450 {
1451 __glibcxx_requires_string(__s);
1452 return this->replace(__i1, __i2, __s, traits_type::length(__s));
1453 }
1454
1455 /**
1456 * @brief Replace range of characters with multiple characters
1457 * @param i1 Iterator referencing start of range to replace.
1458 * @param i2 Iterator referencing end of range to replace.
1459 * @param n Number of characters to insert.
1460 * @param c Character to insert.
1461 * @return Reference to this string.
1462 * @throw std::length_error If new length exceeds @c max_size().
1463 *
1464 * Removes the characters in the range [i1,i2). In place, @a n copies
1465 * of @a c are inserted. If the length of result exceeds max_size(),
1466 * length_error is thrown. The value of the string doesn't change if
1467 * an error is thrown.
1468 */
1469 basic_string&
1470 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1471 {
1472 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1473 && __i2 <= _M_iend());
1474 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1475 }
1476
1477 /**
1478 * @brief Replace range of characters with range.
1479 * @param i1 Iterator referencing start of range to replace.
1480 * @param i2 Iterator referencing end of range to replace.
1481 * @param k1 Iterator referencing start of range to insert.
1482 * @param k2 Iterator referencing end of range to insert.
1483 * @return Reference to this string.
1484 * @throw std::length_error If new length exceeds @c max_size().
1485 *
1486 * Removes the characters in the range [i1,i2). In place, characters
1487 * in the range [k1,k2) are inserted. If the length of result exceeds
1488 * max_size(), length_error is thrown. The value of the string doesn't
1489 * change if an error is thrown.
1490 */
1491 template<class _InputIterator>
1492 basic_string&
1493 replace(iterator __i1, iterator __i2,
1494 _InputIterator __k1, _InputIterator __k2)
1495 {
1496 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1497 && __i2 <= _M_iend());
1498 __glibcxx_requires_valid_range(__k1, __k2);
1499 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1500 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1501 }
1502
1503 // Specializations for the common case of pointer and iterator:
1504 // useful to avoid the overhead of temporary buffering in _M_replace.
1505 basic_string&
1506 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1507 {
1508 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1509 && __i2 <= _M_iend());
1510 __glibcxx_requires_valid_range(__k1, __k2);
1511 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1512 __k1, __k2 - __k1);
1513 }
1514
1515 basic_string&
1516 replace(iterator __i1, iterator __i2,
1517 const _CharT* __k1, const _CharT* __k2)
1518 {
1519 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1520 && __i2 <= _M_iend());
1521 __glibcxx_requires_valid_range(__k1, __k2);
1522 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1523 __k1, __k2 - __k1);
1524 }
1525
1526 basic_string&
1527 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1528 {
1529 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1530 && __i2 <= _M_iend());
1531 __glibcxx_requires_valid_range(__k1, __k2);
1532 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1533 __k1.base(), __k2 - __k1);
1534 }
1535
1536 basic_string&
1537 replace(iterator __i1, iterator __i2,
1538 const_iterator __k1, const_iterator __k2)
1539 {
1540 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1541 && __i2 <= _M_iend());
1542 __glibcxx_requires_valid_range(__k1, __k2);
1543 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1544 __k1.base(), __k2 - __k1);
1545 }
1546
1547 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1548 /**
1549 * @brief Replace range of characters with initializer_list.
1550 * @param i1 Iterator referencing start of range to replace.
1551 * @param i2 Iterator referencing end of range to replace.
1552 * @param l The initializer_list of characters to insert.
1553 * @return Reference to this string.
1554 * @throw std::length_error If new length exceeds @c max_size().
1555 *
1556 * Removes the characters in the range [i1,i2). In place, characters
1557 * in the range [k1,k2) are inserted. If the length of result exceeds
1558 * max_size(), length_error is thrown. The value of the string doesn't
1559 * change if an error is thrown.
1560 */
1561 basic_string& replace(iterator __i1, iterator __i2,
1562 initializer_list<_CharT> __l)
1563 { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
1564 #endif // __GXX_EXPERIMENTAL_CXX0X__
1565
1566 private:
1567 template<class _Integer>
1568 basic_string&
1569 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1570 _Integer __val, __true_type)
1571 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1572
1573 template<class _InputIterator>
1574 basic_string&
1575 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1576 _InputIterator __k2, __false_type);
1577
1578 basic_string&
1579 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1580 _CharT __c);
1581
1582 basic_string&
1583 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1584 size_type __n2);
1585
1586 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1587 // requires special behaviour if _InIter is an integral type
1588 template<class _InIterator>
1589 static _CharT*
1590 _S_construct_aux(_InIterator __beg, _InIterator __end,
1591 const _Alloc& __a, __false_type)
1592 {
1593 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1594 return _S_construct(__beg, __end, __a, _Tag());
1595 }
1596
1597 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1598 // 438. Ambiguity in the "do the right thing" clause
1599 template<class _Integer>
1600 static _CharT*
1601 _S_construct_aux(_Integer __beg, _Integer __end,
1602 const _Alloc& __a, __true_type)
1603 { return _S_construct_aux_2(static_cast<size_type>(__beg),
1604 __end, __a); }
1605
1606 static _CharT*
1607 _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
1608 { return _S_construct(__req, __c, __a); }
1609
1610 template<class _InIterator>
1611 static _CharT*
1612 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1613 {
1614 typedef typename std::__is_integer<_InIterator>::__type _Integral;
1615 return _S_construct_aux(__beg, __end, __a, _Integral());
1616 }
1617
1618 // For Input Iterators, used in istreambuf_iterators, etc.
1619 template<class _InIterator>
1620 static _CharT*
1621 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1622 input_iterator_tag);
1623
1624 // For forward_iterators up to random_access_iterators, used for
1625 // string::iterator, _CharT*, etc.
1626 template<class _FwdIterator>
1627 static _CharT*
1628 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1629 forward_iterator_tag);
1630
1631 static _CharT*
1632 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1633
1634 public:
1635
1636 /**
1637 * @brief Copy substring into C string.
1638 * @param s C string to copy value into.
1639 * @param n Number of characters to copy.
1640 * @param pos Index of first character to copy.
1641 * @return Number of characters actually copied
1642 * @throw std::out_of_range If pos > size().
1643 *
1644 * Copies up to @a n characters starting at @a pos into the C string @a
1645 * s. If @a pos is greater than size(), out_of_range is thrown.
1646 */
1647 size_type
1648 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1649
1650 /**
1651 * @brief Swap contents with another string.
1652 * @param s String to swap with.
1653 *
1654 * Exchanges the contents of this string with that of @a s in constant
1655 * time.
1656 */
1657 void
1658 swap(basic_string& __s);
1659
1660 // String operations:
1661 /**
1662 * @brief Return const pointer to null-terminated contents.
1663 *
1664 * This is a handle to internal data. Do not modify or dire things may
1665 * happen.
1666 */
1667 const _CharT*
1668 c_str() const
1669 { return _M_data(); }
1670
1671 /**
1672 * @brief Return const pointer to contents.
1673 *
1674 * This is a handle to internal data. Do not modify or dire things may
1675 * happen.
1676 */
1677 const _CharT*
1678 data() const
1679 { return _M_data(); }
1680
1681 /**
1682 * @brief Return copy of allocator used to construct this string.
1683 */
1684 allocator_type
1685 get_allocator() const
1686 { return _M_dataplus; }
1687
1688 /**
1689 * @brief Find position of a C substring.
1690 * @param s C string to locate.
1691 * @param pos Index of character to search from.
1692 * @param n Number of characters from @a s to search for.
1693 * @return Index of start of first occurrence.
1694 *
1695 * Starting from @a pos, searches forward for the first @a n characters
1696 * in @a s within this string. If found, returns the index where it
1697 * begins. If not found, returns npos.
1698 */
1699 size_type
1700 find(const _CharT* __s, size_type __pos, size_type __n) const;
1701
1702 /**
1703 * @brief Find position of a string.
1704 * @param str String to locate.
1705 * @param pos Index of character to search from (default 0).
1706 * @return Index of start of first occurrence.
1707 *
1708 * Starting from @a pos, searches forward for value of @a str within
1709 * this string. If found, returns the index where it begins. If not
1710 * found, returns npos.
1711 */
1712 size_type
1713 find(const basic_string& __str, size_type __pos = 0) const
1714 { return this->find(__str.data(), __pos, __str.size()); }
1715
1716 /**
1717 * @brief Find position of a C string.
1718 * @param s C string to locate.
1719 * @param pos Index of character to search from (default 0).
1720 * @return Index of start of first occurrence.
1721 *
1722 * Starting from @a pos, searches forward for the value of @a s within
1723 * this string. If found, returns the index where it begins. If not
1724 * found, returns npos.
1725 */
1726 size_type
1727 find(const _CharT* __s, size_type __pos = 0) const
1728 {
1729 __glibcxx_requires_string(__s);
1730 return this->find(__s, __pos, traits_type::length(__s));
1731 }
1732
1733 /**
1734 * @brief Find position of a character.
1735 * @param c Character to locate.
1736 * @param pos Index of character to search from (default 0).
1737 * @return Index of first occurrence.
1738 *
1739 * Starting from @a pos, searches forward for @a c within this string.
1740 * If found, returns the index where it was found. If not found,
1741 * returns npos.
1742 */
1743 size_type
1744 find(_CharT __c, size_type __pos = 0) const;
1745
1746 /**
1747 * @brief Find last position of a string.
1748 * @param str String to locate.
1749 * @param pos Index of character to search back from (default end).
1750 * @return Index of start of last occurrence.
1751 *
1752 * Starting from @a pos, searches backward for value of @a str within
1753 * this string. If found, returns the index where it begins. If not
1754 * found, returns npos.
1755 */
1756 size_type
1757 rfind(const basic_string& __str, size_type __pos = npos) const
1758 { return this->rfind(__str.data(), __pos, __str.size()); }
1759
1760 /**
1761 * @brief Find last position of a C substring.
1762 * @param s C string to locate.
1763 * @param pos Index of character to search back from.
1764 * @param n Number of characters from s to search for.
1765 * @return Index of start of last occurrence.
1766 *
1767 * Starting from @a pos, searches backward for the first @a n
1768 * characters in @a s within this string. If found, returns the index
1769 * where it begins. If not found, returns npos.
1770 */
1771 size_type
1772 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1773
1774 /**
1775 * @brief Find last position of a C string.
1776 * @param s C string to locate.
1777 * @param pos Index of character to start search at (default end).
1778 * @return Index of start of last occurrence.
1779 *
1780 * Starting from @a pos, searches backward for the value of @a s within
1781 * this string. If found, returns the index where it begins. If not
1782 * found, returns npos.
1783 */
1784 size_type
1785 rfind(const _CharT* __s, size_type __pos = npos) const
1786 {
1787 __glibcxx_requires_string(__s);
1788 return this->rfind(__s, __pos, traits_type::length(__s));
1789 }
1790
1791 /**
1792 * @brief Find last position of a character.
1793 * @param c Character to locate.
1794 * @param pos Index of character to search back from (default end).
1795 * @return Index of last occurrence.
1796 *
1797 * Starting from @a pos, searches backward for @a c within this string.
1798 * If found, returns the index where it was found. If not found,
1799 * returns npos.
1800 */
1801 size_type
1802 rfind(_CharT __c, size_type __pos = npos) const;
1803
1804 /**
1805 * @brief Find position of a character of string.
1806 * @param str String containing characters to locate.
1807 * @param pos Index of character to search from (default 0).
1808 * @return Index of first occurrence.
1809 *
1810 * Starting from @a pos, searches forward for one of the characters of
1811 * @a str within this string. If found, returns the index where it was
1812 * found. If not found, returns npos.
1813 */
1814 size_type
1815 find_first_of(const basic_string& __str, size_type __pos = 0) const
1816 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1817
1818 /**
1819 * @brief Find position of a character of C substring.
1820 * @param s String containing characters to locate.
1821 * @param pos Index of character to search from.
1822 * @param n Number of characters from s to search for.
1823 * @return Index of first occurrence.
1824 *
1825 * Starting from @a pos, searches forward for one of the first @a n
1826 * characters of @a s within this string. If found, returns the index
1827 * where it was found. If not found, returns npos.
1828 */
1829 size_type
1830 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1831
1832 /**
1833 * @brief Find position of a character of C string.
1834 * @param s String containing characters to locate.
1835 * @param pos Index of character to search from (default 0).
1836 * @return Index of first occurrence.
1837 *
1838 * Starting from @a pos, searches forward for one of the characters of
1839 * @a s within this string. If found, returns the index where it was
1840 * found. If not found, returns npos.
1841 */
1842 size_type
1843 find_first_of(const _CharT* __s, size_type __pos = 0) const
1844 {
1845 __glibcxx_requires_string(__s);
1846 return this->find_first_of(__s, __pos, traits_type::length(__s));
1847 }
1848
1849 /**
1850 * @brief Find position of a character.
1851 * @param c Character to locate.
1852 * @param pos Index of character to search from (default 0).
1853 * @return Index of first occurrence.
1854 *
1855 * Starting from @a pos, searches forward for the character @a c within
1856 * this string. If found, returns the index where it was found. If
1857 * not found, returns npos.
1858 *
1859 * Note: equivalent to find(c, pos).
1860 */
1861 size_type
1862 find_first_of(_CharT __c, size_type __pos = 0) const
1863 { return this->find(__c, __pos); }
1864
1865 /**
1866 * @brief Find last position of a character of string.
1867 * @param str String containing characters to locate.
1868 * @param pos Index of character to search back from (default end).
1869 * @return Index of last occurrence.
1870 *
1871 * Starting from @a pos, searches backward for one of the characters of
1872 * @a str within this string. If found, returns the index where it was
1873 * found. If not found, returns npos.
1874 */
1875 size_type
1876 find_last_of(const basic_string& __str, size_type __pos = npos) const
1877 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1878
1879 /**
1880 * @brief Find last position of a character of C substring.
1881 * @param s C string containing characters to locate.
1882 * @param pos Index of character to search back from.
1883 * @param n Number of characters from s to search for.
1884 * @return Index of last occurrence.
1885 *
1886 * Starting from @a pos, searches backward for one of the first @a n
1887 * characters of @a s within this string. If found, returns the index
1888 * where it was found. If not found, returns npos.
1889 */
1890 size_type
1891 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1892
1893 /**
1894 * @brief Find last position of a character of C string.
1895 * @param s C string containing characters to locate.
1896 * @param pos Index of character to search back from (default end).
1897 * @return Index of last occurrence.
1898 *
1899 * Starting from @a pos, searches backward for one of the characters of
1900 * @a s within this string. If found, returns the index where it was
1901 * found. If not found, returns npos.
1902 */
1903 size_type
1904 find_last_of(const _CharT* __s, size_type __pos = npos) const
1905 {
1906 __glibcxx_requires_string(__s);
1907 return this->find_last_of(__s, __pos, traits_type::length(__s));
1908 }
1909
1910 /**
1911 * @brief Find last position of a character.
1912 * @param c Character to locate.
1913 * @param pos Index of character to search back from (default end).
1914 * @return Index of last occurrence.
1915 *
1916 * Starting from @a pos, searches backward for @a c within this string.
1917 * If found, returns the index where it was found. If not found,
1918 * returns npos.
1919 *
1920 * Note: equivalent to rfind(c, pos).
1921 */
1922 size_type
1923 find_last_of(_CharT __c, size_type __pos = npos) const
1924 { return this->rfind(__c, __pos); }
1925
1926 /**
1927 * @brief Find position of a character not in string.
1928 * @param str String containing characters to avoid.
1929 * @param pos Index of character to search from (default 0).
1930 * @return Index of first occurrence.
1931 *
1932 * Starting from @a pos, searches forward for a character not contained
1933 * in @a str within this string. If found, returns the index where it
1934 * was found. If not found, returns npos.
1935 */
1936 size_type
1937 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1938 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1939
1940 /**
1941 * @brief Find position of a character not in C substring.
1942 * @param s C string containing characters to avoid.
1943 * @param pos Index of character to search from.
1944 * @param n Number of characters from s to consider.
1945 * @return Index of first occurrence.
1946 *
1947 * Starting from @a pos, searches forward for a character not contained
1948 * in the first @a n characters of @a s within this string. If found,
1949 * returns the index where it was found. If not found, returns npos.
1950 */
1951 size_type
1952 find_first_not_of(const _CharT* __s, size_type __pos,
1953 size_type __n) const;
1954
1955 /**
1956 * @brief Find position of a character not in C string.
1957 * @param s C string containing characters to avoid.
1958 * @param pos Index of character to search from (default 0).
1959 * @return Index of first occurrence.
1960 *
1961 * Starting from @a pos, searches forward for a character not contained
1962 * in @a s within this string. If found, returns the index where it
1963 * was found. If not found, returns npos.
1964 */
1965 size_type
1966 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1967 {
1968 __glibcxx_requires_string(__s);
1969 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
1970 }
1971
1972 /**
1973 * @brief Find position of a different character.
1974 * @param c Character to avoid.
1975 * @param pos Index of character to search from (default 0).
1976 * @return Index of first occurrence.
1977 *
1978 * Starting from @a pos, searches forward for a character other than @a c
1979 * within this string. If found, returns the index where it was found.
1980 * If not found, returns npos.
1981 */
1982 size_type
1983 find_first_not_of(_CharT __c, size_type __pos = 0) const;
1984
1985 /**
1986 * @brief Find last position of a character not in string.
1987 * @param str String containing characters to avoid.
1988 * @param pos Index of character to search back from (default end).
1989 * @return Index of last occurrence.
1990 *
1991 * Starting from @a pos, searches backward for a character not
1992 * contained in @a str within this string. If found, returns the index
1993 * where it was found. If not found, returns npos.
1994 */
1995 size_type
1996 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1997 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1998
1999 /**
2000 * @brief Find last position of a character not in C substring.
2001 * @param s C string containing characters to avoid.
2002 * @param pos Index of character to search back from.
2003 * @param n Number of characters from s to consider.
2004 * @return Index of last occurrence.
2005 *
2006 * Starting from @a pos, searches backward for a character not
2007 * contained in the first @a n characters of @a s within this string.
2008 * If found, returns the index where it was found. If not found,
2009 * returns npos.
2010 */
2011 size_type
2012 find_last_not_of(const _CharT* __s, size_type __pos,
2013 size_type __n) const;
2014 /**
2015 * @brief Find last position of a character not in C string.
2016 * @param s C string containing characters to avoid.
2017 * @param pos Index of character to search back from (default end).
2018 * @return Index of last occurrence.
2019 *
2020 * Starting from @a pos, searches backward for a character not
2021 * contained in @a s within this string. If found, returns the index
2022 * where it was found. If not found, returns npos.
2023 */
2024 size_type
2025 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
2026 {
2027 __glibcxx_requires_string(__s);
2028 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
2029 }
2030
2031 /**
2032 * @brief Find last position of a different character.
2033 * @param c Character to avoid.
2034 * @param pos Index of character to search back from (default end).
2035 * @return Index of last occurrence.
2036 *
2037 * Starting from @a pos, searches backward for a character other than
2038 * @a c within this string. If found, returns the index where it was
2039 * found. If not found, returns npos.
2040 */
2041 size_type
2042 find_last_not_of(_CharT __c, size_type __pos = npos) const;
2043
2044 /**
2045 * @brief Get a substring.
2046 * @param pos Index of first character (default 0).
2047 * @param n Number of characters in substring (default remainder).
2048 * @return The new string.
2049 * @throw std::out_of_range If pos > size().
2050 *
2051 * Construct and return a new string using the @a n characters starting
2052 * at @a pos. If the string is too short, use the remainder of the
2053 * characters. If @a pos is beyond the end of the string, out_of_range
2054 * is thrown.
2055 */
2056 basic_string
2057 substr(size_type __pos = 0, size_type __n = npos) const
2058 { return basic_string(*this,
2059 _M_check(__pos, "basic_string::substr"), __n); }
2060
2061 /**
2062 * @brief Compare to a string.
2063 * @param str String to compare against.
2064 * @return Integer < 0, 0, or > 0.
2065 *
2066 * Returns an integer < 0 if this string is ordered before @a str, 0 if
2067 * their values are equivalent, or > 0 if this string is ordered after
2068 * @a str. Determines the effective length rlen of the strings to
2069 * compare as the smallest of size() and str.size(). The function
2070 * then compares the two strings by calling traits::compare(data(),
2071 * str.data(),rlen). If the result of the comparison is nonzero returns
2072 * it, otherwise the shorter one is ordered first.
2073 */
2074 int
2075 compare(const basic_string& __str) const
2076 {
2077 const size_type __size = this->size();
2078 const size_type __osize = __str.size();
2079 const size_type __len = std::min(__size, __osize);
2080
2081 int __r = traits_type::compare(_M_data(), __str.data(), __len);
2082 if (!__r)
2083 __r = _S_compare(__size, __osize);
2084 return __r;
2085 }
2086
2087 /**
2088 * @brief Compare substring to a string.
2089 * @param pos Index of first character of substring.
2090 * @param n Number of characters in substring.
2091 * @param str String to compare against.
2092 * @return Integer < 0, 0, or > 0.
2093 *
2094 * Form the substring of this string from the @a n characters starting
2095 * at @a pos. Returns an integer < 0 if the substring is ordered
2096 * before @a str, 0 if their values are equivalent, or > 0 if the
2097 * substring is ordered after @a str. Determines the effective length
2098 * rlen of the strings to compare as the smallest of the length of the
2099 * substring and @a str.size(). The function then compares the two
2100 * strings by calling traits::compare(substring.data(),str.data(),rlen).
2101 * If the result of the comparison is nonzero returns it, otherwise the
2102 * shorter one is ordered first.
2103 */
2104 int
2105 compare(size_type __pos, size_type __n, const basic_string& __str) const;
2106
2107 /**
2108 * @brief Compare substring to a substring.
2109 * @param pos1 Index of first character of substring.
2110 * @param n1 Number of characters in substring.
2111 * @param str String to compare against.
2112 * @param pos2 Index of first character of substring of str.
2113 * @param n2 Number of characters in substring of str.
2114 * @return Integer < 0, 0, or > 0.
2115 *
2116 * Form the substring of this string from the @a n1 characters starting
2117 * at @a pos1. Form the substring of @a str from the @a n2 characters
2118 * starting at @a pos2. Returns an integer < 0 if this substring is
2119 * ordered before the substring of @a str, 0 if their values are
2120 * equivalent, or > 0 if this substring is ordered after the substring
2121 * of @a str. Determines the effective length rlen of the strings
2122 * to compare as the smallest of the lengths of the substrings. The
2123 * function then compares the two strings by calling
2124 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2125 * If the result of the comparison is nonzero returns it, otherwise the
2126 * shorter one is ordered first.
2127 */
2128 int
2129 compare(size_type __pos1, size_type __n1, const basic_string& __str,
2130 size_type __pos2, size_type __n2) const;
2131
2132 /**
2133 * @brief Compare to a C string.
2134 * @param s C string to compare against.
2135 * @return Integer < 0, 0, or > 0.
2136 *
2137 * Returns an integer < 0 if this string is ordered before @a s, 0 if
2138 * their values are equivalent, or > 0 if this string is ordered after
2139 * @a s. Determines the effective length rlen of the strings to
2140 * compare as the smallest of size() and the length of a string
2141 * constructed from @a s. The function then compares the two strings
2142 * by calling traits::compare(data(),s,rlen). If the result of the
2143 * comparison is nonzero returns it, otherwise the shorter one is
2144 * ordered first.
2145 */
2146 int
2147 compare(const _CharT* __s) const;
2148
2149 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2150 // 5 String::compare specification questionable
2151 /**
2152 * @brief Compare substring to a C string.
2153 * @param pos Index of first character of substring.
2154 * @param n1 Number of characters in substring.
2155 * @param s C string to compare against.
2156 * @return Integer < 0, 0, or > 0.
2157 *
2158 * Form the substring of this string from the @a n1 characters starting
2159 * at @a pos. Returns an integer < 0 if the substring is ordered
2160 * before @a s, 0 if their values are equivalent, or > 0 if the
2161 * substring is ordered after @a s. Determines the effective length
2162 * rlen of the strings to compare as the smallest of the length of the
2163 * substring and the length of a string constructed from @a s. The
2164 * function then compares the two string by calling
2165 * traits::compare(substring.data(),s,rlen). If the result of the
2166 * comparison is nonzero returns it, otherwise the shorter one is
2167 * ordered first.
2168 */
2169 int
2170 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2171
2172 /**
2173 * @brief Compare substring against a character array.
2174 * @param pos1 Index of first character of substring.
2175 * @param n1 Number of characters in substring.
2176 * @param s character array to compare against.
2177 * @param n2 Number of characters of s.
2178 * @return Integer < 0, 0, or > 0.
2179 *
2180 * Form the substring of this string from the @a n1 characters starting
2181 * at @a pos1. Form a string from the first @a n2 characters of @a s.
2182 * Returns an integer < 0 if this substring is ordered before the string
2183 * from @a s, 0 if their values are equivalent, or > 0 if this substring
2184 * is ordered after the string from @a s. Determines the effective
2185 * length rlen of the strings to compare as the smallest of the length
2186 * of the substring and @a n2. The function then compares the two
2187 * strings by calling traits::compare(substring.data(),s,rlen). If the
2188 * result of the comparison is nonzero returns it, otherwise the shorter
2189 * one is ordered first.
2190 *
2191 * NB: s must have at least n2 characters, '\\0' has no special
2192 * meaning.
2193 */
2194 int
2195 compare(size_type __pos, size_type __n1, const _CharT* __s,
2196 size_type __n2) const;
2197 };
2198
2199 template<typename _CharT, typename _Traits, typename _Alloc>
2200 inline basic_string<_CharT, _Traits, _Alloc>::
2201 basic_string()
2202 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
2203 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2204 #else
2205 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) { }
2206 #endif
2207
2208 // operator+
2209 /**
2210 * @brief Concatenate two strings.
2211 * @param lhs First string.
2212 * @param rhs Last string.
2213 * @return New string with value of @a lhs followed by @a rhs.
2214 */
2215 template<typename _CharT, typename _Traits, typename _Alloc>
2216 basic_string<_CharT, _Traits, _Alloc>
2217 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2218 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2219 {
2220 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2221 __str.append(__rhs);
2222 return __str;
2223 }
2224
2225 /**
2226 * @brief Concatenate C string and string.
2227 * @param lhs First string.
2228 * @param rhs Last string.
2229 * @return New string with value of @a lhs followed by @a rhs.
2230 */
2231 template<typename _CharT, typename _Traits, typename _Alloc>
2232 basic_string<_CharT,_Traits,_Alloc>
2233 operator+(const _CharT* __lhs,
2234 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2235
2236 /**
2237 * @brief Concatenate character and string.
2238 * @param lhs First string.
2239 * @param rhs Last string.
2240 * @return New string with @a lhs followed by @a rhs.
2241 */
2242 template<typename _CharT, typename _Traits, typename _Alloc>
2243 basic_string<_CharT,_Traits,_Alloc>
2244 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2245
2246 /**
2247 * @brief Concatenate string and C string.
2248 * @param lhs First string.
2249 * @param rhs Last string.
2250 * @return New string with @a lhs followed by @a rhs.
2251 */
2252 template<typename _CharT, typename _Traits, typename _Alloc>
2253 inline basic_string<_CharT, _Traits, _Alloc>
2254 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2255 const _CharT* __rhs)
2256 {
2257 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2258 __str.append(__rhs);
2259 return __str;
2260 }
2261
2262 /**
2263 * @brief Concatenate string and character.
2264 * @param lhs First string.
2265 * @param rhs Last string.
2266 * @return New string with @a lhs followed by @a rhs.
2267 */
2268 template<typename _CharT, typename _Traits, typename _Alloc>
2269 inline basic_string<_CharT, _Traits, _Alloc>
2270 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2271 {
2272 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
2273 typedef typename __string_type::size_type __size_type;
2274 __string_type __str(__lhs);
2275 __str.append(__size_type(1), __rhs);
2276 return __str;
2277 }
2278
2279 // operator ==
2280 /**
2281 * @brief Test equivalence of two strings.
2282 * @param lhs First string.
2283 * @param rhs Second string.
2284 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2285 */
2286 template<typename _CharT, typename _Traits, typename _Alloc>
2287 inline bool
2288 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2289 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2290 { return __lhs.compare(__rhs) == 0; }
2291
2292 template<typename _CharT>
2293 inline
2294 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
2295 operator==(const basic_string<_CharT>& __lhs,
2296 const basic_string<_CharT>& __rhs)
2297 { return (__lhs.size() == __rhs.size()
2298 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
2299 __lhs.size())); }
2300
2301 /**
2302 * @brief Test equivalence of C string and string.
2303 * @param lhs C string.
2304 * @param rhs String.
2305 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
2306 */
2307 template<typename _CharT, typename _Traits, typename _Alloc>
2308 inline bool
2309 operator==(const _CharT* __lhs,
2310 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2311 { return __rhs.compare(__lhs) == 0; }
2312
2313 /**
2314 * @brief Test equivalence of string and C string.
2315 * @param lhs String.
2316 * @param rhs C string.
2317 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2318 */
2319 template<typename _CharT, typename _Traits, typename _Alloc>
2320 inline bool
2321 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2322 const _CharT* __rhs)
2323 { return __lhs.compare(__rhs) == 0; }
2324
2325 // operator !=
2326 /**
2327 * @brief Test difference of two strings.
2328 * @param lhs First string.
2329 * @param rhs Second string.
2330 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2331 */
2332 template<typename _CharT, typename _Traits, typename _Alloc>
2333 inline bool
2334 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2335 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2336 { return !(__lhs == __rhs); }
2337
2338 /**
2339 * @brief Test difference of C string and string.
2340 * @param lhs C string.
2341 * @param rhs String.
2342 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2343 */
2344 template<typename _CharT, typename _Traits, typename _Alloc>
2345 inline bool
2346 operator!=(const _CharT* __lhs,
2347 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2348 { return !(__lhs == __rhs); }
2349
2350 /**
2351 * @brief Test difference of string and C string.
2352 * @param lhs String.
2353 * @param rhs C string.
2354 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2355 */
2356 template<typename _CharT, typename _Traits, typename _Alloc>
2357 inline bool
2358 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2359 const _CharT* __rhs)
2360 { return !(__lhs == __rhs); }
2361
2362 // operator <
2363 /**
2364 * @brief Test if string precedes string.
2365 * @param lhs First string.
2366 * @param rhs Second string.
2367 * @return True if @a lhs precedes @a rhs. False otherwise.
2368 */
2369 template<typename _CharT, typename _Traits, typename _Alloc>
2370 inline bool
2371 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2372 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2373 { return __lhs.compare(__rhs) < 0; }
2374
2375 /**
2376 * @brief Test if string precedes C string.
2377 * @param lhs String.
2378 * @param rhs C string.
2379 * @return True if @a lhs precedes @a rhs. False otherwise.
2380 */
2381 template<typename _CharT, typename _Traits, typename _Alloc>
2382 inline bool
2383 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2384 const _CharT* __rhs)
2385 { return __lhs.compare(__rhs) < 0; }
2386
2387 /**
2388 * @brief Test if C string precedes string.
2389 * @param lhs C string.
2390 * @param rhs String.
2391 * @return True if @a lhs precedes @a rhs. False otherwise.
2392 */
2393 template<typename _CharT, typename _Traits, typename _Alloc>
2394 inline bool
2395 operator<(const _CharT* __lhs,
2396 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2397 { return __rhs.compare(__lhs) > 0; }
2398
2399 // operator >
2400 /**
2401 * @brief Test if string follows string.
2402 * @param lhs First string.
2403 * @param rhs Second string.
2404 * @return True if @a lhs follows @a rhs. False otherwise.
2405 */
2406 template<typename _CharT, typename _Traits, typename _Alloc>
2407 inline bool
2408 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2409 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2410 { return __lhs.compare(__rhs) > 0; }
2411
2412 /**
2413 * @brief Test if string follows C string.
2414 * @param lhs String.
2415 * @param rhs C string.
2416 * @return True if @a lhs follows @a rhs. False otherwise.
2417 */
2418 template<typename _CharT, typename _Traits, typename _Alloc>
2419 inline bool
2420 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2421 const _CharT* __rhs)
2422 { return __lhs.compare(__rhs) > 0; }
2423
2424 /**
2425 * @brief Test if C string follows string.
2426 * @param lhs C string.
2427 * @param rhs String.
2428 * @return True if @a lhs follows @a rhs. False otherwise.
2429 */
2430 template<typename _CharT, typename _Traits, typename _Alloc>
2431 inline bool
2432 operator>(const _CharT* __lhs,
2433 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2434 { return __rhs.compare(__lhs) < 0; }
2435
2436 // operator <=
2437 /**
2438 * @brief Test if string doesn't follow string.
2439 * @param lhs First string.
2440 * @param rhs Second string.
2441 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2442 */
2443 template<typename _CharT, typename _Traits, typename _Alloc>
2444 inline bool
2445 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2446 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2447 { return __lhs.compare(__rhs) <= 0; }
2448
2449 /**
2450 * @brief Test if string doesn't follow C string.
2451 * @param lhs String.
2452 * @param rhs C string.
2453 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2454 */
2455 template<typename _CharT, typename _Traits, typename _Alloc>
2456 inline bool
2457 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2458 const _CharT* __rhs)
2459 { return __lhs.compare(__rhs) <= 0; }
2460
2461 /**
2462 * @brief Test if C string doesn't follow string.
2463 * @param lhs C string.
2464 * @param rhs String.
2465 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2466 */
2467 template<typename _CharT, typename _Traits, typename _Alloc>
2468 inline bool
2469 operator<=(const _CharT* __lhs,
2470 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2471 { return __rhs.compare(__lhs) >= 0; }
2472
2473 // operator >=
2474 /**
2475 * @brief Test if string doesn't precede string.
2476 * @param lhs First string.
2477 * @param rhs Second string.
2478 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2479 */
2480 template<typename _CharT, typename _Traits, typename _Alloc>
2481 inline bool
2482 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2483 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2484 { return __lhs.compare(__rhs) >= 0; }
2485
2486 /**
2487 * @brief Test if string doesn't precede C string.
2488 * @param lhs String.
2489 * @param rhs C string.
2490 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2491 */
2492 template<typename _CharT, typename _Traits, typename _Alloc>
2493 inline bool
2494 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2495 const _CharT* __rhs)
2496 { return __lhs.compare(__rhs) >= 0; }
2497
2498 /**
2499 * @brief Test if C string doesn't precede string.
2500 * @param lhs C string.
2501 * @param rhs String.
2502 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2503 */
2504 template<typename _CharT, typename _Traits, typename _Alloc>
2505 inline bool
2506 operator>=(const _CharT* __lhs,
2507 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2508 { return __rhs.compare(__lhs) <= 0; }
2509
2510 /**
2511 * @brief Swap contents of two strings.
2512 * @param lhs First string.
2513 * @param rhs Second string.
2514 *
2515 * Exchanges the contents of @a lhs and @a rhs in constant time.
2516 */
2517 template<typename _CharT, typename _Traits, typename _Alloc>
2518 inline void
2519 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2520 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2521 { __lhs.swap(__rhs); }
2522
2523 /**
2524 * @brief Read stream into a string.
2525 * @param is Input stream.
2526 * @param str Buffer to store into.
2527 * @return Reference to the input stream.
2528 *
2529 * Stores characters from @a is into @a str until whitespace is found, the
2530 * end of the stream is encountered, or str.max_size() is reached. If
2531 * is.width() is non-zero, that is the limit on the number of characters
2532 * stored into @a str. Any previous contents of @a str are erased.
2533 */
2534 template<typename _CharT, typename _Traits, typename _Alloc>
2535 basic_istream<_CharT, _Traits>&
2536 operator>>(basic_istream<_CharT, _Traits>& __is,
2537 basic_string<_CharT, _Traits, _Alloc>& __str);
2538
2539 template<>
2540 basic_istream<char>&
2541 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
2542
2543 /**
2544 * @brief Write string to a stream.
2545 * @param os Output stream.
2546 * @param str String to write out.
2547 * @return Reference to the output stream.
2548 *
2549 * Output characters of @a str into os following the same rules as for
2550 * writing a C string.
2551 */
2552 template<typename _CharT, typename _Traits, typename _Alloc>
2553 inline basic_ostream<_CharT, _Traits>&
2554 operator<<(basic_ostream<_CharT, _Traits>& __os,
2555 const basic_string<_CharT, _Traits, _Alloc>& __str)
2556 {
2557 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2558 // 586. string inserter not a formatted function
2559 return __ostream_insert(__os, __str.data(), __str.size());
2560 }
2561
2562 /**
2563 * @brief Read a line from stream into a string.
2564 * @param is Input stream.
2565 * @param str Buffer to store into.
2566 * @param delim Character marking end of line.
2567 * @return Reference to the input stream.
2568 *
2569 * Stores characters from @a is into @a str until @a delim is found, the
2570 * end of the stream is encountered, or str.max_size() is reached. If
2571 * is.width() is non-zero, that is the limit on the number of characters
2572 * stored into @a str. Any previous contents of @a str are erased. If @a
2573 * delim was encountered, it is extracted but not stored into @a str.
2574 */
2575 template<typename _CharT, typename _Traits, typename _Alloc>
2576 basic_istream<_CharT, _Traits>&
2577 getline(basic_istream<_CharT, _Traits>& __is,
2578 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2579
2580 /**
2581 * @brief Read a line from stream into a string.
2582 * @param is Input stream.
2583 * @param str Buffer to store into.
2584 * @return Reference to the input stream.
2585 *
2586 * Stores characters from is into @a str until '\n' is found, the end of
2587 * the stream is encountered, or str.max_size() is reached. If is.width()
2588 * is non-zero, that is the limit on the number of characters stored into
2589 * @a str. Any previous contents of @a str are erased. If end of line was
2590 * encountered, it is extracted but not stored into @a str.
2591 */
2592 template<typename _CharT, typename _Traits, typename _Alloc>
2593 inline basic_istream<_CharT, _Traits>&
2594 getline(basic_istream<_CharT, _Traits>& __is,
2595 basic_string<_CharT, _Traits, _Alloc>& __str)
2596 { return getline(__is, __str, __is.widen('\n')); }
2597
2598 template<>
2599 basic_istream<char>&
2600 getline(basic_istream<char>& __in, basic_string<char>& __str,
2601 char __delim);
2602
2603 #ifdef _GLIBCXX_USE_WCHAR_T
2604 template<>
2605 basic_istream<wchar_t>&
2606 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
2607 wchar_t __delim);
2608 #endif
2609
2610 _GLIBCXX_END_NAMESPACE
2611
2612 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99) \
2613 && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
2614
2615 #include <ext/string_conversions.h>
2616
2617 _GLIBCXX_BEGIN_NAMESPACE(std)
2618
2619 // 21.4 Numeric Conversions [string.conversions].
2620 inline int
2621 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
2622 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
2623 __idx, __base); }
2624
2625 inline long
2626 stol(const string& __str, size_t* __idx = 0, int __base = 10)
2627 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
2628 __idx, __base); }
2629
2630 inline unsigned long
2631 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
2632 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
2633 __idx, __base); }
2634
2635 inline long long
2636 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
2637 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
2638 __idx, __base); }
2639
2640 inline unsigned long long
2641 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
2642 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
2643 __idx, __base); }
2644
2645 // NB: strtof vs strtod.
2646 inline float
2647 stof(const string& __str, size_t* __idx = 0)
2648 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
2649
2650 inline double
2651 stod(const string& __str, size_t* __idx = 0)
2652 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
2653
2654 inline long double
2655 stold(const string& __str, size_t* __idx = 0)
2656 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
2657
2658 // NB: (v)snprintf vs sprintf.
2659
2660 // DR 1261.
2661 inline string
2662 to_string(int __val)
2663 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
2664 "%d", __val); }
2665
2666 inline string
2667 to_string(unsigned __val)
2668 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2669 4 * sizeof(unsigned),
2670 "%u", __val); }
2671
2672 inline string
2673 to_string(long __val)
2674 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
2675 "%ld", __val); }
2676
2677 inline string
2678 to_string(unsigned long __val)
2679 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2680 4 * sizeof(unsigned long),
2681 "%lu", __val); }
2682
2683 inline string
2684 to_string(long long __val)
2685 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2686 4 * sizeof(long long),
2687 "%lld", __val); }
2688
2689 inline string
2690 to_string(unsigned long long __val)
2691 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2692 4 * sizeof(unsigned long long),
2693 "%llu", __val); }
2694
2695 inline string
2696 to_string(float __val)
2697 {
2698 const int __n =
2699 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
2700 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2701 "%f", __val);
2702 }
2703
2704 inline string
2705 to_string(double __val)
2706 {
2707 const int __n =
2708 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
2709 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2710 "%f", __val);
2711 }
2712
2713 inline string
2714 to_string(long double __val)
2715 {
2716 const int __n =
2717 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
2718 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2719 "%Lf", __val);
2720 }
2721
2722 #ifdef _GLIBCXX_USE_WCHAR_T
2723 inline int
2724 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
2725 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
2726 __idx, __base); }
2727
2728 inline long
2729 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
2730 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
2731 __idx, __base); }
2732
2733 inline unsigned long
2734 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
2735 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
2736 __idx, __base); }
2737
2738 inline long long
2739 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
2740 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
2741 __idx, __base); }
2742
2743 inline unsigned long long
2744 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
2745 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
2746 __idx, __base); }
2747
2748 // NB: wcstof vs wcstod.
2749 inline float
2750 stof(const wstring& __str, size_t* __idx = 0)
2751 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
2752
2753 inline double
2754 stod(const wstring& __str, size_t* __idx = 0)
2755 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
2756
2757 inline long double
2758 stold(const wstring& __str, size_t* __idx = 0)
2759 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
2760
2761 // DR 1261.
2762 inline wstring
2763 to_wstring(int __val)
2764 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
2765 L"%d", __val); }
2766
2767 inline wstring
2768 to_wstring(unsigned __val)
2769 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2770 4 * sizeof(unsigned),
2771 L"%u", __val); }
2772
2773 inline wstring
2774 to_wstring(long __val)
2775 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
2776 L"%ld", __val); }
2777
2778 inline wstring
2779 to_wstring(unsigned long __val)
2780 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2781 4 * sizeof(unsigned long),
2782 L"%lu", __val); }
2783
2784 inline wstring
2785 to_wstring(long long __val)
2786 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2787 4 * sizeof(long long),
2788 L"%lld", __val); }
2789
2790 inline wstring
2791 to_wstring(unsigned long long __val)
2792 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2793 4 * sizeof(unsigned long long),
2794 L"%llu", __val); }
2795
2796 inline wstring
2797 to_wstring(float __val)
2798 {
2799 const int __n =
2800 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
2801 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
2802 L"%f", __val);
2803 }
2804
2805 inline wstring
2806 to_wstring(double __val)
2807 {
2808 const int __n =
2809 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
2810 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
2811 L"%f", __val);
2812 }
2813
2814 inline wstring
2815 to_wstring(long double __val)
2816 {
2817 const int __n =
2818 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
2819 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
2820 L"%Lf", __val);
2821 }
2822 #endif
2823
2824 _GLIBCXX_END_NAMESPACE
2825
2826 #endif
2827
2828 #endif /* _BASIC_STRING_H */