Fix testsuite failures for __gnu_debug::string with old ABI
[official-gcc.git] / libstdc++-v3 / include / bits / basic_string.h
blobba94b51f616aad1446b83354b5c02352553dcde5
1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997-2018 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
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.
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.
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/>.
25 /** @file bits/basic_string.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{string}
31 // ISO C++ 14882: 21 Strings library
34 #ifndef _BASIC_STRING_H
35 #define _BASIC_STRING_H 1
37 #pragma GCC system_header
39 #include <ext/atomicity.h>
40 #include <ext/alloc_traits.h>
41 #include <debug/debug.h>
43 #if __cplusplus >= 201103L
44 #include <initializer_list>
45 #endif
47 #if __cplusplus > 201402L
48 # include <string_view>
49 #endif
52 namespace std _GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
56 #if _GLIBCXX_USE_CXX11_ABI
57 _GLIBCXX_BEGIN_NAMESPACE_CXX11
58 /**
59 * @class basic_string basic_string.h <string>
60 * @brief Managing sequences of characters and character-like objects.
62 * @ingroup strings
63 * @ingroup sequences
65 * @tparam _CharT Type of character
66 * @tparam _Traits Traits for character type, defaults to
67 * char_traits<_CharT>.
68 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
70 * Meets the requirements of a <a href="tables.html#65">container</a>, a
71 * <a href="tables.html#66">reversible container</a>, and a
72 * <a href="tables.html#67">sequence</a>. Of the
73 * <a href="tables.html#68">optional sequence requirements</a>, only
74 * @c push_back, @c at, and @c %array access are supported.
76 template<typename _CharT, typename _Traits, typename _Alloc>
77 class basic_string
79 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
80 rebind<_CharT>::other _Char_alloc_type;
81 typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
83 // Types:
84 public:
85 typedef _Traits traits_type;
86 typedef typename _Traits::char_type value_type;
87 typedef _Char_alloc_type allocator_type;
88 typedef typename _Alloc_traits::size_type size_type;
89 typedef typename _Alloc_traits::difference_type difference_type;
90 typedef typename _Alloc_traits::reference reference;
91 typedef typename _Alloc_traits::const_reference const_reference;
92 typedef typename _Alloc_traits::pointer pointer;
93 typedef typename _Alloc_traits::const_pointer const_pointer;
94 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
95 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
96 const_iterator;
97 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
98 typedef std::reverse_iterator<iterator> reverse_iterator;
100 /// Value returned by various member functions when they fail.
101 static const size_type npos = static_cast<size_type>(-1);
103 protected:
104 // type used for positions in insert, erase etc.
105 #if __cplusplus < 201103L
106 typedef iterator __const_iterator;
107 #else
108 typedef const_iterator __const_iterator;
109 #endif
111 private:
112 #if __cplusplus > 201402L
113 // A helper type for avoiding boiler-plate.
114 typedef basic_string_view<_CharT, _Traits> __sv_type;
116 template<typename _Tp, typename _Res>
117 using _If_sv = enable_if_t<
118 __and_<is_convertible<const _Tp&, __sv_type>,
119 __not_<is_convertible<const _Tp*, const basic_string*>>,
120 __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
121 _Res>;
123 // Allows an implicit conversion to __sv_type.
124 static __sv_type
125 _S_to_string_view(__sv_type __svt) noexcept
126 { return __svt; }
128 // Wraps a string_view by explicit conversion and thus
129 // allows to add an internal constructor that does not
130 // participate in overload resolution when a string_view
131 // is provided.
132 struct __sv_wrapper
134 explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
135 __sv_type _M_sv;
137 #endif
139 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
140 struct _Alloc_hider : allocator_type // TODO check __is_final
142 #if __cplusplus < 201103L
143 _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
144 : allocator_type(__a), _M_p(__dat) { }
145 #else
146 _Alloc_hider(pointer __dat, const _Alloc& __a)
147 : allocator_type(__a), _M_p(__dat) { }
149 _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
150 : allocator_type(std::move(__a)), _M_p(__dat) { }
151 #endif
153 pointer _M_p; // The actual data.
156 _Alloc_hider _M_dataplus;
157 size_type _M_string_length;
159 enum { _S_local_capacity = 15 / sizeof(_CharT) };
161 union
163 _CharT _M_local_buf[_S_local_capacity + 1];
164 size_type _M_allocated_capacity;
167 void
168 _M_data(pointer __p)
169 { _M_dataplus._M_p = __p; }
171 void
172 _M_length(size_type __length)
173 { _M_string_length = __length; }
175 pointer
176 _M_data() const
177 { return _M_dataplus._M_p; }
179 pointer
180 _M_local_data()
182 #if __cplusplus >= 201103L
183 return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
184 #else
185 return pointer(_M_local_buf);
186 #endif
189 const_pointer
190 _M_local_data() const
192 #if __cplusplus >= 201103L
193 return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf);
194 #else
195 return const_pointer(_M_local_buf);
196 #endif
199 void
200 _M_capacity(size_type __capacity)
201 { _M_allocated_capacity = __capacity; }
203 void
204 _M_set_length(size_type __n)
206 _M_length(__n);
207 traits_type::assign(_M_data()[__n], _CharT());
210 bool
211 _M_is_local() const
212 { return _M_data() == _M_local_data(); }
214 // Create & Destroy
215 pointer
216 _M_create(size_type&, size_type);
218 void
219 _M_dispose()
221 if (!_M_is_local())
222 _M_destroy(_M_allocated_capacity);
225 void
226 _M_destroy(size_type __size) throw()
227 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
229 // _M_construct_aux is used to implement the 21.3.1 para 15 which
230 // requires special behaviour if _InIterator is an integral type
231 template<typename _InIterator>
232 void
233 _M_construct_aux(_InIterator __beg, _InIterator __end,
234 std::__false_type)
236 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
237 _M_construct(__beg, __end, _Tag());
240 // _GLIBCXX_RESOLVE_LIB_DEFECTS
241 // 438. Ambiguity in the "do the right thing" clause
242 template<typename _Integer>
243 void
244 _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
245 { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
247 void
248 _M_construct_aux_2(size_type __req, _CharT __c)
249 { _M_construct(__req, __c); }
251 template<typename _InIterator>
252 void
253 _M_construct(_InIterator __beg, _InIterator __end)
255 typedef typename std::__is_integer<_InIterator>::__type _Integral;
256 _M_construct_aux(__beg, __end, _Integral());
259 // For Input Iterators, used in istreambuf_iterators, etc.
260 template<typename _InIterator>
261 void
262 _M_construct(_InIterator __beg, _InIterator __end,
263 std::input_iterator_tag);
265 // For forward_iterators up to random_access_iterators, used for
266 // string::iterator, _CharT*, etc.
267 template<typename _FwdIterator>
268 void
269 _M_construct(_FwdIterator __beg, _FwdIterator __end,
270 std::forward_iterator_tag);
272 void
273 _M_construct(size_type __req, _CharT __c);
275 allocator_type&
276 _M_get_allocator()
277 { return _M_dataplus; }
279 const allocator_type&
280 _M_get_allocator() const
281 { return _M_dataplus; }
283 private:
285 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
286 // The explicit instantiations in misc-inst.cc require this due to
287 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
288 template<typename _Tp, bool _Requires =
289 !__are_same<_Tp, _CharT*>::__value
290 && !__are_same<_Tp, const _CharT*>::__value
291 && !__are_same<_Tp, iterator>::__value
292 && !__are_same<_Tp, const_iterator>::__value>
293 struct __enable_if_not_native_iterator
294 { typedef basic_string& __type; };
295 template<typename _Tp>
296 struct __enable_if_not_native_iterator<_Tp, false> { };
297 #endif
299 size_type
300 _M_check(size_type __pos, const char* __s) const
302 if (__pos > this->size())
303 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
304 "this->size() (which is %zu)"),
305 __s, __pos, this->size());
306 return __pos;
309 void
310 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
312 if (this->max_size() - (this->size() - __n1) < __n2)
313 __throw_length_error(__N(__s));
317 // NB: _M_limit doesn't check for a bad __pos value.
318 size_type
319 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
321 const bool __testoff = __off < this->size() - __pos;
322 return __testoff ? __off : this->size() - __pos;
325 // True if _Rep and source do not overlap.
326 bool
327 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
329 return (less<const _CharT*>()(__s, _M_data())
330 || less<const _CharT*>()(_M_data() + this->size(), __s));
333 // When __n = 1 way faster than the general multichar
334 // traits_type::copy/move/assign.
335 static void
336 _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
338 if (__n == 1)
339 traits_type::assign(*__d, *__s);
340 else
341 traits_type::copy(__d, __s, __n);
344 static void
345 _S_move(_CharT* __d, const _CharT* __s, size_type __n)
347 if (__n == 1)
348 traits_type::assign(*__d, *__s);
349 else
350 traits_type::move(__d, __s, __n);
353 static void
354 _S_assign(_CharT* __d, size_type __n, _CharT __c)
356 if (__n == 1)
357 traits_type::assign(*__d, __c);
358 else
359 traits_type::assign(__d, __n, __c);
362 // _S_copy_chars is a separate template to permit specialization
363 // to optimize for the common case of pointers as iterators.
364 template<class _Iterator>
365 static void
366 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
368 for (; __k1 != __k2; ++__k1, (void)++__p)
369 traits_type::assign(*__p, *__k1); // These types are off.
372 static void
373 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
374 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
376 static void
377 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
378 _GLIBCXX_NOEXCEPT
379 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
381 static void
382 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
383 { _S_copy(__p, __k1, __k2 - __k1); }
385 static void
386 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
387 _GLIBCXX_NOEXCEPT
388 { _S_copy(__p, __k1, __k2 - __k1); }
390 static int
391 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
393 const difference_type __d = difference_type(__n1 - __n2);
395 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
396 return __gnu_cxx::__numeric_traits<int>::__max;
397 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
398 return __gnu_cxx::__numeric_traits<int>::__min;
399 else
400 return int(__d);
403 void
404 _M_assign(const basic_string&);
406 void
407 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
408 size_type __len2);
410 void
411 _M_erase(size_type __pos, size_type __n);
413 public:
414 // Construct/copy/destroy:
415 // NB: We overload ctors in some cases instead of using default
416 // arguments, per 17.4.4.4 para. 2 item 2.
419 * @brief Default constructor creates an empty string.
421 basic_string()
422 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
423 : _M_dataplus(_M_local_data())
424 { _M_set_length(0); }
427 * @brief Construct an empty string using allocator @a a.
429 explicit
430 basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
431 : _M_dataplus(_M_local_data(), __a)
432 { _M_set_length(0); }
435 * @brief Construct string with copy of value of @a __str.
436 * @param __str Source string.
438 basic_string(const basic_string& __str)
439 : _M_dataplus(_M_local_data(),
440 _Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
441 { _M_construct(__str._M_data(), __str._M_data() + __str.length()); }
443 // _GLIBCXX_RESOLVE_LIB_DEFECTS
444 // 2583. no way to supply an allocator for basic_string(str, pos)
446 * @brief Construct string as copy of a substring.
447 * @param __str Source string.
448 * @param __pos Index of first character to copy from.
449 * @param __a Allocator to use.
451 basic_string(const basic_string& __str, size_type __pos,
452 const _Alloc& __a = _Alloc())
453 : _M_dataplus(_M_local_data(), __a)
455 const _CharT* __start = __str._M_data()
456 + __str._M_check(__pos, "basic_string::basic_string");
457 _M_construct(__start, __start + __str._M_limit(__pos, npos));
461 * @brief Construct string as copy of a substring.
462 * @param __str Source string.
463 * @param __pos Index of first character to copy from.
464 * @param __n Number of characters to copy.
466 basic_string(const basic_string& __str, size_type __pos,
467 size_type __n)
468 : _M_dataplus(_M_local_data())
470 const _CharT* __start = __str._M_data()
471 + __str._M_check(__pos, "basic_string::basic_string");
472 _M_construct(__start, __start + __str._M_limit(__pos, __n));
476 * @brief Construct string as copy of a substring.
477 * @param __str Source string.
478 * @param __pos Index of first character to copy from.
479 * @param __n Number of characters to copy.
480 * @param __a Allocator to use.
482 basic_string(const basic_string& __str, size_type __pos,
483 size_type __n, const _Alloc& __a)
484 : _M_dataplus(_M_local_data(), __a)
486 const _CharT* __start
487 = __str._M_data() + __str._M_check(__pos, "string::string");
488 _M_construct(__start, __start + __str._M_limit(__pos, __n));
492 * @brief Construct string initialized by a character %array.
493 * @param __s Source character %array.
494 * @param __n Number of characters to copy.
495 * @param __a Allocator to use (default is default allocator).
497 * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
498 * has no special meaning.
500 basic_string(const _CharT* __s, size_type __n,
501 const _Alloc& __a = _Alloc())
502 : _M_dataplus(_M_local_data(), __a)
503 { _M_construct(__s, __s + __n); }
506 * @brief Construct string as copy of a C string.
507 * @param __s Source C string.
508 * @param __a Allocator to use (default is default allocator).
510 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
511 // _GLIBCXX_RESOLVE_LIB_DEFECTS
512 // 3076. basic_string CTAD ambiguity
513 template<typename = _RequireAllocator<_Alloc>>
514 #endif
515 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
516 : _M_dataplus(_M_local_data(), __a)
517 { _M_construct(__s, __s ? __s + traits_type::length(__s) : __s+npos); }
520 * @brief Construct string as multiple characters.
521 * @param __n Number of characters.
522 * @param __c Character to use.
523 * @param __a Allocator to use (default is default allocator).
525 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
526 // _GLIBCXX_RESOLVE_LIB_DEFECTS
527 // 3076. basic_string CTAD ambiguity
528 template<typename = _RequireAllocator<_Alloc>>
529 #endif
530 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
531 : _M_dataplus(_M_local_data(), __a)
532 { _M_construct(__n, __c); }
534 #if __cplusplus >= 201103L
536 * @brief Move construct string.
537 * @param __str Source string.
539 * The newly-created string contains the exact contents of @a __str.
540 * @a __str is a valid, but unspecified string.
542 basic_string(basic_string&& __str) noexcept
543 : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
545 if (__str._M_is_local())
547 traits_type::copy(_M_local_buf, __str._M_local_buf,
548 _S_local_capacity + 1);
550 else
552 _M_data(__str._M_data());
553 _M_capacity(__str._M_allocated_capacity);
556 // Must use _M_length() here not _M_set_length() because
557 // basic_stringbuf relies on writing into unallocated capacity so
558 // we mess up the contents if we put a '\0' in the string.
559 _M_length(__str.length());
560 __str._M_data(__str._M_local_data());
561 __str._M_set_length(0);
565 * @brief Construct string from an initializer %list.
566 * @param __l std::initializer_list of characters.
567 * @param __a Allocator to use (default is default allocator).
569 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
570 : _M_dataplus(_M_local_data(), __a)
571 { _M_construct(__l.begin(), __l.end()); }
573 basic_string(const basic_string& __str, const _Alloc& __a)
574 : _M_dataplus(_M_local_data(), __a)
575 { _M_construct(__str.begin(), __str.end()); }
577 basic_string(basic_string&& __str, const _Alloc& __a)
578 noexcept(_Alloc_traits::_S_always_equal())
579 : _M_dataplus(_M_local_data(), __a)
581 if (__str._M_is_local())
583 traits_type::copy(_M_local_buf, __str._M_local_buf,
584 _S_local_capacity + 1);
585 _M_length(__str.length());
586 __str._M_set_length(0);
588 else if (_Alloc_traits::_S_always_equal()
589 || __str.get_allocator() == __a)
591 _M_data(__str._M_data());
592 _M_length(__str.length());
593 _M_capacity(__str._M_allocated_capacity);
594 __str._M_data(__str._M_local_buf);
595 __str._M_set_length(0);
597 else
598 _M_construct(__str.begin(), __str.end());
601 #endif // C++11
604 * @brief Construct string as copy of a range.
605 * @param __beg Start of range.
606 * @param __end End of range.
607 * @param __a Allocator to use (default is default allocator).
609 #if __cplusplus >= 201103L
610 template<typename _InputIterator,
611 typename = std::_RequireInputIter<_InputIterator>>
612 #else
613 template<typename _InputIterator>
614 #endif
615 basic_string(_InputIterator __beg, _InputIterator __end,
616 const _Alloc& __a = _Alloc())
617 : _M_dataplus(_M_local_data(), __a)
618 { _M_construct(__beg, __end); }
620 #if __cplusplus > 201402L
622 * @brief Construct string from a substring of a string_view.
623 * @param __t Source object convertible to string view.
624 * @param __pos The index of the first character to copy from __t.
625 * @param __n The number of characters to copy from __t.
626 * @param __a Allocator to use.
628 template<typename _Tp, typename = _If_sv<_Tp, void>>
629 basic_string(const _Tp& __t, size_type __pos, size_type __n,
630 const _Alloc& __a = _Alloc())
631 : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
634 * @brief Construct string from a string_view.
635 * @param __t Source object convertible to string view.
636 * @param __a Allocator to use (default is default allocator).
638 template<typename _Tp, typename = _If_sv<_Tp, void>>
639 explicit
640 basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
641 : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
644 * @brief Only internally used: Construct string from a string view
645 * wrapper.
646 * @param __svw string view wrapper.
647 * @param __a Allocator to use.
649 explicit
650 basic_string(__sv_wrapper __svw, const _Alloc& __a)
651 : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
652 #endif // C++17
655 * @brief Destroy the string instance.
657 ~basic_string()
658 { _M_dispose(); }
661 * @brief Assign the value of @a str to this string.
662 * @param __str Source string.
664 basic_string&
665 operator=(const basic_string& __str)
667 #if __cplusplus >= 201103L
668 if (_Alloc_traits::_S_propagate_on_copy_assign())
670 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
671 && _M_get_allocator() != __str._M_get_allocator())
673 // Propagating allocator cannot free existing storage so must
674 // deallocate it before replacing current allocator.
675 if (__str.size() <= _S_local_capacity)
677 _M_destroy(_M_allocated_capacity);
678 _M_data(_M_local_data());
679 _M_set_length(0);
681 else
683 const auto __len = __str.size();
684 auto __alloc = __str._M_get_allocator();
685 // If this allocation throws there are no effects:
686 auto __ptr = _Alloc_traits::allocate(__alloc, __len + 1);
687 _M_destroy(_M_allocated_capacity);
688 _M_data(__ptr);
689 _M_capacity(__len);
690 _M_set_length(__len);
693 std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator());
695 #endif
696 return this->assign(__str);
700 * @brief Copy contents of @a s into this string.
701 * @param __s Source null-terminated string.
703 basic_string&
704 operator=(const _CharT* __s)
705 { return this->assign(__s); }
708 * @brief Set value to string of length 1.
709 * @param __c Source character.
711 * Assigning to a character makes this string length 1 and
712 * (*this)[0] == @a c.
714 basic_string&
715 operator=(_CharT __c)
717 this->assign(1, __c);
718 return *this;
721 #if __cplusplus >= 201103L
723 * @brief Move assign the value of @a str to this string.
724 * @param __str Source string.
726 * The contents of @a str are moved into this string (without copying).
727 * @a str is a valid, but unspecified string.
729 // _GLIBCXX_RESOLVE_LIB_DEFECTS
730 // 2063. Contradictory requirements for string move assignment
731 basic_string&
732 operator=(basic_string&& __str)
733 noexcept(_Alloc_traits::_S_nothrow_move())
735 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
736 && !_Alloc_traits::_S_always_equal()
737 && _M_get_allocator() != __str._M_get_allocator())
739 // Destroy existing storage before replacing allocator.
740 _M_destroy(_M_allocated_capacity);
741 _M_data(_M_local_data());
742 _M_set_length(0);
744 // Replace allocator if POCMA is true.
745 std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
747 if (!__str._M_is_local()
748 && (_Alloc_traits::_S_propagate_on_move_assign()
749 || _Alloc_traits::_S_always_equal()))
751 pointer __data = nullptr;
752 size_type __capacity;
753 if (!_M_is_local())
755 if (_Alloc_traits::_S_always_equal())
757 __data = _M_data();
758 __capacity = _M_allocated_capacity;
760 else
761 _M_destroy(_M_allocated_capacity);
764 _M_data(__str._M_data());
765 _M_length(__str.length());
766 _M_capacity(__str._M_allocated_capacity);
767 if (__data)
769 __str._M_data(__data);
770 __str._M_capacity(__capacity);
772 else
773 __str._M_data(__str._M_local_buf);
775 else
776 assign(__str);
777 __str.clear();
778 return *this;
782 * @brief Set value to string constructed from initializer %list.
783 * @param __l std::initializer_list.
785 basic_string&
786 operator=(initializer_list<_CharT> __l)
788 this->assign(__l.begin(), __l.size());
789 return *this;
791 #endif // C++11
793 #if __cplusplus > 201402L
795 * @brief Set value to string constructed from a string_view.
796 * @param __svt An object convertible to string_view.
798 template<typename _Tp>
799 _If_sv<_Tp, basic_string&>
800 operator=(const _Tp& __svt)
801 { return this->assign(__svt); }
804 * @brief Convert to a string_view.
805 * @return A string_view.
807 operator __sv_type() const noexcept
808 { return __sv_type(data(), size()); }
809 #endif // C++17
811 // Iterators:
813 * Returns a read/write iterator that points to the first character in
814 * the %string.
816 iterator
817 begin() _GLIBCXX_NOEXCEPT
818 { return iterator(_M_data()); }
821 * Returns a read-only (constant) iterator that points to the first
822 * character in the %string.
824 const_iterator
825 begin() const _GLIBCXX_NOEXCEPT
826 { return const_iterator(_M_data()); }
829 * Returns a read/write iterator that points one past the last
830 * character in the %string.
832 iterator
833 end() _GLIBCXX_NOEXCEPT
834 { return iterator(_M_data() + this->size()); }
837 * Returns a read-only (constant) iterator that points one past the
838 * last character in the %string.
840 const_iterator
841 end() const _GLIBCXX_NOEXCEPT
842 { return const_iterator(_M_data() + this->size()); }
845 * Returns a read/write reverse iterator that points to the last
846 * character in the %string. Iteration is done in reverse element
847 * order.
849 reverse_iterator
850 rbegin() _GLIBCXX_NOEXCEPT
851 { return reverse_iterator(this->end()); }
854 * Returns a read-only (constant) reverse iterator that points
855 * to the last character in the %string. Iteration is done in
856 * reverse element order.
858 const_reverse_iterator
859 rbegin() const _GLIBCXX_NOEXCEPT
860 { return const_reverse_iterator(this->end()); }
863 * Returns a read/write reverse iterator that points to one before the
864 * first character in the %string. Iteration is done in reverse
865 * element order.
867 reverse_iterator
868 rend() _GLIBCXX_NOEXCEPT
869 { return reverse_iterator(this->begin()); }
872 * Returns a read-only (constant) reverse iterator that points
873 * to one before the first character in the %string. Iteration
874 * is done in reverse element order.
876 const_reverse_iterator
877 rend() const _GLIBCXX_NOEXCEPT
878 { return const_reverse_iterator(this->begin()); }
880 #if __cplusplus >= 201103L
882 * Returns a read-only (constant) iterator that points to the first
883 * character in the %string.
885 const_iterator
886 cbegin() const noexcept
887 { return const_iterator(this->_M_data()); }
890 * Returns a read-only (constant) iterator that points one past the
891 * last character in the %string.
893 const_iterator
894 cend() const noexcept
895 { return const_iterator(this->_M_data() + this->size()); }
898 * Returns a read-only (constant) reverse iterator that points
899 * to the last character in the %string. Iteration is done in
900 * reverse element order.
902 const_reverse_iterator
903 crbegin() const noexcept
904 { return const_reverse_iterator(this->end()); }
907 * Returns a read-only (constant) reverse iterator that points
908 * to one before the first character in the %string. Iteration
909 * is done in reverse element order.
911 const_reverse_iterator
912 crend() const noexcept
913 { return const_reverse_iterator(this->begin()); }
914 #endif
916 public:
917 // Capacity:
918 /// Returns the number of characters in the string, not including any
919 /// null-termination.
920 size_type
921 size() const _GLIBCXX_NOEXCEPT
922 { return _M_string_length; }
924 /// Returns the number of characters in the string, not including any
925 /// null-termination.
926 size_type
927 length() const _GLIBCXX_NOEXCEPT
928 { return _M_string_length; }
930 /// Returns the size() of the largest possible %string.
931 size_type
932 max_size() const _GLIBCXX_NOEXCEPT
933 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
936 * @brief Resizes the %string to the specified number of characters.
937 * @param __n Number of characters the %string should contain.
938 * @param __c Character to fill any new elements.
940 * This function will %resize the %string to the specified
941 * number of characters. If the number is smaller than the
942 * %string's current size the %string is truncated, otherwise
943 * the %string is extended and new elements are %set to @a __c.
945 void
946 resize(size_type __n, _CharT __c);
949 * @brief Resizes the %string to the specified number of characters.
950 * @param __n Number of characters the %string should contain.
952 * This function will resize the %string to the specified length. If
953 * the new size is smaller than the %string's current size the %string
954 * is truncated, otherwise the %string is extended and new characters
955 * are default-constructed. For basic types such as char, this means
956 * setting them to 0.
958 void
959 resize(size_type __n)
960 { this->resize(__n, _CharT()); }
962 #if __cplusplus >= 201103L
963 /// A non-binding request to reduce capacity() to size().
964 void
965 shrink_to_fit() noexcept
967 #if __cpp_exceptions
968 if (capacity() > size())
971 { reserve(0); }
972 catch(...)
975 #endif
977 #endif
980 * Returns the total number of characters that the %string can hold
981 * before needing to allocate more memory.
983 size_type
984 capacity() const _GLIBCXX_NOEXCEPT
986 return _M_is_local() ? size_type(_S_local_capacity)
987 : _M_allocated_capacity;
991 * @brief Attempt to preallocate enough memory for specified number of
992 * characters.
993 * @param __res_arg Number of characters required.
994 * @throw std::length_error If @a __res_arg exceeds @c max_size().
996 * This function attempts to reserve enough memory for the
997 * %string to hold the specified number of characters. If the
998 * number requested is more than max_size(), length_error is
999 * thrown.
1001 * The advantage of this function is that if optimal code is a
1002 * necessity and the user can determine the string length that will be
1003 * required, the user can reserve the memory in %advance, and thus
1004 * prevent a possible reallocation of memory and copying of %string
1005 * data.
1007 void
1008 reserve(size_type __res_arg = 0);
1011 * Erases the string, making it empty.
1013 void
1014 clear() _GLIBCXX_NOEXCEPT
1015 { _M_set_length(0); }
1018 * Returns true if the %string is empty. Equivalent to
1019 * <code>*this == ""</code>.
1021 bool
1022 empty() const _GLIBCXX_NOEXCEPT
1023 { return this->size() == 0; }
1025 // Element access:
1027 * @brief Subscript access to the data contained in the %string.
1028 * @param __pos The index of the character to access.
1029 * @return Read-only (constant) reference to the character.
1031 * This operator allows for easy, array-style, data access.
1032 * Note that data access with this operator is unchecked and
1033 * out_of_range lookups are not defined. (For checked lookups
1034 * see at().)
1036 const_reference
1037 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
1039 __glibcxx_assert(__pos <= size());
1040 return _M_data()[__pos];
1044 * @brief Subscript access to the data contained in the %string.
1045 * @param __pos The index of the character to access.
1046 * @return Read/write reference to the character.
1048 * This operator allows for easy, array-style, data access.
1049 * Note that data access with this operator is unchecked and
1050 * out_of_range lookups are not defined. (For checked lookups
1051 * see at().)
1053 reference
1054 operator[](size_type __pos)
1056 // Allow pos == size() both in C++98 mode, as v3 extension,
1057 // and in C++11 mode.
1058 __glibcxx_assert(__pos <= size());
1059 // In pedantic mode be strict in C++98 mode.
1060 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
1061 return _M_data()[__pos];
1065 * @brief Provides access to the data contained in the %string.
1066 * @param __n The index of the character to access.
1067 * @return Read-only (const) reference to the character.
1068 * @throw std::out_of_range If @a n is an invalid index.
1070 * This function provides for safer data access. The parameter is
1071 * first checked that it is in the range of the string. The function
1072 * throws out_of_range if the check fails.
1074 const_reference
1075 at(size_type __n) const
1077 if (__n >= this->size())
1078 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1079 "(which is %zu) >= this->size() "
1080 "(which is %zu)"),
1081 __n, this->size());
1082 return _M_data()[__n];
1086 * @brief Provides access to the data contained in the %string.
1087 * @param __n The index of the character to access.
1088 * @return Read/write reference to the character.
1089 * @throw std::out_of_range If @a n is an invalid index.
1091 * This function provides for safer data access. The parameter is
1092 * first checked that it is in the range of the string. The function
1093 * throws out_of_range if the check fails.
1095 reference
1096 at(size_type __n)
1098 if (__n >= size())
1099 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1100 "(which is %zu) >= this->size() "
1101 "(which is %zu)"),
1102 __n, this->size());
1103 return _M_data()[__n];
1106 #if __cplusplus >= 201103L
1108 * Returns a read/write reference to the data at the first
1109 * element of the %string.
1111 reference
1112 front() noexcept
1114 __glibcxx_assert(!empty());
1115 return operator[](0);
1119 * Returns a read-only (constant) reference to the data at the first
1120 * element of the %string.
1122 const_reference
1123 front() const noexcept
1125 __glibcxx_assert(!empty());
1126 return operator[](0);
1130 * Returns a read/write reference to the data at the last
1131 * element of the %string.
1133 reference
1134 back() noexcept
1136 __glibcxx_assert(!empty());
1137 return operator[](this->size() - 1);
1141 * Returns a read-only (constant) reference to the data at the
1142 * last element of the %string.
1144 const_reference
1145 back() const noexcept
1147 __glibcxx_assert(!empty());
1148 return operator[](this->size() - 1);
1150 #endif
1152 // Modifiers:
1154 * @brief Append a string to this string.
1155 * @param __str The string to append.
1156 * @return Reference to this string.
1158 basic_string&
1159 operator+=(const basic_string& __str)
1160 { return this->append(__str); }
1163 * @brief Append a C string.
1164 * @param __s The C string to append.
1165 * @return Reference to this string.
1167 basic_string&
1168 operator+=(const _CharT* __s)
1169 { return this->append(__s); }
1172 * @brief Append a character.
1173 * @param __c The character to append.
1174 * @return Reference to this string.
1176 basic_string&
1177 operator+=(_CharT __c)
1179 this->push_back(__c);
1180 return *this;
1183 #if __cplusplus >= 201103L
1185 * @brief Append an initializer_list of characters.
1186 * @param __l The initializer_list of characters to be appended.
1187 * @return Reference to this string.
1189 basic_string&
1190 operator+=(initializer_list<_CharT> __l)
1191 { return this->append(__l.begin(), __l.size()); }
1192 #endif // C++11
1194 #if __cplusplus > 201402L
1196 * @brief Append a string_view.
1197 * @param __svt An object convertible to string_view to be appended.
1198 * @return Reference to this string.
1200 template<typename _Tp>
1201 _If_sv<_Tp, basic_string&>
1202 operator+=(const _Tp& __svt)
1203 { return this->append(__svt); }
1204 #endif // C++17
1207 * @brief Append a string to this string.
1208 * @param __str The string to append.
1209 * @return Reference to this string.
1211 basic_string&
1212 append(const basic_string& __str)
1213 { return _M_append(__str._M_data(), __str.size()); }
1216 * @brief Append a substring.
1217 * @param __str The string to append.
1218 * @param __pos Index of the first character of str to append.
1219 * @param __n The number of characters to append.
1220 * @return Reference to this string.
1221 * @throw std::out_of_range if @a __pos is not a valid index.
1223 * This function appends @a __n characters from @a __str
1224 * starting at @a __pos to this string. If @a __n is is larger
1225 * than the number of available characters in @a __str, the
1226 * remainder of @a __str is appended.
1228 basic_string&
1229 append(const basic_string& __str, size_type __pos, size_type __n = npos)
1230 { return _M_append(__str._M_data()
1231 + __str._M_check(__pos, "basic_string::append"),
1232 __str._M_limit(__pos, __n)); }
1235 * @brief Append a C substring.
1236 * @param __s The C string to append.
1237 * @param __n The number of characters to append.
1238 * @return Reference to this string.
1240 basic_string&
1241 append(const _CharT* __s, size_type __n)
1243 __glibcxx_requires_string_len(__s, __n);
1244 _M_check_length(size_type(0), __n, "basic_string::append");
1245 return _M_append(__s, __n);
1249 * @brief Append a C string.
1250 * @param __s The C string to append.
1251 * @return Reference to this string.
1253 basic_string&
1254 append(const _CharT* __s)
1256 __glibcxx_requires_string(__s);
1257 const size_type __n = traits_type::length(__s);
1258 _M_check_length(size_type(0), __n, "basic_string::append");
1259 return _M_append(__s, __n);
1263 * @brief Append multiple characters.
1264 * @param __n The number of characters to append.
1265 * @param __c The character to use.
1266 * @return Reference to this string.
1268 * Appends __n copies of __c to this string.
1270 basic_string&
1271 append(size_type __n, _CharT __c)
1272 { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
1274 #if __cplusplus >= 201103L
1276 * @brief Append an initializer_list of characters.
1277 * @param __l The initializer_list of characters to append.
1278 * @return Reference to this string.
1280 basic_string&
1281 append(initializer_list<_CharT> __l)
1282 { return this->append(__l.begin(), __l.size()); }
1283 #endif // C++11
1286 * @brief Append a range of characters.
1287 * @param __first Iterator referencing the first character to append.
1288 * @param __last Iterator marking the end of the range.
1289 * @return Reference to this string.
1291 * Appends characters in the range [__first,__last) to this string.
1293 #if __cplusplus >= 201103L
1294 template<class _InputIterator,
1295 typename = std::_RequireInputIter<_InputIterator>>
1296 #else
1297 template<class _InputIterator>
1298 #endif
1299 basic_string&
1300 append(_InputIterator __first, _InputIterator __last)
1301 { return this->replace(end(), end(), __first, __last); }
1303 #if __cplusplus > 201402L
1305 * @brief Append a string_view.
1306 * @param __svt An object convertible to string_view to be appended.
1307 * @return Reference to this string.
1309 template<typename _Tp>
1310 _If_sv<_Tp, basic_string&>
1311 append(const _Tp& __svt)
1313 __sv_type __sv = __svt;
1314 return this->append(__sv.data(), __sv.size());
1318 * @brief Append a range of characters from a string_view.
1319 * @param __svt An object convertible to string_view to be appended from.
1320 * @param __pos The position in the string_view to append from.
1321 * @param __n The number of characters to append from the string_view.
1322 * @return Reference to this string.
1324 template<typename _Tp>
1325 _If_sv<_Tp, basic_string&>
1326 append(const _Tp& __svt, size_type __pos, size_type __n = npos)
1328 __sv_type __sv = __svt;
1329 return _M_append(__sv.data()
1330 + __sv._M_check(__pos, "basic_string::append"),
1331 __sv._M_limit(__pos, __n));
1333 #endif // C++17
1336 * @brief Append a single character.
1337 * @param __c Character to append.
1339 void
1340 push_back(_CharT __c)
1342 const size_type __size = this->size();
1343 if (__size + 1 > this->capacity())
1344 this->_M_mutate(__size, size_type(0), 0, size_type(1));
1345 traits_type::assign(this->_M_data()[__size], __c);
1346 this->_M_set_length(__size + 1);
1350 * @brief Set value to contents of another string.
1351 * @param __str Source string to use.
1352 * @return Reference to this string.
1354 basic_string&
1355 assign(const basic_string& __str)
1357 this->_M_assign(__str);
1358 return *this;
1361 #if __cplusplus >= 201103L
1363 * @brief Set value to contents of another string.
1364 * @param __str Source string to use.
1365 * @return Reference to this string.
1367 * This function sets this string to the exact contents of @a __str.
1368 * @a __str is a valid, but unspecified string.
1370 basic_string&
1371 assign(basic_string&& __str)
1372 noexcept(_Alloc_traits::_S_nothrow_move())
1374 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1375 // 2063. Contradictory requirements for string move assignment
1376 return *this = std::move(__str);
1378 #endif // C++11
1381 * @brief Set value to a substring of a string.
1382 * @param __str The string to use.
1383 * @param __pos Index of the first character of str.
1384 * @param __n Number of characters to use.
1385 * @return Reference to this string.
1386 * @throw std::out_of_range if @a pos is not a valid index.
1388 * This function sets this string to the substring of @a __str
1389 * consisting of @a __n characters at @a __pos. If @a __n is
1390 * is larger than the number of available characters in @a
1391 * __str, the remainder of @a __str is used.
1393 basic_string&
1394 assign(const basic_string& __str, size_type __pos, size_type __n = npos)
1395 { return _M_replace(size_type(0), this->size(), __str._M_data()
1396 + __str._M_check(__pos, "basic_string::assign"),
1397 __str._M_limit(__pos, __n)); }
1400 * @brief Set value to a C substring.
1401 * @param __s The C string to use.
1402 * @param __n Number of characters to use.
1403 * @return Reference to this string.
1405 * This function sets the value of this string to the first @a __n
1406 * characters of @a __s. If @a __n is is larger than the number of
1407 * available characters in @a __s, the remainder of @a __s is used.
1409 basic_string&
1410 assign(const _CharT* __s, size_type __n)
1412 __glibcxx_requires_string_len(__s, __n);
1413 return _M_replace(size_type(0), this->size(), __s, __n);
1417 * @brief Set value to contents of a C string.
1418 * @param __s The C string to use.
1419 * @return Reference to this string.
1421 * This function sets the value of this string to the value of @a __s.
1422 * The data is copied, so there is no dependence on @a __s once the
1423 * function returns.
1425 basic_string&
1426 assign(const _CharT* __s)
1428 __glibcxx_requires_string(__s);
1429 return _M_replace(size_type(0), this->size(), __s,
1430 traits_type::length(__s));
1434 * @brief Set value to multiple characters.
1435 * @param __n Length of the resulting string.
1436 * @param __c The character to use.
1437 * @return Reference to this string.
1439 * This function sets the value of this string to @a __n copies of
1440 * character @a __c.
1442 basic_string&
1443 assign(size_type __n, _CharT __c)
1444 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1447 * @brief Set value to a range of characters.
1448 * @param __first Iterator referencing the first character to append.
1449 * @param __last Iterator marking the end of the range.
1450 * @return Reference to this string.
1452 * Sets value of string to characters in the range [__first,__last).
1454 #if __cplusplus >= 201103L
1455 template<class _InputIterator,
1456 typename = std::_RequireInputIter<_InputIterator>>
1457 #else
1458 template<class _InputIterator>
1459 #endif
1460 basic_string&
1461 assign(_InputIterator __first, _InputIterator __last)
1462 { return this->replace(begin(), end(), __first, __last); }
1464 #if __cplusplus >= 201103L
1466 * @brief Set value to an initializer_list of characters.
1467 * @param __l The initializer_list of characters to assign.
1468 * @return Reference to this string.
1470 basic_string&
1471 assign(initializer_list<_CharT> __l)
1472 { return this->assign(__l.begin(), __l.size()); }
1473 #endif // C++11
1475 #if __cplusplus > 201402L
1477 * @brief Set value from a string_view.
1478 * @param __svt The source object convertible to string_view.
1479 * @return Reference to this string.
1481 template<typename _Tp>
1482 _If_sv<_Tp, basic_string&>
1483 assign(const _Tp& __svt)
1485 __sv_type __sv = __svt;
1486 return this->assign(__sv.data(), __sv.size());
1490 * @brief Set value from a range of characters in a string_view.
1491 * @param __svt The source object convertible to string_view.
1492 * @param __pos The position in the string_view to assign from.
1493 * @param __n The number of characters to assign.
1494 * @return Reference to this string.
1496 template<typename _Tp>
1497 _If_sv<_Tp, basic_string&>
1498 assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
1500 __sv_type __sv = __svt;
1501 return _M_replace(size_type(0), this->size(), __sv.data()
1502 + __sv._M_check(__pos, "basic_string::assign"),
1503 __sv._M_limit(__pos, __n));
1505 #endif // C++17
1507 #if __cplusplus >= 201103L
1509 * @brief Insert multiple characters.
1510 * @param __p Const_iterator referencing location in string to
1511 * insert at.
1512 * @param __n Number of characters to insert
1513 * @param __c The character to insert.
1514 * @return Iterator referencing the first inserted char.
1515 * @throw std::length_error If new length exceeds @c max_size().
1517 * Inserts @a __n copies of character @a __c starting at the
1518 * position referenced by iterator @a __p. If adding
1519 * characters causes the length to exceed max_size(),
1520 * length_error is thrown. The value of the string doesn't
1521 * change if an error is thrown.
1523 iterator
1524 insert(const_iterator __p, size_type __n, _CharT __c)
1526 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1527 const size_type __pos = __p - begin();
1528 this->replace(__p, __p, __n, __c);
1529 return iterator(this->_M_data() + __pos);
1531 #else
1533 * @brief Insert multiple characters.
1534 * @param __p Iterator referencing location in string to insert at.
1535 * @param __n Number of characters to insert
1536 * @param __c The character to insert.
1537 * @throw std::length_error If new length exceeds @c max_size().
1539 * Inserts @a __n copies of character @a __c starting at the
1540 * position referenced by iterator @a __p. If adding
1541 * characters causes the length to exceed max_size(),
1542 * length_error is thrown. The value of the string doesn't
1543 * change if an error is thrown.
1545 void
1546 insert(iterator __p, size_type __n, _CharT __c)
1547 { this->replace(__p, __p, __n, __c); }
1548 #endif
1550 #if __cplusplus >= 201103L
1552 * @brief Insert a range of characters.
1553 * @param __p Const_iterator referencing location in string to
1554 * insert at.
1555 * @param __beg Start of range.
1556 * @param __end End of range.
1557 * @return Iterator referencing the first inserted char.
1558 * @throw std::length_error If new length exceeds @c max_size().
1560 * Inserts characters in range [beg,end). If adding characters
1561 * causes the length to exceed max_size(), length_error is
1562 * thrown. The value of the string doesn't change if an error
1563 * is thrown.
1565 template<class _InputIterator,
1566 typename = std::_RequireInputIter<_InputIterator>>
1567 iterator
1568 insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
1570 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1571 const size_type __pos = __p - begin();
1572 this->replace(__p, __p, __beg, __end);
1573 return iterator(this->_M_data() + __pos);
1575 #else
1577 * @brief Insert a range of characters.
1578 * @param __p Iterator referencing location in string to insert at.
1579 * @param __beg Start of range.
1580 * @param __end End of range.
1581 * @throw std::length_error If new length exceeds @c max_size().
1583 * Inserts characters in range [__beg,__end). If adding
1584 * characters causes the length to exceed max_size(),
1585 * length_error is thrown. The value of the string doesn't
1586 * change if an error is thrown.
1588 template<class _InputIterator>
1589 void
1590 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1591 { this->replace(__p, __p, __beg, __end); }
1592 #endif
1594 #if __cplusplus >= 201103L
1596 * @brief Insert an initializer_list of characters.
1597 * @param __p Iterator referencing location in string to insert at.
1598 * @param __l The initializer_list of characters to insert.
1599 * @throw std::length_error If new length exceeds @c max_size().
1601 iterator
1602 insert(const_iterator __p, initializer_list<_CharT> __l)
1603 { return this->insert(__p, __l.begin(), __l.end()); }
1605 #ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
1606 // See PR libstdc++/83328
1607 void
1608 insert(iterator __p, initializer_list<_CharT> __l)
1610 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1611 this->insert(__p - begin(), __l.begin(), __l.size());
1613 #endif
1614 #endif // C++11
1617 * @brief Insert value of a string.
1618 * @param __pos1 Iterator referencing location in string to insert at.
1619 * @param __str The string to insert.
1620 * @return Reference to this string.
1621 * @throw std::length_error If new length exceeds @c max_size().
1623 * Inserts value of @a __str starting at @a __pos1. If adding
1624 * characters causes the length to exceed max_size(),
1625 * length_error is thrown. The value of the string doesn't
1626 * change if an error is thrown.
1628 basic_string&
1629 insert(size_type __pos1, const basic_string& __str)
1630 { return this->replace(__pos1, size_type(0),
1631 __str._M_data(), __str.size()); }
1634 * @brief Insert a substring.
1635 * @param __pos1 Iterator referencing location in string to insert at.
1636 * @param __str The string to insert.
1637 * @param __pos2 Start of characters in str to insert.
1638 * @param __n Number of characters to insert.
1639 * @return Reference to this string.
1640 * @throw std::length_error If new length exceeds @c max_size().
1641 * @throw std::out_of_range If @a pos1 > size() or
1642 * @a __pos2 > @a str.size().
1644 * Starting at @a pos1, insert @a __n character of @a __str
1645 * beginning with @a __pos2. If adding characters causes the
1646 * length to exceed max_size(), length_error is thrown. If @a
1647 * __pos1 is beyond the end of this string or @a __pos2 is
1648 * beyond the end of @a __str, out_of_range is thrown. The
1649 * value of the string doesn't change if an error is thrown.
1651 basic_string&
1652 insert(size_type __pos1, const basic_string& __str,
1653 size_type __pos2, size_type __n = npos)
1654 { return this->replace(__pos1, size_type(0), __str._M_data()
1655 + __str._M_check(__pos2, "basic_string::insert"),
1656 __str._M_limit(__pos2, __n)); }
1659 * @brief Insert a C substring.
1660 * @param __pos Iterator referencing location in string to insert at.
1661 * @param __s The C string to insert.
1662 * @param __n The number of characters to insert.
1663 * @return Reference to this string.
1664 * @throw std::length_error If new length exceeds @c max_size().
1665 * @throw std::out_of_range If @a __pos is beyond the end of this
1666 * string.
1668 * Inserts the first @a __n characters of @a __s starting at @a
1669 * __pos. If adding characters causes the length to exceed
1670 * max_size(), length_error is thrown. If @a __pos is beyond
1671 * end(), out_of_range is thrown. The value of the string
1672 * doesn't change if an error is thrown.
1674 basic_string&
1675 insert(size_type __pos, const _CharT* __s, size_type __n)
1676 { return this->replace(__pos, size_type(0), __s, __n); }
1679 * @brief Insert a C string.
1680 * @param __pos Iterator referencing location in string to insert at.
1681 * @param __s The C string to insert.
1682 * @return Reference to this string.
1683 * @throw std::length_error If new length exceeds @c max_size().
1684 * @throw std::out_of_range If @a pos is beyond the end of this
1685 * string.
1687 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1688 * adding characters causes the length to exceed max_size(),
1689 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1690 * thrown. The value of the string doesn't change if an error is
1691 * thrown.
1693 basic_string&
1694 insert(size_type __pos, const _CharT* __s)
1696 __glibcxx_requires_string(__s);
1697 return this->replace(__pos, size_type(0), __s,
1698 traits_type::length(__s));
1702 * @brief Insert multiple characters.
1703 * @param __pos Index in string to insert at.
1704 * @param __n Number of characters to insert
1705 * @param __c The character to insert.
1706 * @return Reference to this string.
1707 * @throw std::length_error If new length exceeds @c max_size().
1708 * @throw std::out_of_range If @a __pos is beyond the end of this
1709 * string.
1711 * Inserts @a __n copies of character @a __c starting at index
1712 * @a __pos. If adding characters causes the length to exceed
1713 * max_size(), length_error is thrown. If @a __pos > length(),
1714 * out_of_range is thrown. The value of the string doesn't
1715 * change if an error is thrown.
1717 basic_string&
1718 insert(size_type __pos, size_type __n, _CharT __c)
1719 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1720 size_type(0), __n, __c); }
1723 * @brief Insert one character.
1724 * @param __p Iterator referencing position in string to insert at.
1725 * @param __c The character to insert.
1726 * @return Iterator referencing newly inserted char.
1727 * @throw std::length_error If new length exceeds @c max_size().
1729 * Inserts character @a __c at position referenced by @a __p.
1730 * If adding character causes the length to exceed max_size(),
1731 * length_error is thrown. If @a __p is beyond end of string,
1732 * out_of_range is thrown. The value of the string doesn't
1733 * change if an error is thrown.
1735 iterator
1736 insert(__const_iterator __p, _CharT __c)
1738 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1739 const size_type __pos = __p - begin();
1740 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1741 return iterator(_M_data() + __pos);
1744 #if __cplusplus > 201402L
1746 * @brief Insert a string_view.
1747 * @param __pos Iterator referencing position in string to insert at.
1748 * @param __svt The object convertible to string_view to insert.
1749 * @return Reference to this string.
1751 template<typename _Tp>
1752 _If_sv<_Tp, basic_string&>
1753 insert(size_type __pos, const _Tp& __svt)
1755 __sv_type __sv = __svt;
1756 return this->insert(__pos, __sv.data(), __sv.size());
1760 * @brief Insert a string_view.
1761 * @param __pos Iterator referencing position in string to insert at.
1762 * @param __svt The object convertible to string_view to insert from.
1763 * @param __pos Iterator referencing position in string_view to insert
1764 * from.
1765 * @param __n The number of characters to insert.
1766 * @return Reference to this string.
1768 template<typename _Tp>
1769 _If_sv<_Tp, basic_string&>
1770 insert(size_type __pos1, const _Tp& __svt,
1771 size_type __pos2, size_type __n = npos)
1773 __sv_type __sv = __svt;
1774 return this->replace(__pos1, size_type(0), __sv.data()
1775 + __sv._M_check(__pos2, "basic_string::insert"),
1776 __sv._M_limit(__pos2, __n));
1778 #endif // C++17
1781 * @brief Remove characters.
1782 * @param __pos Index of first character to remove (default 0).
1783 * @param __n Number of characters to remove (default remainder).
1784 * @return Reference to this string.
1785 * @throw std::out_of_range If @a pos is beyond the end of this
1786 * string.
1788 * Removes @a __n characters from this string starting at @a
1789 * __pos. The length of the string is reduced by @a __n. If
1790 * there are < @a __n characters to remove, the remainder of
1791 * the string is truncated. If @a __p is beyond end of string,
1792 * out_of_range is thrown. The value of the string doesn't
1793 * change if an error is thrown.
1795 basic_string&
1796 erase(size_type __pos = 0, size_type __n = npos)
1798 _M_check(__pos, "basic_string::erase");
1799 if (__n == npos)
1800 this->_M_set_length(__pos);
1801 else if (__n != 0)
1802 this->_M_erase(__pos, _M_limit(__pos, __n));
1803 return *this;
1807 * @brief Remove one character.
1808 * @param __position Iterator referencing the character to remove.
1809 * @return iterator referencing same location after removal.
1811 * Removes the character at @a __position from this string. The value
1812 * of the string doesn't change if an error is thrown.
1814 iterator
1815 erase(__const_iterator __position)
1817 _GLIBCXX_DEBUG_PEDASSERT(__position >= begin()
1818 && __position < end());
1819 const size_type __pos = __position - begin();
1820 this->_M_erase(__pos, size_type(1));
1821 return iterator(_M_data() + __pos);
1825 * @brief Remove a range of characters.
1826 * @param __first Iterator referencing the first character to remove.
1827 * @param __last Iterator referencing the end of the range.
1828 * @return Iterator referencing location of first after removal.
1830 * Removes the characters in the range [first,last) from this string.
1831 * The value of the string doesn't change if an error is thrown.
1833 iterator
1834 erase(__const_iterator __first, __const_iterator __last)
1836 _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
1837 && __last <= end());
1838 const size_type __pos = __first - begin();
1839 if (__last == end())
1840 this->_M_set_length(__pos);
1841 else
1842 this->_M_erase(__pos, __last - __first);
1843 return iterator(this->_M_data() + __pos);
1846 #if __cplusplus >= 201103L
1848 * @brief Remove the last character.
1850 * The string must be non-empty.
1852 void
1853 pop_back() noexcept
1855 __glibcxx_assert(!empty());
1856 _M_erase(size() - 1, 1);
1858 #endif // C++11
1861 * @brief Replace characters with value from another string.
1862 * @param __pos Index of first character to replace.
1863 * @param __n Number of characters to be replaced.
1864 * @param __str String to insert.
1865 * @return Reference to this string.
1866 * @throw std::out_of_range If @a pos is beyond the end of this
1867 * string.
1868 * @throw std::length_error If new length exceeds @c max_size().
1870 * Removes the characters in the range [__pos,__pos+__n) from
1871 * this string. In place, the value of @a __str is inserted.
1872 * If @a __pos is beyond end of string, out_of_range is thrown.
1873 * If the length of the result exceeds max_size(), length_error
1874 * is thrown. The value of the string doesn't change if an
1875 * error is thrown.
1877 basic_string&
1878 replace(size_type __pos, size_type __n, const basic_string& __str)
1879 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1882 * @brief Replace characters with value from another string.
1883 * @param __pos1 Index of first character to replace.
1884 * @param __n1 Number of characters to be replaced.
1885 * @param __str String to insert.
1886 * @param __pos2 Index of first character of str to use.
1887 * @param __n2 Number of characters from str to use.
1888 * @return Reference to this string.
1889 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1890 * __str.size().
1891 * @throw std::length_error If new length exceeds @c max_size().
1893 * Removes the characters in the range [__pos1,__pos1 + n) from this
1894 * string. In place, the value of @a __str is inserted. If @a __pos is
1895 * beyond end of string, out_of_range is thrown. If the length of the
1896 * result exceeds max_size(), length_error is thrown. The value of the
1897 * string doesn't change if an error is thrown.
1899 basic_string&
1900 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1901 size_type __pos2, size_type __n2 = npos)
1902 { return this->replace(__pos1, __n1, __str._M_data()
1903 + __str._M_check(__pos2, "basic_string::replace"),
1904 __str._M_limit(__pos2, __n2)); }
1907 * @brief Replace characters with value of a C substring.
1908 * @param __pos Index of first character to replace.
1909 * @param __n1 Number of characters to be replaced.
1910 * @param __s C string to insert.
1911 * @param __n2 Number of characters from @a s to use.
1912 * @return Reference to this string.
1913 * @throw std::out_of_range If @a pos1 > size().
1914 * @throw std::length_error If new length exceeds @c max_size().
1916 * Removes the characters in the range [__pos,__pos + __n1)
1917 * from this string. In place, the first @a __n2 characters of
1918 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1919 * @a __pos is beyond end of string, out_of_range is thrown. If
1920 * the length of result exceeds max_size(), length_error is
1921 * thrown. The value of the string doesn't change if an error
1922 * is thrown.
1924 basic_string&
1925 replace(size_type __pos, size_type __n1, const _CharT* __s,
1926 size_type __n2)
1928 __glibcxx_requires_string_len(__s, __n2);
1929 return _M_replace(_M_check(__pos, "basic_string::replace"),
1930 _M_limit(__pos, __n1), __s, __n2);
1934 * @brief Replace characters with value of a C string.
1935 * @param __pos Index of first character to replace.
1936 * @param __n1 Number of characters to be replaced.
1937 * @param __s C string to insert.
1938 * @return Reference to this string.
1939 * @throw std::out_of_range If @a pos > size().
1940 * @throw std::length_error If new length exceeds @c max_size().
1942 * Removes the characters in the range [__pos,__pos + __n1)
1943 * from this string. In place, the characters of @a __s are
1944 * inserted. If @a __pos is beyond end of string, out_of_range
1945 * is thrown. If the length of result exceeds max_size(),
1946 * length_error is thrown. The value of the string doesn't
1947 * change if an error is thrown.
1949 basic_string&
1950 replace(size_type __pos, size_type __n1, const _CharT* __s)
1952 __glibcxx_requires_string(__s);
1953 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1957 * @brief Replace characters with multiple characters.
1958 * @param __pos Index of first character to replace.
1959 * @param __n1 Number of characters to be replaced.
1960 * @param __n2 Number of characters to insert.
1961 * @param __c Character to insert.
1962 * @return Reference to this string.
1963 * @throw std::out_of_range If @a __pos > size().
1964 * @throw std::length_error If new length exceeds @c max_size().
1966 * Removes the characters in the range [pos,pos + n1) from this
1967 * string. In place, @a __n2 copies of @a __c are inserted.
1968 * If @a __pos is beyond end of string, out_of_range is thrown.
1969 * If the length of result exceeds max_size(), length_error is
1970 * thrown. The value of the string doesn't change if an error
1971 * is thrown.
1973 basic_string&
1974 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1975 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1976 _M_limit(__pos, __n1), __n2, __c); }
1979 * @brief Replace range of characters with string.
1980 * @param __i1 Iterator referencing start of range to replace.
1981 * @param __i2 Iterator referencing end of range to replace.
1982 * @param __str String value to insert.
1983 * @return Reference to this string.
1984 * @throw std::length_error If new length exceeds @c max_size().
1986 * Removes the characters in the range [__i1,__i2). In place,
1987 * the value of @a __str is inserted. If the length of result
1988 * exceeds max_size(), length_error is thrown. The value of
1989 * the string doesn't change if an error is thrown.
1991 basic_string&
1992 replace(__const_iterator __i1, __const_iterator __i2,
1993 const basic_string& __str)
1994 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1997 * @brief Replace range of characters with C substring.
1998 * @param __i1 Iterator referencing start of range to replace.
1999 * @param __i2 Iterator referencing end of range to replace.
2000 * @param __s C string value to insert.
2001 * @param __n Number of characters from s to insert.
2002 * @return Reference to this string.
2003 * @throw std::length_error If new length exceeds @c max_size().
2005 * Removes the characters in the range [__i1,__i2). In place,
2006 * the first @a __n characters of @a __s are inserted. If the
2007 * length of result exceeds max_size(), length_error is thrown.
2008 * The value of the string doesn't change if an error is
2009 * thrown.
2011 basic_string&
2012 replace(__const_iterator __i1, __const_iterator __i2,
2013 const _CharT* __s, size_type __n)
2015 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2016 && __i2 <= end());
2017 return this->replace(__i1 - begin(), __i2 - __i1, __s, __n);
2021 * @brief Replace range of characters with C string.
2022 * @param __i1 Iterator referencing start of range to replace.
2023 * @param __i2 Iterator referencing end of range to replace.
2024 * @param __s C string value to insert.
2025 * @return Reference to this string.
2026 * @throw std::length_error If new length exceeds @c max_size().
2028 * Removes the characters in the range [__i1,__i2). In place,
2029 * the characters of @a __s are inserted. If the length of
2030 * result exceeds max_size(), length_error is thrown. The
2031 * value of the string doesn't change if an error is thrown.
2033 basic_string&
2034 replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s)
2036 __glibcxx_requires_string(__s);
2037 return this->replace(__i1, __i2, __s, traits_type::length(__s));
2041 * @brief Replace range of characters with multiple characters
2042 * @param __i1 Iterator referencing start of range to replace.
2043 * @param __i2 Iterator referencing end of range to replace.
2044 * @param __n Number of characters to insert.
2045 * @param __c Character to insert.
2046 * @return Reference to this string.
2047 * @throw std::length_error If new length exceeds @c max_size().
2049 * Removes the characters in the range [__i1,__i2). In place,
2050 * @a __n copies of @a __c are inserted. If the length of
2051 * result exceeds max_size(), length_error is thrown. The
2052 * value of the string doesn't change if an error is thrown.
2054 basic_string&
2055 replace(__const_iterator __i1, __const_iterator __i2, size_type __n,
2056 _CharT __c)
2058 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2059 && __i2 <= end());
2060 return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c);
2064 * @brief Replace range of characters with range.
2065 * @param __i1 Iterator referencing start of range to replace.
2066 * @param __i2 Iterator referencing end of range to replace.
2067 * @param __k1 Iterator referencing start of range to insert.
2068 * @param __k2 Iterator referencing end of range to insert.
2069 * @return Reference to this string.
2070 * @throw std::length_error If new length exceeds @c max_size().
2072 * Removes the characters in the range [__i1,__i2). In place,
2073 * characters in the range [__k1,__k2) are inserted. If the
2074 * length of result exceeds max_size(), length_error is thrown.
2075 * The value of the string doesn't change if an error is
2076 * thrown.
2078 #if __cplusplus >= 201103L
2079 template<class _InputIterator,
2080 typename = std::_RequireInputIter<_InputIterator>>
2081 basic_string&
2082 replace(const_iterator __i1, const_iterator __i2,
2083 _InputIterator __k1, _InputIterator __k2)
2085 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2086 && __i2 <= end());
2087 __glibcxx_requires_valid_range(__k1, __k2);
2088 return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
2089 std::__false_type());
2091 #else
2092 template<class _InputIterator>
2093 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2094 typename __enable_if_not_native_iterator<_InputIterator>::__type
2095 #else
2096 basic_string&
2097 #endif
2098 replace(iterator __i1, iterator __i2,
2099 _InputIterator __k1, _InputIterator __k2)
2101 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2102 && __i2 <= end());
2103 __glibcxx_requires_valid_range(__k1, __k2);
2104 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
2105 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
2107 #endif
2109 // Specializations for the common case of pointer and iterator:
2110 // useful to avoid the overhead of temporary buffering in _M_replace.
2111 basic_string&
2112 replace(__const_iterator __i1, __const_iterator __i2,
2113 _CharT* __k1, _CharT* __k2)
2115 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2116 && __i2 <= end());
2117 __glibcxx_requires_valid_range(__k1, __k2);
2118 return this->replace(__i1 - begin(), __i2 - __i1,
2119 __k1, __k2 - __k1);
2122 basic_string&
2123 replace(__const_iterator __i1, __const_iterator __i2,
2124 const _CharT* __k1, const _CharT* __k2)
2126 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2127 && __i2 <= end());
2128 __glibcxx_requires_valid_range(__k1, __k2);
2129 return this->replace(__i1 - begin(), __i2 - __i1,
2130 __k1, __k2 - __k1);
2133 basic_string&
2134 replace(__const_iterator __i1, __const_iterator __i2,
2135 iterator __k1, iterator __k2)
2137 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2138 && __i2 <= end());
2139 __glibcxx_requires_valid_range(__k1, __k2);
2140 return this->replace(__i1 - begin(), __i2 - __i1,
2141 __k1.base(), __k2 - __k1);
2144 basic_string&
2145 replace(__const_iterator __i1, __const_iterator __i2,
2146 const_iterator __k1, const_iterator __k2)
2148 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2149 && __i2 <= end());
2150 __glibcxx_requires_valid_range(__k1, __k2);
2151 return this->replace(__i1 - begin(), __i2 - __i1,
2152 __k1.base(), __k2 - __k1);
2155 #if __cplusplus >= 201103L
2157 * @brief Replace range of characters with initializer_list.
2158 * @param __i1 Iterator referencing start of range to replace.
2159 * @param __i2 Iterator referencing end of range to replace.
2160 * @param __l The initializer_list of characters to insert.
2161 * @return Reference to this string.
2162 * @throw std::length_error If new length exceeds @c max_size().
2164 * Removes the characters in the range [__i1,__i2). In place,
2165 * characters in the range [__k1,__k2) are inserted. If the
2166 * length of result exceeds max_size(), length_error is thrown.
2167 * The value of the string doesn't change if an error is
2168 * thrown.
2170 basic_string& replace(const_iterator __i1, const_iterator __i2,
2171 initializer_list<_CharT> __l)
2172 { return this->replace(__i1, __i2, __l.begin(), __l.size()); }
2173 #endif // C++11
2175 #if __cplusplus > 201402L
2177 * @brief Replace range of characters with string_view.
2178 * @param __pos The position to replace at.
2179 * @param __n The number of characters to replace.
2180 * @param __svt The object convertible to string_view to insert.
2181 * @return Reference to this string.
2183 template<typename _Tp>
2184 _If_sv<_Tp, basic_string&>
2185 replace(size_type __pos, size_type __n, const _Tp& __svt)
2187 __sv_type __sv = __svt;
2188 return this->replace(__pos, __n, __sv.data(), __sv.size());
2192 * @brief Replace range of characters with string_view.
2193 * @param __pos1 The position to replace at.
2194 * @param __n1 The number of characters to replace.
2195 * @param __svt The object convertible to string_view to insert from.
2196 * @param __pos2 The position in the string_view to insert from.
2197 * @param __n2 The number of characters to insert.
2198 * @return Reference to this string.
2200 template<typename _Tp>
2201 _If_sv<_Tp, basic_string&>
2202 replace(size_type __pos1, size_type __n1, const _Tp& __svt,
2203 size_type __pos2, size_type __n2 = npos)
2205 __sv_type __sv = __svt;
2206 return this->replace(__pos1, __n1, __sv.data()
2207 + __sv._M_check(__pos2, "basic_string::replace"),
2208 __sv._M_limit(__pos2, __n2));
2212 * @brief Replace range of characters with string_view.
2213 * @param __i1 An iterator referencing the start position
2214 to replace at.
2215 * @param __i2 An iterator referencing the end position
2216 for the replace.
2217 * @param __svt The object convertible to string_view to insert from.
2218 * @return Reference to this string.
2220 template<typename _Tp>
2221 _If_sv<_Tp, basic_string&>
2222 replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
2224 __sv_type __sv = __svt;
2225 return this->replace(__i1 - begin(), __i2 - __i1, __sv);
2227 #endif // C++17
2229 private:
2230 template<class _Integer>
2231 basic_string&
2232 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2233 _Integer __n, _Integer __val, __true_type)
2234 { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); }
2236 template<class _InputIterator>
2237 basic_string&
2238 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2239 _InputIterator __k1, _InputIterator __k2,
2240 __false_type);
2242 basic_string&
2243 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
2244 _CharT __c);
2246 basic_string&
2247 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
2248 const size_type __len2);
2250 basic_string&
2251 _M_append(const _CharT* __s, size_type __n);
2253 public:
2256 * @brief Copy substring into C string.
2257 * @param __s C string to copy value into.
2258 * @param __n Number of characters to copy.
2259 * @param __pos Index of first character to copy.
2260 * @return Number of characters actually copied
2261 * @throw std::out_of_range If __pos > size().
2263 * Copies up to @a __n characters starting at @a __pos into the
2264 * C string @a __s. If @a __pos is %greater than size(),
2265 * out_of_range is thrown.
2267 size_type
2268 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
2271 * @brief Swap contents with another string.
2272 * @param __s String to swap with.
2274 * Exchanges the contents of this string with that of @a __s in constant
2275 * time.
2277 void
2278 swap(basic_string& __s) _GLIBCXX_NOEXCEPT;
2280 // String operations:
2282 * @brief Return const pointer to null-terminated contents.
2284 * This is a handle to internal data. Do not modify or dire things may
2285 * happen.
2287 const _CharT*
2288 c_str() const _GLIBCXX_NOEXCEPT
2289 { return _M_data(); }
2292 * @brief Return const pointer to contents.
2294 * This is a pointer to internal data. It is undefined to modify
2295 * the contents through the returned pointer. To get a pointer that
2296 * allows modifying the contents use @c &str[0] instead,
2297 * (or in C++17 the non-const @c str.data() overload).
2299 const _CharT*
2300 data() const _GLIBCXX_NOEXCEPT
2301 { return _M_data(); }
2303 #if __cplusplus > 201402L
2305 * @brief Return non-const pointer to contents.
2307 * This is a pointer to the character sequence held by the string.
2308 * Modifying the characters in the sequence is allowed.
2310 _CharT*
2311 data() noexcept
2312 { return _M_data(); }
2313 #endif
2316 * @brief Return copy of allocator used to construct this string.
2318 allocator_type
2319 get_allocator() const _GLIBCXX_NOEXCEPT
2320 { return _M_get_allocator(); }
2323 * @brief Find position of a C substring.
2324 * @param __s C string to locate.
2325 * @param __pos Index of character to search from.
2326 * @param __n Number of characters from @a s to search for.
2327 * @return Index of start of first occurrence.
2329 * Starting from @a __pos, searches forward for the first @a
2330 * __n characters in @a __s within this string. If found,
2331 * returns the index where it begins. If not found, returns
2332 * npos.
2334 size_type
2335 find(const _CharT* __s, size_type __pos, size_type __n) const
2336 _GLIBCXX_NOEXCEPT;
2339 * @brief Find position of a string.
2340 * @param __str String to locate.
2341 * @param __pos Index of character to search from (default 0).
2342 * @return Index of start of first occurrence.
2344 * Starting from @a __pos, searches forward for value of @a __str within
2345 * this string. If found, returns the index where it begins. If not
2346 * found, returns npos.
2348 size_type
2349 find(const basic_string& __str, size_type __pos = 0) const
2350 _GLIBCXX_NOEXCEPT
2351 { return this->find(__str.data(), __pos, __str.size()); }
2353 #if __cplusplus > 201402L
2355 * @brief Find position of a string_view.
2356 * @param __svt The object convertible to string_view to locate.
2357 * @param __pos Index of character to search from (default 0).
2358 * @return Index of start of first occurrence.
2360 template<typename _Tp>
2361 _If_sv<_Tp, size_type>
2362 find(const _Tp& __svt, size_type __pos = 0) const
2363 noexcept(is_same<_Tp, __sv_type>::value)
2365 __sv_type __sv = __svt;
2366 return this->find(__sv.data(), __pos, __sv.size());
2368 #endif // C++17
2371 * @brief Find position of a C string.
2372 * @param __s C string to locate.
2373 * @param __pos Index of character to search from (default 0).
2374 * @return Index of start of first occurrence.
2376 * Starting from @a __pos, searches forward for the value of @a
2377 * __s within this string. If found, returns the index where
2378 * it begins. If not found, returns npos.
2380 size_type
2381 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2383 __glibcxx_requires_string(__s);
2384 return this->find(__s, __pos, traits_type::length(__s));
2388 * @brief Find position of a character.
2389 * @param __c Character to locate.
2390 * @param __pos Index of character to search from (default 0).
2391 * @return Index of first occurrence.
2393 * Starting from @a __pos, searches forward for @a __c within
2394 * this string. If found, returns the index where it was
2395 * found. If not found, returns npos.
2397 size_type
2398 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
2401 * @brief Find last position of a string.
2402 * @param __str String to locate.
2403 * @param __pos Index of character to search back from (default end).
2404 * @return Index of start of last occurrence.
2406 * Starting from @a __pos, searches backward for value of @a
2407 * __str within this string. If found, returns the index where
2408 * it begins. If not found, returns npos.
2410 size_type
2411 rfind(const basic_string& __str, size_type __pos = npos) const
2412 _GLIBCXX_NOEXCEPT
2413 { return this->rfind(__str.data(), __pos, __str.size()); }
2415 #if __cplusplus > 201402L
2417 * @brief Find last position of a string_view.
2418 * @param __svt The object convertible to string_view to locate.
2419 * @param __pos Index of character to search back from (default end).
2420 * @return Index of start of last occurrence.
2422 template<typename _Tp>
2423 _If_sv<_Tp, size_type>
2424 rfind(const _Tp& __svt, size_type __pos = npos) const
2425 noexcept(is_same<_Tp, __sv_type>::value)
2427 __sv_type __sv = __svt;
2428 return this->rfind(__sv.data(), __pos, __sv.size());
2430 #endif // C++17
2433 * @brief Find last position of a C substring.
2434 * @param __s C string to locate.
2435 * @param __pos Index of character to search back from.
2436 * @param __n Number of characters from s to search for.
2437 * @return Index of start of last occurrence.
2439 * Starting from @a __pos, searches backward for the first @a
2440 * __n characters in @a __s within this string. If found,
2441 * returns the index where it begins. If not found, returns
2442 * npos.
2444 size_type
2445 rfind(const _CharT* __s, size_type __pos, size_type __n) const
2446 _GLIBCXX_NOEXCEPT;
2449 * @brief Find last position of a C string.
2450 * @param __s C string to locate.
2451 * @param __pos Index of character to start search at (default end).
2452 * @return Index of start of last occurrence.
2454 * Starting from @a __pos, searches backward for the value of
2455 * @a __s within this string. If found, returns the index
2456 * where it begins. If not found, returns npos.
2458 size_type
2459 rfind(const _CharT* __s, size_type __pos = npos) const
2461 __glibcxx_requires_string(__s);
2462 return this->rfind(__s, __pos, traits_type::length(__s));
2466 * @brief Find last position of a character.
2467 * @param __c Character to locate.
2468 * @param __pos Index of character to search back from (default end).
2469 * @return Index of last occurrence.
2471 * Starting from @a __pos, searches backward for @a __c within
2472 * this string. If found, returns the index where it was
2473 * found. If not found, returns npos.
2475 size_type
2476 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
2479 * @brief Find position of a character of string.
2480 * @param __str String containing characters to locate.
2481 * @param __pos Index of character to search from (default 0).
2482 * @return Index of first occurrence.
2484 * Starting from @a __pos, searches forward for one of the
2485 * characters of @a __str within this string. If found,
2486 * returns the index where it was found. If not found, returns
2487 * npos.
2489 size_type
2490 find_first_of(const basic_string& __str, size_type __pos = 0) const
2491 _GLIBCXX_NOEXCEPT
2492 { return this->find_first_of(__str.data(), __pos, __str.size()); }
2494 #if __cplusplus > 201402L
2496 * @brief Find position of a character of a string_view.
2497 * @param __svt An object convertible to string_view containing
2498 * characters to locate.
2499 * @param __pos Index of character to search from (default 0).
2500 * @return Index of first occurrence.
2502 template<typename _Tp>
2503 _If_sv<_Tp, size_type>
2504 find_first_of(const _Tp& __svt, size_type __pos = 0) const
2505 noexcept(is_same<_Tp, __sv_type>::value)
2507 __sv_type __sv = __svt;
2508 return this->find_first_of(__sv.data(), __pos, __sv.size());
2510 #endif // C++17
2513 * @brief Find position of a character of C substring.
2514 * @param __s String containing characters to locate.
2515 * @param __pos Index of character to search from.
2516 * @param __n Number of characters from s to search for.
2517 * @return Index of first occurrence.
2519 * Starting from @a __pos, searches forward for one of the
2520 * first @a __n characters of @a __s within this string. If
2521 * found, returns the index where it was found. If not found,
2522 * returns npos.
2524 size_type
2525 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
2526 _GLIBCXX_NOEXCEPT;
2529 * @brief Find position of a character of C string.
2530 * @param __s String containing characters to locate.
2531 * @param __pos Index of character to search from (default 0).
2532 * @return Index of first occurrence.
2534 * Starting from @a __pos, searches forward for one of the
2535 * characters of @a __s within this string. If found, returns
2536 * the index where it was found. If not found, returns npos.
2538 size_type
2539 find_first_of(const _CharT* __s, size_type __pos = 0) const
2540 _GLIBCXX_NOEXCEPT
2542 __glibcxx_requires_string(__s);
2543 return this->find_first_of(__s, __pos, traits_type::length(__s));
2547 * @brief Find position of a character.
2548 * @param __c Character to locate.
2549 * @param __pos Index of character to search from (default 0).
2550 * @return Index of first occurrence.
2552 * Starting from @a __pos, searches forward for the character
2553 * @a __c within this string. If found, returns the index
2554 * where it was found. If not found, returns npos.
2556 * Note: equivalent to find(__c, __pos).
2558 size_type
2559 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2560 { return this->find(__c, __pos); }
2563 * @brief Find last position of a character of string.
2564 * @param __str String containing characters to locate.
2565 * @param __pos Index of character to search back from (default end).
2566 * @return Index of last occurrence.
2568 * Starting from @a __pos, searches backward for one of the
2569 * characters of @a __str within this string. If found,
2570 * returns the index where it was found. If not found, returns
2571 * npos.
2573 size_type
2574 find_last_of(const basic_string& __str, size_type __pos = npos) const
2575 _GLIBCXX_NOEXCEPT
2576 { return this->find_last_of(__str.data(), __pos, __str.size()); }
2578 #if __cplusplus > 201402L
2580 * @brief Find last position of a character of string.
2581 * @param __svt An object convertible to string_view containing
2582 * characters to locate.
2583 * @param __pos Index of character to search back from (default end).
2584 * @return Index of last occurrence.
2586 template<typename _Tp>
2587 _If_sv<_Tp, size_type>
2588 find_last_of(const _Tp& __svt, size_type __pos = npos) const
2589 noexcept(is_same<_Tp, __sv_type>::value)
2591 __sv_type __sv = __svt;
2592 return this->find_last_of(__sv.data(), __pos, __sv.size());
2594 #endif // C++17
2597 * @brief Find last position of a character of C substring.
2598 * @param __s C string containing characters to locate.
2599 * @param __pos Index of character to search back from.
2600 * @param __n Number of characters from s to search for.
2601 * @return Index of last occurrence.
2603 * Starting from @a __pos, searches backward for one of the
2604 * first @a __n characters of @a __s within this string. If
2605 * found, returns the index where it was found. If not found,
2606 * returns npos.
2608 size_type
2609 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
2610 _GLIBCXX_NOEXCEPT;
2613 * @brief Find last position of a character of C string.
2614 * @param __s C string containing characters to locate.
2615 * @param __pos Index of character to search back from (default end).
2616 * @return Index of last occurrence.
2618 * Starting from @a __pos, searches backward for one of the
2619 * characters of @a __s within this string. If found, returns
2620 * the index where it was found. If not found, returns npos.
2622 size_type
2623 find_last_of(const _CharT* __s, size_type __pos = npos) const
2624 _GLIBCXX_NOEXCEPT
2626 __glibcxx_requires_string(__s);
2627 return this->find_last_of(__s, __pos, traits_type::length(__s));
2631 * @brief Find last position of a character.
2632 * @param __c Character to locate.
2633 * @param __pos Index of character to search back from (default end).
2634 * @return Index of last occurrence.
2636 * Starting from @a __pos, searches backward for @a __c within
2637 * this string. If found, returns the index where it was
2638 * found. If not found, returns npos.
2640 * Note: equivalent to rfind(__c, __pos).
2642 size_type
2643 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
2644 { return this->rfind(__c, __pos); }
2647 * @brief Find position of a character not in string.
2648 * @param __str String containing characters to avoid.
2649 * @param __pos Index of character to search from (default 0).
2650 * @return Index of first occurrence.
2652 * Starting from @a __pos, searches forward for a character not contained
2653 * in @a __str within this string. If found, returns the index where it
2654 * was found. If not found, returns npos.
2656 size_type
2657 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
2658 _GLIBCXX_NOEXCEPT
2659 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
2661 #if __cplusplus > 201402L
2663 * @brief Find position of a character not in a string_view.
2664 * @param __svt A object convertible to string_view containing
2665 * characters to avoid.
2666 * @param __pos Index of character to search from (default 0).
2667 * @return Index of first occurrence.
2669 template<typename _Tp>
2670 _If_sv<_Tp, size_type>
2671 find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
2672 noexcept(is_same<_Tp, __sv_type>::value)
2674 __sv_type __sv = __svt;
2675 return this->find_first_not_of(__sv.data(), __pos, __sv.size());
2677 #endif // C++17
2680 * @brief Find position of a character not in C substring.
2681 * @param __s C string containing characters to avoid.
2682 * @param __pos Index of character to search from.
2683 * @param __n Number of characters from __s to consider.
2684 * @return Index of first occurrence.
2686 * Starting from @a __pos, searches forward for a character not
2687 * contained in the first @a __n characters of @a __s within
2688 * this string. If found, returns the index where it was
2689 * found. If not found, returns npos.
2691 size_type
2692 find_first_not_of(const _CharT* __s, size_type __pos,
2693 size_type __n) const _GLIBCXX_NOEXCEPT;
2696 * @brief Find position of a character not in C string.
2697 * @param __s C string containing characters to avoid.
2698 * @param __pos Index of character to search from (default 0).
2699 * @return Index of first occurrence.
2701 * Starting from @a __pos, searches forward for a character not
2702 * contained in @a __s within this string. If found, returns
2703 * the index where it was found. If not found, returns npos.
2705 size_type
2706 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
2707 _GLIBCXX_NOEXCEPT
2709 __glibcxx_requires_string(__s);
2710 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
2714 * @brief Find position of a different character.
2715 * @param __c Character to avoid.
2716 * @param __pos Index of character to search from (default 0).
2717 * @return Index of first occurrence.
2719 * Starting from @a __pos, searches forward for a character
2720 * other than @a __c within this string. If found, returns the
2721 * index where it was found. If not found, returns npos.
2723 size_type
2724 find_first_not_of(_CharT __c, size_type __pos = 0) const
2725 _GLIBCXX_NOEXCEPT;
2728 * @brief Find last position of a character not in string.
2729 * @param __str String containing characters to avoid.
2730 * @param __pos Index of character to search back from (default end).
2731 * @return Index of last occurrence.
2733 * Starting from @a __pos, searches backward for a character
2734 * not contained in @a __str within this string. If found,
2735 * returns the index where it was found. If not found, returns
2736 * npos.
2738 size_type
2739 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
2740 _GLIBCXX_NOEXCEPT
2741 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
2743 #if __cplusplus > 201402L
2745 * @brief Find last position of a character not in a string_view.
2746 * @param __svt An object convertible to string_view containing
2747 * characters to avoid.
2748 * @param __pos Index of character to search back from (default end).
2749 * @return Index of last occurrence.
2751 template<typename _Tp>
2752 _If_sv<_Tp, size_type>
2753 find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
2754 noexcept(is_same<_Tp, __sv_type>::value)
2756 __sv_type __sv = __svt;
2757 return this->find_last_not_of(__sv.data(), __pos, __sv.size());
2759 #endif // C++17
2762 * @brief Find last position of a character not in C substring.
2763 * @param __s C string containing characters to avoid.
2764 * @param __pos Index of character to search back from.
2765 * @param __n Number of characters from s to consider.
2766 * @return Index of last occurrence.
2768 * Starting from @a __pos, searches backward for a character not
2769 * contained in the first @a __n characters of @a __s within this string.
2770 * If found, returns the index where it was found. If not found,
2771 * returns npos.
2773 size_type
2774 find_last_not_of(const _CharT* __s, size_type __pos,
2775 size_type __n) const _GLIBCXX_NOEXCEPT;
2777 * @brief Find last position of a character not in C string.
2778 * @param __s C string containing characters to avoid.
2779 * @param __pos Index of character to search back from (default end).
2780 * @return Index of last occurrence.
2782 * Starting from @a __pos, searches backward for a character
2783 * not contained in @a __s within this string. If found,
2784 * returns the index where it was found. If not found, returns
2785 * npos.
2787 size_type
2788 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
2789 _GLIBCXX_NOEXCEPT
2791 __glibcxx_requires_string(__s);
2792 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
2796 * @brief Find last position of a different character.
2797 * @param __c Character to avoid.
2798 * @param __pos Index of character to search back from (default end).
2799 * @return Index of last occurrence.
2801 * Starting from @a __pos, searches backward for a character other than
2802 * @a __c within this string. If found, returns the index where it was
2803 * found. If not found, returns npos.
2805 size_type
2806 find_last_not_of(_CharT __c, size_type __pos = npos) const
2807 _GLIBCXX_NOEXCEPT;
2810 * @brief Get a substring.
2811 * @param __pos Index of first character (default 0).
2812 * @param __n Number of characters in substring (default remainder).
2813 * @return The new string.
2814 * @throw std::out_of_range If __pos > size().
2816 * Construct and return a new string using the @a __n
2817 * characters starting at @a __pos. If the string is too
2818 * short, use the remainder of the characters. If @a __pos is
2819 * beyond the end of the string, out_of_range is thrown.
2821 basic_string
2822 substr(size_type __pos = 0, size_type __n = npos) const
2823 { return basic_string(*this,
2824 _M_check(__pos, "basic_string::substr"), __n); }
2827 * @brief Compare to a string.
2828 * @param __str String to compare against.
2829 * @return Integer < 0, 0, or > 0.
2831 * Returns an integer < 0 if this string is ordered before @a
2832 * __str, 0 if their values are equivalent, or > 0 if this
2833 * string is ordered after @a __str. Determines the effective
2834 * length rlen of the strings to compare as the smallest of
2835 * size() and str.size(). The function then compares the two
2836 * strings by calling traits::compare(data(), str.data(),rlen).
2837 * If the result of the comparison is nonzero returns it,
2838 * otherwise the shorter one is ordered first.
2841 compare(const basic_string& __str) const
2843 const size_type __size = this->size();
2844 const size_type __osize = __str.size();
2845 const size_type __len = std::min(__size, __osize);
2847 int __r = traits_type::compare(_M_data(), __str.data(), __len);
2848 if (!__r)
2849 __r = _S_compare(__size, __osize);
2850 return __r;
2853 #if __cplusplus > 201402L
2855 * @brief Compare to a string_view.
2856 * @param __svt An object convertible to string_view to compare against.
2857 * @return Integer < 0, 0, or > 0.
2859 template<typename _Tp>
2860 _If_sv<_Tp, int>
2861 compare(const _Tp& __svt) const
2862 noexcept(is_same<_Tp, __sv_type>::value)
2864 __sv_type __sv = __svt;
2865 const size_type __size = this->size();
2866 const size_type __osize = __sv.size();
2867 const size_type __len = std::min(__size, __osize);
2869 int __r = traits_type::compare(_M_data(), __sv.data(), __len);
2870 if (!__r)
2871 __r = _S_compare(__size, __osize);
2872 return __r;
2876 * @brief Compare to a string_view.
2877 * @param __pos A position in the string to start comparing from.
2878 * @param __n The number of characters to compare.
2879 * @param __svt An object convertible to string_view to compare
2880 * against.
2881 * @return Integer < 0, 0, or > 0.
2883 template<typename _Tp>
2884 _If_sv<_Tp, int>
2885 compare(size_type __pos, size_type __n, const _Tp& __svt) const
2886 noexcept(is_same<_Tp, __sv_type>::value)
2888 __sv_type __sv = __svt;
2889 return __sv_type(*this).substr(__pos, __n).compare(__sv);
2893 * @brief Compare to a string_view.
2894 * @param __pos1 A position in the string to start comparing from.
2895 * @param __n1 The number of characters to compare.
2896 * @param __svt An object convertible to string_view to compare
2897 * against.
2898 * @param __pos2 A position in the string_view to start comparing from.
2899 * @param __n2 The number of characters to compare.
2900 * @return Integer < 0, 0, or > 0.
2902 template<typename _Tp>
2903 _If_sv<_Tp, int>
2904 compare(size_type __pos1, size_type __n1, const _Tp& __svt,
2905 size_type __pos2, size_type __n2 = npos) const
2906 noexcept(is_same<_Tp, __sv_type>::value)
2908 __sv_type __sv = __svt;
2909 return __sv_type(*this)
2910 .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
2912 #endif // C++17
2915 * @brief Compare substring to a string.
2916 * @param __pos Index of first character of substring.
2917 * @param __n Number of characters in substring.
2918 * @param __str String to compare against.
2919 * @return Integer < 0, 0, or > 0.
2921 * Form the substring of this string from the @a __n characters
2922 * starting at @a __pos. Returns an integer < 0 if the
2923 * substring is ordered before @a __str, 0 if their values are
2924 * equivalent, or > 0 if the substring is ordered after @a
2925 * __str. Determines the effective length rlen of the strings
2926 * to compare as the smallest of the length of the substring
2927 * and @a __str.size(). The function then compares the two
2928 * strings by calling
2929 * traits::compare(substring.data(),str.data(),rlen). If the
2930 * result of the comparison is nonzero returns it, otherwise
2931 * the shorter one is ordered first.
2934 compare(size_type __pos, size_type __n, const basic_string& __str) const;
2937 * @brief Compare substring to a substring.
2938 * @param __pos1 Index of first character of substring.
2939 * @param __n1 Number of characters in substring.
2940 * @param __str String to compare against.
2941 * @param __pos2 Index of first character of substring of str.
2942 * @param __n2 Number of characters in substring of str.
2943 * @return Integer < 0, 0, or > 0.
2945 * Form the substring of this string from the @a __n1
2946 * characters starting at @a __pos1. Form the substring of @a
2947 * __str from the @a __n2 characters starting at @a __pos2.
2948 * Returns an integer < 0 if this substring is ordered before
2949 * the substring of @a __str, 0 if their values are equivalent,
2950 * or > 0 if this substring is ordered after the substring of
2951 * @a __str. Determines the effective length rlen of the
2952 * strings to compare as the smallest of the lengths of the
2953 * substrings. The function then compares the two strings by
2954 * calling
2955 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2956 * If the result of the comparison is nonzero returns it,
2957 * otherwise the shorter one is ordered first.
2960 compare(size_type __pos1, size_type __n1, const basic_string& __str,
2961 size_type __pos2, size_type __n2 = npos) const;
2964 * @brief Compare to a C string.
2965 * @param __s C string to compare against.
2966 * @return Integer < 0, 0, or > 0.
2968 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
2969 * their values are equivalent, or > 0 if this string is ordered after
2970 * @a __s. Determines the effective length rlen of the strings to
2971 * compare as the smallest of size() and the length of a string
2972 * constructed from @a __s. The function then compares the two strings
2973 * by calling traits::compare(data(),s,rlen). If the result of the
2974 * comparison is nonzero returns it, otherwise the shorter one is
2975 * ordered first.
2978 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
2980 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2981 // 5 String::compare specification questionable
2983 * @brief Compare substring to a C string.
2984 * @param __pos Index of first character of substring.
2985 * @param __n1 Number of characters in substring.
2986 * @param __s C string to compare against.
2987 * @return Integer < 0, 0, or > 0.
2989 * Form the substring of this string from the @a __n1
2990 * characters starting at @a pos. Returns an integer < 0 if
2991 * the substring is ordered before @a __s, 0 if their values
2992 * are equivalent, or > 0 if the substring is ordered after @a
2993 * __s. Determines the effective length rlen of the strings to
2994 * compare as the smallest of the length of the substring and
2995 * the length of a string constructed from @a __s. The
2996 * function then compares the two string by calling
2997 * traits::compare(substring.data(),__s,rlen). If the result of
2998 * the comparison is nonzero returns it, otherwise the shorter
2999 * one is ordered first.
3002 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
3005 * @brief Compare substring against a character %array.
3006 * @param __pos Index of first character of substring.
3007 * @param __n1 Number of characters in substring.
3008 * @param __s character %array to compare against.
3009 * @param __n2 Number of characters of s.
3010 * @return Integer < 0, 0, or > 0.
3012 * Form the substring of this string from the @a __n1
3013 * characters starting at @a __pos. Form a string from the
3014 * first @a __n2 characters of @a __s. Returns an integer < 0
3015 * if this substring is ordered before the string from @a __s,
3016 * 0 if their values are equivalent, or > 0 if this substring
3017 * is ordered after the string from @a __s. Determines the
3018 * effective length rlen of the strings to compare as the
3019 * smallest of the length of the substring and @a __n2. The
3020 * function then compares the two strings by calling
3021 * traits::compare(substring.data(),s,rlen). If the result of
3022 * the comparison is nonzero returns it, otherwise the shorter
3023 * one is ordered first.
3025 * NB: s must have at least n2 characters, &apos;\\0&apos; has
3026 * no special meaning.
3029 compare(size_type __pos, size_type __n1, const _CharT* __s,
3030 size_type __n2) const;
3032 // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3033 template<typename, typename, typename> friend class basic_stringbuf;
3035 _GLIBCXX_END_NAMESPACE_CXX11
3036 #else // !_GLIBCXX_USE_CXX11_ABI
3037 // Reference-counted COW string implentation
3040 * @class basic_string basic_string.h <string>
3041 * @brief Managing sequences of characters and character-like objects.
3043 * @ingroup strings
3044 * @ingroup sequences
3046 * @tparam _CharT Type of character
3047 * @tparam _Traits Traits for character type, defaults to
3048 * char_traits<_CharT>.
3049 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
3051 * Meets the requirements of a <a href="tables.html#65">container</a>, a
3052 * <a href="tables.html#66">reversible container</a>, and a
3053 * <a href="tables.html#67">sequence</a>. Of the
3054 * <a href="tables.html#68">optional sequence requirements</a>, only
3055 * @c push_back, @c at, and @c %array access are supported.
3057 * @doctodo
3060 * Documentation? What's that?
3061 * Nathan Myers <ncm@cantrip.org>.
3063 * A string looks like this:
3065 * @code
3066 * [_Rep]
3067 * _M_length
3068 * [basic_string<char_type>] _M_capacity
3069 * _M_dataplus _M_refcount
3070 * _M_p ----------------> unnamed array of char_type
3071 * @endcode
3073 * Where the _M_p points to the first character in the string, and
3074 * you cast it to a pointer-to-_Rep and subtract 1 to get a
3075 * pointer to the header.
3077 * This approach has the enormous advantage that a string object
3078 * requires only one allocation. All the ugliness is confined
3079 * within a single %pair of inline functions, which each compile to
3080 * a single @a add instruction: _Rep::_M_data(), and
3081 * string::_M_rep(); and the allocation function which gets a
3082 * block of raw bytes and with room enough and constructs a _Rep
3083 * object at the front.
3085 * The reason you want _M_data pointing to the character %array and
3086 * not the _Rep is so that the debugger can see the string
3087 * contents. (Probably we should add a non-inline member to get
3088 * the _Rep for the debugger to use, so users can check the actual
3089 * string length.)
3091 * Note that the _Rep object is a POD so that you can have a
3092 * static <em>empty string</em> _Rep object already @a constructed before
3093 * static constructors have run. The reference-count encoding is
3094 * chosen so that a 0 indicates one reference, so you never try to
3095 * destroy the empty-string _Rep object.
3097 * All but the last paragraph is considered pretty conventional
3098 * for a C++ string implementation.
3100 // 21.3 Template class basic_string
3101 template<typename _CharT, typename _Traits, typename _Alloc>
3102 class basic_string
3104 typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
3106 // Types:
3107 public:
3108 typedef _Traits traits_type;
3109 typedef typename _Traits::char_type value_type;
3110 typedef _Alloc allocator_type;
3111 typedef typename _CharT_alloc_type::size_type size_type;
3112 typedef typename _CharT_alloc_type::difference_type difference_type;
3113 typedef typename _CharT_alloc_type::reference reference;
3114 typedef typename _CharT_alloc_type::const_reference const_reference;
3115 typedef typename _CharT_alloc_type::pointer pointer;
3116 typedef typename _CharT_alloc_type::const_pointer const_pointer;
3117 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
3118 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
3119 const_iterator;
3120 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
3121 typedef std::reverse_iterator<iterator> reverse_iterator;
3123 protected:
3124 // type used for positions in insert, erase etc.
3125 typedef iterator __const_iterator;
3127 private:
3128 // _Rep: string representation
3129 // Invariants:
3130 // 1. String really contains _M_length + 1 characters: due to 21.3.4
3131 // must be kept null-terminated.
3132 // 2. _M_capacity >= _M_length
3133 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
3134 // 3. _M_refcount has three states:
3135 // -1: leaked, one reference, no ref-copies allowed, non-const.
3136 // 0: one reference, non-const.
3137 // n>0: n + 1 references, operations require a lock, const.
3138 // 4. All fields==0 is an empty string, given the extra storage
3139 // beyond-the-end for a null terminator; thus, the shared
3140 // empty string representation needs no constructor.
3142 struct _Rep_base
3144 size_type _M_length;
3145 size_type _M_capacity;
3146 _Atomic_word _M_refcount;
3149 struct _Rep : _Rep_base
3151 // Types:
3152 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
3154 // (Public) Data members:
3156 // The maximum number of individual char_type elements of an
3157 // individual string is determined by _S_max_size. This is the
3158 // value that will be returned by max_size(). (Whereas npos
3159 // is the maximum number of bytes the allocator can allocate.)
3160 // If one was to divvy up the theoretical largest size string,
3161 // with a terminating character and m _CharT elements, it'd
3162 // look like this:
3163 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
3164 // Solving for m:
3165 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
3166 // In addition, this implementation quarters this amount.
3167 static const size_type _S_max_size;
3168 static const _CharT _S_terminal;
3170 // The following storage is init'd to 0 by the linker, resulting
3171 // (carefully) in an empty string with one reference.
3172 static size_type _S_empty_rep_storage[];
3174 static _Rep&
3175 _S_empty_rep() _GLIBCXX_NOEXCEPT
3177 // NB: Mild hack to avoid strict-aliasing warnings. Note that
3178 // _S_empty_rep_storage is never modified and the punning should
3179 // be reasonably safe in this case.
3180 void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
3181 return *reinterpret_cast<_Rep*>(__p);
3184 bool
3185 _M_is_leaked() const _GLIBCXX_NOEXCEPT
3187 #if defined(__GTHREADS)
3188 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3189 // so we need to use an atomic load. However, _M_is_leaked
3190 // predicate does not change concurrently (i.e. the string is either
3191 // leaked or not), so a relaxed load is enough.
3192 return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED) < 0;
3193 #else
3194 return this->_M_refcount < 0;
3195 #endif
3198 bool
3199 _M_is_shared() const _GLIBCXX_NOEXCEPT
3201 #if defined(__GTHREADS)
3202 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3203 // so we need to use an atomic load. Another thread can drop last
3204 // but one reference concurrently with this check, so we need this
3205 // load to be acquire to synchronize with release fetch_and_add in
3206 // _M_dispose.
3207 return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0;
3208 #else
3209 return this->_M_refcount > 0;
3210 #endif
3213 void
3214 _M_set_leaked() _GLIBCXX_NOEXCEPT
3215 { this->_M_refcount = -1; }
3217 void
3218 _M_set_sharable() _GLIBCXX_NOEXCEPT
3219 { this->_M_refcount = 0; }
3221 void
3222 _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT
3224 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3225 if (__builtin_expect(this != &_S_empty_rep(), false))
3226 #endif
3228 this->_M_set_sharable(); // One reference.
3229 this->_M_length = __n;
3230 traits_type::assign(this->_M_refdata()[__n], _S_terminal);
3231 // grrr. (per 21.3.4)
3232 // You cannot leave those LWG people alone for a second.
3236 _CharT*
3237 _M_refdata() throw()
3238 { return reinterpret_cast<_CharT*>(this + 1); }
3240 _CharT*
3241 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
3243 return (!_M_is_leaked() && __alloc1 == __alloc2)
3244 ? _M_refcopy() : _M_clone(__alloc1);
3247 // Create & Destroy
3248 static _Rep*
3249 _S_create(size_type, size_type, const _Alloc&);
3251 void
3252 _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
3254 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3255 if (__builtin_expect(this != &_S_empty_rep(), false))
3256 #endif
3258 // Be race-detector-friendly. For more info see bits/c++config.
3259 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
3260 // Decrement of _M_refcount is acq_rel, because:
3261 // - all but last decrements need to release to synchronize with
3262 // the last decrement that will delete the object.
3263 // - the last decrement needs to acquire to synchronize with
3264 // all the previous decrements.
3265 // - last but one decrement needs to release to synchronize with
3266 // the acquire load in _M_is_shared that will conclude that
3267 // the object is not shared anymore.
3268 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
3269 -1) <= 0)
3271 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
3272 _M_destroy(__a);
3275 } // XXX MT
3277 void
3278 _M_destroy(const _Alloc&) throw();
3280 _CharT*
3281 _M_refcopy() throw()
3283 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3284 if (__builtin_expect(this != &_S_empty_rep(), false))
3285 #endif
3286 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
3287 return _M_refdata();
3288 } // XXX MT
3290 _CharT*
3291 _M_clone(const _Alloc&, size_type __res = 0);
3294 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
3295 struct _Alloc_hider : _Alloc
3297 _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT
3298 : _Alloc(__a), _M_p(__dat) { }
3300 _CharT* _M_p; // The actual data.
3303 public:
3304 // Data Members (public):
3305 // NB: This is an unsigned type, and thus represents the maximum
3306 // size that the allocator can hold.
3307 /// Value returned by various member functions when they fail.
3308 static const size_type npos = static_cast<size_type>(-1);
3310 private:
3311 // Data Members (private):
3312 mutable _Alloc_hider _M_dataplus;
3314 _CharT*
3315 _M_data() const _GLIBCXX_NOEXCEPT
3316 { return _M_dataplus._M_p; }
3318 _CharT*
3319 _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT
3320 { return (_M_dataplus._M_p = __p); }
3322 _Rep*
3323 _M_rep() const _GLIBCXX_NOEXCEPT
3324 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
3326 // For the internal use we have functions similar to `begin'/`end'
3327 // but they do not call _M_leak.
3328 iterator
3329 _M_ibegin() const _GLIBCXX_NOEXCEPT
3330 { return iterator(_M_data()); }
3332 iterator
3333 _M_iend() const _GLIBCXX_NOEXCEPT
3334 { return iterator(_M_data() + this->size()); }
3336 void
3337 _M_leak() // for use in begin() & non-const op[]
3339 if (!_M_rep()->_M_is_leaked())
3340 _M_leak_hard();
3343 size_type
3344 _M_check(size_type __pos, const char* __s) const
3346 if (__pos > this->size())
3347 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
3348 "this->size() (which is %zu)"),
3349 __s, __pos, this->size());
3350 return __pos;
3353 void
3354 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
3356 if (this->max_size() - (this->size() - __n1) < __n2)
3357 __throw_length_error(__N(__s));
3360 // NB: _M_limit doesn't check for a bad __pos value.
3361 size_type
3362 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
3364 const bool __testoff = __off < this->size() - __pos;
3365 return __testoff ? __off : this->size() - __pos;
3368 // True if _Rep and source do not overlap.
3369 bool
3370 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
3372 return (less<const _CharT*>()(__s, _M_data())
3373 || less<const _CharT*>()(_M_data() + this->size(), __s));
3376 // When __n = 1 way faster than the general multichar
3377 // traits_type::copy/move/assign.
3378 static void
3379 _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
3381 if (__n == 1)
3382 traits_type::assign(*__d, *__s);
3383 else
3384 traits_type::copy(__d, __s, __n);
3387 static void
3388 _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
3390 if (__n == 1)
3391 traits_type::assign(*__d, *__s);
3392 else
3393 traits_type::move(__d, __s, __n);
3396 static void
3397 _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT
3399 if (__n == 1)
3400 traits_type::assign(*__d, __c);
3401 else
3402 traits_type::assign(__d, __n, __c);
3405 // _S_copy_chars is a separate template to permit specialization
3406 // to optimize for the common case of pointers as iterators.
3407 template<class _Iterator>
3408 static void
3409 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
3411 for (; __k1 != __k2; ++__k1, (void)++__p)
3412 traits_type::assign(*__p, *__k1); // These types are off.
3415 static void
3416 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
3417 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
3419 static void
3420 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
3421 _GLIBCXX_NOEXCEPT
3422 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
3424 static void
3425 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
3426 { _M_copy(__p, __k1, __k2 - __k1); }
3428 static void
3429 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
3430 _GLIBCXX_NOEXCEPT
3431 { _M_copy(__p, __k1, __k2 - __k1); }
3433 static int
3434 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
3436 const difference_type __d = difference_type(__n1 - __n2);
3438 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
3439 return __gnu_cxx::__numeric_traits<int>::__max;
3440 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
3441 return __gnu_cxx::__numeric_traits<int>::__min;
3442 else
3443 return int(__d);
3446 void
3447 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
3449 void
3450 _M_leak_hard();
3452 static _Rep&
3453 _S_empty_rep() _GLIBCXX_NOEXCEPT
3454 { return _Rep::_S_empty_rep(); }
3456 #if __cplusplus > 201402L
3457 // A helper type for avoiding boiler-plate.
3458 typedef basic_string_view<_CharT, _Traits> __sv_type;
3460 template<typename _Tp, typename _Res>
3461 using _If_sv = enable_if_t<
3462 __and_<is_convertible<const _Tp&, __sv_type>,
3463 __not_<is_convertible<const _Tp*, const basic_string*>>,
3464 __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
3465 _Res>;
3467 // Allows an implicit conversion to __sv_type.
3468 static __sv_type
3469 _S_to_string_view(__sv_type __svt) noexcept
3470 { return __svt; }
3472 // Wraps a string_view by explicit conversion and thus
3473 // allows to add an internal constructor that does not
3474 // participate in overload resolution when a string_view
3475 // is provided.
3476 struct __sv_wrapper
3478 explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
3479 __sv_type _M_sv;
3481 #endif
3483 public:
3484 // Construct/copy/destroy:
3485 // NB: We overload ctors in some cases instead of using default
3486 // arguments, per 17.4.4.4 para. 2 item 2.
3489 * @brief Default constructor creates an empty string.
3491 basic_string()
3492 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3493 _GLIBCXX_NOEXCEPT
3494 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
3495 #else
3496 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
3497 #endif
3500 * @brief Construct an empty string using allocator @a a.
3502 explicit
3503 basic_string(const _Alloc& __a);
3505 // NB: per LWG issue 42, semantics different from IS:
3507 * @brief Construct string with copy of value of @a str.
3508 * @param __str Source string.
3510 basic_string(const basic_string& __str);
3512 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3513 // 2583. no way to supply an allocator for basic_string(str, pos)
3515 * @brief Construct string as copy of a substring.
3516 * @param __str Source string.
3517 * @param __pos Index of first character to copy from.
3518 * @param __a Allocator to use.
3520 basic_string(const basic_string& __str, size_type __pos,
3521 const _Alloc& __a = _Alloc());
3524 * @brief Construct string as copy of a substring.
3525 * @param __str Source string.
3526 * @param __pos Index of first character to copy from.
3527 * @param __n Number of characters to copy.
3529 basic_string(const basic_string& __str, size_type __pos,
3530 size_type __n);
3532 * @brief Construct string as copy of a substring.
3533 * @param __str Source string.
3534 * @param __pos Index of first character to copy from.
3535 * @param __n Number of characters to copy.
3536 * @param __a Allocator to use.
3538 basic_string(const basic_string& __str, size_type __pos,
3539 size_type __n, const _Alloc& __a);
3542 * @brief Construct string initialized by a character %array.
3543 * @param __s Source character %array.
3544 * @param __n Number of characters to copy.
3545 * @param __a Allocator to use (default is default allocator).
3547 * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
3548 * has no special meaning.
3550 basic_string(const _CharT* __s, size_type __n,
3551 const _Alloc& __a = _Alloc());
3553 * @brief Construct string as copy of a C string.
3554 * @param __s Source C string.
3555 * @param __a Allocator to use (default is default allocator).
3557 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
3559 * @brief Construct string as multiple characters.
3560 * @param __n Number of characters.
3561 * @param __c Character to use.
3562 * @param __a Allocator to use (default is default allocator).
3564 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
3566 #if __cplusplus >= 201103L
3568 * @brief Move construct string.
3569 * @param __str Source string.
3571 * The newly-created string contains the exact contents of @a __str.
3572 * @a __str is a valid, but unspecified string.
3574 basic_string(basic_string&& __str)
3575 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3576 noexcept // FIXME C++11: should always be noexcept.
3577 #endif
3578 : _M_dataplus(__str._M_dataplus)
3580 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3581 __str._M_data(_S_empty_rep()._M_refdata());
3582 #else
3583 __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
3584 #endif
3588 * @brief Construct string from an initializer %list.
3589 * @param __l std::initializer_list of characters.
3590 * @param __a Allocator to use (default is default allocator).
3592 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
3593 #endif // C++11
3596 * @brief Construct string as copy of a range.
3597 * @param __beg Start of range.
3598 * @param __end End of range.
3599 * @param __a Allocator to use (default is default allocator).
3601 template<class _InputIterator>
3602 basic_string(_InputIterator __beg, _InputIterator __end,
3603 const _Alloc& __a = _Alloc());
3605 #if __cplusplus > 201402L
3607 * @brief Construct string from a substring of a string_view.
3608 * @param __t Source object convertible to string view.
3609 * @param __pos The index of the first character to copy from __t.
3610 * @param __n The number of characters to copy from __t.
3611 * @param __a Allocator to use.
3613 template<typename _Tp, typename = _If_sv<_Tp, void>>
3614 basic_string(const _Tp& __t, size_type __pos, size_type __n,
3615 const _Alloc& __a = _Alloc())
3616 : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
3619 * @brief Construct string from a string_view.
3620 * @param __t Source object convertible to string view.
3621 * @param __a Allocator to use (default is default allocator).
3623 template<typename _Tp, typename = _If_sv<_Tp, void>>
3624 explicit
3625 basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
3626 : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
3629 * @brief Only internally used: Construct string from a string view
3630 * wrapper.
3631 * @param __svw string view wrapper.
3632 * @param __a Allocator to use.
3634 explicit
3635 basic_string(__sv_wrapper __svw, const _Alloc& __a)
3636 : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
3637 #endif // C++17
3640 * @brief Destroy the string instance.
3642 ~basic_string() _GLIBCXX_NOEXCEPT
3643 { _M_rep()->_M_dispose(this->get_allocator()); }
3646 * @brief Assign the value of @a str to this string.
3647 * @param __str Source string.
3649 basic_string&
3650 operator=(const basic_string& __str)
3651 { return this->assign(__str); }
3654 * @brief Copy contents of @a s into this string.
3655 * @param __s Source null-terminated string.
3657 basic_string&
3658 operator=(const _CharT* __s)
3659 { return this->assign(__s); }
3662 * @brief Set value to string of length 1.
3663 * @param __c Source character.
3665 * Assigning to a character makes this string length 1 and
3666 * (*this)[0] == @a c.
3668 basic_string&
3669 operator=(_CharT __c)
3671 this->assign(1, __c);
3672 return *this;
3675 #if __cplusplus >= 201103L
3677 * @brief Move assign the value of @a str to this string.
3678 * @param __str Source string.
3680 * The contents of @a str are moved into this string (without copying).
3681 * @a str is a valid, but unspecified string.
3683 basic_string&
3684 operator=(basic_string&& __str)
3685 _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value)
3687 // NB: DR 1204.
3688 this->swap(__str);
3689 return *this;
3693 * @brief Set value to string constructed from initializer %list.
3694 * @param __l std::initializer_list.
3696 basic_string&
3697 operator=(initializer_list<_CharT> __l)
3699 this->assign(__l.begin(), __l.size());
3700 return *this;
3702 #endif // C++11
3704 #if __cplusplus > 201402L
3706 * @brief Set value to string constructed from a string_view.
3707 * @param __svt An object convertible to string_view.
3709 template<typename _Tp>
3710 _If_sv<_Tp, basic_string&>
3711 operator=(const _Tp& __svt)
3712 { return this->assign(__svt); }
3715 * @brief Convert to a string_view.
3716 * @return A string_view.
3718 operator __sv_type() const noexcept
3719 { return __sv_type(data(), size()); }
3720 #endif // C++17
3722 // Iterators:
3724 * Returns a read/write iterator that points to the first character in
3725 * the %string. Unshares the string.
3727 iterator
3728 begin() // FIXME C++11: should be noexcept.
3730 _M_leak();
3731 return iterator(_M_data());
3735 * Returns a read-only (constant) iterator that points to the first
3736 * character in the %string.
3738 const_iterator
3739 begin() const _GLIBCXX_NOEXCEPT
3740 { return const_iterator(_M_data()); }
3743 * Returns a read/write iterator that points one past the last
3744 * character in the %string. Unshares the string.
3746 iterator
3747 end() // FIXME C++11: should be noexcept.
3749 _M_leak();
3750 return iterator(_M_data() + this->size());
3754 * Returns a read-only (constant) iterator that points one past the
3755 * last character in the %string.
3757 const_iterator
3758 end() const _GLIBCXX_NOEXCEPT
3759 { return const_iterator(_M_data() + this->size()); }
3762 * Returns a read/write reverse iterator that points to the last
3763 * character in the %string. Iteration is done in reverse element
3764 * order. Unshares the string.
3766 reverse_iterator
3767 rbegin() // FIXME C++11: should be noexcept.
3768 { return reverse_iterator(this->end()); }
3771 * Returns a read-only (constant) reverse iterator that points
3772 * to the last character in the %string. Iteration is done in
3773 * reverse element order.
3775 const_reverse_iterator
3776 rbegin() const _GLIBCXX_NOEXCEPT
3777 { return const_reverse_iterator(this->end()); }
3780 * Returns a read/write reverse iterator that points to one before the
3781 * first character in the %string. Iteration is done in reverse
3782 * element order. Unshares the string.
3784 reverse_iterator
3785 rend() // FIXME C++11: should be noexcept.
3786 { return reverse_iterator(this->begin()); }
3789 * Returns a read-only (constant) reverse iterator that points
3790 * to one before the first character in the %string. Iteration
3791 * is done in reverse element order.
3793 const_reverse_iterator
3794 rend() const _GLIBCXX_NOEXCEPT
3795 { return const_reverse_iterator(this->begin()); }
3797 #if __cplusplus >= 201103L
3799 * Returns a read-only (constant) iterator that points to the first
3800 * character in the %string.
3802 const_iterator
3803 cbegin() const noexcept
3804 { return const_iterator(this->_M_data()); }
3807 * Returns a read-only (constant) iterator that points one past the
3808 * last character in the %string.
3810 const_iterator
3811 cend() const noexcept
3812 { return const_iterator(this->_M_data() + this->size()); }
3815 * Returns a read-only (constant) reverse iterator that points
3816 * to the last character in the %string. Iteration is done in
3817 * reverse element order.
3819 const_reverse_iterator
3820 crbegin() const noexcept
3821 { return const_reverse_iterator(this->end()); }
3824 * Returns a read-only (constant) reverse iterator that points
3825 * to one before the first character in the %string. Iteration
3826 * is done in reverse element order.
3828 const_reverse_iterator
3829 crend() const noexcept
3830 { return const_reverse_iterator(this->begin()); }
3831 #endif
3833 public:
3834 // Capacity:
3835 /// Returns the number of characters in the string, not including any
3836 /// null-termination.
3837 size_type
3838 size() const _GLIBCXX_NOEXCEPT
3839 { return _M_rep()->_M_length; }
3841 /// Returns the number of characters in the string, not including any
3842 /// null-termination.
3843 size_type
3844 length() const _GLIBCXX_NOEXCEPT
3845 { return _M_rep()->_M_length; }
3847 /// Returns the size() of the largest possible %string.
3848 size_type
3849 max_size() const _GLIBCXX_NOEXCEPT
3850 { return _Rep::_S_max_size; }
3853 * @brief Resizes the %string to the specified number of characters.
3854 * @param __n Number of characters the %string should contain.
3855 * @param __c Character to fill any new elements.
3857 * This function will %resize the %string to the specified
3858 * number of characters. If the number is smaller than the
3859 * %string's current size the %string is truncated, otherwise
3860 * the %string is extended and new elements are %set to @a __c.
3862 void
3863 resize(size_type __n, _CharT __c);
3866 * @brief Resizes the %string to the specified number of characters.
3867 * @param __n Number of characters the %string should contain.
3869 * This function will resize the %string to the specified length. If
3870 * the new size is smaller than the %string's current size the %string
3871 * is truncated, otherwise the %string is extended and new characters
3872 * are default-constructed. For basic types such as char, this means
3873 * setting them to 0.
3875 void
3876 resize(size_type __n)
3877 { this->resize(__n, _CharT()); }
3879 #if __cplusplus >= 201103L
3880 /// A non-binding request to reduce capacity() to size().
3881 void
3882 shrink_to_fit() _GLIBCXX_NOEXCEPT
3884 #if __cpp_exceptions
3885 if (capacity() > size())
3888 { reserve(0); }
3889 catch(...)
3892 #endif
3894 #endif
3897 * Returns the total number of characters that the %string can hold
3898 * before needing to allocate more memory.
3900 size_type
3901 capacity() const _GLIBCXX_NOEXCEPT
3902 { return _M_rep()->_M_capacity; }
3905 * @brief Attempt to preallocate enough memory for specified number of
3906 * characters.
3907 * @param __res_arg Number of characters required.
3908 * @throw std::length_error If @a __res_arg exceeds @c max_size().
3910 * This function attempts to reserve enough memory for the
3911 * %string to hold the specified number of characters. If the
3912 * number requested is more than max_size(), length_error is
3913 * thrown.
3915 * The advantage of this function is that if optimal code is a
3916 * necessity and the user can determine the string length that will be
3917 * required, the user can reserve the memory in %advance, and thus
3918 * prevent a possible reallocation of memory and copying of %string
3919 * data.
3921 void
3922 reserve(size_type __res_arg = 0);
3925 * Erases the string, making it empty.
3927 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3928 void
3929 clear() _GLIBCXX_NOEXCEPT
3931 if (_M_rep()->_M_is_shared())
3933 _M_rep()->_M_dispose(this->get_allocator());
3934 _M_data(_S_empty_rep()._M_refdata());
3936 else
3937 _M_rep()->_M_set_length_and_sharable(0);
3939 #else
3940 // PR 56166: this should not throw.
3941 void
3942 clear()
3943 { _M_mutate(0, this->size(), 0); }
3944 #endif
3947 * Returns true if the %string is empty. Equivalent to
3948 * <code>*this == ""</code>.
3950 bool
3951 empty() const _GLIBCXX_NOEXCEPT
3952 { return this->size() == 0; }
3954 // Element access:
3956 * @brief Subscript access to the data contained in the %string.
3957 * @param __pos The index of the character to access.
3958 * @return Read-only (constant) reference to the character.
3960 * This operator allows for easy, array-style, data access.
3961 * Note that data access with this operator is unchecked and
3962 * out_of_range lookups are not defined. (For checked lookups
3963 * see at().)
3965 const_reference
3966 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
3968 __glibcxx_assert(__pos <= size());
3969 return _M_data()[__pos];
3973 * @brief Subscript access to the data contained in the %string.
3974 * @param __pos The index of the character to access.
3975 * @return Read/write reference to the character.
3977 * This operator allows for easy, array-style, data access.
3978 * Note that data access with this operator is unchecked and
3979 * out_of_range lookups are not defined. (For checked lookups
3980 * see at().) Unshares the string.
3982 reference
3983 operator[](size_type __pos)
3985 // Allow pos == size() both in C++98 mode, as v3 extension,
3986 // and in C++11 mode.
3987 __glibcxx_assert(__pos <= size());
3988 // In pedantic mode be strict in C++98 mode.
3989 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
3990 _M_leak();
3991 return _M_data()[__pos];
3995 * @brief Provides access to the data contained in the %string.
3996 * @param __n The index of the character to access.
3997 * @return Read-only (const) reference to the character.
3998 * @throw std::out_of_range If @a n is an invalid index.
4000 * This function provides for safer data access. The parameter is
4001 * first checked that it is in the range of the string. The function
4002 * throws out_of_range if the check fails.
4004 const_reference
4005 at(size_type __n) const
4007 if (__n >= this->size())
4008 __throw_out_of_range_fmt(__N("basic_string::at: __n "
4009 "(which is %zu) >= this->size() "
4010 "(which is %zu)"),
4011 __n, this->size());
4012 return _M_data()[__n];
4016 * @brief Provides access to the data contained in the %string.
4017 * @param __n The index of the character to access.
4018 * @return Read/write reference to the character.
4019 * @throw std::out_of_range If @a n is an invalid index.
4021 * This function provides for safer data access. The parameter is
4022 * first checked that it is in the range of the string. The function
4023 * throws out_of_range if the check fails. Success results in
4024 * unsharing the string.
4026 reference
4027 at(size_type __n)
4029 if (__n >= size())
4030 __throw_out_of_range_fmt(__N("basic_string::at: __n "
4031 "(which is %zu) >= this->size() "
4032 "(which is %zu)"),
4033 __n, this->size());
4034 _M_leak();
4035 return _M_data()[__n];
4038 #if __cplusplus >= 201103L
4040 * Returns a read/write reference to the data at the first
4041 * element of the %string.
4043 reference
4044 front()
4046 __glibcxx_assert(!empty());
4047 return operator[](0);
4051 * Returns a read-only (constant) reference to the data at the first
4052 * element of the %string.
4054 const_reference
4055 front() const noexcept
4057 __glibcxx_assert(!empty());
4058 return operator[](0);
4062 * Returns a read/write reference to the data at the last
4063 * element of the %string.
4065 reference
4066 back()
4068 __glibcxx_assert(!empty());
4069 return operator[](this->size() - 1);
4073 * Returns a read-only (constant) reference to the data at the
4074 * last element of the %string.
4076 const_reference
4077 back() const noexcept
4079 __glibcxx_assert(!empty());
4080 return operator[](this->size() - 1);
4082 #endif
4084 // Modifiers:
4086 * @brief Append a string to this string.
4087 * @param __str The string to append.
4088 * @return Reference to this string.
4090 basic_string&
4091 operator+=(const basic_string& __str)
4092 { return this->append(__str); }
4095 * @brief Append a C string.
4096 * @param __s The C string to append.
4097 * @return Reference to this string.
4099 basic_string&
4100 operator+=(const _CharT* __s)
4101 { return this->append(__s); }
4104 * @brief Append a character.
4105 * @param __c The character to append.
4106 * @return Reference to this string.
4108 basic_string&
4109 operator+=(_CharT __c)
4111 this->push_back(__c);
4112 return *this;
4115 #if __cplusplus >= 201103L
4117 * @brief Append an initializer_list of characters.
4118 * @param __l The initializer_list of characters to be appended.
4119 * @return Reference to this string.
4121 basic_string&
4122 operator+=(initializer_list<_CharT> __l)
4123 { return this->append(__l.begin(), __l.size()); }
4124 #endif // C++11
4126 #if __cplusplus > 201402L
4128 * @brief Append a string_view.
4129 * @param __svt The object convertible to string_view to be appended.
4130 * @return Reference to this string.
4132 template<typename _Tp>
4133 _If_sv<_Tp, basic_string&>
4134 operator+=(const _Tp& __svt)
4135 { return this->append(__svt); }
4136 #endif // C++17
4139 * @brief Append a string to this string.
4140 * @param __str The string to append.
4141 * @return Reference to this string.
4143 basic_string&
4144 append(const basic_string& __str);
4147 * @brief Append a substring.
4148 * @param __str The string to append.
4149 * @param __pos Index of the first character of str to append.
4150 * @param __n The number of characters to append.
4151 * @return Reference to this string.
4152 * @throw std::out_of_range if @a __pos is not a valid index.
4154 * This function appends @a __n characters from @a __str
4155 * starting at @a __pos to this string. If @a __n is is larger
4156 * than the number of available characters in @a __str, the
4157 * remainder of @a __str is appended.
4159 basic_string&
4160 append(const basic_string& __str, size_type __pos, size_type __n = npos);
4163 * @brief Append a C substring.
4164 * @param __s The C string to append.
4165 * @param __n The number of characters to append.
4166 * @return Reference to this string.
4168 basic_string&
4169 append(const _CharT* __s, size_type __n);
4172 * @brief Append a C string.
4173 * @param __s The C string to append.
4174 * @return Reference to this string.
4176 basic_string&
4177 append(const _CharT* __s)
4179 __glibcxx_requires_string(__s);
4180 return this->append(__s, traits_type::length(__s));
4184 * @brief Append multiple characters.
4185 * @param __n The number of characters to append.
4186 * @param __c The character to use.
4187 * @return Reference to this string.
4189 * Appends __n copies of __c to this string.
4191 basic_string&
4192 append(size_type __n, _CharT __c);
4194 #if __cplusplus >= 201103L
4196 * @brief Append an initializer_list of characters.
4197 * @param __l The initializer_list of characters to append.
4198 * @return Reference to this string.
4200 basic_string&
4201 append(initializer_list<_CharT> __l)
4202 { return this->append(__l.begin(), __l.size()); }
4203 #endif // C++11
4206 * @brief Append a range of characters.
4207 * @param __first Iterator referencing the first character to append.
4208 * @param __last Iterator marking the end of the range.
4209 * @return Reference to this string.
4211 * Appends characters in the range [__first,__last) to this string.
4213 template<class _InputIterator>
4214 basic_string&
4215 append(_InputIterator __first, _InputIterator __last)
4216 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
4218 #if __cplusplus > 201402L
4220 * @brief Append a string_view.
4221 * @param __svt The object convertible to string_view to be appended.
4222 * @return Reference to this string.
4224 template<typename _Tp>
4225 _If_sv<_Tp, basic_string&>
4226 append(const _Tp& __svt)
4228 __sv_type __sv = __svt;
4229 return this->append(__sv.data(), __sv.size());
4233 * @brief Append a range of characters from a string_view.
4234 * @param __svt The object convertible to string_view to be appended
4235 * from.
4236 * @param __pos The position in the string_view to append from.
4237 * @param __n The number of characters to append from the string_view.
4238 * @return Reference to this string.
4240 template<typename _Tp>
4241 _If_sv<_Tp, basic_string&>
4242 append(const _Tp& __svt, size_type __pos, size_type __n = npos)
4244 __sv_type __sv = __svt;
4245 return append(__sv.data()
4246 + __sv._M_check(__pos, "basic_string::append"),
4247 __sv._M_limit(__pos, __n));
4249 #endif // C++17
4252 * @brief Append a single character.
4253 * @param __c Character to append.
4255 void
4256 push_back(_CharT __c)
4258 const size_type __len = 1 + this->size();
4259 if (__len > this->capacity() || _M_rep()->_M_is_shared())
4260 this->reserve(__len);
4261 traits_type::assign(_M_data()[this->size()], __c);
4262 _M_rep()->_M_set_length_and_sharable(__len);
4266 * @brief Set value to contents of another string.
4267 * @param __str Source string to use.
4268 * @return Reference to this string.
4270 basic_string&
4271 assign(const basic_string& __str);
4273 #if __cplusplus >= 201103L
4275 * @brief Set value to contents of another string.
4276 * @param __str Source string to use.
4277 * @return Reference to this string.
4279 * This function sets this string to the exact contents of @a __str.
4280 * @a __str is a valid, but unspecified string.
4282 basic_string&
4283 assign(basic_string&& __str)
4284 noexcept(allocator_traits<_Alloc>::is_always_equal::value)
4286 this->swap(__str);
4287 return *this;
4289 #endif // C++11
4292 * @brief Set value to a substring of a string.
4293 * @param __str The string to use.
4294 * @param __pos Index of the first character of str.
4295 * @param __n Number of characters to use.
4296 * @return Reference to this string.
4297 * @throw std::out_of_range if @a pos is not a valid index.
4299 * This function sets this string to the substring of @a __str
4300 * consisting of @a __n characters at @a __pos. If @a __n is
4301 * is larger than the number of available characters in @a
4302 * __str, the remainder of @a __str is used.
4304 basic_string&
4305 assign(const basic_string& __str, size_type __pos, size_type __n = npos)
4306 { return this->assign(__str._M_data()
4307 + __str._M_check(__pos, "basic_string::assign"),
4308 __str._M_limit(__pos, __n)); }
4311 * @brief Set value to a C substring.
4312 * @param __s The C string to use.
4313 * @param __n Number of characters to use.
4314 * @return Reference to this string.
4316 * This function sets the value of this string to the first @a __n
4317 * characters of @a __s. If @a __n is is larger than the number of
4318 * available characters in @a __s, the remainder of @a __s is used.
4320 basic_string&
4321 assign(const _CharT* __s, size_type __n);
4324 * @brief Set value to contents of a C string.
4325 * @param __s The C string to use.
4326 * @return Reference to this string.
4328 * This function sets the value of this string to the value of @a __s.
4329 * The data is copied, so there is no dependence on @a __s once the
4330 * function returns.
4332 basic_string&
4333 assign(const _CharT* __s)
4335 __glibcxx_requires_string(__s);
4336 return this->assign(__s, traits_type::length(__s));
4340 * @brief Set value to multiple characters.
4341 * @param __n Length of the resulting string.
4342 * @param __c The character to use.
4343 * @return Reference to this string.
4345 * This function sets the value of this string to @a __n copies of
4346 * character @a __c.
4348 basic_string&
4349 assign(size_type __n, _CharT __c)
4350 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
4353 * @brief Set value to a range of characters.
4354 * @param __first Iterator referencing the first character to append.
4355 * @param __last Iterator marking the end of the range.
4356 * @return Reference to this string.
4358 * Sets value of string to characters in the range [__first,__last).
4360 template<class _InputIterator>
4361 basic_string&
4362 assign(_InputIterator __first, _InputIterator __last)
4363 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
4365 #if __cplusplus >= 201103L
4367 * @brief Set value to an initializer_list of characters.
4368 * @param __l The initializer_list of characters to assign.
4369 * @return Reference to this string.
4371 basic_string&
4372 assign(initializer_list<_CharT> __l)
4373 { return this->assign(__l.begin(), __l.size()); }
4374 #endif // C++11
4376 #if __cplusplus > 201402L
4378 * @brief Set value from a string_view.
4379 * @param __svt The source object convertible to string_view.
4380 * @return Reference to this string.
4382 template<typename _Tp>
4383 _If_sv<_Tp, basic_string&>
4384 assign(const _Tp& __svt)
4386 __sv_type __sv = __svt;
4387 return this->assign(__sv.data(), __sv.size());
4391 * @brief Set value from a range of characters in a string_view.
4392 * @param __svt The source object convertible to string_view.
4393 * @param __pos The position in the string_view to assign from.
4394 * @param __n The number of characters to assign.
4395 * @return Reference to this string.
4397 template<typename _Tp>
4398 _If_sv<_Tp, basic_string&>
4399 assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
4401 __sv_type __sv = __svt;
4402 return assign(__sv.data()
4403 + __sv._M_check(__pos, "basic_string::assign"),
4404 __sv._M_limit(__pos, __n));
4406 #endif // C++17
4409 * @brief Insert multiple characters.
4410 * @param __p Iterator referencing location in string to insert at.
4411 * @param __n Number of characters to insert
4412 * @param __c The character to insert.
4413 * @throw std::length_error If new length exceeds @c max_size().
4415 * Inserts @a __n copies of character @a __c starting at the
4416 * position referenced by iterator @a __p. If adding
4417 * characters causes the length to exceed max_size(),
4418 * length_error is thrown. The value of the string doesn't
4419 * change if an error is thrown.
4421 void
4422 insert(iterator __p, size_type __n, _CharT __c)
4423 { this->replace(__p, __p, __n, __c); }
4426 * @brief Insert a range of characters.
4427 * @param __p Iterator referencing location in string to insert at.
4428 * @param __beg Start of range.
4429 * @param __end End of range.
4430 * @throw std::length_error If new length exceeds @c max_size().
4432 * Inserts characters in range [__beg,__end). If adding
4433 * characters causes the length to exceed max_size(),
4434 * length_error is thrown. The value of the string doesn't
4435 * change if an error is thrown.
4437 template<class _InputIterator>
4438 void
4439 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
4440 { this->replace(__p, __p, __beg, __end); }
4442 #if __cplusplus >= 201103L
4444 * @brief Insert an initializer_list of characters.
4445 * @param __p Iterator referencing location in string to insert at.
4446 * @param __l The initializer_list of characters to insert.
4447 * @throw std::length_error If new length exceeds @c max_size().
4449 void
4450 insert(iterator __p, initializer_list<_CharT> __l)
4452 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
4453 this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
4455 #endif // C++11
4458 * @brief Insert value of a string.
4459 * @param __pos1 Iterator referencing location in string to insert at.
4460 * @param __str The string to insert.
4461 * @return Reference to this string.
4462 * @throw std::length_error If new length exceeds @c max_size().
4464 * Inserts value of @a __str starting at @a __pos1. If adding
4465 * characters causes the length to exceed max_size(),
4466 * length_error is thrown. The value of the string doesn't
4467 * change if an error is thrown.
4469 basic_string&
4470 insert(size_type __pos1, const basic_string& __str)
4471 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
4474 * @brief Insert a substring.
4475 * @param __pos1 Iterator referencing location in string to insert at.
4476 * @param __str The string to insert.
4477 * @param __pos2 Start of characters in str to insert.
4478 * @param __n Number of characters to insert.
4479 * @return Reference to this string.
4480 * @throw std::length_error If new length exceeds @c max_size().
4481 * @throw std::out_of_range If @a pos1 > size() or
4482 * @a __pos2 > @a str.size().
4484 * Starting at @a pos1, insert @a __n character of @a __str
4485 * beginning with @a __pos2. If adding characters causes the
4486 * length to exceed max_size(), length_error is thrown. If @a
4487 * __pos1 is beyond the end of this string or @a __pos2 is
4488 * beyond the end of @a __str, out_of_range is thrown. The
4489 * value of the string doesn't change if an error is thrown.
4491 basic_string&
4492 insert(size_type __pos1, const basic_string& __str,
4493 size_type __pos2, size_type __n = npos)
4494 { return this->insert(__pos1, __str._M_data()
4495 + __str._M_check(__pos2, "basic_string::insert"),
4496 __str._M_limit(__pos2, __n)); }
4499 * @brief Insert a C substring.
4500 * @param __pos Iterator referencing location in string to insert at.
4501 * @param __s The C string to insert.
4502 * @param __n The number of characters to insert.
4503 * @return Reference to this string.
4504 * @throw std::length_error If new length exceeds @c max_size().
4505 * @throw std::out_of_range If @a __pos is beyond the end of this
4506 * string.
4508 * Inserts the first @a __n characters of @a __s starting at @a
4509 * __pos. If adding characters causes the length to exceed
4510 * max_size(), length_error is thrown. If @a __pos is beyond
4511 * end(), out_of_range is thrown. The value of the string
4512 * doesn't change if an error is thrown.
4514 basic_string&
4515 insert(size_type __pos, const _CharT* __s, size_type __n);
4518 * @brief Insert a C string.
4519 * @param __pos Iterator referencing location in string to insert at.
4520 * @param __s The C string to insert.
4521 * @return Reference to this string.
4522 * @throw std::length_error If new length exceeds @c max_size().
4523 * @throw std::out_of_range If @a pos is beyond the end of this
4524 * string.
4526 * Inserts the first @a n characters of @a __s starting at @a __pos. If
4527 * adding characters causes the length to exceed max_size(),
4528 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
4529 * thrown. The value of the string doesn't change if an error is
4530 * thrown.
4532 basic_string&
4533 insert(size_type __pos, const _CharT* __s)
4535 __glibcxx_requires_string(__s);
4536 return this->insert(__pos, __s, traits_type::length(__s));
4540 * @brief Insert multiple characters.
4541 * @param __pos Index in string to insert at.
4542 * @param __n Number of characters to insert
4543 * @param __c The character to insert.
4544 * @return Reference to this string.
4545 * @throw std::length_error If new length exceeds @c max_size().
4546 * @throw std::out_of_range If @a __pos is beyond the end of this
4547 * string.
4549 * Inserts @a __n copies of character @a __c starting at index
4550 * @a __pos. If adding characters causes the length to exceed
4551 * max_size(), length_error is thrown. If @a __pos > length(),
4552 * out_of_range is thrown. The value of the string doesn't
4553 * change if an error is thrown.
4555 basic_string&
4556 insert(size_type __pos, size_type __n, _CharT __c)
4557 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
4558 size_type(0), __n, __c); }
4561 * @brief Insert one character.
4562 * @param __p Iterator referencing position in string to insert at.
4563 * @param __c The character to insert.
4564 * @return Iterator referencing newly inserted char.
4565 * @throw std::length_error If new length exceeds @c max_size().
4567 * Inserts character @a __c at position referenced by @a __p.
4568 * If adding character causes the length to exceed max_size(),
4569 * length_error is thrown. If @a __p is beyond end of string,
4570 * out_of_range is thrown. The value of the string doesn't
4571 * change if an error is thrown.
4573 iterator
4574 insert(iterator __p, _CharT __c)
4576 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
4577 const size_type __pos = __p - _M_ibegin();
4578 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
4579 _M_rep()->_M_set_leaked();
4580 return iterator(_M_data() + __pos);
4583 #if __cplusplus > 201402L
4585 * @brief Insert a string_view.
4586 * @param __pos Iterator referencing position in string to insert at.
4587 * @param __svt The object convertible to string_view to insert.
4588 * @return Reference to this string.
4590 template<typename _Tp>
4591 _If_sv<_Tp, basic_string&>
4592 insert(size_type __pos, const _Tp& __svt)
4594 __sv_type __sv = __svt;
4595 return this->insert(__pos, __sv.data(), __sv.size());
4599 * @brief Insert a string_view.
4600 * @param __pos Iterator referencing position in string to insert at.
4601 * @param __svt The object convertible to string_view to insert from.
4602 * @param __pos Iterator referencing position in string_view to insert
4603 * from.
4604 * @param __n The number of characters to insert.
4605 * @return Reference to this string.
4607 template<typename _Tp>
4608 _If_sv<_Tp, basic_string&>
4609 insert(size_type __pos1, const _Tp& __svt,
4610 size_type __pos2, size_type __n = npos)
4612 __sv_type __sv = __svt;
4613 return this->replace(__pos1, size_type(0), __sv.data()
4614 + __sv._M_check(__pos2, "basic_string::insert"),
4615 __sv._M_limit(__pos2, __n));
4617 #endif // C++17
4620 * @brief Remove characters.
4621 * @param __pos Index of first character to remove (default 0).
4622 * @param __n Number of characters to remove (default remainder).
4623 * @return Reference to this string.
4624 * @throw std::out_of_range If @a pos is beyond the end of this
4625 * string.
4627 * Removes @a __n characters from this string starting at @a
4628 * __pos. The length of the string is reduced by @a __n. If
4629 * there are < @a __n characters to remove, the remainder of
4630 * the string is truncated. If @a __p is beyond end of string,
4631 * out_of_range is thrown. The value of the string doesn't
4632 * change if an error is thrown.
4634 basic_string&
4635 erase(size_type __pos = 0, size_type __n = npos)
4637 _M_mutate(_M_check(__pos, "basic_string::erase"),
4638 _M_limit(__pos, __n), size_type(0));
4639 return *this;
4643 * @brief Remove one character.
4644 * @param __position Iterator referencing the character to remove.
4645 * @return iterator referencing same location after removal.
4647 * Removes the character at @a __position from this string. The value
4648 * of the string doesn't change if an error is thrown.
4650 iterator
4651 erase(iterator __position)
4653 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
4654 && __position < _M_iend());
4655 const size_type __pos = __position - _M_ibegin();
4656 _M_mutate(__pos, size_type(1), size_type(0));
4657 _M_rep()->_M_set_leaked();
4658 return iterator(_M_data() + __pos);
4662 * @brief Remove a range of characters.
4663 * @param __first Iterator referencing the first character to remove.
4664 * @param __last Iterator referencing the end of the range.
4665 * @return Iterator referencing location of first after removal.
4667 * Removes the characters in the range [first,last) from this string.
4668 * The value of the string doesn't change if an error is thrown.
4670 iterator
4671 erase(iterator __first, iterator __last);
4673 #if __cplusplus >= 201103L
4675 * @brief Remove the last character.
4677 * The string must be non-empty.
4679 void
4680 pop_back() // FIXME C++11: should be noexcept.
4682 __glibcxx_assert(!empty());
4683 erase(size() - 1, 1);
4685 #endif // C++11
4688 * @brief Replace characters with value from another string.
4689 * @param __pos Index of first character to replace.
4690 * @param __n Number of characters to be replaced.
4691 * @param __str String to insert.
4692 * @return Reference to this string.
4693 * @throw std::out_of_range If @a pos is beyond the end of this
4694 * string.
4695 * @throw std::length_error If new length exceeds @c max_size().
4697 * Removes the characters in the range [__pos,__pos+__n) from
4698 * this string. In place, the value of @a __str is inserted.
4699 * If @a __pos is beyond end of string, out_of_range is thrown.
4700 * If the length of the result exceeds max_size(), length_error
4701 * is thrown. The value of the string doesn't change if an
4702 * error is thrown.
4704 basic_string&
4705 replace(size_type __pos, size_type __n, const basic_string& __str)
4706 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
4709 * @brief Replace characters with value from another string.
4710 * @param __pos1 Index of first character to replace.
4711 * @param __n1 Number of characters to be replaced.
4712 * @param __str String to insert.
4713 * @param __pos2 Index of first character of str to use.
4714 * @param __n2 Number of characters from str to use.
4715 * @return Reference to this string.
4716 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
4717 * __str.size().
4718 * @throw std::length_error If new length exceeds @c max_size().
4720 * Removes the characters in the range [__pos1,__pos1 + n) from this
4721 * string. In place, the value of @a __str is inserted. If @a __pos is
4722 * beyond end of string, out_of_range is thrown. If the length of the
4723 * result exceeds max_size(), length_error is thrown. The value of the
4724 * string doesn't change if an error is thrown.
4726 basic_string&
4727 replace(size_type __pos1, size_type __n1, const basic_string& __str,
4728 size_type __pos2, size_type __n2 = npos)
4729 { return this->replace(__pos1, __n1, __str._M_data()
4730 + __str._M_check(__pos2, "basic_string::replace"),
4731 __str._M_limit(__pos2, __n2)); }
4734 * @brief Replace characters with value of a C substring.
4735 * @param __pos Index of first character to replace.
4736 * @param __n1 Number of characters to be replaced.
4737 * @param __s C string to insert.
4738 * @param __n2 Number of characters from @a s to use.
4739 * @return Reference to this string.
4740 * @throw std::out_of_range If @a pos1 > size().
4741 * @throw std::length_error If new length exceeds @c max_size().
4743 * Removes the characters in the range [__pos,__pos + __n1)
4744 * from this string. In place, the first @a __n2 characters of
4745 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
4746 * @a __pos is beyond end of string, out_of_range is thrown. If
4747 * the length of result exceeds max_size(), length_error is
4748 * thrown. The value of the string doesn't change if an error
4749 * is thrown.
4751 basic_string&
4752 replace(size_type __pos, size_type __n1, const _CharT* __s,
4753 size_type __n2);
4756 * @brief Replace characters with value of a C string.
4757 * @param __pos Index of first character to replace.
4758 * @param __n1 Number of characters to be replaced.
4759 * @param __s C string to insert.
4760 * @return Reference to this string.
4761 * @throw std::out_of_range If @a pos > size().
4762 * @throw std::length_error If new length exceeds @c max_size().
4764 * Removes the characters in the range [__pos,__pos + __n1)
4765 * from this string. In place, the characters of @a __s are
4766 * inserted. If @a __pos is beyond end of string, out_of_range
4767 * is thrown. If the length of result exceeds max_size(),
4768 * length_error is thrown. The value of the string doesn't
4769 * change if an error is thrown.
4771 basic_string&
4772 replace(size_type __pos, size_type __n1, const _CharT* __s)
4774 __glibcxx_requires_string(__s);
4775 return this->replace(__pos, __n1, __s, traits_type::length(__s));
4779 * @brief Replace characters with multiple characters.
4780 * @param __pos Index of first character to replace.
4781 * @param __n1 Number of characters to be replaced.
4782 * @param __n2 Number of characters to insert.
4783 * @param __c Character to insert.
4784 * @return Reference to this string.
4785 * @throw std::out_of_range If @a __pos > size().
4786 * @throw std::length_error If new length exceeds @c max_size().
4788 * Removes the characters in the range [pos,pos + n1) from this
4789 * string. In place, @a __n2 copies of @a __c are inserted.
4790 * If @a __pos is beyond end of string, out_of_range is thrown.
4791 * If the length of result exceeds max_size(), length_error is
4792 * thrown. The value of the string doesn't change if an error
4793 * is thrown.
4795 basic_string&
4796 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
4797 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
4798 _M_limit(__pos, __n1), __n2, __c); }
4801 * @brief Replace range of characters with string.
4802 * @param __i1 Iterator referencing start of range to replace.
4803 * @param __i2 Iterator referencing end of range to replace.
4804 * @param __str String value to insert.
4805 * @return Reference to this string.
4806 * @throw std::length_error If new length exceeds @c max_size().
4808 * Removes the characters in the range [__i1,__i2). In place,
4809 * the value of @a __str is inserted. If the length of result
4810 * exceeds max_size(), length_error is thrown. The value of
4811 * the string doesn't change if an error is thrown.
4813 basic_string&
4814 replace(iterator __i1, iterator __i2, const basic_string& __str)
4815 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
4818 * @brief Replace range of characters with C substring.
4819 * @param __i1 Iterator referencing start of range to replace.
4820 * @param __i2 Iterator referencing end of range to replace.
4821 * @param __s C string value to insert.
4822 * @param __n Number of characters from s to insert.
4823 * @return Reference to this string.
4824 * @throw std::length_error If new length exceeds @c max_size().
4826 * Removes the characters in the range [__i1,__i2). In place,
4827 * the first @a __n characters of @a __s are inserted. If the
4828 * length of result exceeds max_size(), length_error is thrown.
4829 * The value of the string doesn't change if an error is
4830 * thrown.
4832 basic_string&
4833 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
4835 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4836 && __i2 <= _M_iend());
4837 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
4841 * @brief Replace range of characters with C string.
4842 * @param __i1 Iterator referencing start of range to replace.
4843 * @param __i2 Iterator referencing end of range to replace.
4844 * @param __s C string value to insert.
4845 * @return Reference to this string.
4846 * @throw std::length_error If new length exceeds @c max_size().
4848 * Removes the characters in the range [__i1,__i2). In place,
4849 * the characters of @a __s are inserted. If the length of
4850 * result exceeds max_size(), length_error is thrown. The
4851 * value of the string doesn't change if an error is thrown.
4853 basic_string&
4854 replace(iterator __i1, iterator __i2, const _CharT* __s)
4856 __glibcxx_requires_string(__s);
4857 return this->replace(__i1, __i2, __s, traits_type::length(__s));
4861 * @brief Replace range of characters with multiple characters
4862 * @param __i1 Iterator referencing start of range to replace.
4863 * @param __i2 Iterator referencing end of range to replace.
4864 * @param __n Number of characters to insert.
4865 * @param __c Character to insert.
4866 * @return Reference to this string.
4867 * @throw std::length_error If new length exceeds @c max_size().
4869 * Removes the characters in the range [__i1,__i2). In place,
4870 * @a __n copies of @a __c are inserted. If the length of
4871 * result exceeds max_size(), length_error is thrown. The
4872 * value of the string doesn't change if an error is thrown.
4874 basic_string&
4875 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
4877 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4878 && __i2 <= _M_iend());
4879 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
4883 * @brief Replace range of characters with range.
4884 * @param __i1 Iterator referencing start of range to replace.
4885 * @param __i2 Iterator referencing end of range to replace.
4886 * @param __k1 Iterator referencing start of range to insert.
4887 * @param __k2 Iterator referencing end of range to insert.
4888 * @return Reference to this string.
4889 * @throw std::length_error If new length exceeds @c max_size().
4891 * Removes the characters in the range [__i1,__i2). In place,
4892 * characters in the range [__k1,__k2) are inserted. If the
4893 * length of result exceeds max_size(), length_error is thrown.
4894 * The value of the string doesn't change if an error is
4895 * thrown.
4897 template<class _InputIterator>
4898 basic_string&
4899 replace(iterator __i1, iterator __i2,
4900 _InputIterator __k1, _InputIterator __k2)
4902 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4903 && __i2 <= _M_iend());
4904 __glibcxx_requires_valid_range(__k1, __k2);
4905 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
4906 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
4909 // Specializations for the common case of pointer and iterator:
4910 // useful to avoid the overhead of temporary buffering in _M_replace.
4911 basic_string&
4912 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
4914 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4915 && __i2 <= _M_iend());
4916 __glibcxx_requires_valid_range(__k1, __k2);
4917 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4918 __k1, __k2 - __k1);
4921 basic_string&
4922 replace(iterator __i1, iterator __i2,
4923 const _CharT* __k1, const _CharT* __k2)
4925 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4926 && __i2 <= _M_iend());
4927 __glibcxx_requires_valid_range(__k1, __k2);
4928 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4929 __k1, __k2 - __k1);
4932 basic_string&
4933 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
4935 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4936 && __i2 <= _M_iend());
4937 __glibcxx_requires_valid_range(__k1, __k2);
4938 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4939 __k1.base(), __k2 - __k1);
4942 basic_string&
4943 replace(iterator __i1, iterator __i2,
4944 const_iterator __k1, const_iterator __k2)
4946 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4947 && __i2 <= _M_iend());
4948 __glibcxx_requires_valid_range(__k1, __k2);
4949 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4950 __k1.base(), __k2 - __k1);
4953 #if __cplusplus >= 201103L
4955 * @brief Replace range of characters with initializer_list.
4956 * @param __i1 Iterator referencing start of range to replace.
4957 * @param __i2 Iterator referencing end of range to replace.
4958 * @param __l The initializer_list of characters to insert.
4959 * @return Reference to this string.
4960 * @throw std::length_error If new length exceeds @c max_size().
4962 * Removes the characters in the range [__i1,__i2). In place,
4963 * characters in the range [__k1,__k2) are inserted. If the
4964 * length of result exceeds max_size(), length_error is thrown.
4965 * The value of the string doesn't change if an error is
4966 * thrown.
4968 basic_string& replace(iterator __i1, iterator __i2,
4969 initializer_list<_CharT> __l)
4970 { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
4971 #endif // C++11
4973 #if __cplusplus > 201402L
4975 * @brief Replace range of characters with string_view.
4976 * @param __pos The position to replace at.
4977 * @param __n The number of characters to replace.
4978 * @param __svt The object convertible to string_view to insert.
4979 * @return Reference to this string.
4981 template<typename _Tp>
4982 _If_sv<_Tp, basic_string&>
4983 replace(size_type __pos, size_type __n, const _Tp& __svt)
4985 __sv_type __sv = __svt;
4986 return this->replace(__pos, __n, __sv.data(), __sv.size());
4990 * @brief Replace range of characters with string_view.
4991 * @param __pos1 The position to replace at.
4992 * @param __n1 The number of characters to replace.
4993 * @param __svt The object convertible to string_view to insert from.
4994 * @param __pos2 The position in the string_view to insert from.
4995 * @param __n2 The number of characters to insert.
4996 * @return Reference to this string.
4998 template<typename _Tp>
4999 _If_sv<_Tp, basic_string&>
5000 replace(size_type __pos1, size_type __n1, const _Tp& __svt,
5001 size_type __pos2, size_type __n2 = npos)
5003 __sv_type __sv = __svt;
5004 return this->replace(__pos1, __n1,
5005 __sv.data() + __sv._M_check(__pos2, "basic_string::replace"),
5006 __sv._M_limit(__pos2, __n2));
5010 * @brief Replace range of characters with string_view.
5011 * @param __i1 An iterator referencing the start position
5012 to replace at.
5013 * @param __i2 An iterator referencing the end position
5014 for the replace.
5015 * @param __svt The object convertible to string_view to insert from.
5016 * @return Reference to this string.
5018 template<typename _Tp>
5019 _If_sv<_Tp, basic_string&>
5020 replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
5022 __sv_type __sv = __svt;
5023 return this->replace(__i1 - begin(), __i2 - __i1, __sv);
5025 #endif // C++17
5027 private:
5028 template<class _Integer>
5029 basic_string&
5030 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
5031 _Integer __val, __true_type)
5032 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
5034 template<class _InputIterator>
5035 basic_string&
5036 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
5037 _InputIterator __k2, __false_type);
5039 basic_string&
5040 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
5041 _CharT __c);
5043 basic_string&
5044 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
5045 size_type __n2);
5047 // _S_construct_aux is used to implement the 21.3.1 para 15 which
5048 // requires special behaviour if _InIter is an integral type
5049 template<class _InIterator>
5050 static _CharT*
5051 _S_construct_aux(_InIterator __beg, _InIterator __end,
5052 const _Alloc& __a, __false_type)
5054 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
5055 return _S_construct(__beg, __end, __a, _Tag());
5058 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5059 // 438. Ambiguity in the "do the right thing" clause
5060 template<class _Integer>
5061 static _CharT*
5062 _S_construct_aux(_Integer __beg, _Integer __end,
5063 const _Alloc& __a, __true_type)
5064 { return _S_construct_aux_2(static_cast<size_type>(__beg),
5065 __end, __a); }
5067 static _CharT*
5068 _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
5069 { return _S_construct(__req, __c, __a); }
5071 template<class _InIterator>
5072 static _CharT*
5073 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
5075 typedef typename std::__is_integer<_InIterator>::__type _Integral;
5076 return _S_construct_aux(__beg, __end, __a, _Integral());
5079 // For Input Iterators, used in istreambuf_iterators, etc.
5080 template<class _InIterator>
5081 static _CharT*
5082 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
5083 input_iterator_tag);
5085 // For forward_iterators up to random_access_iterators, used for
5086 // string::iterator, _CharT*, etc.
5087 template<class _FwdIterator>
5088 static _CharT*
5089 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
5090 forward_iterator_tag);
5092 static _CharT*
5093 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
5095 public:
5098 * @brief Copy substring into C string.
5099 * @param __s C string to copy value into.
5100 * @param __n Number of characters to copy.
5101 * @param __pos Index of first character to copy.
5102 * @return Number of characters actually copied
5103 * @throw std::out_of_range If __pos > size().
5105 * Copies up to @a __n characters starting at @a __pos into the
5106 * C string @a __s. If @a __pos is %greater than size(),
5107 * out_of_range is thrown.
5109 size_type
5110 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
5113 * @brief Swap contents with another string.
5114 * @param __s String to swap with.
5116 * Exchanges the contents of this string with that of @a __s in constant
5117 * time.
5119 void
5120 swap(basic_string& __s)
5121 _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value);
5123 // String operations:
5125 * @brief Return const pointer to null-terminated contents.
5127 * This is a handle to internal data. Do not modify or dire things may
5128 * happen.
5130 const _CharT*
5131 c_str() const _GLIBCXX_NOEXCEPT
5132 { return _M_data(); }
5135 * @brief Return const pointer to contents.
5137 * This is a pointer to internal data. It is undefined to modify
5138 * the contents through the returned pointer. To get a pointer that
5139 * allows modifying the contents use @c &str[0] instead,
5140 * (or in C++17 the non-const @c str.data() overload).
5142 const _CharT*
5143 data() const _GLIBCXX_NOEXCEPT
5144 { return _M_data(); }
5146 #if __cplusplus > 201402L
5148 * @brief Return non-const pointer to contents.
5150 * This is a pointer to the character sequence held by the string.
5151 * Modifying the characters in the sequence is allowed.
5153 _CharT*
5154 data() noexcept
5156 _M_leak();
5157 return _M_data();
5159 #endif
5162 * @brief Return copy of allocator used to construct this string.
5164 allocator_type
5165 get_allocator() const _GLIBCXX_NOEXCEPT
5166 { return _M_dataplus; }
5169 * @brief Find position of a C substring.
5170 * @param __s C string to locate.
5171 * @param __pos Index of character to search from.
5172 * @param __n Number of characters from @a s to search for.
5173 * @return Index of start of first occurrence.
5175 * Starting from @a __pos, searches forward for the first @a
5176 * __n characters in @a __s within this string. If found,
5177 * returns the index where it begins. If not found, returns
5178 * npos.
5180 size_type
5181 find(const _CharT* __s, size_type __pos, size_type __n) const
5182 _GLIBCXX_NOEXCEPT;
5185 * @brief Find position of a string.
5186 * @param __str String to locate.
5187 * @param __pos Index of character to search from (default 0).
5188 * @return Index of start of first occurrence.
5190 * Starting from @a __pos, searches forward for value of @a __str within
5191 * this string. If found, returns the index where it begins. If not
5192 * found, returns npos.
5194 size_type
5195 find(const basic_string& __str, size_type __pos = 0) const
5196 _GLIBCXX_NOEXCEPT
5197 { return this->find(__str.data(), __pos, __str.size()); }
5200 * @brief Find position of a C string.
5201 * @param __s C string to locate.
5202 * @param __pos Index of character to search from (default 0).
5203 * @return Index of start of first occurrence.
5205 * Starting from @a __pos, searches forward for the value of @a
5206 * __s within this string. If found, returns the index where
5207 * it begins. If not found, returns npos.
5209 size_type
5210 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
5212 __glibcxx_requires_string(__s);
5213 return this->find(__s, __pos, traits_type::length(__s));
5217 * @brief Find position of a character.
5218 * @param __c Character to locate.
5219 * @param __pos Index of character to search from (default 0).
5220 * @return Index of first occurrence.
5222 * Starting from @a __pos, searches forward for @a __c within
5223 * this string. If found, returns the index where it was
5224 * found. If not found, returns npos.
5226 size_type
5227 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
5229 #if __cplusplus > 201402L
5231 * @brief Find position of a string_view.
5232 * @param __svt The object convertible to string_view to locate.
5233 * @param __pos Index of character to search from (default 0).
5234 * @return Index of start of first occurrence.
5236 template<typename _Tp>
5237 _If_sv<_Tp, size_type>
5238 find(const _Tp& __svt, size_type __pos = 0) const
5239 noexcept(is_same<_Tp, __sv_type>::value)
5241 __sv_type __sv = __svt;
5242 return this->find(__sv.data(), __pos, __sv.size());
5244 #endif // C++17
5247 * @brief Find last position of a string.
5248 * @param __str String to locate.
5249 * @param __pos Index of character to search back from (default end).
5250 * @return Index of start of last occurrence.
5252 * Starting from @a __pos, searches backward for value of @a
5253 * __str within this string. If found, returns the index where
5254 * it begins. If not found, returns npos.
5256 size_type
5257 rfind(const basic_string& __str, size_type __pos = npos) const
5258 _GLIBCXX_NOEXCEPT
5259 { return this->rfind(__str.data(), __pos, __str.size()); }
5262 * @brief Find last position of a C substring.
5263 * @param __s C string to locate.
5264 * @param __pos Index of character to search back from.
5265 * @param __n Number of characters from s to search for.
5266 * @return Index of start of last occurrence.
5268 * Starting from @a __pos, searches backward for the first @a
5269 * __n characters in @a __s within this string. If found,
5270 * returns the index where it begins. If not found, returns
5271 * npos.
5273 size_type
5274 rfind(const _CharT* __s, size_type __pos, size_type __n) const
5275 _GLIBCXX_NOEXCEPT;
5278 * @brief Find last position of a C string.
5279 * @param __s C string to locate.
5280 * @param __pos Index of character to start search at (default end).
5281 * @return Index of start of last occurrence.
5283 * Starting from @a __pos, searches backward for the value of
5284 * @a __s within this string. If found, returns the index
5285 * where it begins. If not found, returns npos.
5287 size_type
5288 rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
5290 __glibcxx_requires_string(__s);
5291 return this->rfind(__s, __pos, traits_type::length(__s));
5295 * @brief Find last position of a character.
5296 * @param __c Character to locate.
5297 * @param __pos Index of character to search back from (default end).
5298 * @return Index of last occurrence.
5300 * Starting from @a __pos, searches backward for @a __c within
5301 * this string. If found, returns the index where it was
5302 * found. If not found, returns npos.
5304 size_type
5305 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
5307 #if __cplusplus > 201402L
5309 * @brief Find last position of a string_view.
5310 * @param __svt The object convertible to string_view to locate.
5311 * @param __pos Index of character to search back from (default end).
5312 * @return Index of start of last occurrence.
5314 template<typename _Tp>
5315 _If_sv<_Tp, size_type>
5316 rfind(const _Tp& __svt, size_type __pos = npos) const
5317 noexcept(is_same<_Tp, __sv_type>::value)
5319 __sv_type __sv = __svt;
5320 return this->rfind(__sv.data(), __pos, __sv.size());
5322 #endif // C++17
5325 * @brief Find position of a character of string.
5326 * @param __str String containing characters to locate.
5327 * @param __pos Index of character to search from (default 0).
5328 * @return Index of first occurrence.
5330 * Starting from @a __pos, searches forward for one of the
5331 * characters of @a __str within this string. If found,
5332 * returns the index where it was found. If not found, returns
5333 * npos.
5335 size_type
5336 find_first_of(const basic_string& __str, size_type __pos = 0) const
5337 _GLIBCXX_NOEXCEPT
5338 { return this->find_first_of(__str.data(), __pos, __str.size()); }
5341 * @brief Find position of a character of C substring.
5342 * @param __s String containing characters to locate.
5343 * @param __pos Index of character to search from.
5344 * @param __n Number of characters from s to search for.
5345 * @return Index of first occurrence.
5347 * Starting from @a __pos, searches forward for one of the
5348 * first @a __n characters of @a __s within this string. If
5349 * found, returns the index where it was found. If not found,
5350 * returns npos.
5352 size_type
5353 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
5354 _GLIBCXX_NOEXCEPT;
5357 * @brief Find position of a character of C string.
5358 * @param __s String containing characters to locate.
5359 * @param __pos Index of character to search from (default 0).
5360 * @return Index of first occurrence.
5362 * Starting from @a __pos, searches forward for one of the
5363 * characters of @a __s within this string. If found, returns
5364 * the index where it was found. If not found, returns npos.
5366 size_type
5367 find_first_of(const _CharT* __s, size_type __pos = 0) const
5368 _GLIBCXX_NOEXCEPT
5370 __glibcxx_requires_string(__s);
5371 return this->find_first_of(__s, __pos, traits_type::length(__s));
5375 * @brief Find position of a character.
5376 * @param __c Character to locate.
5377 * @param __pos Index of character to search from (default 0).
5378 * @return Index of first occurrence.
5380 * Starting from @a __pos, searches forward for the character
5381 * @a __c within this string. If found, returns the index
5382 * where it was found. If not found, returns npos.
5384 * Note: equivalent to find(__c, __pos).
5386 size_type
5387 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
5388 { return this->find(__c, __pos); }
5390 #if __cplusplus > 201402L
5392 * @brief Find position of a character of a string_view.
5393 * @param __svt An object convertible to string_view containing
5394 * characters to locate.
5395 * @param __pos Index of character to search from (default 0).
5396 * @return Index of first occurrence.
5398 template<typename _Tp>
5399 _If_sv<_Tp, size_type>
5400 find_first_of(const _Tp& __svt, size_type __pos = 0) const
5401 noexcept(is_same<_Tp, __sv_type>::value)
5403 __sv_type __sv = __svt;
5404 return this->find_first_of(__sv.data(), __pos, __sv.size());
5406 #endif // C++17
5409 * @brief Find last position of a character of string.
5410 * @param __str String containing characters to locate.
5411 * @param __pos Index of character to search back from (default end).
5412 * @return Index of last occurrence.
5414 * Starting from @a __pos, searches backward for one of the
5415 * characters of @a __str within this string. If found,
5416 * returns the index where it was found. If not found, returns
5417 * npos.
5419 size_type
5420 find_last_of(const basic_string& __str, size_type __pos = npos) const
5421 _GLIBCXX_NOEXCEPT
5422 { return this->find_last_of(__str.data(), __pos, __str.size()); }
5425 * @brief Find last position of a character of C substring.
5426 * @param __s C string containing characters to locate.
5427 * @param __pos Index of character to search back from.
5428 * @param __n Number of characters from s to search for.
5429 * @return Index of last occurrence.
5431 * Starting from @a __pos, searches backward for one of the
5432 * first @a __n characters of @a __s within this string. If
5433 * found, returns the index where it was found. If not found,
5434 * returns npos.
5436 size_type
5437 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
5438 _GLIBCXX_NOEXCEPT;
5441 * @brief Find last position of a character of C string.
5442 * @param __s C string containing characters to locate.
5443 * @param __pos Index of character to search back from (default end).
5444 * @return Index of last occurrence.
5446 * Starting from @a __pos, searches backward for one of the
5447 * characters of @a __s within this string. If found, returns
5448 * the index where it was found. If not found, returns npos.
5450 size_type
5451 find_last_of(const _CharT* __s, size_type __pos = npos) const
5452 _GLIBCXX_NOEXCEPT
5454 __glibcxx_requires_string(__s);
5455 return this->find_last_of(__s, __pos, traits_type::length(__s));
5459 * @brief Find last position of a character.
5460 * @param __c Character to locate.
5461 * @param __pos Index of character to search back from (default end).
5462 * @return Index of last occurrence.
5464 * Starting from @a __pos, searches backward for @a __c within
5465 * this string. If found, returns the index where it was
5466 * found. If not found, returns npos.
5468 * Note: equivalent to rfind(__c, __pos).
5470 size_type
5471 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
5472 { return this->rfind(__c, __pos); }
5474 #if __cplusplus > 201402L
5476 * @brief Find last position of a character of string.
5477 * @param __svt An object convertible to string_view containing
5478 * characters to locate.
5479 * @param __pos Index of character to search back from (default end).
5480 * @return Index of last occurrence.
5482 template<typename _Tp>
5483 _If_sv<_Tp, size_type>
5484 find_last_of(const _Tp& __svt, size_type __pos = npos) const
5485 noexcept(is_same<_Tp, __sv_type>::value)
5487 __sv_type __sv = __svt;
5488 return this->find_last_of(__sv.data(), __pos, __sv.size());
5490 #endif // C++17
5493 * @brief Find position of a character not in string.
5494 * @param __str String containing characters to avoid.
5495 * @param __pos Index of character to search from (default 0).
5496 * @return Index of first occurrence.
5498 * Starting from @a __pos, searches forward for a character not contained
5499 * in @a __str within this string. If found, returns the index where it
5500 * was found. If not found, returns npos.
5502 size_type
5503 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
5504 _GLIBCXX_NOEXCEPT
5505 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
5508 * @brief Find position of a character not in C substring.
5509 * @param __s C string containing characters to avoid.
5510 * @param __pos Index of character to search from.
5511 * @param __n Number of characters from __s to consider.
5512 * @return Index of first occurrence.
5514 * Starting from @a __pos, searches forward for a character not
5515 * contained in the first @a __n characters of @a __s within
5516 * this string. If found, returns the index where it was
5517 * found. If not found, returns npos.
5519 size_type
5520 find_first_not_of(const _CharT* __s, size_type __pos,
5521 size_type __n) const _GLIBCXX_NOEXCEPT;
5524 * @brief Find position of a character not in C string.
5525 * @param __s C string containing characters to avoid.
5526 * @param __pos Index of character to search from (default 0).
5527 * @return Index of first occurrence.
5529 * Starting from @a __pos, searches forward for a character not
5530 * contained in @a __s within this string. If found, returns
5531 * the index where it was found. If not found, returns npos.
5533 size_type
5534 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
5535 _GLIBCXX_NOEXCEPT
5537 __glibcxx_requires_string(__s);
5538 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
5542 * @brief Find position of a different character.
5543 * @param __c Character to avoid.
5544 * @param __pos Index of character to search from (default 0).
5545 * @return Index of first occurrence.
5547 * Starting from @a __pos, searches forward for a character
5548 * other than @a __c within this string. If found, returns the
5549 * index where it was found. If not found, returns npos.
5551 size_type
5552 find_first_not_of(_CharT __c, size_type __pos = 0) const
5553 _GLIBCXX_NOEXCEPT;
5555 #if __cplusplus > 201402L
5557 * @brief Find position of a character not in a string_view.
5558 * @param __svt An object convertible to string_view containing
5559 * characters to avoid.
5560 * @param __pos Index of character to search from (default 0).
5561 * @return Index of first occurrence.
5563 template<typename _Tp>
5564 _If_sv<_Tp, size_type>
5565 find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
5566 noexcept(is_same<_Tp, __sv_type>::value)
5568 __sv_type __sv = __svt;
5569 return this->find_first_not_of(__sv.data(), __pos, __sv.size());
5571 #endif // C++17
5574 * @brief Find last position of a character not in string.
5575 * @param __str String containing characters to avoid.
5576 * @param __pos Index of character to search back from (default end).
5577 * @return Index of last occurrence.
5579 * Starting from @a __pos, searches backward for a character
5580 * not contained in @a __str within this string. If found,
5581 * returns the index where it was found. If not found, returns
5582 * npos.
5584 size_type
5585 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
5586 _GLIBCXX_NOEXCEPT
5587 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
5590 * @brief Find last position of a character not in C substring.
5591 * @param __s C string containing characters to avoid.
5592 * @param __pos Index of character to search back from.
5593 * @param __n Number of characters from s to consider.
5594 * @return Index of last occurrence.
5596 * Starting from @a __pos, searches backward for a character not
5597 * contained in the first @a __n characters of @a __s within this string.
5598 * If found, returns the index where it was found. If not found,
5599 * returns npos.
5601 size_type
5602 find_last_not_of(const _CharT* __s, size_type __pos,
5603 size_type __n) const _GLIBCXX_NOEXCEPT;
5605 * @brief Find last position of a character not in C string.
5606 * @param __s C string containing characters to avoid.
5607 * @param __pos Index of character to search back from (default end).
5608 * @return Index of last occurrence.
5610 * Starting from @a __pos, searches backward for a character
5611 * not contained in @a __s within this string. If found,
5612 * returns the index where it was found. If not found, returns
5613 * npos.
5615 size_type
5616 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
5617 _GLIBCXX_NOEXCEPT
5619 __glibcxx_requires_string(__s);
5620 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
5624 * @brief Find last position of a different character.
5625 * @param __c Character to avoid.
5626 * @param __pos Index of character to search back from (default end).
5627 * @return Index of last occurrence.
5629 * Starting from @a __pos, searches backward for a character other than
5630 * @a __c within this string. If found, returns the index where it was
5631 * found. If not found, returns npos.
5633 size_type
5634 find_last_not_of(_CharT __c, size_type __pos = npos) const
5635 _GLIBCXX_NOEXCEPT;
5637 #if __cplusplus > 201402L
5639 * @brief Find last position of a character not in a string_view.
5640 * @param __svt An object convertible to string_view containing
5641 * characters to avoid.
5642 * @param __pos Index of character to search back from (default end).
5643 * @return Index of last occurrence.
5645 template<typename _Tp>
5646 _If_sv<_Tp, size_type>
5647 find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
5648 noexcept(is_same<_Tp, __sv_type>::value)
5650 __sv_type __sv = __svt;
5651 return this->find_last_not_of(__sv.data(), __pos, __sv.size());
5653 #endif // C++17
5656 * @brief Get a substring.
5657 * @param __pos Index of first character (default 0).
5658 * @param __n Number of characters in substring (default remainder).
5659 * @return The new string.
5660 * @throw std::out_of_range If __pos > size().
5662 * Construct and return a new string using the @a __n
5663 * characters starting at @a __pos. If the string is too
5664 * short, use the remainder of the characters. If @a __pos is
5665 * beyond the end of the string, out_of_range is thrown.
5667 basic_string
5668 substr(size_type __pos = 0, size_type __n = npos) const
5669 { return basic_string(*this,
5670 _M_check(__pos, "basic_string::substr"), __n); }
5673 * @brief Compare to a string.
5674 * @param __str String to compare against.
5675 * @return Integer < 0, 0, or > 0.
5677 * Returns an integer < 0 if this string is ordered before @a
5678 * __str, 0 if their values are equivalent, or > 0 if this
5679 * string is ordered after @a __str. Determines the effective
5680 * length rlen of the strings to compare as the smallest of
5681 * size() and str.size(). The function then compares the two
5682 * strings by calling traits::compare(data(), str.data(),rlen).
5683 * If the result of the comparison is nonzero returns it,
5684 * otherwise the shorter one is ordered first.
5687 compare(const basic_string& __str) const
5689 const size_type __size = this->size();
5690 const size_type __osize = __str.size();
5691 const size_type __len = std::min(__size, __osize);
5693 int __r = traits_type::compare(_M_data(), __str.data(), __len);
5694 if (!__r)
5695 __r = _S_compare(__size, __osize);
5696 return __r;
5699 #if __cplusplus > 201402L
5701 * @brief Compare to a string_view.
5702 * @param __svt An object convertible to string_view to compare against.
5703 * @return Integer < 0, 0, or > 0.
5705 template<typename _Tp>
5706 _If_sv<_Tp, int>
5707 compare(const _Tp& __svt) const
5708 noexcept(is_same<_Tp, __sv_type>::value)
5710 __sv_type __sv = __svt;
5711 const size_type __size = this->size();
5712 const size_type __osize = __sv.size();
5713 const size_type __len = std::min(__size, __osize);
5715 int __r = traits_type::compare(_M_data(), __sv.data(), __len);
5716 if (!__r)
5717 __r = _S_compare(__size, __osize);
5718 return __r;
5722 * @brief Compare to a string_view.
5723 * @param __pos A position in the string to start comparing from.
5724 * @param __n The number of characters to compare.
5725 * @param __svt An object convertible to string_view to compare
5726 * against.
5727 * @return Integer < 0, 0, or > 0.
5729 template<typename _Tp>
5730 _If_sv<_Tp, int>
5731 compare(size_type __pos, size_type __n, const _Tp& __svt) const
5732 noexcept(is_same<_Tp, __sv_type>::value)
5734 __sv_type __sv = __svt;
5735 return __sv_type(*this).substr(__pos, __n).compare(__sv);
5739 * @brief Compare to a string_view.
5740 * @param __pos1 A position in the string to start comparing from.
5741 * @param __n1 The number of characters to compare.
5742 * @param __svt An object convertible to string_view to compare
5743 * against.
5744 * @param __pos2 A position in the string_view to start comparing from.
5745 * @param __n2 The number of characters to compare.
5746 * @return Integer < 0, 0, or > 0.
5748 template<typename _Tp>
5749 _If_sv<_Tp, int>
5750 compare(size_type __pos1, size_type __n1, const _Tp& __svt,
5751 size_type __pos2, size_type __n2 = npos) const
5752 noexcept(is_same<_Tp, __sv_type>::value)
5754 __sv_type __sv = __svt;
5755 return __sv_type(*this)
5756 .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
5758 #endif // C++17
5761 * @brief Compare substring to a string.
5762 * @param __pos Index of first character of substring.
5763 * @param __n Number of characters in substring.
5764 * @param __str String to compare against.
5765 * @return Integer < 0, 0, or > 0.
5767 * Form the substring of this string from the @a __n characters
5768 * starting at @a __pos. Returns an integer < 0 if the
5769 * substring is ordered before @a __str, 0 if their values are
5770 * equivalent, or > 0 if the substring is ordered after @a
5771 * __str. Determines the effective length rlen of the strings
5772 * to compare as the smallest of the length of the substring
5773 * and @a __str.size(). The function then compares the two
5774 * strings by calling
5775 * traits::compare(substring.data(),str.data(),rlen). If the
5776 * result of the comparison is nonzero returns it, otherwise
5777 * the shorter one is ordered first.
5780 compare(size_type __pos, size_type __n, const basic_string& __str) const;
5783 * @brief Compare substring to a substring.
5784 * @param __pos1 Index of first character of substring.
5785 * @param __n1 Number of characters in substring.
5786 * @param __str String to compare against.
5787 * @param __pos2 Index of first character of substring of str.
5788 * @param __n2 Number of characters in substring of str.
5789 * @return Integer < 0, 0, or > 0.
5791 * Form the substring of this string from the @a __n1
5792 * characters starting at @a __pos1. Form the substring of @a
5793 * __str from the @a __n2 characters starting at @a __pos2.
5794 * Returns an integer < 0 if this substring is ordered before
5795 * the substring of @a __str, 0 if their values are equivalent,
5796 * or > 0 if this substring is ordered after the substring of
5797 * @a __str. Determines the effective length rlen of the
5798 * strings to compare as the smallest of the lengths of the
5799 * substrings. The function then compares the two strings by
5800 * calling
5801 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
5802 * If the result of the comparison is nonzero returns it,
5803 * otherwise the shorter one is ordered first.
5806 compare(size_type __pos1, size_type __n1, const basic_string& __str,
5807 size_type __pos2, size_type __n2 = npos) const;
5810 * @brief Compare to a C string.
5811 * @param __s C string to compare against.
5812 * @return Integer < 0, 0, or > 0.
5814 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
5815 * their values are equivalent, or > 0 if this string is ordered after
5816 * @a __s. Determines the effective length rlen of the strings to
5817 * compare as the smallest of size() and the length of a string
5818 * constructed from @a __s. The function then compares the two strings
5819 * by calling traits::compare(data(),s,rlen). If the result of the
5820 * comparison is nonzero returns it, otherwise the shorter one is
5821 * ordered first.
5824 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
5826 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5827 // 5 String::compare specification questionable
5829 * @brief Compare substring to a C string.
5830 * @param __pos Index of first character of substring.
5831 * @param __n1 Number of characters in substring.
5832 * @param __s C string to compare against.
5833 * @return Integer < 0, 0, or > 0.
5835 * Form the substring of this string from the @a __n1
5836 * characters starting at @a pos. Returns an integer < 0 if
5837 * the substring is ordered before @a __s, 0 if their values
5838 * are equivalent, or > 0 if the substring is ordered after @a
5839 * __s. Determines the effective length rlen of the strings to
5840 * compare as the smallest of the length of the substring and
5841 * the length of a string constructed from @a __s. The
5842 * function then compares the two string by calling
5843 * traits::compare(substring.data(),__s,rlen). If the result of
5844 * the comparison is nonzero returns it, otherwise the shorter
5845 * one is ordered first.
5848 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
5851 * @brief Compare substring against a character %array.
5852 * @param __pos Index of first character of substring.
5853 * @param __n1 Number of characters in substring.
5854 * @param __s character %array to compare against.
5855 * @param __n2 Number of characters of s.
5856 * @return Integer < 0, 0, or > 0.
5858 * Form the substring of this string from the @a __n1
5859 * characters starting at @a __pos. Form a string from the
5860 * first @a __n2 characters of @a __s. Returns an integer < 0
5861 * if this substring is ordered before the string from @a __s,
5862 * 0 if their values are equivalent, or > 0 if this substring
5863 * is ordered after the string from @a __s. Determines the
5864 * effective length rlen of the strings to compare as the
5865 * smallest of the length of the substring and @a __n2. The
5866 * function then compares the two strings by calling
5867 * traits::compare(substring.data(),s,rlen). If the result of
5868 * the comparison is nonzero returns it, otherwise the shorter
5869 * one is ordered first.
5871 * NB: s must have at least n2 characters, &apos;\\0&apos; has
5872 * no special meaning.
5875 compare(size_type __pos, size_type __n1, const _CharT* __s,
5876 size_type __n2) const;
5878 # ifdef _GLIBCXX_TM_TS_INTERNAL
5879 friend void
5880 ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
5881 void* exc);
5882 friend const char*
5883 ::_txnal_cow_string_c_str(const void *that);
5884 friend void
5885 ::_txnal_cow_string_D1(void *that);
5886 friend void
5887 ::_txnal_cow_string_D1_commit(void *that);
5888 # endif
5890 #endif // !_GLIBCXX_USE_CXX11_ABI
5892 #if __cpp_deduction_guides >= 201606
5893 _GLIBCXX_BEGIN_NAMESPACE_CXX11
5894 template<typename _InputIterator, typename _CharT
5895 = typename iterator_traits<_InputIterator>::value_type,
5896 typename _Allocator = allocator<_CharT>,
5897 typename = _RequireInputIter<_InputIterator>,
5898 typename = _RequireAllocator<_Allocator>>
5899 basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
5900 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
5902 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5903 // 3075. basic_string needs deduction guides from basic_string_view
5904 template<typename _CharT, typename _Traits,
5905 typename _Allocator = allocator<_CharT>,
5906 typename = _RequireAllocator<_Allocator>>
5907 basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
5908 -> basic_string<_CharT, _Traits, _Allocator>;
5910 template<typename _CharT, typename _Traits,
5911 typename _Allocator = allocator<_CharT>,
5912 typename = _RequireAllocator<_Allocator>>
5913 basic_string(basic_string_view<_CharT, _Traits>,
5914 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
5915 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
5916 const _Allocator& = _Allocator())
5917 -> basic_string<_CharT, _Traits, _Allocator>;
5918 _GLIBCXX_END_NAMESPACE_CXX11
5919 #endif
5921 // operator+
5923 * @brief Concatenate two strings.
5924 * @param __lhs First string.
5925 * @param __rhs Last string.
5926 * @return New string with value of @a __lhs followed by @a __rhs.
5928 template<typename _CharT, typename _Traits, typename _Alloc>
5929 basic_string<_CharT, _Traits, _Alloc>
5930 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5931 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5933 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
5934 __str.append(__rhs);
5935 return __str;
5939 * @brief Concatenate C string and string.
5940 * @param __lhs First string.
5941 * @param __rhs Last string.
5942 * @return New string with value of @a __lhs followed by @a __rhs.
5944 template<typename _CharT, typename _Traits, typename _Alloc>
5945 basic_string<_CharT,_Traits,_Alloc>
5946 operator+(const _CharT* __lhs,
5947 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
5950 * @brief Concatenate character and string.
5951 * @param __lhs First string.
5952 * @param __rhs Last string.
5953 * @return New string with @a __lhs followed by @a __rhs.
5955 template<typename _CharT, typename _Traits, typename _Alloc>
5956 basic_string<_CharT,_Traits,_Alloc>
5957 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
5960 * @brief Concatenate string and C string.
5961 * @param __lhs First string.
5962 * @param __rhs Last string.
5963 * @return New string with @a __lhs followed by @a __rhs.
5965 template<typename _CharT, typename _Traits, typename _Alloc>
5966 inline basic_string<_CharT, _Traits, _Alloc>
5967 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5968 const _CharT* __rhs)
5970 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
5971 __str.append(__rhs);
5972 return __str;
5976 * @brief Concatenate string and character.
5977 * @param __lhs First string.
5978 * @param __rhs Last string.
5979 * @return New string with @a __lhs followed by @a __rhs.
5981 template<typename _CharT, typename _Traits, typename _Alloc>
5982 inline basic_string<_CharT, _Traits, _Alloc>
5983 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
5985 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
5986 typedef typename __string_type::size_type __size_type;
5987 __string_type __str(__lhs);
5988 __str.append(__size_type(1), __rhs);
5989 return __str;
5992 #if __cplusplus >= 201103L
5993 template<typename _CharT, typename _Traits, typename _Alloc>
5994 inline basic_string<_CharT, _Traits, _Alloc>
5995 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
5996 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5997 { return std::move(__lhs.append(__rhs)); }
5999 template<typename _CharT, typename _Traits, typename _Alloc>
6000 inline basic_string<_CharT, _Traits, _Alloc>
6001 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6002 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6003 { return std::move(__rhs.insert(0, __lhs)); }
6005 template<typename _CharT, typename _Traits, typename _Alloc>
6006 inline basic_string<_CharT, _Traits, _Alloc>
6007 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6008 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6010 const auto __size = __lhs.size() + __rhs.size();
6011 const bool __cond = (__size > __lhs.capacity()
6012 && __size <= __rhs.capacity());
6013 return __cond ? std::move(__rhs.insert(0, __lhs))
6014 : std::move(__lhs.append(__rhs));
6017 template<typename _CharT, typename _Traits, typename _Alloc>
6018 inline basic_string<_CharT, _Traits, _Alloc>
6019 operator+(const _CharT* __lhs,
6020 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6021 { return std::move(__rhs.insert(0, __lhs)); }
6023 template<typename _CharT, typename _Traits, typename _Alloc>
6024 inline basic_string<_CharT, _Traits, _Alloc>
6025 operator+(_CharT __lhs,
6026 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6027 { return std::move(__rhs.insert(0, 1, __lhs)); }
6029 template<typename _CharT, typename _Traits, typename _Alloc>
6030 inline basic_string<_CharT, _Traits, _Alloc>
6031 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6032 const _CharT* __rhs)
6033 { return std::move(__lhs.append(__rhs)); }
6035 template<typename _CharT, typename _Traits, typename _Alloc>
6036 inline basic_string<_CharT, _Traits, _Alloc>
6037 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6038 _CharT __rhs)
6039 { return std::move(__lhs.append(1, __rhs)); }
6040 #endif
6042 // operator ==
6044 * @brief Test equivalence of two strings.
6045 * @param __lhs First string.
6046 * @param __rhs Second string.
6047 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6049 template<typename _CharT, typename _Traits, typename _Alloc>
6050 inline bool
6051 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6052 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6053 _GLIBCXX_NOEXCEPT
6054 { return __lhs.compare(__rhs) == 0; }
6056 template<typename _CharT>
6057 inline
6058 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
6059 operator==(const basic_string<_CharT>& __lhs,
6060 const basic_string<_CharT>& __rhs) _GLIBCXX_NOEXCEPT
6061 { return (__lhs.size() == __rhs.size()
6062 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
6063 __lhs.size())); }
6066 * @brief Test equivalence of C string and string.
6067 * @param __lhs C string.
6068 * @param __rhs String.
6069 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
6071 template<typename _CharT, typename _Traits, typename _Alloc>
6072 inline bool
6073 operator==(const _CharT* __lhs,
6074 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6075 { return __rhs.compare(__lhs) == 0; }
6078 * @brief Test equivalence of string and C string.
6079 * @param __lhs String.
6080 * @param __rhs C string.
6081 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6083 template<typename _CharT, typename _Traits, typename _Alloc>
6084 inline bool
6085 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6086 const _CharT* __rhs)
6087 { return __lhs.compare(__rhs) == 0; }
6089 // operator !=
6091 * @brief Test difference of two strings.
6092 * @param __lhs First string.
6093 * @param __rhs Second string.
6094 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6096 template<typename _CharT, typename _Traits, typename _Alloc>
6097 inline bool
6098 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6099 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6100 _GLIBCXX_NOEXCEPT
6101 { return !(__lhs == __rhs); }
6104 * @brief Test difference of C string and string.
6105 * @param __lhs C string.
6106 * @param __rhs String.
6107 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
6109 template<typename _CharT, typename _Traits, typename _Alloc>
6110 inline bool
6111 operator!=(const _CharT* __lhs,
6112 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6113 { return !(__lhs == __rhs); }
6116 * @brief Test difference of string and C string.
6117 * @param __lhs String.
6118 * @param __rhs C string.
6119 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6121 template<typename _CharT, typename _Traits, typename _Alloc>
6122 inline bool
6123 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6124 const _CharT* __rhs)
6125 { return !(__lhs == __rhs); }
6127 // operator <
6129 * @brief Test if string precedes string.
6130 * @param __lhs First string.
6131 * @param __rhs Second string.
6132 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6134 template<typename _CharT, typename _Traits, typename _Alloc>
6135 inline bool
6136 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6137 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6138 _GLIBCXX_NOEXCEPT
6139 { return __lhs.compare(__rhs) < 0; }
6142 * @brief Test if string precedes C string.
6143 * @param __lhs String.
6144 * @param __rhs C string.
6145 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6147 template<typename _CharT, typename _Traits, typename _Alloc>
6148 inline bool
6149 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6150 const _CharT* __rhs)
6151 { return __lhs.compare(__rhs) < 0; }
6154 * @brief Test if C string precedes string.
6155 * @param __lhs C string.
6156 * @param __rhs String.
6157 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6159 template<typename _CharT, typename _Traits, typename _Alloc>
6160 inline bool
6161 operator<(const _CharT* __lhs,
6162 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6163 { return __rhs.compare(__lhs) > 0; }
6165 // operator >
6167 * @brief Test if string follows string.
6168 * @param __lhs First string.
6169 * @param __rhs Second string.
6170 * @return True if @a __lhs follows @a __rhs. False otherwise.
6172 template<typename _CharT, typename _Traits, typename _Alloc>
6173 inline bool
6174 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6175 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6176 _GLIBCXX_NOEXCEPT
6177 { return __lhs.compare(__rhs) > 0; }
6180 * @brief Test if string follows C string.
6181 * @param __lhs String.
6182 * @param __rhs C string.
6183 * @return True if @a __lhs follows @a __rhs. False otherwise.
6185 template<typename _CharT, typename _Traits, typename _Alloc>
6186 inline bool
6187 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6188 const _CharT* __rhs)
6189 { return __lhs.compare(__rhs) > 0; }
6192 * @brief Test if C string follows string.
6193 * @param __lhs C string.
6194 * @param __rhs String.
6195 * @return True if @a __lhs follows @a __rhs. False otherwise.
6197 template<typename _CharT, typename _Traits, typename _Alloc>
6198 inline bool
6199 operator>(const _CharT* __lhs,
6200 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6201 { return __rhs.compare(__lhs) < 0; }
6203 // operator <=
6205 * @brief Test if string doesn't follow string.
6206 * @param __lhs First string.
6207 * @param __rhs Second string.
6208 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6210 template<typename _CharT, typename _Traits, typename _Alloc>
6211 inline bool
6212 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6213 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6214 _GLIBCXX_NOEXCEPT
6215 { return __lhs.compare(__rhs) <= 0; }
6218 * @brief Test if string doesn't follow C string.
6219 * @param __lhs String.
6220 * @param __rhs C string.
6221 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6223 template<typename _CharT, typename _Traits, typename _Alloc>
6224 inline bool
6225 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6226 const _CharT* __rhs)
6227 { return __lhs.compare(__rhs) <= 0; }
6230 * @brief Test if C string doesn't follow string.
6231 * @param __lhs C string.
6232 * @param __rhs String.
6233 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6235 template<typename _CharT, typename _Traits, typename _Alloc>
6236 inline bool
6237 operator<=(const _CharT* __lhs,
6238 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6239 { return __rhs.compare(__lhs) >= 0; }
6241 // operator >=
6243 * @brief Test if string doesn't precede string.
6244 * @param __lhs First string.
6245 * @param __rhs Second string.
6246 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6248 template<typename _CharT, typename _Traits, typename _Alloc>
6249 inline bool
6250 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6251 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6252 _GLIBCXX_NOEXCEPT
6253 { return __lhs.compare(__rhs) >= 0; }
6256 * @brief Test if string doesn't precede C string.
6257 * @param __lhs String.
6258 * @param __rhs C string.
6259 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6261 template<typename _CharT, typename _Traits, typename _Alloc>
6262 inline bool
6263 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6264 const _CharT* __rhs)
6265 { return __lhs.compare(__rhs) >= 0; }
6268 * @brief Test if C string doesn't precede string.
6269 * @param __lhs C string.
6270 * @param __rhs String.
6271 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6273 template<typename _CharT, typename _Traits, typename _Alloc>
6274 inline bool
6275 operator>=(const _CharT* __lhs,
6276 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6277 { return __rhs.compare(__lhs) <= 0; }
6280 * @brief Swap contents of two strings.
6281 * @param __lhs First string.
6282 * @param __rhs Second string.
6284 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
6286 template<typename _CharT, typename _Traits, typename _Alloc>
6287 inline void
6288 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
6289 basic_string<_CharT, _Traits, _Alloc>& __rhs)
6290 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
6291 { __lhs.swap(__rhs); }
6295 * @brief Read stream into a string.
6296 * @param __is Input stream.
6297 * @param __str Buffer to store into.
6298 * @return Reference to the input stream.
6300 * Stores characters from @a __is into @a __str until whitespace is
6301 * found, the end of the stream is encountered, or str.max_size()
6302 * is reached. If is.width() is non-zero, that is the limit on the
6303 * number of characters stored into @a __str. Any previous
6304 * contents of @a __str are erased.
6306 template<typename _CharT, typename _Traits, typename _Alloc>
6307 basic_istream<_CharT, _Traits>&
6308 operator>>(basic_istream<_CharT, _Traits>& __is,
6309 basic_string<_CharT, _Traits, _Alloc>& __str);
6311 template<>
6312 basic_istream<char>&
6313 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
6316 * @brief Write string to a stream.
6317 * @param __os Output stream.
6318 * @param __str String to write out.
6319 * @return Reference to the output stream.
6321 * Output characters of @a __str into os following the same rules as for
6322 * writing a C string.
6324 template<typename _CharT, typename _Traits, typename _Alloc>
6325 inline basic_ostream<_CharT, _Traits>&
6326 operator<<(basic_ostream<_CharT, _Traits>& __os,
6327 const basic_string<_CharT, _Traits, _Alloc>& __str)
6329 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6330 // 586. string inserter not a formatted function
6331 return __ostream_insert(__os, __str.data(), __str.size());
6335 * @brief Read a line from stream into a string.
6336 * @param __is Input stream.
6337 * @param __str Buffer to store into.
6338 * @param __delim Character marking end of line.
6339 * @return Reference to the input stream.
6341 * Stores characters from @a __is into @a __str until @a __delim is
6342 * found, the end of the stream is encountered, or str.max_size()
6343 * is reached. Any previous contents of @a __str are erased. If
6344 * @a __delim is encountered, it is extracted but not stored into
6345 * @a __str.
6347 template<typename _CharT, typename _Traits, typename _Alloc>
6348 basic_istream<_CharT, _Traits>&
6349 getline(basic_istream<_CharT, _Traits>& __is,
6350 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
6353 * @brief Read a line from stream into a string.
6354 * @param __is Input stream.
6355 * @param __str Buffer to store into.
6356 * @return Reference to the input stream.
6358 * Stores characters from is into @a __str until &apos;\n&apos; is
6359 * found, the end of the stream is encountered, or str.max_size()
6360 * is reached. Any previous contents of @a __str are erased. If
6361 * end of line is encountered, it is extracted but not stored into
6362 * @a __str.
6364 template<typename _CharT, typename _Traits, typename _Alloc>
6365 inline basic_istream<_CharT, _Traits>&
6366 getline(basic_istream<_CharT, _Traits>& __is,
6367 basic_string<_CharT, _Traits, _Alloc>& __str)
6368 { return std::getline(__is, __str, __is.widen('\n')); }
6370 #if __cplusplus >= 201103L
6371 /// Read a line from an rvalue stream into a string.
6372 template<typename _CharT, typename _Traits, typename _Alloc>
6373 inline basic_istream<_CharT, _Traits>&
6374 getline(basic_istream<_CharT, _Traits>&& __is,
6375 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
6376 { return std::getline(__is, __str, __delim); }
6378 /// Read a line from an rvalue stream into a string.
6379 template<typename _CharT, typename _Traits, typename _Alloc>
6380 inline basic_istream<_CharT, _Traits>&
6381 getline(basic_istream<_CharT, _Traits>&& __is,
6382 basic_string<_CharT, _Traits, _Alloc>& __str)
6383 { return std::getline(__is, __str); }
6384 #endif
6386 template<>
6387 basic_istream<char>&
6388 getline(basic_istream<char>& __in, basic_string<char>& __str,
6389 char __delim);
6391 #ifdef _GLIBCXX_USE_WCHAR_T
6392 template<>
6393 basic_istream<wchar_t>&
6394 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
6395 wchar_t __delim);
6396 #endif
6398 _GLIBCXX_END_NAMESPACE_VERSION
6399 } // namespace
6401 #if __cplusplus >= 201103L
6403 #include <ext/string_conversions.h>
6405 namespace std _GLIBCXX_VISIBILITY(default)
6407 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6408 _GLIBCXX_BEGIN_NAMESPACE_CXX11
6410 #if _GLIBCXX_USE_C99_STDLIB
6411 // 21.4 Numeric Conversions [string.conversions].
6412 inline int
6413 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
6414 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
6415 __idx, __base); }
6417 inline long
6418 stol(const string& __str, size_t* __idx = 0, int __base = 10)
6419 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
6420 __idx, __base); }
6422 inline unsigned long
6423 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
6424 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
6425 __idx, __base); }
6427 inline long long
6428 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
6429 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
6430 __idx, __base); }
6432 inline unsigned long long
6433 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
6434 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
6435 __idx, __base); }
6437 // NB: strtof vs strtod.
6438 inline float
6439 stof(const string& __str, size_t* __idx = 0)
6440 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
6442 inline double
6443 stod(const string& __str, size_t* __idx = 0)
6444 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
6446 inline long double
6447 stold(const string& __str, size_t* __idx = 0)
6448 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
6449 #endif // _GLIBCXX_USE_C99_STDLIB
6451 #if _GLIBCXX_USE_C99_STDIO
6452 // NB: (v)snprintf vs sprintf.
6454 // DR 1261.
6455 inline string
6456 to_string(int __val)
6457 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
6458 "%d", __val); }
6460 inline string
6461 to_string(unsigned __val)
6462 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
6463 4 * sizeof(unsigned),
6464 "%u", __val); }
6466 inline string
6467 to_string(long __val)
6468 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
6469 "%ld", __val); }
6471 inline string
6472 to_string(unsigned long __val)
6473 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
6474 4 * sizeof(unsigned long),
6475 "%lu", __val); }
6477 inline string
6478 to_string(long long __val)
6479 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
6480 4 * sizeof(long long),
6481 "%lld", __val); }
6483 inline string
6484 to_string(unsigned long long __val)
6485 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
6486 4 * sizeof(unsigned long long),
6487 "%llu", __val); }
6489 inline string
6490 to_string(float __val)
6492 const int __n =
6493 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
6494 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6495 "%f", __val);
6498 inline string
6499 to_string(double __val)
6501 const int __n =
6502 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
6503 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6504 "%f", __val);
6507 inline string
6508 to_string(long double __val)
6510 const int __n =
6511 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
6512 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6513 "%Lf", __val);
6515 #endif // _GLIBCXX_USE_C99_STDIO
6517 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
6518 inline int
6519 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
6520 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
6521 __idx, __base); }
6523 inline long
6524 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
6525 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
6526 __idx, __base); }
6528 inline unsigned long
6529 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
6530 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
6531 __idx, __base); }
6533 inline long long
6534 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
6535 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
6536 __idx, __base); }
6538 inline unsigned long long
6539 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
6540 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
6541 __idx, __base); }
6543 // NB: wcstof vs wcstod.
6544 inline float
6545 stof(const wstring& __str, size_t* __idx = 0)
6546 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
6548 inline double
6549 stod(const wstring& __str, size_t* __idx = 0)
6550 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
6552 inline long double
6553 stold(const wstring& __str, size_t* __idx = 0)
6554 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
6556 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6557 // DR 1261.
6558 inline wstring
6559 to_wstring(int __val)
6560 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
6561 L"%d", __val); }
6563 inline wstring
6564 to_wstring(unsigned __val)
6565 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6566 4 * sizeof(unsigned),
6567 L"%u", __val); }
6569 inline wstring
6570 to_wstring(long __val)
6571 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
6572 L"%ld", __val); }
6574 inline wstring
6575 to_wstring(unsigned long __val)
6576 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6577 4 * sizeof(unsigned long),
6578 L"%lu", __val); }
6580 inline wstring
6581 to_wstring(long long __val)
6582 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6583 4 * sizeof(long long),
6584 L"%lld", __val); }
6586 inline wstring
6587 to_wstring(unsigned long long __val)
6588 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6589 4 * sizeof(unsigned long long),
6590 L"%llu", __val); }
6592 inline wstring
6593 to_wstring(float __val)
6595 const int __n =
6596 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
6597 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6598 L"%f", __val);
6601 inline wstring
6602 to_wstring(double __val)
6604 const int __n =
6605 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
6606 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6607 L"%f", __val);
6610 inline wstring
6611 to_wstring(long double __val)
6613 const int __n =
6614 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
6615 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6616 L"%Lf", __val);
6618 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6619 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
6621 _GLIBCXX_END_NAMESPACE_CXX11
6622 _GLIBCXX_END_NAMESPACE_VERSION
6623 } // namespace
6625 #endif /* C++11 */
6627 #if __cplusplus >= 201103L
6629 #include <bits/functional_hash.h>
6631 namespace std _GLIBCXX_VISIBILITY(default)
6633 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6635 // DR 1182.
6637 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
6638 /// std::hash specialization for string.
6639 template<>
6640 struct hash<string>
6641 : public __hash_base<size_t, string>
6643 size_t
6644 operator()(const string& __s) const noexcept
6645 { return std::_Hash_impl::hash(__s.data(), __s.length()); }
6648 template<>
6649 struct __is_fast_hash<hash<string>> : std::false_type
6650 { };
6652 #ifdef _GLIBCXX_USE_WCHAR_T
6653 /// std::hash specialization for wstring.
6654 template<>
6655 struct hash<wstring>
6656 : public __hash_base<size_t, wstring>
6658 size_t
6659 operator()(const wstring& __s) const noexcept
6660 { return std::_Hash_impl::hash(__s.data(),
6661 __s.length() * sizeof(wchar_t)); }
6664 template<>
6665 struct __is_fast_hash<hash<wstring>> : std::false_type
6666 { };
6667 #endif
6668 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
6670 /// std::hash specialization for u16string.
6671 template<>
6672 struct hash<u16string>
6673 : public __hash_base<size_t, u16string>
6675 size_t
6676 operator()(const u16string& __s) const noexcept
6677 { return std::_Hash_impl::hash(__s.data(),
6678 __s.length() * sizeof(char16_t)); }
6681 template<>
6682 struct __is_fast_hash<hash<u16string>> : std::false_type
6683 { };
6685 /// std::hash specialization for u32string.
6686 template<>
6687 struct hash<u32string>
6688 : public __hash_base<size_t, u32string>
6690 size_t
6691 operator()(const u32string& __s) const noexcept
6692 { return std::_Hash_impl::hash(__s.data(),
6693 __s.length() * sizeof(char32_t)); }
6696 template<>
6697 struct __is_fast_hash<hash<u32string>> : std::false_type
6698 { };
6700 #if __cplusplus > 201103L
6702 #define __cpp_lib_string_udls 201304
6704 inline namespace literals
6706 inline namespace string_literals
6708 #pragma GCC diagnostic push
6709 #pragma GCC diagnostic ignored "-Wliteral-suffix"
6710 _GLIBCXX_DEFAULT_ABI_TAG
6711 inline basic_string<char>
6712 operator""s(const char* __str, size_t __len)
6713 { return basic_string<char>{__str, __len}; }
6715 #ifdef _GLIBCXX_USE_WCHAR_T
6716 _GLIBCXX_DEFAULT_ABI_TAG
6717 inline basic_string<wchar_t>
6718 operator""s(const wchar_t* __str, size_t __len)
6719 { return basic_string<wchar_t>{__str, __len}; }
6720 #endif
6722 _GLIBCXX_DEFAULT_ABI_TAG
6723 inline basic_string<char16_t>
6724 operator""s(const char16_t* __str, size_t __len)
6725 { return basic_string<char16_t>{__str, __len}; }
6727 _GLIBCXX_DEFAULT_ABI_TAG
6728 inline basic_string<char32_t>
6729 operator""s(const char32_t* __str, size_t __len)
6730 { return basic_string<char32_t>{__str, __len}; }
6732 #pragma GCC diagnostic pop
6733 } // inline namespace string_literals
6734 } // inline namespace literals
6736 #endif // __cplusplus > 201103L
6738 _GLIBCXX_END_NAMESPACE_VERSION
6739 } // namespace std
6741 #endif // C++11
6743 #endif /* _BASIC_STRING_H */