* fold-const.c (fold_unary): Use build1 instead of copy_node.
[official-gcc.git] / libstdc++-v3 / include / bits / basic_string.h
blob2b26fc54d51071d55386f51cb6eecff02803f379
1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 21 Strings library
35 /** @file basic_string.h
36 * This is an internal header file, included by other library headers.
37 * You should not attempt to use it directly.
40 #ifndef _BASIC_STRING_H
41 #define _BASIC_STRING_H 1
43 #pragma GCC system_header
45 #include <bits/atomicity.h>
46 #include <debug/debug.h>
48 namespace std
50 /**
51 * @class basic_string basic_string.h <string>
52 * @brief Managing sequences of characters and character-like objects.
54 * @ingroup Containers
55 * @ingroup Sequences
57 * Meets the requirements of a <a href="tables.html#65">container</a>, a
58 * <a href="tables.html#66">reversible container</a>, and a
59 * <a href="tables.html#67">sequence</a>. Of the
60 * <a href="tables.html#68">optional sequence requirements</a>, only
61 * @c push_back, @c at, and array access are supported.
63 * @doctodo
66 * @if maint
67 * Documentation? What's that?
68 * Nathan Myers <ncm@cantrip.org>.
70 * A string looks like this:
72 * @code
73 * [_Rep]
74 * _M_length
75 * [basic_string<char_type>] _M_capacity
76 * _M_dataplus _M_refcount
77 * _M_p ----------------> unnamed array of char_type
78 * @endcode
80 * Where the _M_p points to the first character in the string, and
81 * you cast it to a pointer-to-_Rep and subtract 1 to get a
82 * pointer to the header.
84 * This approach has the enormous advantage that a string object
85 * requires only one allocation. All the ugliness is confined
86 * within a single pair of inline functions, which each compile to
87 * a single "add" instruction: _Rep::_M_data(), and
88 * string::_M_rep(); and the allocation function which gets a
89 * block of raw bytes and with room enough and constructs a _Rep
90 * object at the front.
92 * The reason you want _M_data pointing to the character array and
93 * not the _Rep is so that the debugger can see the string
94 * contents. (Probably we should add a non-inline member to get
95 * the _Rep for the debugger to use, so users can check the actual
96 * string length.)
98 * Note that the _Rep object is a POD so that you can have a
99 * static "empty string" _Rep object already "constructed" before
100 * static constructors have run. The reference-count encoding is
101 * chosen so that a 0 indicates one reference, so you never try to
102 * destroy the empty-string _Rep object.
104 * All but the last paragraph is considered pretty conventional
105 * for a C++ string implementation.
106 * @endif
108 // 21.3 Template class basic_string
109 template<typename _CharT, typename _Traits, typename _Alloc>
110 class basic_string
112 // Types:
113 public:
114 typedef _Traits traits_type;
115 typedef typename _Traits::char_type value_type;
116 typedef _Alloc allocator_type;
117 typedef typename _Alloc::size_type size_type;
118 typedef typename _Alloc::difference_type difference_type;
119 typedef typename _Alloc::reference reference;
120 typedef typename _Alloc::const_reference const_reference;
121 typedef typename _Alloc::pointer pointer;
122 typedef typename _Alloc::const_pointer const_pointer;
123 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
124 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
125 const_iterator;
126 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
127 typedef std::reverse_iterator<iterator> reverse_iterator;
129 private:
130 // _Rep: string representation
131 // Invariants:
132 // 1. String really contains _M_length + 1 characters: due to 21.3.4
133 // must be kept null-terminated.
134 // 2. _M_capacity >= _M_length
135 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
136 // 3. _M_refcount has three states:
137 // -1: leaked, one reference, no ref-copies allowed, non-const.
138 // 0: one reference, non-const.
139 // n>0: n + 1 references, operations require a lock, const.
140 // 4. All fields==0 is an empty string, given the extra storage
141 // beyond-the-end for a null terminator; thus, the shared
142 // empty string representation needs no constructor.
144 struct _Rep_base
146 size_type _M_length;
147 size_type _M_capacity;
148 _Atomic_word _M_refcount;
151 struct _Rep : _Rep_base
153 // Types:
154 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
156 // (Public) Data members:
158 // The maximum number of individual char_type elements of an
159 // individual string is determined by _S_max_size. This is the
160 // value that will be returned by max_size(). (Whereas npos
161 // is the maximum number of bytes the allocator can allocate.)
162 // If one was to divvy up the theoretical largest size string,
163 // with a terminating character and m _CharT elements, it'd
164 // look like this:
165 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
166 // Solving for m:
167 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
168 // In addition, this implementation quarters this amount.
169 static const size_type _S_max_size;
170 static const _CharT _S_terminal;
172 // The following storage is init'd to 0 by the linker, resulting
173 // (carefully) in an empty string with one reference.
174 static size_type _S_empty_rep_storage[];
176 static _Rep&
177 _S_empty_rep()
178 { return *reinterpret_cast<_Rep*>(&_S_empty_rep_storage); }
180 bool
181 _M_is_leaked() const
182 { return this->_M_refcount < 0; }
184 bool
185 _M_is_shared() const
186 { return this->_M_refcount > 0; }
188 void
189 _M_set_leaked()
190 { this->_M_refcount = -1; }
192 void
193 _M_set_sharable()
194 { this->_M_refcount = 0; }
196 void
197 _M_set_length_and_sharable(size_type __n)
199 this->_M_set_sharable(); // One reference.
200 this->_M_length = __n;
201 this->_M_refdata()[__n] = _S_terminal; // grrr. (per 21.3.4)
202 // You cannot leave those LWG people alone for a second.
205 _CharT*
206 _M_refdata() throw()
207 { return reinterpret_cast<_CharT*>(this + 1); }
209 _CharT*
210 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
212 return (!_M_is_leaked() && __alloc1 == __alloc2)
213 ? _M_refcopy() : _M_clone(__alloc1);
216 // Create & Destroy
217 static _Rep*
218 _S_create(size_type, size_type, const _Alloc&);
220 void
221 _M_dispose(const _Alloc& __a)
223 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
224 if (__builtin_expect(this != &_S_empty_rep(), false))
225 #endif
226 if (__gnu_cxx::__exchange_and_add(&this->_M_refcount, -1) <= 0)
227 _M_destroy(__a);
228 } // XXX MT
230 void
231 _M_destroy(const _Alloc&) throw();
233 _CharT*
234 _M_refcopy() throw()
236 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
237 if (__builtin_expect(this != &_S_empty_rep(), false))
238 #endif
239 __gnu_cxx::__atomic_add(&this->_M_refcount, 1);
240 return _M_refdata();
241 } // XXX MT
243 _CharT*
244 _M_clone(const _Alloc&, size_type __res = 0);
247 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
248 struct _Alloc_hider : _Alloc
250 _Alloc_hider(_CharT* __dat, const _Alloc& __a)
251 : _Alloc(__a), _M_p(__dat) { }
253 _CharT* _M_p; // The actual data.
256 public:
257 // Data Members (public):
258 // NB: This is an unsigned type, and thus represents the maximum
259 // size that the allocator can hold.
260 /// Value returned by various member functions when they fail.
261 static const size_type npos = static_cast<size_type>(-1);
263 private:
264 // Data Members (private):
265 mutable _Alloc_hider _M_dataplus;
267 _CharT*
268 _M_data() const
269 { return _M_dataplus._M_p; }
271 _CharT*
272 _M_data(_CharT* __p)
273 { return (_M_dataplus._M_p = __p); }
275 _Rep*
276 _M_rep() const
277 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
279 // For the internal use we have functions similar to `begin'/`end'
280 // but they do not call _M_leak.
281 iterator
282 _M_ibegin() const
283 { return iterator(_M_data()); }
285 iterator
286 _M_iend() const
287 { return iterator(_M_data() + this->size()); }
289 void
290 _M_leak() // for use in begin() & non-const op[]
292 if (!_M_rep()->_M_is_leaked())
293 _M_leak_hard();
296 size_type
297 _M_check(size_type __pos, const char* __s) const
299 if (__pos > this->size())
300 __throw_out_of_range(__N(__s));
301 return __pos;
304 void
305 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
307 if (this->max_size() - (this->size() - __n1) < __n2)
308 __throw_length_error(__N(__s));
311 // NB: _M_limit doesn't check for a bad __pos value.
312 size_type
313 _M_limit(size_type __pos, size_type __off) const
315 const bool __testoff = __off < this->size() - __pos;
316 return __testoff ? __off : this->size() - __pos;
319 // True if _Rep and source do not overlap.
320 bool
321 _M_disjunct(const _CharT* __s) const
323 return (less<const _CharT*>()(__s, _M_data())
324 || less<const _CharT*>()(_M_data() + this->size(), __s));
327 // When __n = 1 way faster than the general multichar
328 // traits_type::copy/move/assign.
329 static void
330 _M_copy(_CharT* __d, const _CharT* __s, size_type __n)
332 if (__n == 1)
333 traits_type::assign(*__d, *__s);
334 else
335 traits_type::copy(__d, __s, __n);
338 static void
339 _M_move(_CharT* __d, const _CharT* __s, size_type __n)
341 if (__n == 1)
342 traits_type::assign(*__d, *__s);
343 else
344 traits_type::move(__d, __s, __n);
347 static void
348 _M_assign(_CharT* __d, size_type __n, _CharT __c)
350 if (__n == 1)
351 traits_type::assign(*__d, __c);
352 else
353 traits_type::assign(__d, __n, __c);
356 // _S_copy_chars is a separate template to permit specialization
357 // to optimize for the common case of pointers as iterators.
358 template<class _Iterator>
359 static void
360 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
362 for (; __k1 != __k2; ++__k1, ++__p)
363 traits_type::assign(*__p, *__k1); // These types are off.
366 static void
367 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
368 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
370 static void
371 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
372 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
374 static void
375 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
376 { _M_copy(__p, __k1, __k2 - __k1); }
378 static void
379 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
380 { _M_copy(__p, __k1, __k2 - __k1); }
382 void
383 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
385 void
386 _M_leak_hard();
388 static _Rep&
389 _S_empty_rep()
390 { return _Rep::_S_empty_rep(); }
392 public:
393 // Construct/copy/destroy:
394 // NB: We overload ctors in some cases instead of using default
395 // arguments, per 17.4.4.4 para. 2 item 2.
398 * @brief Default constructor creates an empty string.
400 inline
401 basic_string();
404 * @brief Construct an empty string using allocator a.
406 explicit
407 basic_string(const _Alloc& __a);
409 // NB: per LWG issue 42, semantics different from IS:
411 * @brief Construct string with copy of value of @a str.
412 * @param str Source string.
414 basic_string(const basic_string& __str);
416 * @brief Construct string as copy of a substring.
417 * @param str Source string.
418 * @param pos Index of first character to copy from.
419 * @param n Number of characters to copy (default remainder).
421 basic_string(const basic_string& __str, size_type __pos,
422 size_type __n = npos);
424 * @brief Construct string as copy of a substring.
425 * @param str Source string.
426 * @param pos Index of first character to copy from.
427 * @param n Number of characters to copy.
428 * @param a Allocator to use.
430 basic_string(const basic_string& __str, size_type __pos,
431 size_type __n, const _Alloc& __a);
434 * @brief Construct string initialized by a character array.
435 * @param s Source character array.
436 * @param n Number of characters to copy.
437 * @param a Allocator to use (default is default allocator).
439 * NB: s must have at least n characters, '\0' has no special
440 * meaning.
442 basic_string(const _CharT* __s, size_type __n,
443 const _Alloc& __a = _Alloc());
445 * @brief Construct string as copy of a C string.
446 * @param s Source C string.
447 * @param a Allocator to use (default is default allocator).
449 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
451 * @brief Construct string as multiple characters.
452 * @param n Number of characters.
453 * @param c Character to use.
454 * @param a Allocator to use (default is default allocator).
456 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
459 * @brief Construct string as copy of a range.
460 * @param beg Start of range.
461 * @param end End of range.
462 * @param a Allocator to use (default is default allocator).
464 template<class _InputIterator>
465 basic_string(_InputIterator __beg, _InputIterator __end,
466 const _Alloc& __a = _Alloc());
469 * @brief Destroy the string instance.
471 ~basic_string()
472 { _M_rep()->_M_dispose(this->get_allocator()); }
475 * @brief Assign the value of @a str to this string.
476 * @param str Source string.
478 basic_string&
479 operator=(const basic_string& __str)
480 { return this->assign(__str); }
483 * @brief Copy contents of @a s into this string.
484 * @param s Source null-terminated string.
486 basic_string&
487 operator=(const _CharT* __s)
488 { return this->assign(__s); }
491 * @brief Set value to string of length 1.
492 * @param c Source character.
494 * Assigning to a character makes this string length 1 and
495 * (*this)[0] == @a c.
497 basic_string&
498 operator=(_CharT __c)
500 this->assign(1, __c);
501 return *this;
504 // Iterators:
506 * Returns a read/write iterator that points to the first character in
507 * the %string. Unshares the string.
509 iterator
510 begin()
512 _M_leak();
513 return iterator(_M_data());
517 * Returns a read-only (constant) iterator that points to the first
518 * character in the %string.
520 const_iterator
521 begin() const
522 { return const_iterator(_M_data()); }
525 * Returns a read/write iterator that points one past the last
526 * character in the %string. Unshares the string.
528 iterator
529 end()
531 _M_leak();
532 return iterator(_M_data() + this->size());
536 * Returns a read-only (constant) iterator that points one past the
537 * last character in the %string.
539 const_iterator
540 end() const
541 { return const_iterator(_M_data() + this->size()); }
544 * Returns a read/write reverse iterator that points to the last
545 * character in the %string. Iteration is done in reverse element
546 * order. Unshares the string.
548 reverse_iterator
549 rbegin()
550 { return reverse_iterator(this->end()); }
553 * Returns a read-only (constant) reverse iterator that points
554 * to the last character in the %string. Iteration is done in
555 * reverse element order.
557 const_reverse_iterator
558 rbegin() const
559 { return const_reverse_iterator(this->end()); }
562 * Returns a read/write reverse iterator that points to one before the
563 * first character in the %string. Iteration is done in reverse
564 * element order. Unshares the string.
566 reverse_iterator
567 rend()
568 { return reverse_iterator(this->begin()); }
571 * Returns a read-only (constant) reverse iterator that points
572 * to one before the first character in the %string. Iteration
573 * is done in reverse element order.
575 const_reverse_iterator
576 rend() const
577 { return const_reverse_iterator(this->begin()); }
579 public:
580 // Capacity:
581 /// Returns the number of characters in the string, not including any
582 /// null-termination.
583 size_type
584 size() const
585 { return _M_rep()->_M_length; }
587 /// Returns the number of characters in the string, not including any
588 /// null-termination.
589 size_type
590 length() const
591 { return _M_rep()->_M_length; }
593 /// Returns the size() of the largest possible %string.
594 size_type
595 max_size() const
596 { return _Rep::_S_max_size; }
599 * @brief Resizes the %string to the specified number of characters.
600 * @param n Number of characters the %string should contain.
601 * @param c Character to fill any new elements.
603 * This function will %resize the %string to the specified
604 * number of characters. If the number is smaller than the
605 * %string's current size the %string is truncated, otherwise
606 * the %string is extended and new elements are set to @a c.
608 void
609 resize(size_type __n, _CharT __c);
612 * @brief Resizes the %string to the specified number of characters.
613 * @param n Number of characters the %string should contain.
615 * This function will resize the %string to the specified length. If
616 * the new size is smaller than the %string's current size the %string
617 * is truncated, otherwise the %string is extended and new characters
618 * are default-constructed. For basic types such as char, this means
619 * setting them to 0.
621 void
622 resize(size_type __n)
623 { this->resize(__n, _CharT()); }
626 * Returns the total number of characters that the %string can hold
627 * before needing to allocate more memory.
629 size_type
630 capacity() const
631 { return _M_rep()->_M_capacity; }
634 * @brief Attempt to preallocate enough memory for specified number of
635 * characters.
636 * @param n Number of characters required.
637 * @throw std::length_error If @a n exceeds @c max_size().
639 * This function attempts to reserve enough memory for the
640 * %string to hold the specified number of characters. If the
641 * number requested is more than max_size(), length_error is
642 * thrown.
644 * The advantage of this function is that if optimal code is a
645 * necessity and the user can determine the string length that will be
646 * required, the user can reserve the memory in %advance, and thus
647 * prevent a possible reallocation of memory and copying of %string
648 * data.
650 void
651 reserve(size_type __res_arg = 0);
654 * Erases the string, making it empty.
656 void
657 clear()
658 { _M_mutate(0, this->size(), 0); }
661 * Returns true if the %string is empty. Equivalent to *this == "".
663 bool
664 empty() const
665 { return this->size() == 0; }
667 // Element access:
669 * @brief Subscript access to the data contained in the %string.
670 * @param n The index of the character to access.
671 * @return Read-only (constant) reference to the character.
673 * This operator allows for easy, array-style, data access.
674 * Note that data access with this operator is unchecked and
675 * out_of_range lookups are not defined. (For checked lookups
676 * see at().)
678 const_reference
679 operator[] (size_type __pos) const
681 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
682 return _M_data()[__pos];
686 * @brief Subscript access to the data contained in the %string.
687 * @param n The index of the character to access.
688 * @return Read/write reference to the character.
690 * This operator allows for easy, array-style, data access.
691 * Note that data access with this operator is unchecked and
692 * out_of_range lookups are not defined. (For checked lookups
693 * see at().) Unshares the string.
695 reference
696 operator[](size_type __pos)
698 _GLIBCXX_DEBUG_ASSERT(__pos < size());
699 _M_leak();
700 return _M_data()[__pos];
704 * @brief Provides access to the data contained in the %string.
705 * @param n The index of the character to access.
706 * @return Read-only (const) reference to the character.
707 * @throw std::out_of_range If @a n is an invalid index.
709 * This function provides for safer data access. The parameter is
710 * first checked that it is in the range of the string. The function
711 * throws out_of_range if the check fails.
713 const_reference
714 at(size_type __n) const
716 if (__n >= this->size())
717 __throw_out_of_range(__N("basic_string::at"));
718 return _M_data()[__n];
722 * @brief Provides access to the data contained in the %string.
723 * @param n The index of the character to access.
724 * @return Read/write reference to the character.
725 * @throw std::out_of_range If @a n is an invalid index.
727 * This function provides for safer data access. The parameter is
728 * first checked that it is in the range of the string. The function
729 * throws out_of_range if the check fails. Success results in
730 * unsharing the string.
732 reference
733 at(size_type __n)
735 if (__n >= size())
736 __throw_out_of_range(__N("basic_string::at"));
737 _M_leak();
738 return _M_data()[__n];
741 // Modifiers:
743 * @brief Append a string to this string.
744 * @param str The string to append.
745 * @return Reference to this string.
747 basic_string&
748 operator+=(const basic_string& __str)
749 { return this->append(__str); }
752 * @brief Append a C string.
753 * @param s The C string to append.
754 * @return Reference to this string.
756 basic_string&
757 operator+=(const _CharT* __s)
758 { return this->append(__s); }
761 * @brief Append a character.
762 * @param s The character to append.
763 * @return Reference to this string.
765 basic_string&
766 operator+=(_CharT __c)
768 this->push_back(__c);
769 return *this;
773 * @brief Append a string to this string.
774 * @param str The string to append.
775 * @return Reference to this string.
777 basic_string&
778 append(const basic_string& __str);
781 * @brief Append a substring.
782 * @param str The string to append.
783 * @param pos Index of the first character of str to append.
784 * @param n The number of characters to append.
785 * @return Reference to this string.
786 * @throw std::out_of_range if @a pos is not a valid index.
788 * This function appends @a n characters from @a str starting at @a pos
789 * to this string. If @a n is is larger than the number of available
790 * characters in @a str, the remainder of @a str is appended.
792 basic_string&
793 append(const basic_string& __str, size_type __pos, size_type __n);
796 * @brief Append a C substring.
797 * @param s The C string to append.
798 * @param n The number of characters to append.
799 * @return Reference to this string.
801 basic_string&
802 append(const _CharT* __s, size_type __n);
805 * @brief Append a C string.
806 * @param s The C string to append.
807 * @return Reference to this string.
809 basic_string&
810 append(const _CharT* __s)
812 __glibcxx_requires_string(__s);
813 return this->append(__s, traits_type::length(__s));
817 * @brief Append multiple characters.
818 * @param n The number of characters to append.
819 * @param c The character to use.
820 * @return Reference to this string.
822 * Appends n copies of c to this string.
824 basic_string&
825 append(size_type __n, _CharT __c);
828 * @brief Append a range of characters.
829 * @param first Iterator referencing the first character to append.
830 * @param last Iterator marking the end of the range.
831 * @return Reference to this string.
833 * Appends characters in the range [first,last) to this string.
835 template<class _InputIterator>
836 basic_string&
837 append(_InputIterator __first, _InputIterator __last)
838 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
841 * @brief Append a single character.
842 * @param c Character to append.
844 void
845 push_back(_CharT __c)
847 const size_type __len = 1 + this->size();
848 if (__len > this->capacity() || _M_rep()->_M_is_shared())
849 this->reserve(__len);
850 traits_type::assign(_M_data()[this->size()], __c);
851 _M_rep()->_M_set_length_and_sharable(__len);
855 * @brief Set value to contents of another string.
856 * @param str Source string to use.
857 * @return Reference to this string.
859 basic_string&
860 assign(const basic_string& __str);
863 * @brief Set value to a substring of a string.
864 * @param str The string to use.
865 * @param pos Index of the first character of str.
866 * @param n Number of characters to use.
867 * @return Reference to this string.
868 * @throw std::out_of_range if @a pos is not a valid index.
870 * This function sets this string to the substring of @a str consisting
871 * of @a n characters at @a pos. If @a n is is larger than the number
872 * of available characters in @a str, the remainder of @a str is used.
874 basic_string&
875 assign(const basic_string& __str, size_type __pos, size_type __n)
876 { return this->assign(__str._M_data()
877 + __str._M_check(__pos, "basic_string::assign"),
878 __str._M_limit(__pos, __n)); }
881 * @brief Set value to a C substring.
882 * @param s The C string to use.
883 * @param n Number of characters to use.
884 * @return Reference to this string.
886 * This function sets the value of this string to the first @a n
887 * characters of @a s. If @a n is is larger than the number of
888 * available characters in @a s, the remainder of @a s is used.
890 basic_string&
891 assign(const _CharT* __s, size_type __n);
894 * @brief Set value to contents of a C string.
895 * @param s The C string to use.
896 * @return Reference to this string.
898 * This function sets the value of this string to the value of @a s.
899 * The data is copied, so there is no dependence on @a s once the
900 * function returns.
902 basic_string&
903 assign(const _CharT* __s)
905 __glibcxx_requires_string(__s);
906 return this->assign(__s, traits_type::length(__s));
910 * @brief Set value to multiple characters.
911 * @param n Length of the resulting string.
912 * @param c The character to use.
913 * @return Reference to this string.
915 * This function sets the value of this string to @a n copies of
916 * character @a c.
918 basic_string&
919 assign(size_type __n, _CharT __c)
920 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
923 * @brief Set value to a range of characters.
924 * @param first Iterator referencing the first character to append.
925 * @param last Iterator marking the end of the range.
926 * @return Reference to this string.
928 * Sets value of string to characters in the range [first,last).
930 template<class _InputIterator>
931 basic_string&
932 assign(_InputIterator __first, _InputIterator __last)
933 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
936 * @brief Insert multiple characters.
937 * @param p Iterator referencing location in string to insert at.
938 * @param n Number of characters to insert
939 * @param c The character to insert.
940 * @throw std::length_error If new length exceeds @c max_size().
942 * Inserts @a n copies of character @a c starting at the position
943 * referenced by iterator @a p. If adding characters causes the length
944 * to exceed max_size(), length_error is thrown. The value of the
945 * string doesn't change if an error is thrown.
947 void
948 insert(iterator __p, size_type __n, _CharT __c)
949 { this->replace(__p, __p, __n, __c); }
952 * @brief Insert a range of characters.
953 * @param p Iterator referencing location in string to insert at.
954 * @param beg Start of range.
955 * @param end End of range.
956 * @throw std::length_error If new length exceeds @c max_size().
958 * Inserts characters in range [beg,end). If adding characters causes
959 * the length to exceed max_size(), length_error is thrown. The value
960 * of the string doesn't change if an error is thrown.
962 template<class _InputIterator>
963 void
964 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
965 { this->replace(__p, __p, __beg, __end); }
968 * @brief Insert value of a string.
969 * @param pos1 Iterator referencing location in string to insert at.
970 * @param str The string to insert.
971 * @return Reference to this string.
972 * @throw std::length_error If new length exceeds @c max_size().
974 * Inserts value of @a str starting at @a pos1. If adding characters
975 * causes the length to exceed max_size(), length_error is thrown. The
976 * value of the string doesn't change if an error is thrown.
978 basic_string&
979 insert(size_type __pos1, const basic_string& __str)
980 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
983 * @brief Insert a substring.
984 * @param pos1 Iterator referencing location in string to insert at.
985 * @param str The string to insert.
986 * @param pos2 Start of characters in str to insert.
987 * @param n Number of characters to insert.
988 * @return Reference to this string.
989 * @throw std::length_error If new length exceeds @c max_size().
990 * @throw std::out_of_range If @a pos1 > size() or
991 * @a pos2 > @a str.size().
993 * Starting at @a pos1, insert @a n character of @a str beginning with
994 * @a pos2. If adding characters causes the length to exceed
995 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
996 * this string or @a pos2 is beyond the end of @a str, out_of_range is
997 * thrown. The value of the string doesn't change if an error is
998 * thrown.
1000 basic_string&
1001 insert(size_type __pos1, const basic_string& __str,
1002 size_type __pos2, size_type __n)
1003 { return this->insert(__pos1, __str._M_data()
1004 + __str._M_check(__pos2, "basic_string::insert"),
1005 __str._M_limit(__pos2, __n)); }
1008 * @brief Insert a C substring.
1009 * @param pos Iterator referencing location in string to insert at.
1010 * @param s The C string to insert.
1011 * @param n The number of characters to insert.
1012 * @return Reference to this string.
1013 * @throw std::length_error If new length exceeds @c max_size().
1014 * @throw std::out_of_range If @a pos is beyond the end of this
1015 * string.
1017 * Inserts the first @a n characters of @a s starting at @a pos. If
1018 * adding characters causes the length to exceed max_size(),
1019 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1020 * thrown. The value of the string doesn't change if an error is
1021 * thrown.
1023 basic_string&
1024 insert(size_type __pos, const _CharT* __s, size_type __n);
1027 * @brief Insert a C string.
1028 * @param pos Iterator referencing location in string to insert at.
1029 * @param s The C string to insert.
1030 * @return Reference to this string.
1031 * @throw std::length_error If new length exceeds @c max_size().
1032 * @throw std::out_of_range If @a pos is beyond the end of this
1033 * string.
1035 * Inserts the first @a n characters of @a s starting at @a pos. If
1036 * adding characters causes the length to exceed max_size(),
1037 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1038 * thrown. The value of the string doesn't change if an error is
1039 * thrown.
1041 basic_string&
1042 insert(size_type __pos, const _CharT* __s)
1044 __glibcxx_requires_string(__s);
1045 return this->insert(__pos, __s, traits_type::length(__s));
1049 * @brief Insert multiple characters.
1050 * @param pos Index in string to insert at.
1051 * @param n Number of characters to insert
1052 * @param c The character to insert.
1053 * @return Reference to this string.
1054 * @throw std::length_error If new length exceeds @c max_size().
1055 * @throw std::out_of_range If @a pos is beyond the end of this
1056 * string.
1058 * Inserts @a n copies of character @a c starting at index @a pos. If
1059 * adding characters causes the length to exceed max_size(),
1060 * length_error is thrown. If @a pos > length(), out_of_range is
1061 * thrown. The value of the string doesn't change if an error is
1062 * thrown.
1064 basic_string&
1065 insert(size_type __pos, size_type __n, _CharT __c)
1066 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1067 size_type(0), __n, __c); }
1070 * @brief Insert one character.
1071 * @param p Iterator referencing position in string to insert at.
1072 * @param c The character to insert.
1073 * @return Iterator referencing newly inserted char.
1074 * @throw std::length_error If new length exceeds @c max_size().
1076 * Inserts character @a c at position referenced by @a p. If adding
1077 * character causes the length to exceed max_size(), length_error is
1078 * thrown. If @a p is beyond end of string, out_of_range is thrown.
1079 * The value of the string doesn't change if an error is thrown.
1081 iterator
1082 insert(iterator __p, _CharT __c)
1084 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1085 const size_type __pos = __p - _M_ibegin();
1086 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1087 _M_rep()->_M_set_leaked();
1088 return this->_M_ibegin() + __pos;
1092 * @brief Remove characters.
1093 * @param pos Index of first character to remove (default 0).
1094 * @param n Number of characters to remove (default remainder).
1095 * @return Reference to this string.
1096 * @throw std::out_of_range If @a pos is beyond the end of this
1097 * string.
1099 * Removes @a n characters from this string starting at @a pos. The
1100 * length of the string is reduced by @a n. If there are < @a n
1101 * characters to remove, the remainder of the string is truncated. If
1102 * @a p is beyond end of string, out_of_range is thrown. The value of
1103 * the string doesn't change if an error is thrown.
1105 basic_string&
1106 erase(size_type __pos = 0, size_type __n = npos)
1108 _M_mutate(_M_check(__pos, "basic_string::erase"),
1109 _M_limit(__pos, __n), size_type(0));
1110 return *this;
1114 * @brief Remove one character.
1115 * @param position Iterator referencing the character to remove.
1116 * @return iterator referencing same location after removal.
1118 * Removes the character at @a position from this string. The value
1119 * of the string doesn't change if an error is thrown.
1121 iterator
1122 erase(iterator __position)
1124 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1125 && __position < _M_iend());
1126 const size_type __pos = __position - _M_ibegin();
1127 _M_mutate(__pos, size_type(1), size_type(0));
1128 _M_rep()->_M_set_leaked();
1129 return _M_ibegin() + __pos;
1133 * @brief Remove a range of characters.
1134 * @param first Iterator referencing the first character to remove.
1135 * @param last Iterator referencing the end of the range.
1136 * @return Iterator referencing location of first after removal.
1138 * Removes the characters in the range [first,last) from this string.
1139 * The value of the string doesn't change if an error is thrown.
1141 iterator
1142 erase(iterator __first, iterator __last)
1144 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
1145 && __last <= _M_iend());
1146 const size_type __pos = __first - _M_ibegin();
1147 _M_mutate(__pos, __last - __first, size_type(0));
1148 _M_rep()->_M_set_leaked();
1149 return _M_ibegin() + __pos;
1153 * @brief Replace characters with value from another string.
1154 * @param pos Index of first character to replace.
1155 * @param n Number of characters to be replaced.
1156 * @param str String to insert.
1157 * @return Reference to this string.
1158 * @throw std::out_of_range If @a pos is beyond the end of this
1159 * string.
1160 * @throw std::length_error If new length exceeds @c max_size().
1162 * Removes the characters in the range [pos,pos+n) from this string.
1163 * In place, the value of @a str is inserted. If @a pos is beyond end
1164 * of string, out_of_range is thrown. If the length of the result
1165 * exceeds max_size(), length_error is thrown. The value of the string
1166 * doesn't change if an error is thrown.
1168 basic_string&
1169 replace(size_type __pos, size_type __n, const basic_string& __str)
1170 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1173 * @brief Replace characters with value from another string.
1174 * @param pos1 Index of first character to replace.
1175 * @param n1 Number of characters to be replaced.
1176 * @param str String to insert.
1177 * @param pos2 Index of first character of str to use.
1178 * @param n2 Number of characters from str to use.
1179 * @return Reference to this string.
1180 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1181 * str.size().
1182 * @throw std::length_error If new length exceeds @c max_size().
1184 * Removes the characters in the range [pos1,pos1 + n) from this
1185 * string. In place, the value of @a str is inserted. If @a pos is
1186 * beyond end of string, out_of_range is thrown. If the length of the
1187 * result exceeds max_size(), length_error is thrown. The value of the
1188 * string doesn't change if an error is thrown.
1190 basic_string&
1191 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1192 size_type __pos2, size_type __n2)
1193 { return this->replace(__pos1, __n1, __str._M_data()
1194 + __str._M_check(__pos2, "basic_string::replace"),
1195 __str._M_limit(__pos2, __n2)); }
1198 * @brief Replace characters with value of a C substring.
1199 * @param pos Index of first character to replace.
1200 * @param n1 Number of characters to be replaced.
1201 * @param str C string to insert.
1202 * @param n2 Number of characters from str to use.
1203 * @return Reference to this string.
1204 * @throw std::out_of_range If @a pos1 > size().
1205 * @throw std::length_error If new length exceeds @c max_size().
1207 * Removes the characters in the range [pos,pos + n1) from this string.
1208 * In place, the first @a n2 characters of @a str are inserted, or all
1209 * of @a str if @a n2 is too large. If @a pos is beyond end of string,
1210 * out_of_range is thrown. If the length of result exceeds max_size(),
1211 * length_error is thrown. The value of the string doesn't change if
1212 * an error is thrown.
1214 basic_string&
1215 replace(size_type __pos, size_type __n1, const _CharT* __s,
1216 size_type __n2);
1219 * @brief Replace characters with value of a C string.
1220 * @param pos Index of first character to replace.
1221 * @param n1 Number of characters to be replaced.
1222 * @param str C string to insert.
1223 * @return Reference to this string.
1224 * @throw std::out_of_range If @a pos > size().
1225 * @throw std::length_error If new length exceeds @c max_size().
1227 * Removes the characters in the range [pos,pos + n1) from this string.
1228 * In place, the first @a n characters of @a str are inserted. If @a
1229 * pos is beyond end of string, out_of_range is thrown. If the length
1230 * of result exceeds max_size(), length_error is thrown. The value of
1231 * the string doesn't change if an error is thrown.
1233 basic_string&
1234 replace(size_type __pos, size_type __n1, const _CharT* __s)
1236 __glibcxx_requires_string(__s);
1237 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1241 * @brief Replace characters with multiple characters.
1242 * @param pos Index of first character to replace.
1243 * @param n1 Number of characters to be replaced.
1244 * @param n2 Number of characters to insert.
1245 * @param c Character to insert.
1246 * @return Reference to this string.
1247 * @throw std::out_of_range If @a pos > size().
1248 * @throw std::length_error If new length exceeds @c max_size().
1250 * Removes the characters in the range [pos,pos + n1) from this string.
1251 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1252 * end of string, out_of_range is thrown. If the length of result
1253 * exceeds max_size(), length_error is thrown. The value of the string
1254 * doesn't change if an error is thrown.
1256 basic_string&
1257 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1258 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1259 _M_limit(__pos, __n1), __n2, __c); }
1262 * @brief Replace range of characters with string.
1263 * @param i1 Iterator referencing start of range to replace.
1264 * @param i2 Iterator referencing end of range to replace.
1265 * @param str String value to insert.
1266 * @return Reference to this string.
1267 * @throw std::length_error If new length exceeds @c max_size().
1269 * Removes the characters in the range [i1,i2). In place, the value of
1270 * @a str is inserted. If the length of result exceeds max_size(),
1271 * length_error is thrown. The value of the string doesn't change if
1272 * an error is thrown.
1274 basic_string&
1275 replace(iterator __i1, iterator __i2, const basic_string& __str)
1276 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1279 * @brief Replace range of characters with C substring.
1280 * @param i1 Iterator referencing start of range to replace.
1281 * @param i2 Iterator referencing end of range to replace.
1282 * @param s C string value to insert.
1283 * @param n Number of characters from s to insert.
1284 * @return Reference to this string.
1285 * @throw std::length_error If new length exceeds @c max_size().
1287 * Removes the characters in the range [i1,i2). In place, the first @a
1288 * n characters of @a s are inserted. If the length of result exceeds
1289 * max_size(), length_error is thrown. The value of the string doesn't
1290 * change if an error is thrown.
1292 basic_string&
1293 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1295 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1296 && __i2 <= _M_iend());
1297 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1301 * @brief Replace range of characters with C string.
1302 * @param i1 Iterator referencing start of range to replace.
1303 * @param i2 Iterator referencing end of range to replace.
1304 * @param s C string value to insert.
1305 * @return Reference to this string.
1306 * @throw std::length_error If new length exceeds @c max_size().
1308 * Removes the characters in the range [i1,i2). In place, the
1309 * characters of @a s are inserted. If the length of result exceeds
1310 * max_size(), length_error is thrown. The value of the string doesn't
1311 * change if an error is thrown.
1313 basic_string&
1314 replace(iterator __i1, iterator __i2, const _CharT* __s)
1316 __glibcxx_requires_string(__s);
1317 return this->replace(__i1, __i2, __s, traits_type::length(__s));
1321 * @brief Replace range of characters with multiple characters
1322 * @param i1 Iterator referencing start of range to replace.
1323 * @param i2 Iterator referencing end of range to replace.
1324 * @param n Number of characters to insert.
1325 * @param c Character to insert.
1326 * @return Reference to this string.
1327 * @throw std::length_error If new length exceeds @c max_size().
1329 * Removes the characters in the range [i1,i2). In place, @a n copies
1330 * of @a c are inserted. If the length of result exceeds max_size(),
1331 * length_error is thrown. The value of the string doesn't change if
1332 * an error is thrown.
1334 basic_string&
1335 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1337 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1338 && __i2 <= _M_iend());
1339 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1343 * @brief Replace range of characters with range.
1344 * @param i1 Iterator referencing start of range to replace.
1345 * @param i2 Iterator referencing end of range to replace.
1346 * @param k1 Iterator referencing start of range to insert.
1347 * @param k2 Iterator referencing end of range to insert.
1348 * @return Reference to this string.
1349 * @throw std::length_error If new length exceeds @c max_size().
1351 * Removes the characters in the range [i1,i2). In place, characters
1352 * in the range [k1,k2) are inserted. If the length of result exceeds
1353 * max_size(), length_error is thrown. The value of the string doesn't
1354 * change if an error is thrown.
1356 template<class _InputIterator>
1357 basic_string&
1358 replace(iterator __i1, iterator __i2,
1359 _InputIterator __k1, _InputIterator __k2)
1361 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1362 && __i2 <= _M_iend());
1363 __glibcxx_requires_valid_range(__k1, __k2);
1364 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1365 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1368 // Specializations for the common case of pointer and iterator:
1369 // useful to avoid the overhead of temporary buffering in _M_replace.
1370 basic_string&
1371 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1373 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1374 && __i2 <= _M_iend());
1375 __glibcxx_requires_valid_range(__k1, __k2);
1376 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1377 __k1, __k2 - __k1);
1380 basic_string&
1381 replace(iterator __i1, iterator __i2,
1382 const _CharT* __k1, const _CharT* __k2)
1384 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1385 && __i2 <= _M_iend());
1386 __glibcxx_requires_valid_range(__k1, __k2);
1387 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1388 __k1, __k2 - __k1);
1391 basic_string&
1392 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1394 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1395 && __i2 <= _M_iend());
1396 __glibcxx_requires_valid_range(__k1, __k2);
1397 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1398 __k1.base(), __k2 - __k1);
1401 basic_string&
1402 replace(iterator __i1, iterator __i2,
1403 const_iterator __k1, const_iterator __k2)
1405 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1406 && __i2 <= _M_iend());
1407 __glibcxx_requires_valid_range(__k1, __k2);
1408 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1409 __k1.base(), __k2 - __k1);
1412 private:
1413 template<class _Integer>
1414 basic_string&
1415 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1416 _Integer __val, __true_type)
1417 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1419 template<class _InputIterator>
1420 basic_string&
1421 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1422 _InputIterator __k2, __false_type);
1424 basic_string&
1425 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1426 _CharT __c);
1428 basic_string&
1429 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1430 size_type __n2);
1432 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1433 // requires special behaviour if _InIter is an integral type
1434 template<class _InIterator>
1435 static _CharT*
1436 _S_construct_aux(_InIterator __beg, _InIterator __end,
1437 const _Alloc& __a, __false_type)
1439 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1440 return _S_construct(__beg, __end, __a, _Tag());
1443 template<class _InIterator>
1444 static _CharT*
1445 _S_construct_aux(_InIterator __beg, _InIterator __end,
1446 const _Alloc& __a, __true_type)
1447 { return _S_construct(static_cast<size_type>(__beg),
1448 static_cast<value_type>(__end), __a); }
1450 template<class _InIterator>
1451 static _CharT*
1452 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1454 typedef typename std::__is_integer<_InIterator>::__type _Integral;
1455 return _S_construct_aux(__beg, __end, __a, _Integral());
1458 // For Input Iterators, used in istreambuf_iterators, etc.
1459 template<class _InIterator>
1460 static _CharT*
1461 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1462 input_iterator_tag);
1464 // For forward_iterators up to random_access_iterators, used for
1465 // string::iterator, _CharT*, etc.
1466 template<class _FwdIterator>
1467 static _CharT*
1468 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1469 forward_iterator_tag);
1471 static _CharT*
1472 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1474 public:
1477 * @brief Copy substring into C string.
1478 * @param s C string to copy value into.
1479 * @param n Number of characters to copy.
1480 * @param pos Index of first character to copy.
1481 * @return Number of characters actually copied
1482 * @throw std::out_of_range If pos > size().
1484 * Copies up to @a n characters starting at @a pos into the C string @a
1485 * s. If @a pos is greater than size(), out_of_range is thrown.
1487 size_type
1488 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1491 * @brief Swap contents with another string.
1492 * @param s String to swap with.
1494 * Exchanges the contents of this string with that of @a s in constant
1495 * time.
1497 void
1498 swap(basic_string& __s);
1500 // String operations:
1502 * @brief Return const pointer to null-terminated contents.
1504 * This is a handle to internal data. Do not modify or dire things may
1505 * happen.
1507 const _CharT*
1508 c_str() const
1509 { return _M_data(); }
1512 * @brief Return const pointer to contents.
1514 * This is a handle to internal data. Do not modify or dire things may
1515 * happen.
1517 const _CharT*
1518 data() const
1519 { return _M_data(); }
1522 * @brief Return copy of allocator used to construct this string.
1524 allocator_type
1525 get_allocator() const
1526 { return _M_dataplus; }
1529 * @brief Find position of a C substring.
1530 * @param s C string to locate.
1531 * @param pos Index of character to search from.
1532 * @param n Number of characters from @a s to search for.
1533 * @return Index of start of first occurrence.
1535 * Starting from @a pos, searches forward for the first @a n characters
1536 * in @a s within this string. If found, returns the index where it
1537 * begins. If not found, returns npos.
1539 size_type
1540 find(const _CharT* __s, size_type __pos, size_type __n) const;
1543 * @brief Find position of a string.
1544 * @param str String to locate.
1545 * @param pos Index of character to search from (default 0).
1546 * @return Index of start of first occurrence.
1548 * Starting from @a pos, searches forward for value of @a str within
1549 * this string. If found, returns the index where it begins. If not
1550 * found, returns npos.
1552 size_type
1553 find(const basic_string& __str, size_type __pos = 0) const
1554 { return this->find(__str.data(), __pos, __str.size()); }
1557 * @brief Find position of a C string.
1558 * @param s C string to locate.
1559 * @param pos Index of character to search from (default 0).
1560 * @return Index of start of first occurrence.
1562 * Starting from @a pos, searches forward for the value of @a s within
1563 * this string. If found, returns the index where it begins. If not
1564 * found, returns npos.
1566 size_type
1567 find(const _CharT* __s, size_type __pos = 0) const
1569 __glibcxx_requires_string(__s);
1570 return this->find(__s, __pos, traits_type::length(__s));
1574 * @brief Find position of a character.
1575 * @param c Character to locate.
1576 * @param pos Index of character to search from (default 0).
1577 * @return Index of first occurrence.
1579 * Starting from @a pos, searches forward for @a c within this string.
1580 * If found, returns the index where it was found. If not found,
1581 * returns npos.
1583 size_type
1584 find(_CharT __c, size_type __pos = 0) const;
1587 * @brief Find last position of a string.
1588 * @param str String to locate.
1589 * @param pos Index of character to search back from (default end).
1590 * @return Index of start of last occurrence.
1592 * Starting from @a pos, searches backward for value of @a str within
1593 * this string. If found, returns the index where it begins. If not
1594 * found, returns npos.
1596 size_type
1597 rfind(const basic_string& __str, size_type __pos = npos) const
1598 { return this->rfind(__str.data(), __pos, __str.size()); }
1601 * @brief Find last position of a C substring.
1602 * @param s C string to locate.
1603 * @param pos Index of character to search back from.
1604 * @param n Number of characters from s to search for.
1605 * @return Index of start of last occurrence.
1607 * Starting from @a pos, searches backward for the first @a n
1608 * characters in @a s within this string. If found, returns the index
1609 * where it begins. If not found, returns npos.
1611 size_type
1612 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1615 * @brief Find last position of a C string.
1616 * @param s C string to locate.
1617 * @param pos Index of character to start search at (default 0).
1618 * @return Index of start of last occurrence.
1620 * Starting from @a pos, searches backward for the value of @a s within
1621 * this string. If found, returns the index where it begins. If not
1622 * found, returns npos.
1624 size_type
1625 rfind(const _CharT* __s, size_type __pos = npos) const
1627 __glibcxx_requires_string(__s);
1628 return this->rfind(__s, __pos, traits_type::length(__s));
1632 * @brief Find last position of a character.
1633 * @param c Character to locate.
1634 * @param pos Index of character to search back from (default 0).
1635 * @return Index of last occurrence.
1637 * Starting from @a pos, searches backward for @a c within this string.
1638 * If found, returns the index where it was found. If not found,
1639 * returns npos.
1641 size_type
1642 rfind(_CharT __c, size_type __pos = npos) const;
1645 * @brief Find position of a character of string.
1646 * @param str String containing characters to locate.
1647 * @param pos Index of character to search from (default 0).
1648 * @return Index of first occurrence.
1650 * Starting from @a pos, searches forward for one of the characters of
1651 * @a str within this string. If found, returns the index where it was
1652 * found. If not found, returns npos.
1654 size_type
1655 find_first_of(const basic_string& __str, size_type __pos = 0) const
1656 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1659 * @brief Find position of a character of C substring.
1660 * @param s String containing characters to locate.
1661 * @param pos Index of character to search from (default 0).
1662 * @param n Number of characters from s to search for.
1663 * @return Index of first occurrence.
1665 * Starting from @a pos, searches forward for one of the first @a n
1666 * characters of @a s within this string. If found, returns the index
1667 * where it was found. If not found, returns npos.
1669 size_type
1670 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1673 * @brief Find position of a character of C string.
1674 * @param s String containing characters to locate.
1675 * @param pos Index of character to search from (default 0).
1676 * @return Index of first occurrence.
1678 * Starting from @a pos, searches forward for one of the characters of
1679 * @a s within this string. If found, returns the index where it was
1680 * found. If not found, returns npos.
1682 size_type
1683 find_first_of(const _CharT* __s, size_type __pos = 0) const
1685 __glibcxx_requires_string(__s);
1686 return this->find_first_of(__s, __pos, traits_type::length(__s));
1690 * @brief Find position of a character.
1691 * @param c Character to locate.
1692 * @param pos Index of character to search from (default 0).
1693 * @return Index of first occurrence.
1695 * Starting from @a pos, searches forward for the character @a c within
1696 * this string. If found, returns the index where it was found. If
1697 * not found, returns npos.
1699 * Note: equivalent to find(c, pos).
1701 size_type
1702 find_first_of(_CharT __c, size_type __pos = 0) const
1703 { return this->find(__c, __pos); }
1706 * @brief Find last position of a character of string.
1707 * @param str String containing characters to locate.
1708 * @param pos Index of character to search back from (default end).
1709 * @return Index of last occurrence.
1711 * Starting from @a pos, searches backward for one of the characters of
1712 * @a str within this string. If found, returns the index where it was
1713 * found. If not found, returns npos.
1715 size_type
1716 find_last_of(const basic_string& __str, size_type __pos = npos) const
1717 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1720 * @brief Find last position of a character of C substring.
1721 * @param s C string containing characters to locate.
1722 * @param pos Index of character to search back from (default end).
1723 * @param n Number of characters from s to search for.
1724 * @return Index of last occurrence.
1726 * Starting from @a pos, searches backward for one of the first @a n
1727 * characters of @a s within this string. If found, returns the index
1728 * where it was found. If not found, returns npos.
1730 size_type
1731 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1734 * @brief Find last position of a character of C string.
1735 * @param s C string containing characters to locate.
1736 * @param pos Index of character to search back from (default end).
1737 * @return Index of last occurrence.
1739 * Starting from @a pos, searches backward for one of the characters of
1740 * @a s within this string. If found, returns the index where it was
1741 * found. If not found, returns npos.
1743 size_type
1744 find_last_of(const _CharT* __s, size_type __pos = npos) const
1746 __glibcxx_requires_string(__s);
1747 return this->find_last_of(__s, __pos, traits_type::length(__s));
1751 * @brief Find last position of a character.
1752 * @param c Character to locate.
1753 * @param pos Index of character to search back from (default 0).
1754 * @return Index of last occurrence.
1756 * Starting from @a pos, searches backward for @a c within this string.
1757 * If found, returns the index where it was found. If not found,
1758 * returns npos.
1760 * Note: equivalent to rfind(c, pos).
1762 size_type
1763 find_last_of(_CharT __c, size_type __pos = npos) const
1764 { return this->rfind(__c, __pos); }
1767 * @brief Find position of a character not in string.
1768 * @param str String containing characters to avoid.
1769 * @param pos Index of character to search from (default 0).
1770 * @return Index of first occurrence.
1772 * Starting from @a pos, searches forward for a character not contained
1773 * in @a str within this string. If found, returns the index where it
1774 * was found. If not found, returns npos.
1776 size_type
1777 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1778 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1781 * @brief Find position of a character not in C substring.
1782 * @param s C string containing characters to avoid.
1783 * @param pos Index of character to search from (default 0).
1784 * @param n Number of characters from s to consider.
1785 * @return Index of first occurrence.
1787 * Starting from @a pos, searches forward for a character not contained
1788 * in the first @a n characters of @a s within this string. If found,
1789 * returns the index where it was found. If not found, returns npos.
1791 size_type
1792 find_first_not_of(const _CharT* __s, size_type __pos,
1793 size_type __n) const;
1796 * @brief Find position of a character not in C string.
1797 * @param s C string containing characters to avoid.
1798 * @param pos Index of character to search from (default 0).
1799 * @return Index of first occurrence.
1801 * Starting from @a pos, searches forward for a character not contained
1802 * in @a s within this string. If found, returns the index where it
1803 * was found. If not found, returns npos.
1805 size_type
1806 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1808 __glibcxx_requires_string(__s);
1809 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
1813 * @brief Find position of a different character.
1814 * @param c Character to avoid.
1815 * @param pos Index of character to search from (default 0).
1816 * @return Index of first occurrence.
1818 * Starting from @a pos, searches forward for a character other than @a c
1819 * within this string. If found, returns the index where it was found.
1820 * If not found, returns npos.
1822 size_type
1823 find_first_not_of(_CharT __c, size_type __pos = 0) const;
1826 * @brief Find last position of a character not in string.
1827 * @param str String containing characters to avoid.
1828 * @param pos Index of character to search from (default 0).
1829 * @return Index of first occurrence.
1831 * Starting from @a pos, searches backward for a character not
1832 * contained in @a str within this string. If found, returns the index
1833 * where it was found. If not found, returns npos.
1835 size_type
1836 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1837 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1840 * @brief Find last position of a character not in C substring.
1841 * @param s C string containing characters to avoid.
1842 * @param pos Index of character to search from (default 0).
1843 * @param n Number of characters from s to consider.
1844 * @return Index of first occurrence.
1846 * Starting from @a pos, searches backward for a character not
1847 * contained in the first @a n characters of @a s within this string.
1848 * If found, returns the index where it was found. If not found,
1849 * returns npos.
1851 size_type
1852 find_last_not_of(const _CharT* __s, size_type __pos,
1853 size_type __n) const;
1855 * @brief Find position of a character not in C string.
1856 * @param s C string containing characters to avoid.
1857 * @param pos Index of character to search from (default 0).
1858 * @return Index of first occurrence.
1860 * Starting from @a pos, searches backward for a character not
1861 * contained in @a s within this string. If found, returns the index
1862 * where it was found. If not found, returns npos.
1864 size_type
1865 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1867 __glibcxx_requires_string(__s);
1868 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
1872 * @brief Find last position of a different character.
1873 * @param c Character to avoid.
1874 * @param pos Index of character to search from (default 0).
1875 * @return Index of first occurrence.
1877 * Starting from @a pos, searches backward for a character other than
1878 * @a c within this string. If found, returns the index where it was
1879 * found. If not found, returns npos.
1881 size_type
1882 find_last_not_of(_CharT __c, size_type __pos = npos) const;
1885 * @brief Get a substring.
1886 * @param pos Index of first character (default 0).
1887 * @param n Number of characters in substring (default remainder).
1888 * @return The new string.
1889 * @throw std::out_of_range If pos > size().
1891 * Construct and return a new string using the @a n characters starting
1892 * at @a pos. If the string is too short, use the remainder of the
1893 * characters. If @a pos is beyond the end of the string, out_of_range
1894 * is thrown.
1896 basic_string
1897 substr(size_type __pos = 0, size_type __n = npos) const
1898 { return basic_string(*this,
1899 _M_check(__pos, "basic_string::substr"), __n); }
1902 * @brief Compare to a string.
1903 * @param str String to compare against.
1904 * @return Integer < 0, 0, or > 0.
1906 * Returns an integer < 0 if this string is ordered before @a str, 0 if
1907 * their values are equivalent, or > 0 if this string is ordered after
1908 * @a str. If the lengths of @a str and this string are different, the
1909 * shorter one is ordered first. If they are the same, returns the
1910 * result of traits::compare(data(),str.data(),size());
1913 compare(const basic_string& __str) const
1915 const size_type __size = this->size();
1916 const size_type __osize = __str.size();
1917 const size_type __len = std::min(__size, __osize);
1919 int __r = traits_type::compare(_M_data(), __str.data(), __len);
1920 if (!__r)
1921 __r = __size - __osize;
1922 return __r;
1926 * @brief Compare substring to a string.
1927 * @param pos Index of first character of substring.
1928 * @param n Number of characters in substring.
1929 * @param str String to compare against.
1930 * @return Integer < 0, 0, or > 0.
1932 * Form the substring of this string from the @a n characters starting
1933 * at @a pos. Returns an integer < 0 if the substring is ordered
1934 * before @a str, 0 if their values are equivalent, or > 0 if the
1935 * substring is ordered after @a str. If the lengths @a of str and the
1936 * substring are different, the shorter one is ordered first. If they
1937 * are the same, returns the result of
1938 * traits::compare(substring.data(),str.data(),size());
1941 compare(size_type __pos, size_type __n, const basic_string& __str) const;
1944 * @brief Compare substring to a substring.
1945 * @param pos1 Index of first character of substring.
1946 * @param n1 Number of characters in substring.
1947 * @param str String to compare against.
1948 * @param pos2 Index of first character of substring of str.
1949 * @param n2 Number of characters in substring of str.
1950 * @return Integer < 0, 0, or > 0.
1952 * Form the substring of this string from the @a n1 characters starting
1953 * at @a pos1. Form the substring of @a str from the @a n2 characters
1954 * starting at @a pos2. Returns an integer < 0 if this substring is
1955 * ordered before the substring of @a str, 0 if their values are
1956 * equivalent, or > 0 if this substring is ordered after the substring
1957 * of @a str. If the lengths of the substring of @a str and this
1958 * substring are different, the shorter one is ordered first. If they
1959 * are the same, returns the result of
1960 * traits::compare(substring.data(),str.substr(pos2,n2).data(),size());
1963 compare(size_type __pos1, size_type __n1, const basic_string& __str,
1964 size_type __pos2, size_type __n2) const;
1967 * @brief Compare to a C string.
1968 * @param s C string to compare against.
1969 * @return Integer < 0, 0, or > 0.
1971 * Returns an integer < 0 if this string is ordered before @a s, 0 if
1972 * their values are equivalent, or > 0 if this string is ordered after
1973 * @a s. If the lengths of @a s and this string are different, the
1974 * shorter one is ordered first. If they are the same, returns the
1975 * result of traits::compare(data(),s,size());
1978 compare(const _CharT* __s) const;
1980 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1981 // 5 String::compare specification questionable
1983 * @brief Compare substring to a C string.
1984 * @param pos Index of first character of substring.
1985 * @param n1 Number of characters in substring.
1986 * @param s C string to compare against.
1987 * @return Integer < 0, 0, or > 0.
1989 * Form the substring of this string from the @a n1 characters starting
1990 * at @a pos. Returns an integer < 0 if the substring is ordered
1991 * before @a s, 0 if their values are equivalent, or > 0 if the
1992 * substring is ordered after @a s. If the lengths of @a s and the
1993 * substring are different, the shorter one is ordered first. If they
1994 * are the same, returns the result of
1995 * traits::compare(substring.data(),s,size());
1998 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2001 * @brief Compare substring against a character array.
2002 * @param pos1 Index of first character of substring.
2003 * @param n1 Number of characters in substring.
2004 * @param s character array to compare against.
2005 * @param n2 Number of characters of s.
2006 * @return Integer < 0, 0, or > 0.
2008 * Form the substring of this string from the @a n1 characters starting
2009 * at @a pos1. Form a string from the first @a n2 characters of @a s.
2010 * Returns an integer < 0 if this substring is ordered before the string
2011 * from @a s, 0 if their values are equivalent, or > 0 if this substring
2012 * is ordered after the string from @a s. If the lengths of this
2013 * substring and @a n2 are different, the shorter one is ordered first.
2014 * If they are the same, returns the result of
2015 * traits::compare(substring.data(),s,size());
2017 * NB: s must have at least n2 characters, '\0' has no special
2018 * meaning.
2021 compare(size_type __pos, size_type __n1, const _CharT* __s,
2022 size_type __n2) const;
2025 template<typename _CharT, typename _Traits, typename _Alloc>
2026 inline basic_string<_CharT, _Traits, _Alloc>::
2027 basic_string()
2028 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
2029 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2030 #else
2031 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) { }
2032 #endif
2034 // operator+
2036 * @brief Concatenate two strings.
2037 * @param lhs First string.
2038 * @param rhs Last string.
2039 * @return New string with value of @a lhs followed by @a rhs.
2041 template<typename _CharT, typename _Traits, typename _Alloc>
2042 basic_string<_CharT, _Traits, _Alloc>
2043 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2044 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2046 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2047 __str.append(__rhs);
2048 return __str;
2052 * @brief Concatenate C string and string.
2053 * @param lhs First string.
2054 * @param rhs Last string.
2055 * @return New string with value of @a lhs followed by @a rhs.
2057 template<typename _CharT, typename _Traits, typename _Alloc>
2058 basic_string<_CharT,_Traits,_Alloc>
2059 operator+(const _CharT* __lhs,
2060 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2063 * @brief Concatenate character and string.
2064 * @param lhs First string.
2065 * @param rhs Last string.
2066 * @return New string with @a lhs followed by @a rhs.
2068 template<typename _CharT, typename _Traits, typename _Alloc>
2069 basic_string<_CharT,_Traits,_Alloc>
2070 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2073 * @brief Concatenate string and C string.
2074 * @param lhs First string.
2075 * @param rhs Last string.
2076 * @return New string with @a lhs followed by @a rhs.
2078 template<typename _CharT, typename _Traits, typename _Alloc>
2079 inline basic_string<_CharT, _Traits, _Alloc>
2080 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2081 const _CharT* __rhs)
2083 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2084 __str.append(__rhs);
2085 return __str;
2089 * @brief Concatenate string and character.
2090 * @param lhs First string.
2091 * @param rhs Last string.
2092 * @return New string with @a lhs followed by @a rhs.
2094 template<typename _CharT, typename _Traits, typename _Alloc>
2095 inline basic_string<_CharT, _Traits, _Alloc>
2096 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2098 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
2099 typedef typename __string_type::size_type __size_type;
2100 __string_type __str(__lhs);
2101 __str.append(__size_type(1), __rhs);
2102 return __str;
2105 // operator ==
2107 * @brief Test equivalence of two strings.
2108 * @param lhs First string.
2109 * @param rhs Second string.
2110 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2112 template<typename _CharT, typename _Traits, typename _Alloc>
2113 inline bool
2114 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2115 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2116 { return __lhs.compare(__rhs) == 0; }
2119 * @brief Test equivalence of C string and string.
2120 * @param lhs C string.
2121 * @param rhs String.
2122 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
2124 template<typename _CharT, typename _Traits, typename _Alloc>
2125 inline bool
2126 operator==(const _CharT* __lhs,
2127 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2128 { return __rhs.compare(__lhs) == 0; }
2131 * @brief Test equivalence of string and C string.
2132 * @param lhs String.
2133 * @param rhs C string.
2134 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2136 template<typename _CharT, typename _Traits, typename _Alloc>
2137 inline bool
2138 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2139 const _CharT* __rhs)
2140 { return __lhs.compare(__rhs) == 0; }
2142 // operator !=
2144 * @brief Test difference of two strings.
2145 * @param lhs First string.
2146 * @param rhs Second string.
2147 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2149 template<typename _CharT, typename _Traits, typename _Alloc>
2150 inline bool
2151 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2152 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2153 { return __rhs.compare(__lhs) != 0; }
2156 * @brief Test difference of C string and string.
2157 * @param lhs C string.
2158 * @param rhs String.
2159 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2161 template<typename _CharT, typename _Traits, typename _Alloc>
2162 inline bool
2163 operator!=(const _CharT* __lhs,
2164 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2165 { return __rhs.compare(__lhs) != 0; }
2168 * @brief Test difference of string and C string.
2169 * @param lhs String.
2170 * @param rhs C string.
2171 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2173 template<typename _CharT, typename _Traits, typename _Alloc>
2174 inline bool
2175 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2176 const _CharT* __rhs)
2177 { return __lhs.compare(__rhs) != 0; }
2179 // operator <
2181 * @brief Test if string precedes string.
2182 * @param lhs First string.
2183 * @param rhs Second string.
2184 * @return True if @a lhs precedes @a rhs. False otherwise.
2186 template<typename _CharT, typename _Traits, typename _Alloc>
2187 inline bool
2188 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2189 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2190 { return __lhs.compare(__rhs) < 0; }
2193 * @brief Test if string precedes C string.
2194 * @param lhs String.
2195 * @param rhs C string.
2196 * @return True if @a lhs precedes @a rhs. False otherwise.
2198 template<typename _CharT, typename _Traits, typename _Alloc>
2199 inline bool
2200 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2201 const _CharT* __rhs)
2202 { return __lhs.compare(__rhs) < 0; }
2205 * @brief Test if C string precedes string.
2206 * @param lhs C string.
2207 * @param rhs String.
2208 * @return True if @a lhs precedes @a rhs. False otherwise.
2210 template<typename _CharT, typename _Traits, typename _Alloc>
2211 inline bool
2212 operator<(const _CharT* __lhs,
2213 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2214 { return __rhs.compare(__lhs) > 0; }
2216 // operator >
2218 * @brief Test if string follows string.
2219 * @param lhs First string.
2220 * @param rhs Second string.
2221 * @return True if @a lhs follows @a rhs. False otherwise.
2223 template<typename _CharT, typename _Traits, typename _Alloc>
2224 inline bool
2225 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2226 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2227 { return __lhs.compare(__rhs) > 0; }
2230 * @brief Test if string follows C string.
2231 * @param lhs String.
2232 * @param rhs C string.
2233 * @return True if @a lhs follows @a rhs. False otherwise.
2235 template<typename _CharT, typename _Traits, typename _Alloc>
2236 inline bool
2237 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2238 const _CharT* __rhs)
2239 { return __lhs.compare(__rhs) > 0; }
2242 * @brief Test if C string follows string.
2243 * @param lhs C string.
2244 * @param rhs String.
2245 * @return True if @a lhs follows @a rhs. False otherwise.
2247 template<typename _CharT, typename _Traits, typename _Alloc>
2248 inline bool
2249 operator>(const _CharT* __lhs,
2250 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2251 { return __rhs.compare(__lhs) < 0; }
2253 // operator <=
2255 * @brief Test if string doesn't follow string.
2256 * @param lhs First string.
2257 * @param rhs Second string.
2258 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2260 template<typename _CharT, typename _Traits, typename _Alloc>
2261 inline bool
2262 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2263 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2264 { return __lhs.compare(__rhs) <= 0; }
2267 * @brief Test if string doesn't follow C string.
2268 * @param lhs String.
2269 * @param rhs C string.
2270 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2272 template<typename _CharT, typename _Traits, typename _Alloc>
2273 inline bool
2274 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2275 const _CharT* __rhs)
2276 { return __lhs.compare(__rhs) <= 0; }
2279 * @brief Test if C string doesn't follow string.
2280 * @param lhs C string.
2281 * @param rhs String.
2282 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2284 template<typename _CharT, typename _Traits, typename _Alloc>
2285 inline bool
2286 operator<=(const _CharT* __lhs,
2287 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2288 { return __rhs.compare(__lhs) >= 0; }
2290 // operator >=
2292 * @brief Test if string doesn't precede string.
2293 * @param lhs First string.
2294 * @param rhs Second string.
2295 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2297 template<typename _CharT, typename _Traits, typename _Alloc>
2298 inline bool
2299 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2300 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2301 { return __lhs.compare(__rhs) >= 0; }
2304 * @brief Test if string doesn't precede C string.
2305 * @param lhs String.
2306 * @param rhs C string.
2307 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2309 template<typename _CharT, typename _Traits, typename _Alloc>
2310 inline bool
2311 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2312 const _CharT* __rhs)
2313 { return __lhs.compare(__rhs) >= 0; }
2316 * @brief Test if C string doesn't precede string.
2317 * @param lhs C string.
2318 * @param rhs String.
2319 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2321 template<typename _CharT, typename _Traits, typename _Alloc>
2322 inline bool
2323 operator>=(const _CharT* __lhs,
2324 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2325 { return __rhs.compare(__lhs) <= 0; }
2328 * @brief Swap contents of two strings.
2329 * @param lhs First string.
2330 * @param rhs Second string.
2332 * Exchanges the contents of @a lhs and @a rhs in constant time.
2334 template<typename _CharT, typename _Traits, typename _Alloc>
2335 inline void
2336 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2337 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2338 { __lhs.swap(__rhs); }
2341 * @brief Read stream into a string.
2342 * @param is Input stream.
2343 * @param str Buffer to store into.
2344 * @return Reference to the input stream.
2346 * Stores characters from @a is into @a str until whitespace is found, the
2347 * end of the stream is encountered, or str.max_size() is reached. If
2348 * is.width() is non-zero, that is the limit on the number of characters
2349 * stored into @a str. Any previous contents of @a str are erased.
2351 template<typename _CharT, typename _Traits, typename _Alloc>
2352 basic_istream<_CharT, _Traits>&
2353 operator>>(basic_istream<_CharT, _Traits>& __is,
2354 basic_string<_CharT, _Traits, _Alloc>& __str);
2357 * @brief Write string to a stream.
2358 * @param os Output stream.
2359 * @param str String to write out.
2360 * @return Reference to the output stream.
2362 * Output characters of @a str into os following the same rules as for
2363 * writing a C string.
2365 template<typename _CharT, typename _Traits, typename _Alloc>
2366 basic_ostream<_CharT, _Traits>&
2367 operator<<(basic_ostream<_CharT, _Traits>& __os,
2368 const basic_string<_CharT, _Traits, _Alloc>& __str);
2371 * @brief Read a line from stream into a string.
2372 * @param is Input stream.
2373 * @param str Buffer to store into.
2374 * @param delim Character marking end of line.
2375 * @return Reference to the input stream.
2377 * Stores characters from @a is into @a str until @a delim is found, the
2378 * end of the stream is encountered, or str.max_size() is reached. If
2379 * is.width() is non-zero, that is the limit on the number of characters
2380 * stored into @a str. Any previous contents of @a str are erased. If @a
2381 * delim was encountered, it is extracted but not stored into @a str.
2383 template<typename _CharT, typename _Traits, typename _Alloc>
2384 basic_istream<_CharT, _Traits>&
2385 getline(basic_istream<_CharT, _Traits>& __is,
2386 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2389 * @brief Read a line from stream into a string.
2390 * @param is Input stream.
2391 * @param str Buffer to store into.
2392 * @return Reference to the input stream.
2394 * Stores characters from is into @a str until '\n' is found, the end of
2395 * the stream is encountered, or str.max_size() is reached. If is.width()
2396 * is non-zero, that is the limit on the number of characters stored into
2397 * @a str. Any previous contents of @a str are erased. If end of line was
2398 * encountered, it is extracted but not stored into @a str.
2400 template<typename _CharT, typename _Traits, typename _Alloc>
2401 inline basic_istream<_CharT, _Traits>&
2402 getline(basic_istream<_CharT, _Traits>& __is,
2403 basic_string<_CharT, _Traits, _Alloc>& __str);
2405 template<>
2406 basic_istream<char>&
2407 getline(basic_istream<char>& __in, basic_string<char>& __str,
2408 char __delim);
2410 #ifdef _GLIBCXX_USE_WCHAR_T
2411 template<>
2412 basic_istream<wchar_t>&
2413 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
2414 wchar_t __delim);
2415 #endif
2416 } // namespace std
2418 #endif /* _BASIC_STRING_H */