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