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