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