1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
27 /** @file basic_string.h
28 * This is an internal header file, included by other library headers.
29 * You should not attempt to use it directly.
33 // ISO C++ 14882: 21 Strings library
36 #ifndef _BASIC_STRING_H
37 #define _BASIC_STRING_H 1
39 #pragma GCC system_header
41 #include <ext/atomicity.h>
42 #include <debug/debug.h>
43 #include <initializer_list>
45 _GLIBCXX_BEGIN_NAMESPACE(std
)
48 * @class basic_string basic_string.h <string>
49 * @brief Managing sequences of characters and character-like objects.
53 * Meets the requirements of a <a href="tables.html#65">container</a>, a
54 * <a href="tables.html#66">reversible container</a>, and a
55 * <a href="tables.html#67">sequence</a>. Of the
56 * <a href="tables.html#68">optional sequence requirements</a>, only
57 * @c push_back, @c at, and array access are supported.
62 * Documentation? What's that?
63 * Nathan Myers <ncm@cantrip.org>.
65 * A string looks like this:
70 * [basic_string<char_type>] _M_capacity
71 * _M_dataplus _M_refcount
72 * _M_p ----------------> unnamed array of char_type
75 * Where the _M_p points to the first character in the string, and
76 * you cast it to a pointer-to-_Rep and subtract 1 to get a
77 * pointer to the header.
79 * This approach has the enormous advantage that a string object
80 * requires only one allocation. All the ugliness is confined
81 * within a single pair of inline functions, which each compile to
82 * a single "add" instruction: _Rep::_M_data(), and
83 * string::_M_rep(); and the allocation function which gets a
84 * block of raw bytes and with room enough and constructs a _Rep
85 * object at the front.
87 * The reason you want _M_data pointing to the character array and
88 * not the _Rep is so that the debugger can see the string
89 * contents. (Probably we should add a non-inline member to get
90 * the _Rep for the debugger to use, so users can check the actual
93 * Note that the _Rep object is a POD so that you can have a
94 * static "empty string" _Rep object already "constructed" before
95 * static constructors have run. The reference-count encoding is
96 * chosen so that a 0 indicates one reference, so you never try to
97 * destroy the empty-string _Rep object.
99 * All but the last paragraph is considered pretty conventional
100 * for a C++ string implementation.
102 // 21.3 Template class basic_string
103 template<typename _CharT
, typename _Traits
, typename _Alloc
>
106 typedef typename
_Alloc::template rebind
<_CharT
>::other _CharT_alloc_type
;
110 typedef _Traits traits_type
;
111 typedef typename
_Traits::char_type value_type
;
112 typedef _Alloc allocator_type
;
113 typedef typename
_CharT_alloc_type::size_type size_type
;
114 typedef typename
_CharT_alloc_type::difference_type difference_type
;
115 typedef typename
_CharT_alloc_type::reference reference
;
116 typedef typename
_CharT_alloc_type::const_reference const_reference
;
117 typedef typename
_CharT_alloc_type::pointer pointer
;
118 typedef typename
_CharT_alloc_type::const_pointer const_pointer
;
119 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
120 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
122 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
123 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
126 // _Rep: string representation
128 // 1. String really contains _M_length + 1 characters: due to 21.3.4
129 // must be kept null-terminated.
130 // 2. _M_capacity >= _M_length
131 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
132 // 3. _M_refcount has three states:
133 // -1: leaked, one reference, no ref-copies allowed, non-const.
134 // 0: one reference, non-const.
135 // n>0: n + 1 references, operations require a lock, const.
136 // 4. All fields==0 is an empty string, given the extra storage
137 // beyond-the-end for a null terminator; thus, the shared
138 // empty string representation needs no constructor.
143 size_type _M_capacity
;
144 _Atomic_word _M_refcount
;
147 struct _Rep
: _Rep_base
150 typedef typename
_Alloc::template rebind
<char>::other _Raw_bytes_alloc
;
152 // (Public) Data members:
154 // The maximum number of individual char_type elements of an
155 // individual string is determined by _S_max_size. This is the
156 // value that will be returned by max_size(). (Whereas npos
157 // is the maximum number of bytes the allocator can allocate.)
158 // If one was to divvy up the theoretical largest size string,
159 // with a terminating character and m _CharT elements, it'd
161 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
163 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
164 // In addition, this implementation quarters this amount.
165 static const size_type _S_max_size
;
166 static const _CharT _S_terminal
;
168 // The following storage is init'd to 0 by the linker, resulting
169 // (carefully) in an empty string with one reference.
170 static size_type _S_empty_rep_storage
[];
175 // NB: Mild hack to avoid strict-aliasing warnings. Note that
176 // _S_empty_rep_storage is never modified and the punning should
177 // be reasonably safe in this case.
178 void* __p
= reinterpret_cast<void*>(&_S_empty_rep_storage
);
179 return *reinterpret_cast<_Rep
*>(__p
);
184 { return this->_M_refcount
< 0; }
188 { return this->_M_refcount
> 0; }
192 { this->_M_refcount
= -1; }
196 { this->_M_refcount
= 0; }
199 _M_set_length_and_sharable(size_type __n
)
201 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
202 if (__builtin_expect(this != &_S_empty_rep(), false))
205 this->_M_set_sharable(); // One reference.
206 this->_M_length
= __n
;
207 traits_type::assign(this->_M_refdata()[__n
], _S_terminal
);
208 // grrr. (per 21.3.4)
209 // You cannot leave those LWG people alone for a second.
215 { return reinterpret_cast<_CharT
*>(this + 1); }
218 _M_grab(const _Alloc
& __alloc1
, const _Alloc
& __alloc2
)
220 return (!_M_is_leaked() && __alloc1
== __alloc2
)
221 ? _M_refcopy() : _M_clone(__alloc1
);
226 _S_create(size_type
, size_type
, const _Alloc
&);
229 _M_dispose(const _Alloc
& __a
)
231 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
232 if (__builtin_expect(this != &_S_empty_rep(), false))
234 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount
,
240 _M_destroy(const _Alloc
&) throw();
245 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
246 if (__builtin_expect(this != &_S_empty_rep(), false))
248 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount
, 1);
253 _M_clone(const _Alloc
&, size_type __res
= 0);
256 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
257 struct _Alloc_hider
: _Alloc
259 _Alloc_hider(_CharT
* __dat
, const _Alloc
& __a
)
260 : _Alloc(__a
), _M_p(__dat
) { }
262 _CharT
* _M_p
; // The actual data.
266 // Data Members (public):
267 // NB: This is an unsigned type, and thus represents the maximum
268 // size that the allocator can hold.
269 /// Value returned by various member functions when they fail.
270 static const size_type npos
= static_cast<size_type
>(-1);
273 // Data Members (private):
274 mutable _Alloc_hider _M_dataplus
;
278 { return _M_dataplus
._M_p
; }
282 { return (_M_dataplus
._M_p
= __p
); }
286 { return &((reinterpret_cast<_Rep
*> (_M_data()))[-1]); }
288 // For the internal use we have functions similar to `begin'/`end'
289 // but they do not call _M_leak.
292 { return iterator(_M_data()); }
296 { return iterator(_M_data() + this->size()); }
299 _M_leak() // for use in begin() & non-const op[]
301 if (!_M_rep()->_M_is_leaked())
306 _M_check(size_type __pos
, const char* __s
) const
308 if (__pos
> this->size())
309 __throw_out_of_range(__N(__s
));
314 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
316 if (this->max_size() - (this->size() - __n1
) < __n2
)
317 __throw_length_error(__N(__s
));
320 // NB: _M_limit doesn't check for a bad __pos value.
322 _M_limit(size_type __pos
, size_type __off
) const
324 const bool __testoff
= __off
< this->size() - __pos
;
325 return __testoff
? __off
: this->size() - __pos
;
328 // True if _Rep and source do not overlap.
330 _M_disjunct(const _CharT
* __s
) const
332 return (less
<const _CharT
*>()(__s
, _M_data())
333 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
336 // When __n = 1 way faster than the general multichar
337 // traits_type::copy/move/assign.
339 _M_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
342 traits_type::assign(*__d
, *__s
);
344 traits_type::copy(__d
, __s
, __n
);
348 _M_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
351 traits_type::assign(*__d
, *__s
);
353 traits_type::move(__d
, __s
, __n
);
357 _M_assign(_CharT
* __d
, size_type __n
, _CharT __c
)
360 traits_type::assign(*__d
, __c
);
362 traits_type::assign(__d
, __n
, __c
);
365 // _S_copy_chars is a separate template to permit specialization
366 // to optimize for the common case of pointers as iterators.
367 template<class _Iterator
>
369 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
371 for (; __k1
!= __k2
; ++__k1
, ++__p
)
372 traits_type::assign(*__p
, *__k1
); // These types are off.
376 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
)
377 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
380 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
381 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
384 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
)
385 { _M_copy(__p
, __k1
, __k2
- __k1
); }
388 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
389 { _M_copy(__p
, __k1
, __k2
- __k1
); }
392 _S_compare(size_type __n1
, size_type __n2
)
394 const difference_type __d
= difference_type(__n1
- __n2
);
396 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
397 return __gnu_cxx::__numeric_traits
<int>::__max
;
398 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
399 return __gnu_cxx::__numeric_traits
<int>::__min
;
405 _M_mutate(size_type __pos
, size_type __len1
, size_type __len2
);
412 { return _Rep::_S_empty_rep(); }
415 // Construct/copy/destroy:
416 // NB: We overload ctors in some cases instead of using default
417 // arguments, per 17.4.4.4 para. 2 item 2.
420 * @brief Default constructor creates an empty string.
423 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
424 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
426 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
430 * @brief Construct an empty string using allocator @a a.
433 basic_string(const _Alloc
& __a
);
435 // NB: per LWG issue 42, semantics different from IS:
437 * @brief Construct string with copy of value of @a str.
438 * @param str Source string.
440 basic_string(const basic_string
& __str
);
442 * @brief Construct string as copy of a substring.
443 * @param str Source string.
444 * @param pos Index of first character to copy from.
445 * @param n Number of characters to copy (default remainder).
447 basic_string(const basic_string
& __str
, size_type __pos
,
448 size_type __n
= npos
);
450 * @brief Construct string as copy of a substring.
451 * @param str Source string.
452 * @param pos Index of first character to copy from.
453 * @param n Number of characters to copy.
454 * @param a Allocator to use.
456 basic_string(const basic_string
& __str
, size_type __pos
,
457 size_type __n
, const _Alloc
& __a
);
460 * @brief Construct string initialized by a character array.
461 * @param s Source character array.
462 * @param n Number of characters to copy.
463 * @param a Allocator to use (default is default allocator).
465 * NB: @a s must have at least @a n characters, '\\0' has no special
468 basic_string(const _CharT
* __s
, size_type __n
,
469 const _Alloc
& __a
= _Alloc());
471 * @brief Construct string as copy of a C string.
472 * @param s Source C string.
473 * @param a Allocator to use (default is default allocator).
475 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc());
477 * @brief Construct string as multiple characters.
478 * @param n Number of characters.
479 * @param c Character to use.
480 * @param a Allocator to use (default is default allocator).
482 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc());
484 #ifdef __GXX_EXPERIMENTAL_CXX0X__
486 * @brief Move construct string.
487 * @param str Source string.
489 * The newly-created string contains the exact contents of @a str.
490 * @a str is a valid, but unspecified string.
492 basic_string(basic_string
&& __str
)
493 : _M_dataplus(__str
._M_dataplus
)
495 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
496 __str
._M_data(_S_empty_rep()._M_refdata());
498 __str
._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
503 * @brief Construct string from an initializer list.
504 * @param l std::initializer_list of characters.
505 * @param a Allocator to use (default is default allocator).
507 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc());
508 #endif // __GXX_EXPERIMENTAL_CXX0X__
511 * @brief Construct string as copy of a range.
512 * @param beg Start of range.
513 * @param end End of range.
514 * @param a Allocator to use (default is default allocator).
516 template<class _InputIterator
>
517 basic_string(_InputIterator __beg
, _InputIterator __end
,
518 const _Alloc
& __a
= _Alloc());
521 * @brief Destroy the string instance.
524 { _M_rep()->_M_dispose(this->get_allocator()); }
527 * @brief Assign the value of @a str to this string.
528 * @param str Source string.
531 operator=(const basic_string
& __str
)
532 { return this->assign(__str
); }
535 * @brief Copy contents of @a s into this string.
536 * @param s Source null-terminated string.
539 operator=(const _CharT
* __s
)
540 { return this->assign(__s
); }
543 * @brief Set value to string of length 1.
544 * @param c Source character.
546 * Assigning to a character makes this string length 1 and
547 * (*this)[0] == @a c.
550 operator=(_CharT __c
)
552 this->assign(1, __c
);
556 #ifdef __GXX_EXPERIMENTAL_CXX0X__
558 * @brief Move assign the value of @a str to this string.
559 * @param str Source string.
561 * The contents of @a str are moved into this string (without copying).
562 * @a str is a valid, but unspecified string.
565 operator=(basic_string
&& __str
)
573 * @brief Set value to string constructed from initializer list.
574 * @param l std::initializer_list.
577 operator=(initializer_list
<_CharT
> __l
)
579 this->assign(__l
.begin(), __l
.size());
582 #endif // __GXX_EXPERIMENTAL_CXX0X__
586 * Returns a read/write iterator that points to the first character in
587 * the %string. Unshares the string.
593 return iterator(_M_data());
597 * Returns a read-only (constant) iterator that points to the first
598 * character in the %string.
602 { return const_iterator(_M_data()); }
605 * Returns a read/write iterator that points one past the last
606 * character in the %string. Unshares the string.
612 return iterator(_M_data() + this->size());
616 * Returns a read-only (constant) iterator that points one past the
617 * last character in the %string.
621 { return const_iterator(_M_data() + this->size()); }
624 * Returns a read/write reverse iterator that points to the last
625 * character in the %string. Iteration is done in reverse element
626 * order. Unshares the string.
630 { return reverse_iterator(this->end()); }
633 * Returns a read-only (constant) reverse iterator that points
634 * to the last character in the %string. Iteration is done in
635 * reverse element order.
637 const_reverse_iterator
639 { return const_reverse_iterator(this->end()); }
642 * Returns a read/write reverse iterator that points to one before the
643 * first character in the %string. Iteration is done in reverse
644 * element order. Unshares the string.
648 { return reverse_iterator(this->begin()); }
651 * Returns a read-only (constant) reverse iterator that points
652 * to one before the first character in the %string. Iteration
653 * is done in reverse element order.
655 const_reverse_iterator
657 { return const_reverse_iterator(this->begin()); }
659 #ifdef __GXX_EXPERIMENTAL_CXX0X__
661 * Returns a read-only (constant) iterator that points to the first
662 * character in the %string.
666 { return const_iterator(this->_M_data()); }
669 * Returns a read-only (constant) iterator that points one past the
670 * last character in the %string.
674 { return const_iterator(this->_M_data() + this->size()); }
677 * Returns a read-only (constant) reverse iterator that points
678 * to the last character in the %string. Iteration is done in
679 * reverse element order.
681 const_reverse_iterator
683 { return const_reverse_iterator(this->end()); }
686 * Returns a read-only (constant) reverse iterator that points
687 * to one before the first character in the %string. Iteration
688 * is done in reverse element order.
690 const_reverse_iterator
692 { return const_reverse_iterator(this->begin()); }
697 /// Returns the number of characters in the string, not including any
698 /// null-termination.
701 { return _M_rep()->_M_length
; }
703 /// Returns the number of characters in the string, not including any
704 /// null-termination.
707 { return _M_rep()->_M_length
; }
709 /// Returns the size() of the largest possible %string.
712 { return _Rep::_S_max_size
; }
715 * @brief Resizes the %string to the specified number of characters.
716 * @param n Number of characters the %string should contain.
717 * @param c Character to fill any new elements.
719 * This function will %resize the %string to the specified
720 * number of characters. If the number is smaller than the
721 * %string's current size the %string is truncated, otherwise
722 * the %string is extended and new elements are set to @a c.
725 resize(size_type __n
, _CharT __c
);
728 * @brief Resizes the %string to the specified number of characters.
729 * @param n Number of characters the %string should contain.
731 * This function will resize the %string to the specified length. If
732 * the new size is smaller than the %string's current size the %string
733 * is truncated, otherwise the %string is extended and new characters
734 * are default-constructed. For basic types such as char, this means
738 resize(size_type __n
)
739 { this->resize(__n
, _CharT()); }
741 #ifdef __GXX_EXPERIMENTAL_CXX0X__
742 /// A non-binding request to reduce capacity() to size().
754 * Returns the total number of characters that the %string can hold
755 * before needing to allocate more memory.
759 { return _M_rep()->_M_capacity
; }
762 * @brief Attempt to preallocate enough memory for specified number of
764 * @param res_arg Number of characters required.
765 * @throw std::length_error If @a res_arg exceeds @c max_size().
767 * This function attempts to reserve enough memory for the
768 * %string to hold the specified number of characters. If the
769 * number requested is more than max_size(), length_error is
772 * The advantage of this function is that if optimal code is a
773 * necessity and the user can determine the string length that will be
774 * required, the user can reserve the memory in %advance, and thus
775 * prevent a possible reallocation of memory and copying of %string
779 reserve(size_type __res_arg
= 0);
782 * Erases the string, making it empty.
786 { _M_mutate(0, this->size(), 0); }
789 * Returns true if the %string is empty. Equivalent to *this == "".
793 { return this->size() == 0; }
797 * @brief Subscript access to the data contained in the %string.
798 * @param pos The index of the character to access.
799 * @return Read-only (constant) reference to the character.
801 * This operator allows for easy, array-style, data access.
802 * Note that data access with this operator is unchecked and
803 * out_of_range lookups are not defined. (For checked lookups
807 operator[] (size_type __pos
) const
809 _GLIBCXX_DEBUG_ASSERT(__pos
<= size());
810 return _M_data()[__pos
];
814 * @brief Subscript access to the data contained in the %string.
815 * @param pos The index of the character to access.
816 * @return Read/write reference to the character.
818 * This operator allows for easy, array-style, data access.
819 * Note that data access with this operator is unchecked and
820 * out_of_range lookups are not defined. (For checked lookups
821 * see at().) Unshares the string.
824 operator[](size_type __pos
)
826 // allow pos == size() as v3 extension:
827 _GLIBCXX_DEBUG_ASSERT(__pos
<= size());
828 // but be strict in pedantic mode:
829 _GLIBCXX_DEBUG_PEDASSERT(__pos
< size());
831 return _M_data()[__pos
];
835 * @brief Provides access to the data contained in the %string.
836 * @param n The index of the character to access.
837 * @return Read-only (const) reference to the character.
838 * @throw std::out_of_range If @a n is an invalid index.
840 * This function provides for safer data access. The parameter is
841 * first checked that it is in the range of the string. The function
842 * throws out_of_range if the check fails.
845 at(size_type __n
) const
847 if (__n
>= this->size())
848 __throw_out_of_range(__N("basic_string::at"));
849 return _M_data()[__n
];
853 * @brief Provides access to the data contained in the %string.
854 * @param n The index of the character to access.
855 * @return Read/write reference to the character.
856 * @throw std::out_of_range If @a n is an invalid index.
858 * This function provides for safer data access. The parameter is
859 * first checked that it is in the range of the string. The function
860 * throws out_of_range if the check fails. Success results in
861 * unsharing the string.
867 __throw_out_of_range(__N("basic_string::at"));
869 return _M_data()[__n
];
874 * @brief Append a string to this string.
875 * @param str The string to append.
876 * @return Reference to this string.
879 operator+=(const basic_string
& __str
)
880 { return this->append(__str
); }
883 * @brief Append a C string.
884 * @param s The C string to append.
885 * @return Reference to this string.
888 operator+=(const _CharT
* __s
)
889 { return this->append(__s
); }
892 * @brief Append a character.
893 * @param c The character to append.
894 * @return Reference to this string.
897 operator+=(_CharT __c
)
899 this->push_back(__c
);
903 #ifdef __GXX_EXPERIMENTAL_CXX0X__
905 * @brief Append an initializer_list of characters.
906 * @param l The initializer_list of characters to be appended.
907 * @return Reference to this string.
910 operator+=(initializer_list
<_CharT
> __l
)
911 { return this->append(__l
.begin(), __l
.size()); }
912 #endif // __GXX_EXPERIMENTAL_CXX0X__
915 * @brief Append a string to this string.
916 * @param str The string to append.
917 * @return Reference to this string.
920 append(const basic_string
& __str
);
923 * @brief Append a substring.
924 * @param str The string to append.
925 * @param pos Index of the first character of str to append.
926 * @param n The number of characters to append.
927 * @return Reference to this string.
928 * @throw std::out_of_range if @a pos is not a valid index.
930 * This function appends @a n characters from @a str starting at @a pos
931 * to this string. If @a n is is larger than the number of available
932 * characters in @a str, the remainder of @a str is appended.
935 append(const basic_string
& __str
, size_type __pos
, size_type __n
);
938 * @brief Append a C substring.
939 * @param s The C string to append.
940 * @param n The number of characters to append.
941 * @return Reference to this string.
944 append(const _CharT
* __s
, size_type __n
);
947 * @brief Append a C string.
948 * @param s The C string to append.
949 * @return Reference to this string.
952 append(const _CharT
* __s
)
954 __glibcxx_requires_string(__s
);
955 return this->append(__s
, traits_type::length(__s
));
959 * @brief Append multiple characters.
960 * @param n The number of characters to append.
961 * @param c The character to use.
962 * @return Reference to this string.
964 * Appends n copies of c to this string.
967 append(size_type __n
, _CharT __c
);
969 #ifdef __GXX_EXPERIMENTAL_CXX0X__
971 * @brief Append an initializer_list of characters.
972 * @param l The initializer_list of characters to append.
973 * @return Reference to this string.
976 append(initializer_list
<_CharT
> __l
)
977 { return this->append(__l
.begin(), __l
.size()); }
978 #endif // __GXX_EXPERIMENTAL_CXX0X__
981 * @brief Append a range of characters.
982 * @param first Iterator referencing the first character to append.
983 * @param last Iterator marking the end of the range.
984 * @return Reference to this string.
986 * Appends characters in the range [first,last) to this string.
988 template<class _InputIterator
>
990 append(_InputIterator __first
, _InputIterator __last
)
991 { return this->replace(_M_iend(), _M_iend(), __first
, __last
); }
994 * @brief Append a single character.
995 * @param c Character to append.
998 push_back(_CharT __c
)
1000 const size_type __len
= 1 + this->size();
1001 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
1002 this->reserve(__len
);
1003 traits_type::assign(_M_data()[this->size()], __c
);
1004 _M_rep()->_M_set_length_and_sharable(__len
);
1008 * @brief Set value to contents of another string.
1009 * @param str Source string to use.
1010 * @return Reference to this string.
1013 assign(const basic_string
& __str
);
1015 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1017 * @brief Set value to contents of another string.
1018 * @param str Source string to use.
1019 * @return Reference to this string.
1021 * This function sets this string to the exact contents of @a str.
1022 * @a str is a valid, but unspecified string.
1025 assign(basic_string
&& __str
)
1030 #endif // __GXX_EXPERIMENTAL_CXX0X__
1033 * @brief Set value to a substring of a string.
1034 * @param str The string to use.
1035 * @param pos Index of the first character of str.
1036 * @param n Number of characters to use.
1037 * @return Reference to this string.
1038 * @throw std::out_of_range if @a pos is not a valid index.
1040 * This function sets this string to the substring of @a str consisting
1041 * of @a n characters at @a pos. If @a n is is larger than the number
1042 * of available characters in @a str, the remainder of @a str is used.
1045 assign(const basic_string
& __str
, size_type __pos
, size_type __n
)
1046 { return this->assign(__str
._M_data()
1047 + __str
._M_check(__pos
, "basic_string::assign"),
1048 __str
._M_limit(__pos
, __n
)); }
1051 * @brief Set value to a C substring.
1052 * @param s The C string to use.
1053 * @param n Number of characters to use.
1054 * @return Reference to this string.
1056 * This function sets the value of this string to the first @a n
1057 * characters of @a s. If @a n is is larger than the number of
1058 * available characters in @a s, the remainder of @a s is used.
1061 assign(const _CharT
* __s
, size_type __n
);
1064 * @brief Set value to contents of a C string.
1065 * @param s The C string to use.
1066 * @return Reference to this string.
1068 * This function sets the value of this string to the value of @a s.
1069 * The data is copied, so there is no dependence on @a s once the
1073 assign(const _CharT
* __s
)
1075 __glibcxx_requires_string(__s
);
1076 return this->assign(__s
, traits_type::length(__s
));
1080 * @brief Set value to multiple characters.
1081 * @param n Length of the resulting string.
1082 * @param c The character to use.
1083 * @return Reference to this string.
1085 * This function sets the value of this string to @a n copies of
1089 assign(size_type __n
, _CharT __c
)
1090 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
1093 * @brief Set value to a range of characters.
1094 * @param first Iterator referencing the first character to append.
1095 * @param last Iterator marking the end of the range.
1096 * @return Reference to this string.
1098 * Sets value of string to characters in the range [first,last).
1100 template<class _InputIterator
>
1102 assign(_InputIterator __first
, _InputIterator __last
)
1103 { return this->replace(_M_ibegin(), _M_iend(), __first
, __last
); }
1105 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1107 * @brief Set value to an initializer_list of characters.
1108 * @param l The initializer_list of characters to assign.
1109 * @return Reference to this string.
1112 assign(initializer_list
<_CharT
> __l
)
1113 { return this->assign(__l
.begin(), __l
.size()); }
1114 #endif // __GXX_EXPERIMENTAL_CXX0X__
1117 * @brief Insert multiple characters.
1118 * @param p Iterator referencing location in string to insert at.
1119 * @param n Number of characters to insert
1120 * @param c The character to insert.
1121 * @throw std::length_error If new length exceeds @c max_size().
1123 * Inserts @a n copies of character @a c starting at the position
1124 * referenced by iterator @a p. If adding characters causes the length
1125 * to exceed max_size(), length_error is thrown. The value of the
1126 * string doesn't change if an error is thrown.
1129 insert(iterator __p
, size_type __n
, _CharT __c
)
1130 { this->replace(__p
, __p
, __n
, __c
); }
1133 * @brief Insert a range of characters.
1134 * @param p Iterator referencing location in string to insert at.
1135 * @param beg Start of range.
1136 * @param end End of range.
1137 * @throw std::length_error If new length exceeds @c max_size().
1139 * Inserts characters in range [beg,end). If adding characters causes
1140 * the length to exceed max_size(), length_error is thrown. The value
1141 * of the string doesn't change if an error is thrown.
1143 template<class _InputIterator
>
1145 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1146 { this->replace(__p
, __p
, __beg
, __end
); }
1148 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1150 * @brief Insert an initializer_list of characters.
1151 * @param p Iterator referencing location in string to insert at.
1152 * @param l The initializer_list of characters to insert.
1153 * @throw std::length_error If new length exceeds @c max_size().
1156 insert(iterator __p
, initializer_list
<_CharT
> __l
)
1158 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
1159 this->insert(__p
- _M_ibegin(), __l
.begin(), __l
.size());
1161 #endif // __GXX_EXPERIMENTAL_CXX0X__
1164 * @brief Insert value of a string.
1165 * @param pos1 Iterator referencing location in string to insert at.
1166 * @param str The string to insert.
1167 * @return Reference to this string.
1168 * @throw std::length_error If new length exceeds @c max_size().
1170 * Inserts value of @a str starting at @a pos1. If adding characters
1171 * causes the length to exceed max_size(), length_error is thrown. The
1172 * value of the string doesn't change if an error is thrown.
1175 insert(size_type __pos1
, const basic_string
& __str
)
1176 { return this->insert(__pos1
, __str
, size_type(0), __str
.size()); }
1179 * @brief Insert a substring.
1180 * @param pos1 Iterator referencing location in string to insert at.
1181 * @param str The string to insert.
1182 * @param pos2 Start of characters in str to insert.
1183 * @param n Number of characters to insert.
1184 * @return Reference to this string.
1185 * @throw std::length_error If new length exceeds @c max_size().
1186 * @throw std::out_of_range If @a pos1 > size() or
1187 * @a pos2 > @a str.size().
1189 * Starting at @a pos1, insert @a n character of @a str beginning with
1190 * @a pos2. If adding characters causes the length to exceed
1191 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
1192 * this string or @a pos2 is beyond the end of @a str, out_of_range is
1193 * thrown. The value of the string doesn't change if an error is
1197 insert(size_type __pos1
, const basic_string
& __str
,
1198 size_type __pos2
, size_type __n
)
1199 { return this->insert(__pos1
, __str
._M_data()
1200 + __str
._M_check(__pos2
, "basic_string::insert"),
1201 __str
._M_limit(__pos2
, __n
)); }
1204 * @brief Insert a C substring.
1205 * @param pos Iterator referencing location in string to insert at.
1206 * @param s The C string to insert.
1207 * @param n The number of characters to insert.
1208 * @return Reference to this string.
1209 * @throw std::length_error If new length exceeds @c max_size().
1210 * @throw std::out_of_range If @a pos is beyond the end of this
1213 * Inserts the first @a n characters of @a s starting at @a pos. If
1214 * adding characters causes the length to exceed max_size(),
1215 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1216 * thrown. The value of the string doesn't change if an error is
1220 insert(size_type __pos
, const _CharT
* __s
, size_type __n
);
1223 * @brief Insert a C string.
1224 * @param pos Iterator referencing location in string to insert at.
1225 * @param s The C string to insert.
1226 * @return Reference to this string.
1227 * @throw std::length_error If new length exceeds @c max_size().
1228 * @throw std::out_of_range If @a pos is beyond the end of this
1231 * Inserts the first @a n characters of @a s starting at @a pos. If
1232 * adding characters causes the length to exceed max_size(),
1233 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1234 * thrown. The value of the string doesn't change if an error is
1238 insert(size_type __pos
, const _CharT
* __s
)
1240 __glibcxx_requires_string(__s
);
1241 return this->insert(__pos
, __s
, traits_type::length(__s
));
1245 * @brief Insert multiple characters.
1246 * @param pos Index in string to insert at.
1247 * @param n Number of characters to insert
1248 * @param c The character to insert.
1249 * @return Reference to this string.
1250 * @throw std::length_error If new length exceeds @c max_size().
1251 * @throw std::out_of_range If @a pos is beyond the end of this
1254 * Inserts @a n copies of character @a c starting at index @a pos. If
1255 * adding characters causes the length to exceed max_size(),
1256 * length_error is thrown. If @a pos > length(), out_of_range is
1257 * thrown. The value of the string doesn't change if an error is
1261 insert(size_type __pos
, size_type __n
, _CharT __c
)
1262 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
1263 size_type(0), __n
, __c
); }
1266 * @brief Insert one character.
1267 * @param p Iterator referencing position in string to insert at.
1268 * @param c The character to insert.
1269 * @return Iterator referencing newly inserted char.
1270 * @throw std::length_error If new length exceeds @c max_size().
1272 * Inserts character @a c at position referenced by @a p. If adding
1273 * character causes the length to exceed max_size(), length_error is
1274 * thrown. If @a p is beyond end of string, out_of_range is thrown.
1275 * The value of the string doesn't change if an error is thrown.
1278 insert(iterator __p
, _CharT __c
)
1280 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
1281 const size_type __pos
= __p
- _M_ibegin();
1282 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
1283 _M_rep()->_M_set_leaked();
1284 return iterator(_M_data() + __pos
);
1288 * @brief Remove characters.
1289 * @param pos Index of first character to remove (default 0).
1290 * @param n Number of characters to remove (default remainder).
1291 * @return Reference to this string.
1292 * @throw std::out_of_range If @a pos is beyond the end of this
1295 * Removes @a n characters from this string starting at @a pos. The
1296 * length of the string is reduced by @a n. If there are < @a n
1297 * characters to remove, the remainder of the string is truncated. If
1298 * @a p is beyond end of string, out_of_range is thrown. The value of
1299 * the string doesn't change if an error is thrown.
1302 erase(size_type __pos
= 0, size_type __n
= npos
)
1304 _M_mutate(_M_check(__pos
, "basic_string::erase"),
1305 _M_limit(__pos
, __n
), size_type(0));
1310 * @brief Remove one character.
1311 * @param position Iterator referencing the character to remove.
1312 * @return iterator referencing same location after removal.
1314 * Removes the character at @a position from this string. The value
1315 * of the string doesn't change if an error is thrown.
1318 erase(iterator __position
)
1320 _GLIBCXX_DEBUG_PEDASSERT(__position
>= _M_ibegin()
1321 && __position
< _M_iend());
1322 const size_type __pos
= __position
- _M_ibegin();
1323 _M_mutate(__pos
, size_type(1), size_type(0));
1324 _M_rep()->_M_set_leaked();
1325 return iterator(_M_data() + __pos
);
1329 * @brief Remove a range of characters.
1330 * @param first Iterator referencing the first character to remove.
1331 * @param last Iterator referencing the end of the range.
1332 * @return Iterator referencing location of first after removal.
1334 * Removes the characters in the range [first,last) from this string.
1335 * The value of the string doesn't change if an error is thrown.
1338 erase(iterator __first
, iterator __last
);
1341 * @brief Replace characters with value from another string.
1342 * @param pos Index of first character to replace.
1343 * @param n Number of characters to be replaced.
1344 * @param str String to insert.
1345 * @return Reference to this string.
1346 * @throw std::out_of_range If @a pos is beyond the end of this
1348 * @throw std::length_error If new length exceeds @c max_size().
1350 * Removes the characters in the range [pos,pos+n) from this string.
1351 * In place, the value of @a str is inserted. If @a pos is beyond end
1352 * of string, out_of_range is thrown. If the length of the result
1353 * exceeds max_size(), length_error is thrown. The value of the string
1354 * doesn't change if an error is thrown.
1357 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
1358 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
1361 * @brief Replace characters with value from another string.
1362 * @param pos1 Index of first character to replace.
1363 * @param n1 Number of characters to be replaced.
1364 * @param str String to insert.
1365 * @param pos2 Index of first character of str to use.
1366 * @param n2 Number of characters from str to use.
1367 * @return Reference to this string.
1368 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1370 * @throw std::length_error If new length exceeds @c max_size().
1372 * Removes the characters in the range [pos1,pos1 + n) from this
1373 * string. In place, the value of @a str is inserted. If @a pos is
1374 * beyond end of string, out_of_range is thrown. If the length of the
1375 * result exceeds max_size(), length_error is thrown. The value of the
1376 * string doesn't change if an error is thrown.
1379 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
1380 size_type __pos2
, size_type __n2
)
1381 { return this->replace(__pos1
, __n1
, __str
._M_data()
1382 + __str
._M_check(__pos2
, "basic_string::replace"),
1383 __str
._M_limit(__pos2
, __n2
)); }
1386 * @brief Replace characters with value of a C substring.
1387 * @param pos Index of first character to replace.
1388 * @param n1 Number of characters to be replaced.
1389 * @param s C string to insert.
1390 * @param n2 Number of characters from @a s to use.
1391 * @return Reference to this string.
1392 * @throw std::out_of_range If @a pos1 > size().
1393 * @throw std::length_error If new length exceeds @c max_size().
1395 * Removes the characters in the range [pos,pos + n1) from this string.
1396 * In place, the first @a n2 characters of @a s are inserted, or all
1397 * of @a s if @a n2 is too large. If @a pos is beyond end of string,
1398 * out_of_range is thrown. If the length of result exceeds max_size(),
1399 * length_error is thrown. The value of the string doesn't change if
1400 * an error is thrown.
1403 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
1407 * @brief Replace characters with value of a C string.
1408 * @param pos Index of first character to replace.
1409 * @param n1 Number of characters to be replaced.
1410 * @param s C string to insert.
1411 * @return Reference to this string.
1412 * @throw std::out_of_range If @a pos > size().
1413 * @throw std::length_error If new length exceeds @c max_size().
1415 * Removes the characters in the range [pos,pos + n1) from this string.
1416 * In place, the first @a n characters of @a s are inserted. If @a
1417 * pos is beyond end of string, out_of_range is thrown. If the length
1418 * of result exceeds max_size(), length_error is thrown. The value of
1419 * the string doesn't change if an error is thrown.
1422 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
1424 __glibcxx_requires_string(__s
);
1425 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
1429 * @brief Replace characters with multiple characters.
1430 * @param pos Index of first character to replace.
1431 * @param n1 Number of characters to be replaced.
1432 * @param n2 Number of characters to insert.
1433 * @param c Character to insert.
1434 * @return Reference to this string.
1435 * @throw std::out_of_range If @a pos > size().
1436 * @throw std::length_error If new length exceeds @c max_size().
1438 * Removes the characters in the range [pos,pos + n1) from this string.
1439 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1440 * end of string, out_of_range is thrown. If the length of result
1441 * exceeds max_size(), length_error is thrown. The value of the string
1442 * doesn't change if an error is thrown.
1445 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
1446 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
1447 _M_limit(__pos
, __n1
), __n2
, __c
); }
1450 * @brief Replace range of characters with string.
1451 * @param i1 Iterator referencing start of range to replace.
1452 * @param i2 Iterator referencing end of range to replace.
1453 * @param str String value to insert.
1454 * @return Reference to this string.
1455 * @throw std::length_error If new length exceeds @c max_size().
1457 * Removes the characters in the range [i1,i2). In place, the value of
1458 * @a str is inserted. If the length of result exceeds max_size(),
1459 * length_error is thrown. The value of the string doesn't change if
1460 * an error is thrown.
1463 replace(iterator __i1
, iterator __i2
, const basic_string
& __str
)
1464 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
1467 * @brief Replace range of characters with C substring.
1468 * @param i1 Iterator referencing start of range to replace.
1469 * @param i2 Iterator referencing end of range to replace.
1470 * @param s C string value to insert.
1471 * @param n Number of characters from s to insert.
1472 * @return Reference to this string.
1473 * @throw std::length_error If new length exceeds @c max_size().
1475 * Removes the characters in the range [i1,i2). In place, the first @a
1476 * n characters of @a s are inserted. If the length of result exceeds
1477 * max_size(), length_error is thrown. The value of the string doesn't
1478 * change if an error is thrown.
1481 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
, size_type __n
)
1483 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
1484 && __i2
<= _M_iend());
1485 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
, __s
, __n
);
1489 * @brief Replace range of characters with C string.
1490 * @param i1 Iterator referencing start of range to replace.
1491 * @param i2 Iterator referencing end of range to replace.
1492 * @param s C string value to insert.
1493 * @return Reference to this string.
1494 * @throw std::length_error If new length exceeds @c max_size().
1496 * Removes the characters in the range [i1,i2). In place, the
1497 * characters of @a s are inserted. If the length of result exceeds
1498 * max_size(), length_error is thrown. The value of the string doesn't
1499 * change if an error is thrown.
1502 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
)
1504 __glibcxx_requires_string(__s
);
1505 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
1509 * @brief Replace range of characters with multiple characters
1510 * @param i1 Iterator referencing start of range to replace.
1511 * @param i2 Iterator referencing end of range to replace.
1512 * @param n Number of characters to insert.
1513 * @param c Character to insert.
1514 * @return Reference to this string.
1515 * @throw std::length_error If new length exceeds @c max_size().
1517 * Removes the characters in the range [i1,i2). In place, @a n copies
1518 * of @a c are inserted. If the length of result exceeds max_size(),
1519 * length_error is thrown. The value of the string doesn't change if
1520 * an error is thrown.
1523 replace(iterator __i1
, iterator __i2
, size_type __n
, _CharT __c
)
1525 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
1526 && __i2
<= _M_iend());
1527 return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __c
);
1531 * @brief Replace range of characters with range.
1532 * @param i1 Iterator referencing start of range to replace.
1533 * @param i2 Iterator referencing end of range to replace.
1534 * @param k1 Iterator referencing start of range to insert.
1535 * @param k2 Iterator referencing end of range to insert.
1536 * @return Reference to this string.
1537 * @throw std::length_error If new length exceeds @c max_size().
1539 * Removes the characters in the range [i1,i2). In place, characters
1540 * in the range [k1,k2) are inserted. If the length of result exceeds
1541 * max_size(), length_error is thrown. The value of the string doesn't
1542 * change if an error is thrown.
1544 template<class _InputIterator
>
1546 replace(iterator __i1
, iterator __i2
,
1547 _InputIterator __k1
, _InputIterator __k2
)
1549 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
1550 && __i2
<= _M_iend());
1551 __glibcxx_requires_valid_range(__k1
, __k2
);
1552 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1553 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
1556 // Specializations for the common case of pointer and iterator:
1557 // useful to avoid the overhead of temporary buffering in _M_replace.
1559 replace(iterator __i1
, iterator __i2
, _CharT
* __k1
, _CharT
* __k2
)
1561 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
1562 && __i2
<= _M_iend());
1563 __glibcxx_requires_valid_range(__k1
, __k2
);
1564 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
1569 replace(iterator __i1
, iterator __i2
,
1570 const _CharT
* __k1
, const _CharT
* __k2
)
1572 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
1573 && __i2
<= _M_iend());
1574 __glibcxx_requires_valid_range(__k1
, __k2
);
1575 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
1580 replace(iterator __i1
, iterator __i2
, iterator __k1
, iterator __k2
)
1582 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
1583 && __i2
<= _M_iend());
1584 __glibcxx_requires_valid_range(__k1
, __k2
);
1585 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
1586 __k1
.base(), __k2
- __k1
);
1590 replace(iterator __i1
, iterator __i2
,
1591 const_iterator __k1
, const_iterator __k2
)
1593 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
1594 && __i2
<= _M_iend());
1595 __glibcxx_requires_valid_range(__k1
, __k2
);
1596 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
1597 __k1
.base(), __k2
- __k1
);
1600 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1602 * @brief Replace range of characters with initializer_list.
1603 * @param i1 Iterator referencing start of range to replace.
1604 * @param i2 Iterator referencing end of range to replace.
1605 * @param l The initializer_list of characters to insert.
1606 * @return Reference to this string.
1607 * @throw std::length_error If new length exceeds @c max_size().
1609 * Removes the characters in the range [i1,i2). In place, characters
1610 * in the range [k1,k2) are inserted. If the length of result exceeds
1611 * max_size(), length_error is thrown. The value of the string doesn't
1612 * change if an error is thrown.
1614 basic_string
& replace(iterator __i1
, iterator __i2
,
1615 initializer_list
<_CharT
> __l
)
1616 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.end()); }
1617 #endif // __GXX_EXPERIMENTAL_CXX0X__
1620 template<class _Integer
>
1622 _M_replace_dispatch(iterator __i1
, iterator __i2
, _Integer __n
,
1623 _Integer __val
, __true_type
)
1624 { return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __val
); }
1626 template<class _InputIterator
>
1628 _M_replace_dispatch(iterator __i1
, iterator __i2
, _InputIterator __k1
,
1629 _InputIterator __k2
, __false_type
);
1632 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
1636 _M_replace_safe(size_type __pos1
, size_type __n1
, const _CharT
* __s
,
1639 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1640 // requires special behaviour if _InIter is an integral type
1641 template<class _InIterator
>
1643 _S_construct_aux(_InIterator __beg
, _InIterator __end
,
1644 const _Alloc
& __a
, __false_type
)
1646 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
1647 return _S_construct(__beg
, __end
, __a
, _Tag());
1650 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1651 // 438. Ambiguity in the "do the right thing" clause
1652 template<class _Integer
>
1654 _S_construct_aux(_Integer __beg
, _Integer __end
,
1655 const _Alloc
& __a
, __true_type
)
1656 { return _S_construct_aux_2(static_cast<size_type
>(__beg
),
1660 _S_construct_aux_2(size_type __req
, _CharT __c
, const _Alloc
& __a
)
1661 { return _S_construct(__req
, __c
, __a
); }
1663 template<class _InIterator
>
1665 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
)
1667 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
1668 return _S_construct_aux(__beg
, __end
, __a
, _Integral());
1671 // For Input Iterators, used in istreambuf_iterators, etc.
1672 template<class _InIterator
>
1674 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
1675 input_iterator_tag
);
1677 // For forward_iterators up to random_access_iterators, used for
1678 // string::iterator, _CharT*, etc.
1679 template<class _FwdIterator
>
1681 _S_construct(_FwdIterator __beg
, _FwdIterator __end
, const _Alloc
& __a
,
1682 forward_iterator_tag
);
1685 _S_construct(size_type __req
, _CharT __c
, const _Alloc
& __a
);
1690 * @brief Copy substring into C string.
1691 * @param s C string to copy value into.
1692 * @param n Number of characters to copy.
1693 * @param pos Index of first character to copy.
1694 * @return Number of characters actually copied
1695 * @throw std::out_of_range If pos > size().
1697 * Copies up to @a n characters starting at @a pos into the C string @a
1698 * s. If @a pos is greater than size(), out_of_range is thrown.
1701 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
1704 * @brief Swap contents with another string.
1705 * @param s String to swap with.
1707 * Exchanges the contents of this string with that of @a s in constant
1711 swap(basic_string
& __s
);
1713 // String operations:
1715 * @brief Return const pointer to null-terminated contents.
1717 * This is a handle to internal data. Do not modify or dire things may
1722 { return _M_data(); }
1725 * @brief Return const pointer to contents.
1727 * This is a handle to internal data. Do not modify or dire things may
1732 { return _M_data(); }
1735 * @brief Return copy of allocator used to construct this string.
1738 get_allocator() const
1739 { return _M_dataplus
; }
1742 * @brief Find position of a C substring.
1743 * @param s C string to locate.
1744 * @param pos Index of character to search from.
1745 * @param n Number of characters from @a s to search for.
1746 * @return Index of start of first occurrence.
1748 * Starting from @a pos, searches forward for the first @a n characters
1749 * in @a s within this string. If found, returns the index where it
1750 * begins. If not found, returns npos.
1753 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
1756 * @brief Find position of a string.
1757 * @param str String to locate.
1758 * @param pos Index of character to search from (default 0).
1759 * @return Index of start of first occurrence.
1761 * Starting from @a pos, searches forward for value of @a str within
1762 * this string. If found, returns the index where it begins. If not
1763 * found, returns npos.
1766 find(const basic_string
& __str
, size_type __pos
= 0) const
1767 { return this->find(__str
.data(), __pos
, __str
.size()); }
1770 * @brief Find position of a C string.
1771 * @param s C string to locate.
1772 * @param pos Index of character to search from (default 0).
1773 * @return Index of start of first occurrence.
1775 * Starting from @a pos, searches forward for the value of @a s within
1776 * this string. If found, returns the index where it begins. If not
1777 * found, returns npos.
1780 find(const _CharT
* __s
, size_type __pos
= 0) const
1782 __glibcxx_requires_string(__s
);
1783 return this->find(__s
, __pos
, traits_type::length(__s
));
1787 * @brief Find position of a character.
1788 * @param c Character to locate.
1789 * @param pos Index of character to search from (default 0).
1790 * @return Index of first occurrence.
1792 * Starting from @a pos, searches forward for @a c within this string.
1793 * If found, returns the index where it was found. If not found,
1797 find(_CharT __c
, size_type __pos
= 0) const;
1800 * @brief Find last position of a string.
1801 * @param str String to locate.
1802 * @param pos Index of character to search back from (default end).
1803 * @return Index of start of last occurrence.
1805 * Starting from @a pos, searches backward for value of @a str within
1806 * this string. If found, returns the index where it begins. If not
1807 * found, returns npos.
1810 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
1811 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
1814 * @brief Find last position of a C substring.
1815 * @param s C string to locate.
1816 * @param pos Index of character to search back from.
1817 * @param n Number of characters from s to search for.
1818 * @return Index of start of last occurrence.
1820 * Starting from @a pos, searches backward for the first @a n
1821 * characters in @a s within this string. If found, returns the index
1822 * where it begins. If not found, returns npos.
1825 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
1828 * @brief Find last position of a C string.
1829 * @param s C string to locate.
1830 * @param pos Index of character to start search at (default end).
1831 * @return Index of start of last occurrence.
1833 * Starting from @a pos, searches backward for the value of @a s within
1834 * this string. If found, returns the index where it begins. If not
1835 * found, returns npos.
1838 rfind(const _CharT
* __s
, size_type __pos
= npos
) const
1840 __glibcxx_requires_string(__s
);
1841 return this->rfind(__s
, __pos
, traits_type::length(__s
));
1845 * @brief Find last position of a character.
1846 * @param c Character to locate.
1847 * @param pos Index of character to search back from (default end).
1848 * @return Index of last occurrence.
1850 * Starting from @a pos, searches backward for @a c within this string.
1851 * If found, returns the index where it was found. If not found,
1855 rfind(_CharT __c
, size_type __pos
= npos
) const;
1858 * @brief Find position of a character of string.
1859 * @param str String containing characters to locate.
1860 * @param pos Index of character to search from (default 0).
1861 * @return Index of first occurrence.
1863 * Starting from @a pos, searches forward for one of the characters of
1864 * @a str within this string. If found, returns the index where it was
1865 * found. If not found, returns npos.
1868 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
1869 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
1872 * @brief Find position of a character of C substring.
1873 * @param s String containing characters to locate.
1874 * @param pos Index of character to search from.
1875 * @param n Number of characters from s to search for.
1876 * @return Index of first occurrence.
1878 * Starting from @a pos, searches forward for one of the first @a n
1879 * characters of @a s within this string. If found, returns the index
1880 * where it was found. If not found, returns npos.
1883 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
1886 * @brief Find position of a character of C string.
1887 * @param s String containing characters to locate.
1888 * @param pos Index of character to search from (default 0).
1889 * @return Index of first occurrence.
1891 * Starting from @a pos, searches forward for one of the characters of
1892 * @a s within this string. If found, returns the index where it was
1893 * found. If not found, returns npos.
1896 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
1898 __glibcxx_requires_string(__s
);
1899 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
1903 * @brief Find position of a character.
1904 * @param c Character to locate.
1905 * @param pos Index of character to search from (default 0).
1906 * @return Index of first occurrence.
1908 * Starting from @a pos, searches forward for the character @a c within
1909 * this string. If found, returns the index where it was found. If
1910 * not found, returns npos.
1912 * Note: equivalent to find(c, pos).
1915 find_first_of(_CharT __c
, size_type __pos
= 0) const
1916 { return this->find(__c
, __pos
); }
1919 * @brief Find last position of a character of string.
1920 * @param str String containing characters to locate.
1921 * @param pos Index of character to search back from (default end).
1922 * @return Index of last occurrence.
1924 * Starting from @a pos, searches backward for one of the characters of
1925 * @a str within this string. If found, returns the index where it was
1926 * found. If not found, returns npos.
1929 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
1930 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
1933 * @brief Find last position of a character of C substring.
1934 * @param s C string containing characters to locate.
1935 * @param pos Index of character to search back from.
1936 * @param n Number of characters from s to search for.
1937 * @return Index of last occurrence.
1939 * Starting from @a pos, searches backward for one of the first @a n
1940 * characters of @a s within this string. If found, returns the index
1941 * where it was found. If not found, returns npos.
1944 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
1947 * @brief Find last position of a character of C string.
1948 * @param s C string containing characters to locate.
1949 * @param pos Index of character to search back from (default end).
1950 * @return Index of last occurrence.
1952 * Starting from @a pos, searches backward for one of the characters of
1953 * @a s within this string. If found, returns the index where it was
1954 * found. If not found, returns npos.
1957 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
1959 __glibcxx_requires_string(__s
);
1960 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
1964 * @brief Find last position of a character.
1965 * @param c Character to locate.
1966 * @param pos Index of character to search back from (default end).
1967 * @return Index of last occurrence.
1969 * Starting from @a pos, searches backward for @a c within this string.
1970 * If found, returns the index where it was found. If not found,
1973 * Note: equivalent to rfind(c, pos).
1976 find_last_of(_CharT __c
, size_type __pos
= npos
) const
1977 { return this->rfind(__c
, __pos
); }
1980 * @brief Find position of a character not in string.
1981 * @param str String containing characters to avoid.
1982 * @param pos Index of character to search from (default 0).
1983 * @return Index of first occurrence.
1985 * Starting from @a pos, searches forward for a character not contained
1986 * in @a str within this string. If found, returns the index where it
1987 * was found. If not found, returns npos.
1990 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
1991 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
1994 * @brief Find position of a character not in C substring.
1995 * @param s C string containing characters to avoid.
1996 * @param pos Index of character to search from.
1997 * @param n Number of characters from s to consider.
1998 * @return Index of first occurrence.
2000 * Starting from @a pos, searches forward for a character not contained
2001 * in the first @a n characters of @a s within this string. If found,
2002 * returns the index where it was found. If not found, returns npos.
2005 find_first_not_of(const _CharT
* __s
, size_type __pos
,
2006 size_type __n
) const;
2009 * @brief Find position of a character not in C string.
2010 * @param s C string containing characters to avoid.
2011 * @param pos Index of character to search from (default 0).
2012 * @return Index of first occurrence.
2014 * Starting from @a pos, searches forward for a character not contained
2015 * in @a s within this string. If found, returns the index where it
2016 * was found. If not found, returns npos.
2019 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
2021 __glibcxx_requires_string(__s
);
2022 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
2026 * @brief Find position of a different character.
2027 * @param c Character to avoid.
2028 * @param pos Index of character to search from (default 0).
2029 * @return Index of first occurrence.
2031 * Starting from @a pos, searches forward for a character other than @a c
2032 * within this string. If found, returns the index where it was found.
2033 * If not found, returns npos.
2036 find_first_not_of(_CharT __c
, size_type __pos
= 0) const;
2039 * @brief Find last position of a character not in string.
2040 * @param str String containing characters to avoid.
2041 * @param pos Index of character to search back from (default end).
2042 * @return Index of last occurrence.
2044 * Starting from @a pos, searches backward for a character not
2045 * contained in @a str within this string. If found, returns the index
2046 * where it was found. If not found, returns npos.
2049 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
2050 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
2053 * @brief Find last position of a character not in C substring.
2054 * @param s C string containing characters to avoid.
2055 * @param pos Index of character to search back from.
2056 * @param n Number of characters from s to consider.
2057 * @return Index of last occurrence.
2059 * Starting from @a pos, searches backward for a character not
2060 * contained in the first @a n characters of @a s within this string.
2061 * If found, returns the index where it was found. If not found,
2065 find_last_not_of(const _CharT
* __s
, size_type __pos
,
2066 size_type __n
) const;
2068 * @brief Find last position of a character not in C string.
2069 * @param s C string containing characters to avoid.
2070 * @param pos Index of character to search back from (default end).
2071 * @return Index of last occurrence.
2073 * Starting from @a pos, searches backward for a character not
2074 * contained in @a s within this string. If found, returns the index
2075 * where it was found. If not found, returns npos.
2078 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
2080 __glibcxx_requires_string(__s
);
2081 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
2085 * @brief Find last position of a different character.
2086 * @param c Character to avoid.
2087 * @param pos Index of character to search back from (default end).
2088 * @return Index of last occurrence.
2090 * Starting from @a pos, searches backward for a character other than
2091 * @a c within this string. If found, returns the index where it was
2092 * found. If not found, returns npos.
2095 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const;
2098 * @brief Get a substring.
2099 * @param pos Index of first character (default 0).
2100 * @param n Number of characters in substring (default remainder).
2101 * @return The new string.
2102 * @throw std::out_of_range If pos > size().
2104 * Construct and return a new string using the @a n characters starting
2105 * at @a pos. If the string is too short, use the remainder of the
2106 * characters. If @a pos is beyond the end of the string, out_of_range
2110 substr(size_type __pos
= 0, size_type __n
= npos
) const
2111 { return basic_string(*this,
2112 _M_check(__pos
, "basic_string::substr"), __n
); }
2115 * @brief Compare to a string.
2116 * @param str String to compare against.
2117 * @return Integer < 0, 0, or > 0.
2119 * Returns an integer < 0 if this string is ordered before @a str, 0 if
2120 * their values are equivalent, or > 0 if this string is ordered after
2121 * @a str. Determines the effective length rlen of the strings to
2122 * compare as the smallest of size() and str.size(). The function
2123 * then compares the two strings by calling traits::compare(data(),
2124 * str.data(),rlen). If the result of the comparison is nonzero returns
2125 * it, otherwise the shorter one is ordered first.
2128 compare(const basic_string
& __str
) const
2130 const size_type __size
= this->size();
2131 const size_type __osize
= __str
.size();
2132 const size_type __len
= std::min(__size
, __osize
);
2134 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
2136 __r
= _S_compare(__size
, __osize
);
2141 * @brief Compare substring to a string.
2142 * @param pos Index of first character of substring.
2143 * @param n Number of characters in substring.
2144 * @param str String to compare against.
2145 * @return Integer < 0, 0, or > 0.
2147 * Form the substring of this string from the @a n characters starting
2148 * at @a pos. Returns an integer < 0 if the substring is ordered
2149 * before @a str, 0 if their values are equivalent, or > 0 if the
2150 * substring is ordered after @a str. Determines the effective length
2151 * rlen of the strings to compare as the smallest of the length of the
2152 * substring and @a str.size(). The function then compares the two
2153 * strings by calling traits::compare(substring.data(),str.data(),rlen).
2154 * If the result of the comparison is nonzero returns it, otherwise the
2155 * shorter one is ordered first.
2158 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
2161 * @brief Compare substring to a substring.
2162 * @param pos1 Index of first character of substring.
2163 * @param n1 Number of characters in substring.
2164 * @param str String to compare against.
2165 * @param pos2 Index of first character of substring of str.
2166 * @param n2 Number of characters in substring of str.
2167 * @return Integer < 0, 0, or > 0.
2169 * Form the substring of this string from the @a n1 characters starting
2170 * at @a pos1. Form the substring of @a str from the @a n2 characters
2171 * starting at @a pos2. Returns an integer < 0 if this substring is
2172 * ordered before the substring of @a str, 0 if their values are
2173 * equivalent, or > 0 if this substring is ordered after the substring
2174 * of @a str. Determines the effective length rlen of the strings
2175 * to compare as the smallest of the lengths of the substrings. The
2176 * function then compares the two strings by calling
2177 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2178 * If the result of the comparison is nonzero returns it, otherwise the
2179 * shorter one is ordered first.
2182 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
2183 size_type __pos2
, size_type __n2
) const;
2186 * @brief Compare to a C string.
2187 * @param s C string to compare against.
2188 * @return Integer < 0, 0, or > 0.
2190 * Returns an integer < 0 if this string is ordered before @a s, 0 if
2191 * their values are equivalent, or > 0 if this string is ordered after
2192 * @a s. Determines the effective length rlen of the strings to
2193 * compare as the smallest of size() and the length of a string
2194 * constructed from @a s. The function then compares the two strings
2195 * by calling traits::compare(data(),s,rlen). If the result of the
2196 * comparison is nonzero returns it, otherwise the shorter one is
2200 compare(const _CharT
* __s
) const;
2202 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2203 // 5 String::compare specification questionable
2205 * @brief Compare substring to a C string.
2206 * @param pos Index of first character of substring.
2207 * @param n1 Number of characters in substring.
2208 * @param s C string to compare against.
2209 * @return Integer < 0, 0, or > 0.
2211 * Form the substring of this string from the @a n1 characters starting
2212 * at @a pos. Returns an integer < 0 if the substring is ordered
2213 * before @a s, 0 if their values are equivalent, or > 0 if the
2214 * substring is ordered after @a s. Determines the effective length
2215 * rlen of the strings to compare as the smallest of the length of the
2216 * substring and the length of a string constructed from @a s. The
2217 * function then compares the two string by calling
2218 * traits::compare(substring.data(),s,rlen). If the result of the
2219 * comparison is nonzero returns it, otherwise the shorter one is
2223 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
2226 * @brief Compare substring against a character array.
2227 * @param pos1 Index of first character of substring.
2228 * @param n1 Number of characters in substring.
2229 * @param s character array to compare against.
2230 * @param n2 Number of characters of s.
2231 * @return Integer < 0, 0, or > 0.
2233 * Form the substring of this string from the @a n1 characters starting
2234 * at @a pos1. Form a string from the first @a n2 characters of @a s.
2235 * Returns an integer < 0 if this substring is ordered before the string
2236 * from @a s, 0 if their values are equivalent, or > 0 if this substring
2237 * is ordered after the string from @a s. Determines the effective
2238 * length rlen of the strings to compare as the smallest of the length
2239 * of the substring and @a n2. The function then compares the two
2240 * strings by calling traits::compare(substring.data(),s,rlen). If the
2241 * result of the comparison is nonzero returns it, otherwise the shorter
2242 * one is ordered first.
2244 * NB: s must have at least n2 characters, '\\0' has no special
2248 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
2249 size_type __n2
) const;
2254 * @brief Concatenate two strings.
2255 * @param lhs First string.
2256 * @param rhs Last string.
2257 * @return New string with value of @a lhs followed by @a rhs.
2259 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2260 basic_string
<_CharT
, _Traits
, _Alloc
>
2261 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2262 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
2264 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
2265 __str
.append(__rhs
);
2270 * @brief Concatenate C string and string.
2271 * @param lhs First string.
2272 * @param rhs Last string.
2273 * @return New string with value of @a lhs followed by @a rhs.
2275 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2276 basic_string
<_CharT
,_Traits
,_Alloc
>
2277 operator+(const _CharT
* __lhs
,
2278 const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
2281 * @brief Concatenate character and string.
2282 * @param lhs First string.
2283 * @param rhs Last string.
2284 * @return New string with @a lhs followed by @a rhs.
2286 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2287 basic_string
<_CharT
,_Traits
,_Alloc
>
2288 operator+(_CharT __lhs
, const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
2291 * @brief Concatenate string and C string.
2292 * @param lhs First string.
2293 * @param rhs Last string.
2294 * @return New string with @a lhs followed by @a rhs.
2296 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2297 inline basic_string
<_CharT
, _Traits
, _Alloc
>
2298 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2299 const _CharT
* __rhs
)
2301 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
2302 __str
.append(__rhs
);
2307 * @brief Concatenate string and character.
2308 * @param lhs First string.
2309 * @param rhs Last string.
2310 * @return New string with @a lhs followed by @a rhs.
2312 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2313 inline basic_string
<_CharT
, _Traits
, _Alloc
>
2314 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
, _CharT __rhs
)
2316 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
2317 typedef typename
__string_type::size_type __size_type
;
2318 __string_type
__str(__lhs
);
2319 __str
.append(__size_type(1), __rhs
);
2325 * @brief Test equivalence of two strings.
2326 * @param lhs First string.
2327 * @param rhs Second string.
2328 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2330 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2332 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2333 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
2334 { return __lhs
.compare(__rhs
) == 0; }
2336 template<typename _CharT
>
2338 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
, bool>::__type
2339 operator==(const basic_string
<_CharT
>& __lhs
,
2340 const basic_string
<_CharT
>& __rhs
)
2341 { return (__lhs
.size() == __rhs
.size()
2342 && !std::char_traits
<_CharT
>::compare(__lhs
.data(), __rhs
.data(),
2346 * @brief Test equivalence of C string and string.
2347 * @param lhs C string.
2348 * @param rhs String.
2349 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
2351 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2353 operator==(const _CharT
* __lhs
,
2354 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
2355 { return __rhs
.compare(__lhs
) == 0; }
2358 * @brief Test equivalence of string and C string.
2359 * @param lhs String.
2360 * @param rhs C string.
2361 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2363 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2365 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2366 const _CharT
* __rhs
)
2367 { return __lhs
.compare(__rhs
) == 0; }
2371 * @brief Test difference of two strings.
2372 * @param lhs First string.
2373 * @param rhs Second string.
2374 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2376 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2378 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2379 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
2380 { return !(__lhs
== __rhs
); }
2383 * @brief Test difference of C string and string.
2384 * @param lhs C string.
2385 * @param rhs String.
2386 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2388 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2390 operator!=(const _CharT
* __lhs
,
2391 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
2392 { return !(__lhs
== __rhs
); }
2395 * @brief Test difference of string and C string.
2396 * @param lhs String.
2397 * @param rhs C string.
2398 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2400 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2402 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2403 const _CharT
* __rhs
)
2404 { return !(__lhs
== __rhs
); }
2408 * @brief Test if string precedes string.
2409 * @param lhs First string.
2410 * @param rhs Second string.
2411 * @return True if @a lhs precedes @a rhs. False otherwise.
2413 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2415 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2416 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
2417 { return __lhs
.compare(__rhs
) < 0; }
2420 * @brief Test if string precedes C string.
2421 * @param lhs String.
2422 * @param rhs C string.
2423 * @return True if @a lhs precedes @a rhs. False otherwise.
2425 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2427 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2428 const _CharT
* __rhs
)
2429 { return __lhs
.compare(__rhs
) < 0; }
2432 * @brief Test if C string precedes string.
2433 * @param lhs C string.
2434 * @param rhs String.
2435 * @return True if @a lhs precedes @a rhs. False otherwise.
2437 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2439 operator<(const _CharT
* __lhs
,
2440 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
2441 { return __rhs
.compare(__lhs
) > 0; }
2445 * @brief Test if string follows string.
2446 * @param lhs First string.
2447 * @param rhs Second string.
2448 * @return True if @a lhs follows @a rhs. False otherwise.
2450 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2452 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2453 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
2454 { return __lhs
.compare(__rhs
) > 0; }
2457 * @brief Test if string follows C string.
2458 * @param lhs String.
2459 * @param rhs C string.
2460 * @return True if @a lhs follows @a rhs. False otherwise.
2462 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2464 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2465 const _CharT
* __rhs
)
2466 { return __lhs
.compare(__rhs
) > 0; }
2469 * @brief Test if C string follows string.
2470 * @param lhs C string.
2471 * @param rhs String.
2472 * @return True if @a lhs follows @a rhs. False otherwise.
2474 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2476 operator>(const _CharT
* __lhs
,
2477 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
2478 { return __rhs
.compare(__lhs
) < 0; }
2482 * @brief Test if string doesn't follow string.
2483 * @param lhs First string.
2484 * @param rhs Second string.
2485 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2487 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2489 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2490 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
2491 { return __lhs
.compare(__rhs
) <= 0; }
2494 * @brief Test if string doesn't follow C string.
2495 * @param lhs String.
2496 * @param rhs C string.
2497 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2499 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2501 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2502 const _CharT
* __rhs
)
2503 { return __lhs
.compare(__rhs
) <= 0; }
2506 * @brief Test if C string doesn't follow string.
2507 * @param lhs C string.
2508 * @param rhs String.
2509 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2511 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2513 operator<=(const _CharT
* __lhs
,
2514 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
2515 { return __rhs
.compare(__lhs
) >= 0; }
2519 * @brief Test if string doesn't precede string.
2520 * @param lhs First string.
2521 * @param rhs Second string.
2522 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2524 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2526 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2527 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
2528 { return __lhs
.compare(__rhs
) >= 0; }
2531 * @brief Test if string doesn't precede C string.
2532 * @param lhs String.
2533 * @param rhs C string.
2534 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2536 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2538 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2539 const _CharT
* __rhs
)
2540 { return __lhs
.compare(__rhs
) >= 0; }
2543 * @brief Test if C string doesn't precede string.
2544 * @param lhs C string.
2545 * @param rhs String.
2546 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2548 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2550 operator>=(const _CharT
* __lhs
,
2551 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
2552 { return __rhs
.compare(__lhs
) <= 0; }
2555 * @brief Swap contents of two strings.
2556 * @param lhs First string.
2557 * @param rhs Second string.
2559 * Exchanges the contents of @a lhs and @a rhs in constant time.
2561 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2563 swap(basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
2564 basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
2565 { __lhs
.swap(__rhs
); }
2568 * @brief Read stream into a string.
2569 * @param is Input stream.
2570 * @param str Buffer to store into.
2571 * @return Reference to the input stream.
2573 * Stores characters from @a is into @a str until whitespace is found, the
2574 * end of the stream is encountered, or str.max_size() is reached. If
2575 * is.width() is non-zero, that is the limit on the number of characters
2576 * stored into @a str. Any previous contents of @a str are erased.
2578 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2579 basic_istream
<_CharT
, _Traits
>&
2580 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
2581 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
);
2584 basic_istream
<char>&
2585 operator>>(basic_istream
<char>& __is
, basic_string
<char>& __str
);
2588 * @brief Write string to a stream.
2589 * @param os Output stream.
2590 * @param str String to write out.
2591 * @return Reference to the output stream.
2593 * Output characters of @a str into os following the same rules as for
2594 * writing a C string.
2596 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2597 inline basic_ostream
<_CharT
, _Traits
>&
2598 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
2599 const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
2601 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2602 // 586. string inserter not a formatted function
2603 return __ostream_insert(__os
, __str
.data(), __str
.size());
2607 * @brief Read a line from stream into a string.
2608 * @param is Input stream.
2609 * @param str Buffer to store into.
2610 * @param delim Character marking end of line.
2611 * @return Reference to the input stream.
2613 * Stores characters from @a is into @a str until @a delim is found, the
2614 * end of the stream is encountered, or str.max_size() is reached. If
2615 * is.width() is non-zero, that is the limit on the number of characters
2616 * stored into @a str. Any previous contents of @a str are erased. If @a
2617 * delim was encountered, it is extracted but not stored into @a str.
2619 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2620 basic_istream
<_CharT
, _Traits
>&
2621 getline(basic_istream
<_CharT
, _Traits
>& __is
,
2622 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
);
2625 * @brief Read a line from stream into a string.
2626 * @param is Input stream.
2627 * @param str Buffer to store into.
2628 * @return Reference to the input stream.
2630 * Stores characters from is into @a str until '\n' is found, the end of
2631 * the stream is encountered, or str.max_size() is reached. If is.width()
2632 * is non-zero, that is the limit on the number of characters stored into
2633 * @a str. Any previous contents of @a str are erased. If end of line was
2634 * encountered, it is extracted but not stored into @a str.
2636 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2637 inline basic_istream
<_CharT
, _Traits
>&
2638 getline(basic_istream
<_CharT
, _Traits
>& __is
,
2639 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
2640 { return getline(__is
, __str
, __is
.widen('\n')); }
2643 basic_istream
<char>&
2644 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
2647 #ifdef _GLIBCXX_USE_WCHAR_T
2649 basic_istream
<wchar_t>&
2650 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
2654 _GLIBCXX_END_NAMESPACE
2656 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99) \
2657 && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
2659 #include <ext/string_conversions.h>
2661 _GLIBCXX_BEGIN_NAMESPACE(std
)
2663 // 21.4 Numeric Conversions [string.conversions].
2665 stoi(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
2666 { return __gnu_cxx::__stoa
<long, int>(&std::strtol
, "stoi", __str
.c_str(),
2670 stol(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
2671 { return __gnu_cxx::__stoa(&std::strtol
, "stol", __str
.c_str(),
2674 inline unsigned long
2675 stoul(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
2676 { return __gnu_cxx::__stoa(&std::strtoul
, "stoul", __str
.c_str(),
2680 stoll(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
2681 { return __gnu_cxx::__stoa(&std::strtoll
, "stoll", __str
.c_str(),
2684 inline unsigned long long
2685 stoull(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
2686 { return __gnu_cxx::__stoa(&std::strtoull
, "stoull", __str
.c_str(),
2689 // NB: strtof vs strtod.
2691 stof(const string
& __str
, size_t* __idx
= 0)
2692 { return __gnu_cxx::__stoa(&std::strtof
, "stof", __str
.c_str(), __idx
); }
2695 stod(const string
& __str
, size_t* __idx
= 0)
2696 { return __gnu_cxx::__stoa(&std::strtod
, "stod", __str
.c_str(), __idx
); }
2699 stold(const string
& __str
, size_t* __idx
= 0)
2700 { return __gnu_cxx::__stoa(&std::strtold
, "stold", __str
.c_str(), __idx
); }
2702 // NB: (v)snprintf vs sprintf.
2706 to_string(int __val
)
2707 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(int),
2711 to_string(unsigned __val
)
2712 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
2713 4 * sizeof(unsigned),
2717 to_string(long __val
)
2718 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(long),
2722 to_string(unsigned long __val
)
2723 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
2724 4 * sizeof(unsigned long),
2728 to_string(long long __val
)
2729 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
2730 4 * sizeof(long long),
2734 to_string(unsigned long long __val
)
2735 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
2736 4 * sizeof(unsigned long long),
2740 to_string(float __val
)
2743 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
2744 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
2749 to_string(double __val
)
2752 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
2753 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
2758 to_string(long double __val
)
2761 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
2762 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
2766 #ifdef _GLIBCXX_USE_WCHAR_T
2768 stoi(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
2769 { return __gnu_cxx::__stoa
<long, int>(&std::wcstol
, "stoi", __str
.c_str(),
2773 stol(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
2774 { return __gnu_cxx::__stoa(&std::wcstol
, "stol", __str
.c_str(),
2777 inline unsigned long
2778 stoul(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
2779 { return __gnu_cxx::__stoa(&std::wcstoul
, "stoul", __str
.c_str(),
2783 stoll(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
2784 { return __gnu_cxx::__stoa(&std::wcstoll
, "stoll", __str
.c_str(),
2787 inline unsigned long long
2788 stoull(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
2789 { return __gnu_cxx::__stoa(&std::wcstoull
, "stoull", __str
.c_str(),
2792 // NB: wcstof vs wcstod.
2794 stof(const wstring
& __str
, size_t* __idx
= 0)
2795 { return __gnu_cxx::__stoa(&std::wcstof
, "stof", __str
.c_str(), __idx
); }
2798 stod(const wstring
& __str
, size_t* __idx
= 0)
2799 { return __gnu_cxx::__stoa(&std::wcstod
, "stod", __str
.c_str(), __idx
); }
2802 stold(const wstring
& __str
, size_t* __idx
= 0)
2803 { return __gnu_cxx::__stoa(&std::wcstold
, "stold", __str
.c_str(), __idx
); }
2807 to_wstring(int __val
)
2808 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(int),
2812 to_wstring(unsigned __val
)
2813 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
2814 4 * sizeof(unsigned),
2818 to_wstring(long __val
)
2819 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(long),
2823 to_wstring(unsigned long __val
)
2824 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
2825 4 * sizeof(unsigned long),
2829 to_wstring(long long __val
)
2830 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
2831 4 * sizeof(long long),
2835 to_wstring(unsigned long long __val
)
2836 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
2837 4 * sizeof(unsigned long long),
2841 to_wstring(float __val
)
2844 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
2845 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
2850 to_wstring(double __val
)
2853 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
2854 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
2859 to_wstring(long double __val
)
2862 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
2863 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
2868 _GLIBCXX_END_NAMESPACE
2872 #endif /* _BASIC_STRING_H */