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