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