basic_string.h (_Rep::_M_is_safe, [...]): New, use througout.
[gcc.git] / libstdc++-v3 / include / bits / basic_string.h
1 // Components for manipulating sequences of characters -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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.
11
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.
16
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.
21
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.
30
31 //
32 // ISO C++ 14882: 21 Strings library
33 //
34
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.
38 */
39
40 #ifndef _BASIC_STRING_H
41 #define _BASIC_STRING_H 1
42
43 #pragma GCC system_header
44
45 #include <bits/atomicity.h>
46 #include <debug/debug.h>
47
48 namespace std
49 {
50 /**
51 * @class basic_string basic_string.h <string>
52 * @brief Managing sequences of characters and character-like objects.
53 *
54 * @ingroup Containers
55 * @ingroup Sequences
56 *
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.
62 *
63 * @doctodo
64 *
65 *
66 * @if maint
67 * Documentation? What's that?
68 * Nathan Myers <ncm@cantrip.org>.
69 *
70 * A string looks like this:
71 *
72 * @code
73 * [_Rep]
74 * _M_length
75 * [basic_string<char_type>] _M_capacity
76 * _M_dataplus _M_refcount
77 * _M_p ----------------> unnamed array of char_type
78 * @endcode
79 *
80 * Where the _M_p points to the first character in the string, and
81 * you cast it to a pointer-to-_Rep and subtract 1 to get a
82 * pointer to the header.
83 *
84 * This approach has the enormous advantage that a string object
85 * requires only one allocation. All the ugliness is confined
86 * within a single pair of inline functions, which each compile to
87 * a single "add" instruction: _Rep::_M_data(), and
88 * string::_M_rep(); and the allocation function which gets a
89 * block of raw bytes and with room enough and constructs a _Rep
90 * object at the front.
91 *
92 * The reason you want _M_data pointing to the character array and
93 * not the _Rep is so that the debugger can see the string
94 * contents. (Probably we should add a non-inline member to get
95 * the _Rep for the debugger to use, so users can check the actual
96 * string length.)
97 *
98 * Note that the _Rep object is a POD so that you can have a
99 * static "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.
103 *
104 * All but the last paragraph is considered pretty conventional
105 * for a C++ string implementation.
106 * @endif
107 */
108 // 21.3 Template class basic_string
109 template<typename _CharT, typename _Traits, typename _Alloc>
110 class basic_string
111 {
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;
128
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.
143
144 struct _Rep_base
145 {
146 size_type _M_length;
147 size_type _M_capacity;
148 _Atomic_word _M_refcount;
149 };
150
151 struct _Rep : _Rep_base
152 {
153 // Types:
154 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
155
156 // (Public) Data members:
157
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;
171
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[];
175
176 static _Rep&
177 _S_empty_rep()
178 { return *reinterpret_cast<_Rep*>(&_S_empty_rep_storage); }
179
180 bool
181 _M_is_leaked() const
182 { return this->_M_refcount < 0; }
183
184 bool
185 _M_is_shared() const
186 { return this->_M_refcount > 0; }
187
188 // True if source and destination do not overlap.
189 bool
190 _M_is_safe(const _CharT* __data, const _CharT* __s) const
191 {
192 return (less<const _CharT*>()(__s, __data)
193 || less<const _CharT*>()(__data + this->_M_length, __s));
194 }
195
196 void
197 _M_set_leaked()
198 { this->_M_refcount = -1; }
199
200 void
201 _M_set_sharable()
202 { this->_M_refcount = 0; }
203
204 void
205 _M_set_length_and_sharable(size_type __n)
206 {
207 this->_M_set_sharable(); // One reference.
208 this->_M_length = __n;
209 this->_M_refdata()[__n] = _S_terminal; // grrr. (per 21.3.4)
210 // You cannot leave those LWG people alone for a second.
211 }
212
213 _CharT*
214 _M_refdata() throw()
215 { return reinterpret_cast<_CharT*>(this + 1); }
216
217 _CharT*
218 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
219 {
220 return (!_M_is_leaked() && __alloc1 == __alloc2)
221 ? _M_refcopy() : _M_clone(__alloc1);
222 }
223
224 // Create & Destroy
225 static _Rep*
226 _S_create(size_type, size_type, const _Alloc&);
227
228 void
229 _M_dispose(const _Alloc& __a)
230 {
231 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
232 if (__builtin_expect(this != &_S_empty_rep(), false))
233 #endif
234 if (__gnu_cxx::__exchange_and_add(&this->_M_refcount, -1) <= 0)
235 _M_destroy(__a);
236 } // XXX MT
237
238 void
239 _M_destroy(const _Alloc&) throw();
240
241 _CharT*
242 _M_refcopy() throw()
243 {
244 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
245 if (__builtin_expect(this != &_S_empty_rep(), false))
246 #endif
247 __gnu_cxx::__atomic_add(&this->_M_refcount, 1);
248 return _M_refdata();
249 } // XXX MT
250
251 _CharT*
252 _M_clone(const _Alloc&, size_type __res = 0);
253 };
254
255 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
256 struct _Alloc_hider : _Alloc
257 {
258 _Alloc_hider(_CharT* __dat, const _Alloc& __a)
259 : _Alloc(__a), _M_p(__dat) { }
260
261 _CharT* _M_p; // The actual data.
262 };
263
264 public:
265 // Data Members (public):
266 // NB: This is an unsigned type, and thus represents the maximum
267 // size that the allocator can hold.
268 /// @var
269 /// Value returned by various member functions when they fail.
270 static const size_type npos = static_cast<size_type>(-1);
271
272 private:
273 // Data Members (private):
274 mutable _Alloc_hider _M_dataplus;
275
276 _CharT*
277 _M_data() const
278 { return _M_dataplus._M_p; }
279
280 _CharT*
281 _M_data(_CharT* __p)
282 { return (_M_dataplus._M_p = __p); }
283
284 _Rep*
285 _M_rep() const
286 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
287
288 // For the internal use we have functions similar to `begin'/`end'
289 // but they do not call _M_leak.
290 iterator
291 _M_ibegin() const
292 { return iterator(_M_data()); }
293
294 iterator
295 _M_iend() const
296 { return iterator(_M_data() + this->size()); }
297
298 void
299 _M_leak() // for use in begin() & non-const op[]
300 {
301 if (!_M_rep()->_M_is_leaked())
302 _M_leak_hard();
303 }
304
305 size_type
306 _M_check(size_type __pos, const char* __s) const
307 {
308 if (__pos > this->size())
309 __throw_out_of_range(__N(__s));
310 return __pos;
311 }
312
313 void
314 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
315 {
316 if (this->max_size() - (this->size() - __n1) < __n2)
317 __throw_length_error(__N(__s));
318 }
319
320 // NB: _M_limit doesn't check for a bad __pos value.
321 size_type
322 _M_limit(size_type __pos, size_type __off) const
323 {
324 const bool __testoff = __off < this->size() - __pos;
325 return __testoff ? __off : this->size() - __pos;
326 }
327
328 // When __n = 1 way faster than the general multichar
329 // traits_type::copy/move/assign.
330 static void
331 _M_copy(_CharT* __d, const _CharT* __s, size_type __n)
332 {
333 if (__n == 1)
334 traits_type::assign(*__d, *__s);
335 else
336 traits_type::copy(__d, __s, __n);
337 }
338
339 static void
340 _M_move(_CharT* __d, const _CharT* __s, size_type __n)
341 {
342 if (__n == 1)
343 traits_type::assign(*__d, *__s);
344 else
345 traits_type::move(__d, __s, __n);
346 }
347
348 static void
349 _M_assign(_CharT* __d, size_type __n, _CharT __c)
350 {
351 if (__n == 1)
352 traits_type::assign(*__d, __c);
353 else
354 traits_type::assign(__d, __n, __c);
355 }
356
357 // _S_copy_chars is a separate template to permit specialization
358 // to optimize for the common case of pointers as iterators.
359 template<class _Iterator>
360 static void
361 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
362 {
363 for (; __k1 != __k2; ++__k1, ++__p)
364 traits_type::assign(*__p, *__k1); // These types are off.
365 }
366
367 static void
368 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
369 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
370
371 static void
372 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
373 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
374
375 static void
376 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
377 { _M_copy(__p, __k1, __k2 - __k1); }
378
379 static void
380 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
381 { _M_copy(__p, __k1, __k2 - __k1); }
382
383 void
384 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
385
386 void
387 _M_leak_hard();
388
389 static _Rep&
390 _S_empty_rep()
391 { return _Rep::_S_empty_rep(); }
392
393 public:
394 // Construct/copy/destroy:
395 // NB: We overload ctors in some cases instead of using default
396 // arguments, per 17.4.4.4 para. 2 item 2.
397
398 /**
399 * @brief Default constructor creates an empty string.
400 */
401 inline
402 basic_string();
403
404 /**
405 * @brief Construct an empty string using allocator a.
406 */
407 explicit
408 basic_string(const _Alloc& __a);
409
410 // NB: per LWG issue 42, semantics different from IS:
411 /**
412 * @brief Construct string with copy of value of @a str.
413 * @param str Source string.
414 */
415 basic_string(const basic_string& __str);
416 /**
417 * @brief Construct string as copy of a substring.
418 * @param str Source string.
419 * @param pos Index of first character to copy from.
420 * @param n Number of characters to copy (default remainder).
421 */
422 basic_string(const basic_string& __str, size_type __pos,
423 size_type __n = npos);
424 /**
425 * @brief Construct string as copy of a substring.
426 * @param str Source string.
427 * @param pos Index of first character to copy from.
428 * @param n Number of characters to copy.
429 * @param a Allocator to use.
430 */
431 basic_string(const basic_string& __str, size_type __pos,
432 size_type __n, const _Alloc& __a);
433
434 /**
435 * @brief Construct string initialized by a character array.
436 * @param s Source character array.
437 * @param n Number of characters to copy.
438 * @param a Allocator to use (default is default allocator).
439 *
440 * NB: s must have at least n characters, '\0' has no special
441 * meaning.
442 */
443 basic_string(const _CharT* __s, size_type __n,
444 const _Alloc& __a = _Alloc());
445 /**
446 * @brief Construct string as copy of a C string.
447 * @param s Source C string.
448 * @param a Allocator to use (default is default allocator).
449 */
450 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
451 /**
452 * @brief Construct string as multiple characters.
453 * @param n Number of characters.
454 * @param c Character to use.
455 * @param a Allocator to use (default is default allocator).
456 */
457 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
458
459 /**
460 * @brief Construct string as copy of a range.
461 * @param beg Start of range.
462 * @param end End of range.
463 * @param a Allocator to use (default is default allocator).
464 */
465 template<class _InputIterator>
466 basic_string(_InputIterator __beg, _InputIterator __end,
467 const _Alloc& __a = _Alloc());
468
469 /**
470 * @brief Destroy the string instance.
471 */
472 ~basic_string()
473 { _M_rep()->_M_dispose(this->get_allocator()); }
474
475 /**
476 * @brief Assign the value of @a str to this string.
477 * @param str Source string.
478 */
479 basic_string&
480 operator=(const basic_string& __str)
481 { return this->assign(__str); }
482
483 /**
484 * @brief Copy contents of @a s into this string.
485 * @param s Source null-terminated string.
486 */
487 basic_string&
488 operator=(const _CharT* __s)
489 { return this->assign(__s); }
490
491 /**
492 * @brief Set value to string of length 1.
493 * @param c Source character.
494 *
495 * Assigning to a character makes this string length 1 and
496 * (*this)[0] == @a c.
497 */
498 basic_string&
499 operator=(_CharT __c)
500 {
501 this->assign(1, __c);
502 return *this;
503 }
504
505 // Iterators:
506 /**
507 * Returns a read/write iterator that points to the first character in
508 * the %string. Unshares the string.
509 */
510 iterator
511 begin()
512 {
513 _M_leak();
514 return iterator(_M_data());
515 }
516
517 /**
518 * Returns a read-only (constant) iterator that points to the first
519 * character in the %string.
520 */
521 const_iterator
522 begin() const
523 { return const_iterator(_M_data()); }
524
525 /**
526 * Returns a read/write iterator that points one past the last
527 * character in the %string. Unshares the string.
528 */
529 iterator
530 end()
531 {
532 _M_leak();
533 return iterator(_M_data() + this->size());
534 }
535
536 /**
537 * Returns a read-only (constant) iterator that points one past the
538 * last character in the %string.
539 */
540 const_iterator
541 end() const
542 { return const_iterator(_M_data() + this->size()); }
543
544 /**
545 * Returns a read/write reverse iterator that points to the last
546 * character in the %string. Iteration is done in reverse element
547 * order. Unshares the string.
548 */
549 reverse_iterator
550 rbegin()
551 { return reverse_iterator(this->end()); }
552
553 /**
554 * Returns a read-only (constant) reverse iterator that points
555 * to the last character in the %string. Iteration is done in
556 * reverse element order.
557 */
558 const_reverse_iterator
559 rbegin() const
560 { return const_reverse_iterator(this->end()); }
561
562 /**
563 * Returns a read/write reverse iterator that points to one before the
564 * first character in the %string. Iteration is done in reverse
565 * element order. Unshares the string.
566 */
567 reverse_iterator
568 rend()
569 { return reverse_iterator(this->begin()); }
570
571 /**
572 * Returns a read-only (constant) reverse iterator that points
573 * to one before the first character in the %string. Iteration
574 * is done in reverse element order.
575 */
576 const_reverse_iterator
577 rend() const
578 { return const_reverse_iterator(this->begin()); }
579
580 public:
581 // Capacity:
582 /// Returns the number of characters in the string, not including any
583 /// null-termination.
584 size_type
585 size() const
586 { return _M_rep()->_M_length; }
587
588 /// Returns the number of characters in the string, not including any
589 /// null-termination.
590 size_type
591 length() const
592 { return _M_rep()->_M_length; }
593
594 /// Returns the size() of the largest possible %string.
595 size_type
596 max_size() const
597 { return _Rep::_S_max_size; }
598
599 /**
600 * @brief Resizes the %string to the specified number of characters.
601 * @param n Number of characters the %string should contain.
602 * @param c Character to fill any new elements.
603 *
604 * This function will %resize the %string to the specified
605 * number of characters. If the number is smaller than the
606 * %string's current size the %string is truncated, otherwise
607 * the %string is extended and new elements are set to @a c.
608 */
609 void
610 resize(size_type __n, _CharT __c);
611
612 /**
613 * @brief Resizes the %string to the specified number of characters.
614 * @param n Number of characters the %string should contain.
615 *
616 * This function will resize the %string to the specified length. If
617 * the new size is smaller than the %string's current size the %string
618 * is truncated, otherwise the %string is extended and new characters
619 * are default-constructed. For basic types such as char, this means
620 * setting them to 0.
621 */
622 void
623 resize(size_type __n)
624 { this->resize(__n, _CharT()); }
625
626 /**
627 * Returns the total number of characters that the %string can hold
628 * before needing to allocate more memory.
629 */
630 size_type
631 capacity() const
632 { return _M_rep()->_M_capacity; }
633
634 /**
635 * @brief Attempt to preallocate enough memory for specified number of
636 * characters.
637 * @param n Number of characters required.
638 * @throw std::length_error If @a n exceeds @c max_size().
639 *
640 * This function attempts to reserve enough memory for the
641 * %string to hold the specified number of characters. If the
642 * number requested is more than max_size(), length_error is
643 * thrown.
644 *
645 * The advantage of this function is that if optimal code is a
646 * necessity and the user can determine the string length that will be
647 * required, the user can reserve the memory in %advance, and thus
648 * prevent a possible reallocation of memory and copying of %string
649 * data.
650 */
651 void
652 reserve(size_type __res_arg = 0);
653
654 /**
655 * Erases the string, making it empty.
656 */
657 void
658 clear()
659 { _M_mutate(0, this->size(), 0); }
660
661 /**
662 * Returns true if the %string is empty. Equivalent to *this == "".
663 */
664 bool
665 empty() const
666 { return this->size() == 0; }
667
668 // Element access:
669 /**
670 * @brief Subscript access to the data contained in the %string.
671 * @param n The index of the character to access.
672 * @return Read-only (constant) reference to the character.
673 *
674 * This operator allows for easy, array-style, data access.
675 * Note that data access with this operator is unchecked and
676 * out_of_range lookups are not defined. (For checked lookups
677 * see at().)
678 */
679 const_reference
680 operator[] (size_type __pos) const
681 {
682 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
683 return _M_data()[__pos];
684 }
685
686 /**
687 * @brief Subscript access to the data contained in the %string.
688 * @param n The index of the character to access.
689 * @return Read/write reference to the character.
690 *
691 * This operator allows for easy, array-style, data access.
692 * Note that data access with this operator is unchecked and
693 * out_of_range lookups are not defined. (For checked lookups
694 * see at().) Unshares the string.
695 */
696 reference
697 operator[](size_type __pos)
698 {
699 _GLIBCXX_DEBUG_ASSERT(__pos < size());
700 _M_leak();
701 return _M_data()[__pos];
702 }
703
704 /**
705 * @brief Provides access to the data contained in the %string.
706 * @param n The index of the character to access.
707 * @return Read-only (const) reference to the character.
708 * @throw std::out_of_range If @a n is an invalid index.
709 *
710 * This function provides for safer data access. The parameter is
711 * first checked that it is in the range of the string. The function
712 * throws out_of_range if the check fails.
713 */
714 const_reference
715 at(size_type __n) const
716 {
717 if (__n >= this->size())
718 __throw_out_of_range(__N("basic_string::at"));
719 return _M_data()[__n];
720 }
721
722 /**
723 * @brief Provides access to the data contained in the %string.
724 * @param n The index of the character to access.
725 * @return Read/write reference to the character.
726 * @throw std::out_of_range If @a n is an invalid index.
727 *
728 * This function provides for safer data access. The parameter is
729 * first checked that it is in the range of the string. The function
730 * throws out_of_range if the check fails. Success results in
731 * unsharing the string.
732 */
733 reference
734 at(size_type __n)
735 {
736 if (__n >= size())
737 __throw_out_of_range(__N("basic_string::at"));
738 _M_leak();
739 return _M_data()[__n];
740 }
741
742 // Modifiers:
743 /**
744 * @brief Append a string to this string.
745 * @param str The string to append.
746 * @return Reference to this string.
747 */
748 basic_string&
749 operator+=(const basic_string& __str)
750 { return this->append(__str); }
751
752 /**
753 * @brief Append a C string.
754 * @param s The C string to append.
755 * @return Reference to this string.
756 */
757 basic_string&
758 operator+=(const _CharT* __s)
759 { return this->append(__s); }
760
761 /**
762 * @brief Append a character.
763 * @param s The character to append.
764 * @return Reference to this string.
765 */
766 basic_string&
767 operator+=(_CharT __c)
768 {
769 this->push_back(__c);
770 return *this;
771 }
772
773 /**
774 * @brief Append a string to this string.
775 * @param str The string to append.
776 * @return Reference to this string.
777 */
778 basic_string&
779 append(const basic_string& __str)
780 {
781 const size_type __size = __str.size();
782 if (__size)
783 {
784 const size_type __len = __size + this->size();
785 if (__len > this->capacity() || _M_rep()->_M_is_shared())
786 this->reserve(__len);
787 _M_copy(_M_data() + this->size(), __str._M_data(), __size);
788 _M_rep()->_M_set_length_and_sharable(__len);
789 }
790 return *this;
791 }
792
793 /**
794 * @brief Append a substring.
795 * @param str The string to append.
796 * @param pos Index of the first character of str to append.
797 * @param n The number of characters to append.
798 * @return Reference to this string.
799 * @throw std::out_of_range if @a pos is not a valid index.
800 *
801 * This function appends @a n characters from @a str starting at @a pos
802 * to this string. If @a n is is larger than the number of available
803 * characters in @a str, the remainder of @a str is appended.
804 */
805 basic_string&
806 append(const basic_string& __str, size_type __pos, size_type __n);
807
808 /**
809 * @brief Append a C substring.
810 * @param s The C string to append.
811 * @param n The number of characters to append.
812 * @return Reference to this string.
813 */
814 basic_string&
815 append(const _CharT* __s, size_type __n);
816
817 /**
818 * @brief Append a C string.
819 * @param s The C string to append.
820 * @return Reference to this string.
821 */
822 basic_string&
823 append(const _CharT* __s)
824 {
825 __glibcxx_requires_string(__s);
826 return this->append(__s, traits_type::length(__s));
827 }
828
829 /**
830 * @brief Append multiple characters.
831 * @param n The number of characters to append.
832 * @param c The character to use.
833 * @return Reference to this string.
834 *
835 * Appends n copies of c to this string.
836 */
837 basic_string&
838 append(size_type __n, _CharT __c)
839 {
840 if (__n)
841 {
842 _M_check_length(size_type(0), __n, "basic_string::append");
843 const size_type __len = __n + this->size();
844 if (__len > this->capacity() || _M_rep()->_M_is_shared())
845 this->reserve(__len);
846 _M_assign(_M_data() + this->size(), __n, __c);
847 _M_rep()->_M_set_length_and_sharable(__len);
848 }
849 return *this;
850 }
851
852 /**
853 * @brief Append a range of characters.
854 * @param first Iterator referencing the first character to append.
855 * @param last Iterator marking the end of the range.
856 * @return Reference to this string.
857 *
858 * Appends characters in the range [first,last) to this string.
859 */
860 template<class _InputIterator>
861 basic_string&
862 append(_InputIterator __first, _InputIterator __last)
863 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
864
865 /**
866 * @brief Append a single character.
867 * @param c Character to append.
868 */
869 void
870 push_back(_CharT __c)
871 {
872 const size_type __len = 1 + this->size();
873 if (__len > this->capacity() || _M_rep()->_M_is_shared())
874 this->reserve(__len);
875 traits_type::assign(_M_data()[this->size()], __c);
876 _M_rep()->_M_set_length_and_sharable(__len);
877 }
878
879 /**
880 * @brief Set value to contents of another string.
881 * @param str Source string to use.
882 * @return Reference to this string.
883 */
884 basic_string&
885 assign(const basic_string& __str)
886 {
887 if (_M_rep() != __str._M_rep())
888 {
889 // XXX MT
890 const allocator_type __a = this->get_allocator();
891 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
892 _M_rep()->_M_dispose(__a);
893 _M_data(__tmp);
894 }
895 return *this;
896 }
897
898 /**
899 * @brief Set value to a substring of a string.
900 * @param str The string to use.
901 * @param pos Index of the first character of str.
902 * @param n Number of characters to use.
903 * @return Reference to this string.
904 * @throw std::out_of_range if @a pos is not a valid index.
905 *
906 * This function sets this string to the substring of @a str consisting
907 * of @a n characters at @a pos. If @a n is is larger than the number
908 * of available characters in @a str, the remainder of @a str is used.
909 */
910 basic_string&
911 assign(const basic_string& __str, size_type __pos, size_type __n)
912 { return this->assign(__str._M_data()
913 + __str._M_check(__pos, "basic_string::assign"),
914 __str._M_limit(__pos, __n)); }
915
916 /**
917 * @brief Set value to a C substring.
918 * @param s The C string to use.
919 * @param n Number of characters to use.
920 * @return Reference to this string.
921 *
922 * This function sets the value of this string to the first @a n
923 * characters of @a s. If @a n is is larger than the number of
924 * available characters in @a s, the remainder of @a s is used.
925 */
926 basic_string&
927 assign(const _CharT* __s, size_type __n);
928
929 /**
930 * @brief Set value to contents of a C string.
931 * @param s The C string to use.
932 * @return Reference to this string.
933 *
934 * This function sets the value of this string to the value of @a s.
935 * The data is copied, so there is no dependence on @a s once the
936 * function returns.
937 */
938 basic_string&
939 assign(const _CharT* __s)
940 {
941 __glibcxx_requires_string(__s);
942 return this->assign(__s, traits_type::length(__s));
943 }
944
945 /**
946 * @brief Set value to multiple characters.
947 * @param n Length of the resulting string.
948 * @param c The character to use.
949 * @return Reference to this string.
950 *
951 * This function sets the value of this string to @a n copies of
952 * character @a c.
953 */
954 basic_string&
955 assign(size_type __n, _CharT __c)
956 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
957
958 /**
959 * @brief Set value to a range of characters.
960 * @param first Iterator referencing the first character to append.
961 * @param last Iterator marking the end of the range.
962 * @return Reference to this string.
963 *
964 * Sets value of string to characters in the range [first,last).
965 */
966 template<class _InputIterator>
967 basic_string&
968 assign(_InputIterator __first, _InputIterator __last)
969 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
970
971 /**
972 * @brief Insert multiple characters.
973 * @param p Iterator referencing location in string to insert at.
974 * @param n Number of characters to insert
975 * @param c The character to insert.
976 * @throw std::length_error If new length exceeds @c max_size().
977 *
978 * Inserts @a n copies of character @a c starting at the position
979 * referenced by iterator @a p. If adding characters causes the length
980 * to exceed max_size(), length_error is thrown. The value of the
981 * string doesn't change if an error is thrown.
982 */
983 void
984 insert(iterator __p, size_type __n, _CharT __c)
985 { this->replace(__p, __p, __n, __c); }
986
987 /**
988 * @brief Insert a range of characters.
989 * @param p Iterator referencing location in string to insert at.
990 * @param beg Start of range.
991 * @param end End of range.
992 * @throw std::length_error If new length exceeds @c max_size().
993 *
994 * Inserts characters in range [beg,end). If adding characters causes
995 * the length to exceed max_size(), length_error is thrown. The value
996 * of the string doesn't change if an error is thrown.
997 */
998 template<class _InputIterator>
999 void
1000 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1001 { this->replace(__p, __p, __beg, __end); }
1002
1003 /**
1004 * @brief Insert value of a string.
1005 * @param pos1 Iterator referencing location in string to insert at.
1006 * @param str The string to insert.
1007 * @return Reference to this string.
1008 * @throw std::length_error If new length exceeds @c max_size().
1009 *
1010 * Inserts value of @a str starting at @a pos1. If adding characters
1011 * causes the length to exceed max_size(), length_error is thrown. The
1012 * value of the string doesn't change if an error is thrown.
1013 */
1014 basic_string&
1015 insert(size_type __pos1, const basic_string& __str)
1016 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1017
1018 /**
1019 * @brief Insert a substring.
1020 * @param pos1 Iterator referencing location in string to insert at.
1021 * @param str The string to insert.
1022 * @param pos2 Start of characters in str to insert.
1023 * @param n Number of characters to insert.
1024 * @return Reference to this string.
1025 * @throw std::length_error If new length exceeds @c max_size().
1026 * @throw std::out_of_range If @a pos1 > size() or
1027 * @a pos2 > @a str.size().
1028 *
1029 * Starting at @a pos1, insert @a n character of @a str beginning with
1030 * @a pos2. If adding characters causes the length to exceed
1031 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
1032 * this string or @a pos2 is beyond the end of @a str, out_of_range is
1033 * thrown. The value of the string doesn't change if an error is
1034 * thrown.
1035 */
1036 basic_string&
1037 insert(size_type __pos1, const basic_string& __str,
1038 size_type __pos2, size_type __n)
1039 { return this->insert(__pos1, __str._M_data()
1040 + __str._M_check(__pos2, "basic_string::insert"),
1041 __str._M_limit(__pos2, __n)); }
1042
1043 /**
1044 * @brief Insert a C substring.
1045 * @param pos Iterator referencing location in string to insert at.
1046 * @param s The C string to insert.
1047 * @param n The number of characters to insert.
1048 * @return Reference to this string.
1049 * @throw std::length_error If new length exceeds @c max_size().
1050 * @throw std::out_of_range If @a pos is beyond the end of this
1051 * string.
1052 *
1053 * Inserts the first @a n characters of @a s starting at @a pos. If
1054 * adding characters causes the length to exceed max_size(),
1055 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1056 * thrown. The value of the string doesn't change if an error is
1057 * thrown.
1058 */
1059 basic_string&
1060 insert(size_type __pos, const _CharT* __s, size_type __n);
1061
1062 /**
1063 * @brief Insert a C string.
1064 * @param pos Iterator referencing location in string to insert at.
1065 * @param s The C string to insert.
1066 * @return Reference to this string.
1067 * @throw std::length_error If new length exceeds @c max_size().
1068 * @throw std::out_of_range If @a pos is beyond the end of this
1069 * string.
1070 *
1071 * Inserts the first @a n characters of @a s starting at @a pos. If
1072 * adding characters causes the length to exceed max_size(),
1073 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1074 * thrown. The value of the string doesn't change if an error is
1075 * thrown.
1076 */
1077 basic_string&
1078 insert(size_type __pos, const _CharT* __s)
1079 {
1080 __glibcxx_requires_string(__s);
1081 return this->insert(__pos, __s, traits_type::length(__s));
1082 }
1083
1084 /**
1085 * @brief Insert multiple characters.
1086 * @param pos Index in string to insert at.
1087 * @param n Number of characters to insert
1088 * @param c The character to insert.
1089 * @return Reference to this string.
1090 * @throw std::length_error If new length exceeds @c max_size().
1091 * @throw std::out_of_range If @a pos is beyond the end of this
1092 * string.
1093 *
1094 * Inserts @a n copies of character @a c starting at index @a pos. If
1095 * adding characters causes the length to exceed max_size(),
1096 * length_error is thrown. If @a pos > length(), out_of_range is
1097 * thrown. The value of the string doesn't change if an error is
1098 * thrown.
1099 */
1100 basic_string&
1101 insert(size_type __pos, size_type __n, _CharT __c)
1102 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1103 size_type(0), __n, __c); }
1104
1105 /**
1106 * @brief Insert one character.
1107 * @param p Iterator referencing position in string to insert at.
1108 * @param c The character to insert.
1109 * @return Iterator referencing newly inserted char.
1110 * @throw std::length_error If new length exceeds @c max_size().
1111 *
1112 * Inserts character @a c at position referenced by @a p. If adding
1113 * character causes the length to exceed max_size(), length_error is
1114 * thrown. If @a p is beyond end of string, out_of_range is thrown.
1115 * The value of the string doesn't change if an error is thrown.
1116 */
1117 iterator
1118 insert(iterator __p, _CharT __c)
1119 {
1120 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1121 const size_type __pos = __p - _M_ibegin();
1122 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1123 _M_rep()->_M_set_leaked();
1124 return this->_M_ibegin() + __pos;
1125 }
1126
1127 /**
1128 * @brief Remove characters.
1129 * @param pos Index of first character to remove (default 0).
1130 * @param n Number of characters to remove (default remainder).
1131 * @return Reference to this string.
1132 * @throw std::out_of_range If @a pos is beyond the end of this
1133 * string.
1134 *
1135 * Removes @a n characters from this string starting at @a pos. The
1136 * length of the string is reduced by @a n. If there are < @a n
1137 * characters to remove, the remainder of the string is truncated. If
1138 * @a p is beyond end of string, out_of_range is thrown. The value of
1139 * the string doesn't change if an error is thrown.
1140 */
1141 basic_string&
1142 erase(size_type __pos = 0, size_type __n = npos)
1143 { return _M_replace_safe(_M_check(__pos, "basic_string::erase"),
1144 _M_limit(__pos, __n), NULL, size_type(0)); }
1145
1146 /**
1147 * @brief Remove one character.
1148 * @param position Iterator referencing the character to remove.
1149 * @return iterator referencing same location after removal.
1150 *
1151 * Removes the character at @a position from this string. The value
1152 * of the string doesn't change if an error is thrown.
1153 */
1154 iterator
1155 erase(iterator __position)
1156 {
1157 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1158 && __position < _M_iend());
1159 const size_type __pos = __position - _M_ibegin();
1160 _M_replace_safe(__pos, size_type(1), NULL, size_type(0));
1161 _M_rep()->_M_set_leaked();
1162 return _M_ibegin() + __pos;
1163 }
1164
1165 /**
1166 * @brief Remove a range of characters.
1167 * @param first Iterator referencing the first character to remove.
1168 * @param last Iterator referencing the end of the range.
1169 * @return Iterator referencing location of first after removal.
1170 *
1171 * Removes the characters in the range [first,last) from this string.
1172 * The value of the string doesn't change if an error is thrown.
1173 */
1174 iterator
1175 erase(iterator __first, iterator __last)
1176 {
1177 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
1178 && __last <= _M_iend());
1179 const size_type __pos = __first - _M_ibegin();
1180 _M_replace_safe(__pos, __last - __first, NULL, size_type(0));
1181 _M_rep()->_M_set_leaked();
1182 return _M_ibegin() + __pos;
1183 }
1184
1185 /**
1186 * @brief Replace characters with value from another string.
1187 * @param pos Index of first character to replace.
1188 * @param n Number of characters to be replaced.
1189 * @param str String to insert.
1190 * @return Reference to this string.
1191 * @throw std::out_of_range If @a pos is beyond the end of this
1192 * string.
1193 * @throw std::length_error If new length exceeds @c max_size().
1194 *
1195 * Removes the characters in the range [pos,pos+n) from this string.
1196 * In place, the value of @a str is inserted. If @a pos is beyond end
1197 * of string, out_of_range is thrown. If the length of the result
1198 * exceeds max_size(), length_error is thrown. The value of the string
1199 * doesn't change if an error is thrown.
1200 */
1201 basic_string&
1202 replace(size_type __pos, size_type __n, const basic_string& __str)
1203 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1204
1205 /**
1206 * @brief Replace characters with value from another string.
1207 * @param pos1 Index of first character to replace.
1208 * @param n1 Number of characters to be replaced.
1209 * @param str String to insert.
1210 * @param pos2 Index of first character of str to use.
1211 * @param n2 Number of characters from str to use.
1212 * @return Reference to this string.
1213 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1214 * str.size().
1215 * @throw std::length_error If new length exceeds @c max_size().
1216 *
1217 * Removes the characters in the range [pos1,pos1 + n) from this
1218 * string. In place, the value of @a str is inserted. If @a pos is
1219 * beyond end of string, out_of_range is thrown. If the length of the
1220 * result exceeds max_size(), length_error is thrown. The value of the
1221 * string doesn't change if an error is thrown.
1222 */
1223 basic_string&
1224 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1225 size_type __pos2, size_type __n2)
1226 { return this->replace(__pos1, __n1, __str._M_data()
1227 + __str._M_check(__pos2, "basic_string::replace"),
1228 __str._M_limit(__pos2, __n2)); }
1229
1230 /**
1231 * @brief Replace characters with value of a C substring.
1232 * @param pos Index of first character to replace.
1233 * @param n1 Number of characters to be replaced.
1234 * @param str C string to insert.
1235 * @param n2 Number of characters from str to use.
1236 * @return Reference to this string.
1237 * @throw std::out_of_range If @a pos1 > size().
1238 * @throw std::length_error If new length exceeds @c max_size().
1239 *
1240 * Removes the characters in the range [pos,pos + n1) from this string.
1241 * In place, the first @a n2 characters of @a str are inserted, or all
1242 * of @a str if @a n2 is too large. If @a pos is beyond end of string,
1243 * out_of_range is thrown. If the length of result exceeds max_size(),
1244 * length_error is thrown. The value of the string doesn't change if
1245 * an error is thrown.
1246 */
1247 basic_string&
1248 replace(size_type __pos, size_type __n1, const _CharT* __s,
1249 size_type __n2);
1250
1251 /**
1252 * @brief Replace characters with value of a C string.
1253 * @param pos Index of first character to replace.
1254 * @param n1 Number of characters to be replaced.
1255 * @param str C string to insert.
1256 * @return Reference to this string.
1257 * @throw std::out_of_range If @a pos > size().
1258 * @throw std::length_error If new length exceeds @c max_size().
1259 *
1260 * Removes the characters in the range [pos,pos + n1) from this string.
1261 * In place, the first @a n characters of @a str are inserted. If @a
1262 * pos is beyond end of string, out_of_range is thrown. If the length
1263 * of result exceeds max_size(), length_error is thrown. The value of
1264 * the string doesn't change if an error is thrown.
1265 */
1266 basic_string&
1267 replace(size_type __pos, size_type __n1, const _CharT* __s)
1268 {
1269 __glibcxx_requires_string(__s);
1270 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1271 }
1272
1273 /**
1274 * @brief Replace characters with multiple characters.
1275 * @param pos Index of first character to replace.
1276 * @param n1 Number of characters to be replaced.
1277 * @param n2 Number of characters to insert.
1278 * @param c Character to insert.
1279 * @return Reference to this string.
1280 * @throw std::out_of_range If @a pos > size().
1281 * @throw std::length_error If new length exceeds @c max_size().
1282 *
1283 * Removes the characters in the range [pos,pos + n1) from this string.
1284 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1285 * end of string, out_of_range is thrown. If the length of result
1286 * exceeds max_size(), length_error is thrown. The value of the string
1287 * doesn't change if an error is thrown.
1288 */
1289 basic_string&
1290 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1291 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1292 _M_limit(__pos, __n1), __n2, __c); }
1293
1294 /**
1295 * @brief Replace range of characters with string.
1296 * @param i1 Iterator referencing start of range to replace.
1297 * @param i2 Iterator referencing end of range to replace.
1298 * @param str String value to insert.
1299 * @return Reference to this string.
1300 * @throw std::length_error If new length exceeds @c max_size().
1301 *
1302 * Removes the characters in the range [i1,i2). In place, the value of
1303 * @a str is inserted. If the length of result exceeds max_size(),
1304 * length_error is thrown. The value of the string doesn't change if
1305 * an error is thrown.
1306 */
1307 basic_string&
1308 replace(iterator __i1, iterator __i2, const basic_string& __str)
1309 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1310
1311 /**
1312 * @brief Replace range of characters with C substring.
1313 * @param i1 Iterator referencing start of range to replace.
1314 * @param i2 Iterator referencing end of range to replace.
1315 * @param s C string value to insert.
1316 * @param n Number of characters from s to insert.
1317 * @return Reference to this string.
1318 * @throw std::length_error If new length exceeds @c max_size().
1319 *
1320 * Removes the characters in the range [i1,i2). In place, the first @a
1321 * n characters of @a s are inserted. If the length of result exceeds
1322 * max_size(), length_error is thrown. The value of the string doesn't
1323 * change if an error is thrown.
1324 */
1325 basic_string&
1326 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1327 {
1328 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1329 && __i2 <= _M_iend());
1330 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1331 }
1332
1333 /**
1334 * @brief Replace range of characters with C string.
1335 * @param i1 Iterator referencing start of range to replace.
1336 * @param i2 Iterator referencing end of range to replace.
1337 * @param s C string value to insert.
1338 * @return Reference to this string.
1339 * @throw std::length_error If new length exceeds @c max_size().
1340 *
1341 * Removes the characters in the range [i1,i2). In place, the
1342 * characters of @a s are inserted. If the length of result exceeds
1343 * max_size(), length_error is thrown. The value of the string doesn't
1344 * change if an error is thrown.
1345 */
1346 basic_string&
1347 replace(iterator __i1, iterator __i2, const _CharT* __s)
1348 {
1349 __glibcxx_requires_string(__s);
1350 return this->replace(__i1, __i2, __s, traits_type::length(__s));
1351 }
1352
1353 /**
1354 * @brief Replace range of characters with multiple characters
1355 * @param i1 Iterator referencing start of range to replace.
1356 * @param i2 Iterator referencing end of range to replace.
1357 * @param n Number of characters to insert.
1358 * @param c Character to insert.
1359 * @return Reference to this string.
1360 * @throw std::length_error If new length exceeds @c max_size().
1361 *
1362 * Removes the characters in the range [i1,i2). In place, @a n copies
1363 * of @a c are inserted. If the length of result exceeds max_size(),
1364 * length_error is thrown. The value of the string doesn't change if
1365 * an error is thrown.
1366 */
1367 basic_string&
1368 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1369 {
1370 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1371 && __i2 <= _M_iend());
1372 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1373 }
1374
1375 /**
1376 * @brief Replace range of characters with range.
1377 * @param i1 Iterator referencing start of range to replace.
1378 * @param i2 Iterator referencing end of range to replace.
1379 * @param k1 Iterator referencing start of range to insert.
1380 * @param k2 Iterator referencing end of range to insert.
1381 * @return Reference to this string.
1382 * @throw std::length_error If new length exceeds @c max_size().
1383 *
1384 * Removes the characters in the range [i1,i2). In place, characters
1385 * in the range [k1,k2) are inserted. If the length of result exceeds
1386 * max_size(), length_error is thrown. The value of the string doesn't
1387 * change if an error is thrown.
1388 */
1389 template<class _InputIterator>
1390 basic_string&
1391 replace(iterator __i1, iterator __i2,
1392 _InputIterator __k1, _InputIterator __k2)
1393 {
1394 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1395 && __i2 <= _M_iend());
1396 __glibcxx_requires_valid_range(__k1, __k2);
1397 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
1398 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1399 }
1400
1401 // Specializations for the common case of pointer and iterator:
1402 // useful to avoid the overhead of temporary buffering in _M_replace.
1403 basic_string&
1404 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1405 {
1406 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1407 && __i2 <= _M_iend());
1408 __glibcxx_requires_valid_range(__k1, __k2);
1409 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1410 __k1, __k2 - __k1);
1411 }
1412
1413 basic_string&
1414 replace(iterator __i1, iterator __i2,
1415 const _CharT* __k1, const _CharT* __k2)
1416 {
1417 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1418 && __i2 <= _M_iend());
1419 __glibcxx_requires_valid_range(__k1, __k2);
1420 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1421 __k1, __k2 - __k1);
1422 }
1423
1424 basic_string&
1425 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1426 {
1427 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1428 && __i2 <= _M_iend());
1429 __glibcxx_requires_valid_range(__k1, __k2);
1430 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1431 __k1.base(), __k2 - __k1);
1432 }
1433
1434 basic_string&
1435 replace(iterator __i1, iterator __i2,
1436 const_iterator __k1, const_iterator __k2)
1437 {
1438 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1439 && __i2 <= _M_iend());
1440 __glibcxx_requires_valid_range(__k1, __k2);
1441 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1442 __k1.base(), __k2 - __k1);
1443 }
1444
1445 private:
1446 template<class _Integer>
1447 basic_string&
1448 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1449 _Integer __val, __true_type)
1450 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1451
1452 template<class _InputIterator>
1453 basic_string&
1454 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1455 _InputIterator __k2, __false_type);
1456
1457 basic_string&
1458 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1459 _CharT __c)
1460 {
1461 _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
1462 _M_mutate(__pos1, __n1, __n2);
1463 if (__n2)
1464 _M_assign(_M_data() + __pos1, __n2, __c);
1465 return *this;
1466 }
1467
1468 basic_string&
1469 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1470 size_type __n2)
1471 {
1472 _M_mutate(__pos1, __n1, __n2);
1473 if (__n2)
1474 _M_copy(_M_data() + __pos1, __s, __n2);
1475 return *this;
1476 }
1477
1478 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1479 // requires special behaviour if _InIter is an integral type
1480 template<class _InIterator>
1481 static _CharT*
1482 _S_construct_aux(_InIterator __beg, _InIterator __end,
1483 const _Alloc& __a, __false_type)
1484 {
1485 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1486 return _S_construct(__beg, __end, __a, _Tag());
1487 }
1488
1489 template<class _InIterator>
1490 static _CharT*
1491 _S_construct_aux(_InIterator __beg, _InIterator __end,
1492 const _Alloc& __a, __true_type)
1493 { return _S_construct(static_cast<size_type>(__beg),
1494 static_cast<value_type>(__end), __a); }
1495
1496 template<class _InIterator>
1497 static _CharT*
1498 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1499 {
1500 typedef typename _Is_integer<_InIterator>::_Integral _Integral;
1501 return _S_construct_aux(__beg, __end, __a, _Integral());
1502 }
1503
1504 // For Input Iterators, used in istreambuf_iterators, etc.
1505 template<class _InIterator>
1506 static _CharT*
1507 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1508 input_iterator_tag);
1509
1510 // For forward_iterators up to random_access_iterators, used for
1511 // string::iterator, _CharT*, etc.
1512 template<class _FwdIterator>
1513 static _CharT*
1514 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1515 forward_iterator_tag);
1516
1517 static _CharT*
1518 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1519
1520 public:
1521
1522 /**
1523 * @brief Copy substring into C string.
1524 * @param s C string to copy value into.
1525 * @param n Number of characters to copy.
1526 * @param pos Index of first character to copy.
1527 * @return Number of characters actually copied
1528 * @throw std::out_of_range If pos > size().
1529 *
1530 * Copies up to @a n characters starting at @a pos into the C string @a
1531 * s. If @a pos is greater than size(), out_of_range is thrown.
1532 */
1533 size_type
1534 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1535
1536 /**
1537 * @brief Swap contents with another string.
1538 * @param s String to swap with.
1539 *
1540 * Exchanges the contents of this string with that of @a s in constant
1541 * time.
1542 */
1543 void
1544 swap(basic_string& __s);
1545
1546 // String operations:
1547 /**
1548 * @brief Return const pointer to null-terminated contents.
1549 *
1550 * This is a handle to internal data. Do not modify or dire things may
1551 * happen.
1552 */
1553 const _CharT*
1554 c_str() const
1555 { return _M_data(); }
1556
1557 /**
1558 * @brief Return const pointer to contents.
1559 *
1560 * This is a handle to internal data. Do not modify or dire things may
1561 * happen.
1562 */
1563 const _CharT*
1564 data() const
1565 { return _M_data(); }
1566
1567 /**
1568 * @brief Return copy of allocator used to construct this string.
1569 */
1570 allocator_type
1571 get_allocator() const
1572 { return _M_dataplus; }
1573
1574 /**
1575 * @brief Find position of a C substring.
1576 * @param s C string to locate.
1577 * @param pos Index of character to search from.
1578 * @param n Number of characters from @a s to search for.
1579 * @return Index of start of first occurrence.
1580 *
1581 * Starting from @a pos, searches forward for the first @a n characters
1582 * in @a s within this string. If found, returns the index where it
1583 * begins. If not found, returns npos.
1584 */
1585 size_type
1586 find(const _CharT* __s, size_type __pos, size_type __n) const;
1587
1588 /**
1589 * @brief Find position of a string.
1590 * @param str String to locate.
1591 * @param pos Index of character to search from (default 0).
1592 * @return Index of start of first occurrence.
1593 *
1594 * Starting from @a pos, searches forward for value of @a str within
1595 * this string. If found, returns the index where it begins. If not
1596 * found, returns npos.
1597 */
1598 size_type
1599 find(const basic_string& __str, size_type __pos = 0) const
1600 { return this->find(__str.data(), __pos, __str.size()); }
1601
1602 /**
1603 * @brief Find position of a C string.
1604 * @param s C string to locate.
1605 * @param pos Index of character to search from (default 0).
1606 * @return Index of start of first occurrence.
1607 *
1608 * Starting from @a pos, searches forward for the value of @a s within
1609 * this string. If found, returns the index where it begins. If not
1610 * found, returns npos.
1611 */
1612 size_type
1613 find(const _CharT* __s, size_type __pos = 0) const
1614 {
1615 __glibcxx_requires_string(__s);
1616 return this->find(__s, __pos, traits_type::length(__s));
1617 }
1618
1619 /**
1620 * @brief Find position of a character.
1621 * @param c Character to locate.
1622 * @param pos Index of character to search from (default 0).
1623 * @return Index of first occurrence.
1624 *
1625 * Starting from @a pos, searches forward for @a c within this string.
1626 * If found, returns the index where it was found. If not found,
1627 * returns npos.
1628 */
1629 size_type
1630 find(_CharT __c, size_type __pos = 0) const;
1631
1632 /**
1633 * @brief Find last position of a string.
1634 * @param str String to locate.
1635 * @param pos Index of character to search back from (default end).
1636 * @return Index of start of last occurrence.
1637 *
1638 * Starting from @a pos, searches backward for value of @a str within
1639 * this string. If found, returns the index where it begins. If not
1640 * found, returns npos.
1641 */
1642 size_type
1643 rfind(const basic_string& __str, size_type __pos = npos) const
1644 { return this->rfind(__str.data(), __pos, __str.size()); }
1645
1646 /**
1647 * @brief Find last position of a C substring.
1648 * @param s C string to locate.
1649 * @param pos Index of character to search back from.
1650 * @param n Number of characters from s to search for.
1651 * @return Index of start of last occurrence.
1652 *
1653 * Starting from @a pos, searches backward for the first @a n
1654 * characters in @a s within this string. If found, returns the index
1655 * where it begins. If not found, returns npos.
1656 */
1657 size_type
1658 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1659
1660 /**
1661 * @brief Find last position of a C string.
1662 * @param s C string to locate.
1663 * @param pos Index of character to start search at (default 0).
1664 * @return Index of start of last occurrence.
1665 *
1666 * Starting from @a pos, searches backward for the value of @a s within
1667 * this string. If found, returns the index where it begins. If not
1668 * found, returns npos.
1669 */
1670 size_type
1671 rfind(const _CharT* __s, size_type __pos = npos) const
1672 {
1673 __glibcxx_requires_string(__s);
1674 return this->rfind(__s, __pos, traits_type::length(__s));
1675 }
1676
1677 /**
1678 * @brief Find last position of a character.
1679 * @param c Character to locate.
1680 * @param pos Index of character to search back from (default 0).
1681 * @return Index of last occurrence.
1682 *
1683 * Starting from @a pos, searches backward for @a c within this string.
1684 * If found, returns the index where it was found. If not found,
1685 * returns npos.
1686 */
1687 size_type
1688 rfind(_CharT __c, size_type __pos = npos) const;
1689
1690 /**
1691 * @brief Find position of a character of string.
1692 * @param str String containing characters to locate.
1693 * @param pos Index of character to search from (default 0).
1694 * @return Index of first occurrence.
1695 *
1696 * Starting from @a pos, searches forward for one of the characters of
1697 * @a str within this string. If found, returns the index where it was
1698 * found. If not found, returns npos.
1699 */
1700 size_type
1701 find_first_of(const basic_string& __str, size_type __pos = 0) const
1702 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1703
1704 /**
1705 * @brief Find position of a character of C substring.
1706 * @param s String containing characters to locate.
1707 * @param pos Index of character to search from (default 0).
1708 * @param n Number of characters from s to search for.
1709 * @return Index of first occurrence.
1710 *
1711 * Starting from @a pos, searches forward for one of the first @a n
1712 * characters of @a s within this string. If found, returns the index
1713 * where it was found. If not found, returns npos.
1714 */
1715 size_type
1716 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1717
1718 /**
1719 * @brief Find position of a character of C string.
1720 * @param s String containing characters to locate.
1721 * @param pos Index of character to search from (default 0).
1722 * @return Index of first occurrence.
1723 *
1724 * Starting from @a pos, searches forward for one of the characters of
1725 * @a s within this string. If found, returns the index where it was
1726 * found. If not found, returns npos.
1727 */
1728 size_type
1729 find_first_of(const _CharT* __s, size_type __pos = 0) const
1730 {
1731 __glibcxx_requires_string(__s);
1732 return this->find_first_of(__s, __pos, traits_type::length(__s));
1733 }
1734
1735 /**
1736 * @brief Find position of a character.
1737 * @param c Character to locate.
1738 * @param pos Index of character to search from (default 0).
1739 * @return Index of first occurrence.
1740 *
1741 * Starting from @a pos, searches forward for the character @a c within
1742 * this string. If found, returns the index where it was found. If
1743 * not found, returns npos.
1744 *
1745 * Note: equivalent to find(c, pos).
1746 */
1747 size_type
1748 find_first_of(_CharT __c, size_type __pos = 0) const
1749 { return this->find(__c, __pos); }
1750
1751 /**
1752 * @brief Find last position of a character of string.
1753 * @param str String containing characters to locate.
1754 * @param pos Index of character to search back from (default end).
1755 * @return Index of last occurrence.
1756 *
1757 * Starting from @a pos, searches backward for one of the characters of
1758 * @a str within this string. If found, returns the index where it was
1759 * found. If not found, returns npos.
1760 */
1761 size_type
1762 find_last_of(const basic_string& __str, size_type __pos = npos) const
1763 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1764
1765 /**
1766 * @brief Find last position of a character of C substring.
1767 * @param s C string containing characters to locate.
1768 * @param pos Index of character to search back from (default end).
1769 * @param n Number of characters from s to search for.
1770 * @return Index of last occurrence.
1771 *
1772 * Starting from @a pos, searches backward for one of the first @a n
1773 * characters of @a s within this string. If found, returns the index
1774 * where it was found. If not found, returns npos.
1775 */
1776 size_type
1777 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1778
1779 /**
1780 * @brief Find last position of a character of C string.
1781 * @param s C string containing characters to locate.
1782 * @param pos Index of character to search back from (default end).
1783 * @return Index of last occurrence.
1784 *
1785 * Starting from @a pos, searches backward for one of the characters of
1786 * @a s within this string. If found, returns the index where it was
1787 * found. If not found, returns npos.
1788 */
1789 size_type
1790 find_last_of(const _CharT* __s, size_type __pos = npos) const
1791 {
1792 __glibcxx_requires_string(__s);
1793 return this->find_last_of(__s, __pos, traits_type::length(__s));
1794 }
1795
1796 /**
1797 * @brief Find last position of a character.
1798 * @param c Character to locate.
1799 * @param pos Index of character to search back from (default 0).
1800 * @return Index of last occurrence.
1801 *
1802 * Starting from @a pos, searches backward for @a c within this string.
1803 * If found, returns the index where it was found. If not found,
1804 * returns npos.
1805 *
1806 * Note: equivalent to rfind(c, pos).
1807 */
1808 size_type
1809 find_last_of(_CharT __c, size_type __pos = npos) const
1810 { return this->rfind(__c, __pos); }
1811
1812 /**
1813 * @brief Find position of a character not in string.
1814 * @param str String containing characters to avoid.
1815 * @param pos Index of character to search from (default 0).
1816 * @return Index of first occurrence.
1817 *
1818 * Starting from @a pos, searches forward for a character not contained
1819 * in @a str within this string. If found, returns the index where it
1820 * was found. If not found, returns npos.
1821 */
1822 size_type
1823 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1824 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1825
1826 /**
1827 * @brief Find position of a character not in C substring.
1828 * @param s C string containing characters to avoid.
1829 * @param pos Index of character to search from (default 0).
1830 * @param n Number of characters from s to consider.
1831 * @return Index of first occurrence.
1832 *
1833 * Starting from @a pos, searches forward for a character not contained
1834 * in the first @a n characters of @a s within this string. If found,
1835 * returns the index where it was found. If not found, returns npos.
1836 */
1837 size_type
1838 find_first_not_of(const _CharT* __s, size_type __pos,
1839 size_type __n) const;
1840
1841 /**
1842 * @brief Find position of a character not in C string.
1843 * @param s C string containing characters to avoid.
1844 * @param pos Index of character to search from (default 0).
1845 * @return Index of first occurrence.
1846 *
1847 * Starting from @a pos, searches forward for a character not contained
1848 * in @a s within this string. If found, returns the index where it
1849 * was found. If not found, returns npos.
1850 */
1851 size_type
1852 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1853 {
1854 __glibcxx_requires_string(__s);
1855 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
1856 }
1857
1858 /**
1859 * @brief Find position of a different character.
1860 * @param c Character to avoid.
1861 * @param pos Index of character to search from (default 0).
1862 * @return Index of first occurrence.
1863 *
1864 * Starting from @a pos, searches forward for a character other than @a c
1865 * within this string. If found, returns the index where it was found.
1866 * If not found, returns npos.
1867 */
1868 size_type
1869 find_first_not_of(_CharT __c, size_type __pos = 0) const;
1870
1871 /**
1872 * @brief Find last position of a character not in string.
1873 * @param str String containing characters to avoid.
1874 * @param pos Index of character to search from (default 0).
1875 * @return Index of first occurrence.
1876 *
1877 * Starting from @a pos, searches backward for a character not
1878 * contained in @a str within this string. If found, returns the index
1879 * where it was found. If not found, returns npos.
1880 */
1881 size_type
1882 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1883 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1884
1885 /**
1886 * @brief Find last position of a character not in C substring.
1887 * @param s C string containing characters to avoid.
1888 * @param pos Index of character to search from (default 0).
1889 * @param n Number of characters from s to consider.
1890 * @return Index of first occurrence.
1891 *
1892 * Starting from @a pos, searches backward for a character not
1893 * contained in the first @a n characters of @a s within this string.
1894 * If found, returns the index where it was found. If not found,
1895 * returns npos.
1896 */
1897 size_type
1898 find_last_not_of(const _CharT* __s, size_type __pos,
1899 size_type __n) const;
1900 /**
1901 * @brief Find position of a character not in C string.
1902 * @param s C string containing characters to avoid.
1903 * @param pos Index of character to search from (default 0).
1904 * @return Index of first occurrence.
1905 *
1906 * Starting from @a pos, searches backward for a character not
1907 * contained in @a s within this string. If found, returns the index
1908 * where it was found. If not found, returns npos.
1909 */
1910 size_type
1911 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1912 {
1913 __glibcxx_requires_string(__s);
1914 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
1915 }
1916
1917 /**
1918 * @brief Find last position of a different character.
1919 * @param c Character to avoid.
1920 * @param pos Index of character to search from (default 0).
1921 * @return Index of first occurrence.
1922 *
1923 * Starting from @a pos, searches backward for a character other than
1924 * @a c within this string. If found, returns the index where it was
1925 * found. If not found, returns npos.
1926 */
1927 size_type
1928 find_last_not_of(_CharT __c, size_type __pos = npos) const;
1929
1930 /**
1931 * @brief Get a substring.
1932 * @param pos Index of first character (default 0).
1933 * @param n Number of characters in substring (default remainder).
1934 * @return The new string.
1935 * @throw std::out_of_range If pos > size().
1936 *
1937 * Construct and return a new string using the @a n characters starting
1938 * at @a pos. If the string is too short, use the remainder of the
1939 * characters. If @a pos is beyond the end of the string, out_of_range
1940 * is thrown.
1941 */
1942 basic_string
1943 substr(size_type __pos = 0, size_type __n = npos) const
1944 { return basic_string(*this,
1945 _M_check(__pos, "basic_string::substr"), __n); }
1946
1947 /**
1948 * @brief Compare to a string.
1949 * @param str String to compare against.
1950 * @return Integer < 0, 0, or > 0.
1951 *
1952 * Returns an integer < 0 if this string is ordered before @a str, 0 if
1953 * their values are equivalent, or > 0 if this string is ordered after
1954 * @a str. If the lengths of @a str and this string are different, the
1955 * shorter one is ordered first. If they are the same, returns the
1956 * result of traits::compare(data(),str.data(),size());
1957 */
1958 int
1959 compare(const basic_string& __str) const
1960 {
1961 const size_type __size = this->size();
1962 const size_type __osize = __str.size();
1963 const size_type __len = std::min(__size, __osize);
1964
1965 int __r = traits_type::compare(_M_data(), __str.data(), __len);
1966 if (!__r)
1967 __r = __size - __osize;
1968 return __r;
1969 }
1970
1971 /**
1972 * @brief Compare substring to a string.
1973 * @param pos Index of first character of substring.
1974 * @param n Number of characters in substring.
1975 * @param str String to compare against.
1976 * @return Integer < 0, 0, or > 0.
1977 *
1978 * Form the substring of this string from the @a n characters starting
1979 * at @a pos. Returns an integer < 0 if the substring is ordered
1980 * before @a str, 0 if their values are equivalent, or > 0 if the
1981 * substring is ordered after @a str. If the lengths @a of str and the
1982 * substring are different, the shorter one is ordered first. If they
1983 * are the same, returns the result of
1984 * traits::compare(substring.data(),str.data(),size());
1985 */
1986 int
1987 compare(size_type __pos, size_type __n, const basic_string& __str) const;
1988
1989 /**
1990 * @brief Compare substring to a substring.
1991 * @param pos1 Index of first character of substring.
1992 * @param n1 Number of characters in substring.
1993 * @param str String to compare against.
1994 * @param pos2 Index of first character of substring of str.
1995 * @param n2 Number of characters in substring of str.
1996 * @return Integer < 0, 0, or > 0.
1997 *
1998 * Form the substring of this string from the @a n1 characters starting
1999 * at @a pos1. Form the substring of @a str from the @a n2 characters
2000 * starting at @a pos2. Returns an integer < 0 if this substring is
2001 * ordered before the substring of @a str, 0 if their values are
2002 * equivalent, or > 0 if this substring is ordered after the substring
2003 * of @a str. If the lengths of the substring of @a str and this
2004 * substring are different, the shorter one is ordered first. If they
2005 * are the same, returns the result of
2006 * traits::compare(substring.data(),str.substr(pos2,n2).data(),size());
2007 */
2008 int
2009 compare(size_type __pos1, size_type __n1, const basic_string& __str,
2010 size_type __pos2, size_type __n2) const;
2011
2012 /**
2013 * @brief Compare to a C string.
2014 * @param s C string to compare against.
2015 * @return Integer < 0, 0, or > 0.
2016 *
2017 * Returns an integer < 0 if this string is ordered before @a s, 0 if
2018 * their values are equivalent, or > 0 if this string is ordered after
2019 * @a s. If the lengths of @a s and this string are different, the
2020 * shorter one is ordered first. If they are the same, returns the
2021 * result of traits::compare(data(),s,size());
2022 */
2023 int
2024 compare(const _CharT* __s) const;
2025
2026 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2027 // 5 String::compare specification questionable
2028 /**
2029 * @brief Compare substring to a C string.
2030 * @param pos Index of first character of substring.
2031 * @param n1 Number of characters in substring.
2032 * @param s C string to compare against.
2033 * @return Integer < 0, 0, or > 0.
2034 *
2035 * Form the substring of this string from the @a n1 characters starting
2036 * at @a pos. Returns an integer < 0 if the substring is ordered
2037 * before @a s, 0 if their values are equivalent, or > 0 if the
2038 * substring is ordered after @a s. If the lengths of @a s and the
2039 * substring are different, the shorter one is ordered first. If they
2040 * are the same, returns the result of
2041 * traits::compare(substring.data(),s,size());
2042 */
2043 int
2044 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2045
2046 /**
2047 * @brief Compare substring against a character array.
2048 * @param pos1 Index of first character of substring.
2049 * @param n1 Number of characters in substring.
2050 * @param s character array to compare against.
2051 * @param n2 Number of characters of s.
2052 * @return Integer < 0, 0, or > 0.
2053 *
2054 * Form the substring of this string from the @a n1 characters starting
2055 * at @a pos1. Form a string from the first @a n2 characters of @a s.
2056 * Returns an integer < 0 if this substring is ordered before the string
2057 * from @a s, 0 if their values are equivalent, or > 0 if this substring
2058 * is ordered after the string from @a s. If the lengths of this
2059 * substring and @a n2 are different, the shorter one is ordered first.
2060 * If they are the same, returns the result of
2061 * traits::compare(substring.data(),s,size());
2062 *
2063 * NB: s must have at least n2 characters, '\0' has no special
2064 * meaning.
2065 */
2066 int
2067 compare(size_type __pos, size_type __n1, const _CharT* __s,
2068 size_type __n2) const;
2069 };
2070
2071 template<typename _CharT, typename _Traits, typename _Alloc>
2072 inline basic_string<_CharT, _Traits, _Alloc>::
2073 basic_string()
2074 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
2075 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2076 #else
2077 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) { }
2078 #endif
2079
2080 // operator+
2081 /**
2082 * @brief Concatenate two strings.
2083 * @param lhs First string.
2084 * @param rhs Last string.
2085 * @return New string with value of @a lhs followed by @a rhs.
2086 */
2087 template<typename _CharT, typename _Traits, typename _Alloc>
2088 basic_string<_CharT, _Traits, _Alloc>
2089 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2090 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2091 {
2092 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2093 __str.append(__rhs);
2094 return __str;
2095 }
2096
2097 /**
2098 * @brief Concatenate C string and string.
2099 * @param lhs First string.
2100 * @param rhs Last string.
2101 * @return New string with value of @a lhs followed by @a rhs.
2102 */
2103 template<typename _CharT, typename _Traits, typename _Alloc>
2104 basic_string<_CharT,_Traits,_Alloc>
2105 operator+(const _CharT* __lhs,
2106 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2107
2108 /**
2109 * @brief Concatenate character and string.
2110 * @param lhs First string.
2111 * @param rhs Last string.
2112 * @return New string with @a lhs followed by @a rhs.
2113 */
2114 template<typename _CharT, typename _Traits, typename _Alloc>
2115 basic_string<_CharT,_Traits,_Alloc>
2116 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2117
2118 /**
2119 * @brief Concatenate string and C string.
2120 * @param lhs First string.
2121 * @param rhs Last string.
2122 * @return New string with @a lhs followed by @a rhs.
2123 */
2124 template<typename _CharT, typename _Traits, typename _Alloc>
2125 inline basic_string<_CharT, _Traits, _Alloc>
2126 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2127 const _CharT* __rhs)
2128 {
2129 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2130 __str.append(__rhs);
2131 return __str;
2132 }
2133
2134 /**
2135 * @brief Concatenate string and character.
2136 * @param lhs First string.
2137 * @param rhs Last string.
2138 * @return New string with @a lhs followed by @a rhs.
2139 */
2140 template<typename _CharT, typename _Traits, typename _Alloc>
2141 inline basic_string<_CharT, _Traits, _Alloc>
2142 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2143 {
2144 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
2145 typedef typename __string_type::size_type __size_type;
2146 __string_type __str(__lhs);
2147 __str.append(__size_type(1), __rhs);
2148 return __str;
2149 }
2150
2151 // operator ==
2152 /**
2153 * @brief Test equivalence of two strings.
2154 * @param lhs First string.
2155 * @param rhs Second string.
2156 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2157 */
2158 template<typename _CharT, typename _Traits, typename _Alloc>
2159 inline bool
2160 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2161 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2162 { return __lhs.compare(__rhs) == 0; }
2163
2164 /**
2165 * @brief Test equivalence of C string and string.
2166 * @param lhs C string.
2167 * @param rhs String.
2168 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
2169 */
2170 template<typename _CharT, typename _Traits, typename _Alloc>
2171 inline bool
2172 operator==(const _CharT* __lhs,
2173 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2174 { return __rhs.compare(__lhs) == 0; }
2175
2176 /**
2177 * @brief Test equivalence of string and C string.
2178 * @param lhs String.
2179 * @param rhs C string.
2180 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2181 */
2182 template<typename _CharT, typename _Traits, typename _Alloc>
2183 inline bool
2184 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2185 const _CharT* __rhs)
2186 { return __lhs.compare(__rhs) == 0; }
2187
2188 // operator !=
2189 /**
2190 * @brief Test difference of two strings.
2191 * @param lhs First string.
2192 * @param rhs Second string.
2193 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2194 */
2195 template<typename _CharT, typename _Traits, typename _Alloc>
2196 inline bool
2197 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2198 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2199 { return __rhs.compare(__lhs) != 0; }
2200
2201 /**
2202 * @brief Test difference of C string and string.
2203 * @param lhs C string.
2204 * @param rhs String.
2205 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2206 */
2207 template<typename _CharT, typename _Traits, typename _Alloc>
2208 inline bool
2209 operator!=(const _CharT* __lhs,
2210 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2211 { return __rhs.compare(__lhs) != 0; }
2212
2213 /**
2214 * @brief Test difference of string and C string.
2215 * @param lhs String.
2216 * @param rhs C string.
2217 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2218 */
2219 template<typename _CharT, typename _Traits, typename _Alloc>
2220 inline bool
2221 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2222 const _CharT* __rhs)
2223 { return __lhs.compare(__rhs) != 0; }
2224
2225 // operator <
2226 /**
2227 * @brief Test if string precedes string.
2228 * @param lhs First string.
2229 * @param rhs Second string.
2230 * @return True if @a lhs precedes @a rhs. False otherwise.
2231 */
2232 template<typename _CharT, typename _Traits, typename _Alloc>
2233 inline bool
2234 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2235 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2236 { return __lhs.compare(__rhs) < 0; }
2237
2238 /**
2239 * @brief Test if string precedes C string.
2240 * @param lhs String.
2241 * @param rhs C string.
2242 * @return True if @a lhs precedes @a rhs. False otherwise.
2243 */
2244 template<typename _CharT, typename _Traits, typename _Alloc>
2245 inline bool
2246 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2247 const _CharT* __rhs)
2248 { return __lhs.compare(__rhs) < 0; }
2249
2250 /**
2251 * @brief Test if C string precedes string.
2252 * @param lhs C string.
2253 * @param rhs String.
2254 * @return True if @a lhs precedes @a rhs. False otherwise.
2255 */
2256 template<typename _CharT, typename _Traits, typename _Alloc>
2257 inline bool
2258 operator<(const _CharT* __lhs,
2259 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2260 { return __rhs.compare(__lhs) > 0; }
2261
2262 // operator >
2263 /**
2264 * @brief Test if string follows string.
2265 * @param lhs First string.
2266 * @param rhs Second string.
2267 * @return True if @a lhs follows @a rhs. False otherwise.
2268 */
2269 template<typename _CharT, typename _Traits, typename _Alloc>
2270 inline bool
2271 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2272 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2273 { return __lhs.compare(__rhs) > 0; }
2274
2275 /**
2276 * @brief Test if string follows C string.
2277 * @param lhs String.
2278 * @param rhs C string.
2279 * @return True if @a lhs follows @a rhs. False otherwise.
2280 */
2281 template<typename _CharT, typename _Traits, typename _Alloc>
2282 inline bool
2283 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2284 const _CharT* __rhs)
2285 { return __lhs.compare(__rhs) > 0; }
2286
2287 /**
2288 * @brief Test if C string follows string.
2289 * @param lhs C string.
2290 * @param rhs String.
2291 * @return True if @a lhs follows @a rhs. False otherwise.
2292 */
2293 template<typename _CharT, typename _Traits, typename _Alloc>
2294 inline bool
2295 operator>(const _CharT* __lhs,
2296 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2297 { return __rhs.compare(__lhs) < 0; }
2298
2299 // operator <=
2300 /**
2301 * @brief Test if string doesn't follow string.
2302 * @param lhs First string.
2303 * @param rhs Second string.
2304 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2305 */
2306 template<typename _CharT, typename _Traits, typename _Alloc>
2307 inline bool
2308 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2309 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2310 { return __lhs.compare(__rhs) <= 0; }
2311
2312 /**
2313 * @brief Test if string doesn't follow C string.
2314 * @param lhs String.
2315 * @param rhs C string.
2316 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2317 */
2318 template<typename _CharT, typename _Traits, typename _Alloc>
2319 inline bool
2320 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2321 const _CharT* __rhs)
2322 { return __lhs.compare(__rhs) <= 0; }
2323
2324 /**
2325 * @brief Test if C string doesn't follow string.
2326 * @param lhs C string.
2327 * @param rhs String.
2328 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2329 */
2330 template<typename _CharT, typename _Traits, typename _Alloc>
2331 inline bool
2332 operator<=(const _CharT* __lhs,
2333 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2334 { return __rhs.compare(__lhs) >= 0; }
2335
2336 // operator >=
2337 /**
2338 * @brief Test if string doesn't precede string.
2339 * @param lhs First string.
2340 * @param rhs Second string.
2341 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2342 */
2343 template<typename _CharT, typename _Traits, typename _Alloc>
2344 inline bool
2345 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2346 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2347 { return __lhs.compare(__rhs) >= 0; }
2348
2349 /**
2350 * @brief Test if string doesn't precede C string.
2351 * @param lhs String.
2352 * @param rhs C string.
2353 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2354 */
2355 template<typename _CharT, typename _Traits, typename _Alloc>
2356 inline bool
2357 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2358 const _CharT* __rhs)
2359 { return __lhs.compare(__rhs) >= 0; }
2360
2361 /**
2362 * @brief Test if C string doesn't precede string.
2363 * @param lhs C string.
2364 * @param rhs String.
2365 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2366 */
2367 template<typename _CharT, typename _Traits, typename _Alloc>
2368 inline bool
2369 operator>=(const _CharT* __lhs,
2370 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2371 { return __rhs.compare(__lhs) <= 0; }
2372
2373 /**
2374 * @brief Swap contents of two strings.
2375 * @param lhs First string.
2376 * @param rhs Second string.
2377 *
2378 * Exchanges the contents of @a lhs and @a rhs in constant time.
2379 */
2380 template<typename _CharT, typename _Traits, typename _Alloc>
2381 inline void
2382 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2383 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2384 { __lhs.swap(__rhs); }
2385
2386 /**
2387 * @brief Read stream into a string.
2388 * @param is Input stream.
2389 * @param str Buffer to store into.
2390 * @return Reference to the input stream.
2391 *
2392 * Stores characters from @a is into @a str until whitespace is found, the
2393 * end of the stream is encountered, or str.max_size() is reached. If
2394 * is.width() is non-zero, that is the limit on the number of characters
2395 * stored into @a str. Any previous contents of @a str are erased.
2396 */
2397 template<typename _CharT, typename _Traits, typename _Alloc>
2398 basic_istream<_CharT, _Traits>&
2399 operator>>(basic_istream<_CharT, _Traits>& __is,
2400 basic_string<_CharT, _Traits, _Alloc>& __str);
2401
2402 /**
2403 * @brief Write string to a stream.
2404 * @param os Output stream.
2405 * @param str String to write out.
2406 * @return Reference to the output stream.
2407 *
2408 * Output characters of @a str into os following the same rules as for
2409 * writing a C string.
2410 */
2411 template<typename _CharT, typename _Traits, typename _Alloc>
2412 basic_ostream<_CharT, _Traits>&
2413 operator<<(basic_ostream<_CharT, _Traits>& __os,
2414 const basic_string<_CharT, _Traits, _Alloc>& __str);
2415
2416 /**
2417 * @brief Read a line from stream into a string.
2418 * @param is Input stream.
2419 * @param str Buffer to store into.
2420 * @param delim Character marking end of line.
2421 * @return Reference to the input stream.
2422 *
2423 * Stores characters from @a is into @a str until @a delim is found, the
2424 * end of the stream is encountered, or str.max_size() is reached. If
2425 * is.width() is non-zero, that is the limit on the number of characters
2426 * stored into @a str. Any previous contents of @a str are erased. If @a
2427 * delim was encountered, it is extracted but not stored into @a str.
2428 */
2429 template<typename _CharT, typename _Traits, typename _Alloc>
2430 basic_istream<_CharT,_Traits>&
2431 getline(basic_istream<_CharT, _Traits>& __is,
2432 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2433
2434 /**
2435 * @brief Read a line from stream into a string.
2436 * @param is Input stream.
2437 * @param str Buffer to store into.
2438 * @return Reference to the input stream.
2439 *
2440 * Stores characters from is into @a str until '\n' is found, the end of
2441 * the stream is encountered, or str.max_size() is reached. If is.width()
2442 * is non-zero, that is the limit on the number of characters stored into
2443 * @a str. Any previous contents of @a str are erased. If end of line was
2444 * encountered, it is extracted but not stored into @a str.
2445 */
2446 template<typename _CharT, typename _Traits, typename _Alloc>
2447 inline basic_istream<_CharT,_Traits>&
2448 getline(basic_istream<_CharT, _Traits>& __is,
2449 basic_string<_CharT, _Traits, _Alloc>& __str);
2450 } // namespace std
2451
2452 #endif /* _BASIC_STRING_H */