1 // Versatile string -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file ext/vstring.tcc
26 * This file is a GNU extension to the Standard C++ Library.
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
32 #define _VSTRING_TCC 1
34 #pragma GCC system_header
36 #include <cxxabi-forced.h>
38 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
40 template<typename _CharT, typename _Traits, typename _Alloc,
41 template <typename, typename, typename> class _Base>
42 const typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
43 __versa_string<_CharT, _Traits, _Alloc, _Base>::npos;
45 template<typename _CharT, typename _Traits, typename _Alloc,
46 template <typename, typename, typename> class _Base>
48 __versa_string<_CharT, _Traits, _Alloc, _Base>::
49 resize(size_type __n, _CharT __c)
51 const size_type __size = this->size();
53 this->append(__n - __size, __c);
54 else if (__n < __size)
55 this->_M_erase(__n, __size - __n);
58 template<typename _CharT, typename _Traits, typename _Alloc,
59 template <typename, typename, typename> class _Base>
60 __versa_string<_CharT, _Traits, _Alloc, _Base>&
61 __versa_string<_CharT, _Traits, _Alloc, _Base>::
62 _M_append(const _CharT* __s, size_type __n)
64 const size_type __len = __n + this->size();
66 if (__len <= this->capacity() && !this->_M_is_shared())
69 this->_S_copy(this->_M_data() + this->size(), __s, __n);
72 this->_M_mutate(this->size(), size_type(0), __s, __n);
74 this->_M_set_length(__len);
78 template<typename _CharT, typename _Traits, typename _Alloc,
79 template <typename, typename, typename> class _Base>
80 template<typename _InputIterator>
81 __versa_string<_CharT, _Traits, _Alloc, _Base>&
82 __versa_string<_CharT, _Traits, _Alloc, _Base>::
83 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
84 _InputIterator __k2, std::__false_type)
86 const __versa_string __s(__k1, __k2);
87 const size_type __n1 = __i2 - __i1;
88 return _M_replace(__i1 - _M_ibegin(), __n1, __s._M_data(),
92 template<typename _CharT, typename _Traits, typename _Alloc,
93 template <typename, typename, typename> class _Base>
94 __versa_string<_CharT, _Traits, _Alloc, _Base>&
95 __versa_string<_CharT, _Traits, _Alloc, _Base>::
96 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
99 _M_check_length(__n1, __n2, "__versa_string::_M_replace_aux");
101 const size_type __old_size = this->size();
102 const size_type __new_size = __old_size + __n2 - __n1;
104 if (__new_size <= this->capacity() && !this->_M_is_shared())
106 _CharT* __p = this->_M_data() + __pos1;
108 const size_type __how_much = __old_size - __pos1 - __n1;
109 if (__how_much && __n1 != __n2)
110 this->_S_move(__p + __n2, __p + __n1, __how_much);
113 this->_M_mutate(__pos1, __n1, 0, __n2);
116 this->_S_assign(this->_M_data() + __pos1, __n2, __c);
118 this->_M_set_length(__new_size);
122 template<typename _CharT, typename _Traits, typename _Alloc,
123 template <typename, typename, typename> class _Base>
124 __versa_string<_CharT, _Traits, _Alloc, _Base>&
125 __versa_string<_CharT, _Traits, _Alloc, _Base>::
126 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
127 const size_type __len2)
129 _M_check_length(__len1, __len2, "__versa_string::_M_replace");
131 const size_type __old_size = this->size();
132 const size_type __new_size = __old_size + __len2 - __len1;
134 if (__new_size <= this->capacity() && !this->_M_is_shared())
136 _CharT* __p = this->_M_data() + __pos;
138 const size_type __how_much = __old_size - __pos - __len1;
139 if (_M_disjunct(__s))
141 if (__how_much && __len1 != __len2)
142 this->_S_move(__p + __len2, __p + __len1, __how_much);
144 this->_S_copy(__p, __s, __len2);
149 if (__len2 && __len2 <= __len1)
150 this->_S_move(__p, __s, __len2);
151 if (__how_much && __len1 != __len2)
152 this->_S_move(__p + __len2, __p + __len1, __how_much);
155 if (__s + __len2 <= __p + __len1)
156 this->_S_move(__p, __s, __len2);
157 else if (__s >= __p + __len1)
158 this->_S_copy(__p, __s + __len2 - __len1, __len2);
161 const size_type __nleft = (__p + __len1) - __s;
162 this->_S_move(__p, __s, __nleft);
163 this->_S_copy(__p + __nleft, __p + __len2,
170 this->_M_mutate(__pos, __len1, __s, __len2);
172 this->_M_set_length(__new_size);
176 template<typename _CharT, typename _Traits, typename _Alloc,
177 template <typename, typename, typename> class _Base>
178 __versa_string<_CharT, _Traits, _Alloc, _Base>
179 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
180 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
182 __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
183 __str.reserve(__lhs.size() + __rhs.size());
189 template<typename _CharT, typename _Traits, typename _Alloc,
190 template <typename, typename, typename> class _Base>
191 __versa_string<_CharT, _Traits, _Alloc, _Base>
192 operator+(const _CharT* __lhs,
193 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
195 __glibcxx_requires_string(__lhs);
196 typedef __versa_string<_CharT, _Traits, _Alloc, _Base> __string_type;
197 typedef typename __string_type::size_type __size_type;
198 const __size_type __len = _Traits::length(__lhs);
200 __str.reserve(__len + __rhs.size());
201 __str.append(__lhs, __len);
206 template<typename _CharT, typename _Traits, typename _Alloc,
207 template <typename, typename, typename> class _Base>
208 __versa_string<_CharT, _Traits, _Alloc, _Base>
209 operator+(_CharT __lhs,
210 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
212 __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
213 __str.reserve(__rhs.size() + 1);
214 __str.push_back(__lhs);
219 template<typename _CharT, typename _Traits, typename _Alloc,
220 template <typename, typename, typename> class _Base>
221 __versa_string<_CharT, _Traits, _Alloc, _Base>
222 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
225 __glibcxx_requires_string(__rhs);
226 typedef __versa_string<_CharT, _Traits, _Alloc, _Base> __string_type;
227 typedef typename __string_type::size_type __size_type;
228 const __size_type __len = _Traits::length(__rhs);
230 __str.reserve(__lhs.size() + __len);
232 __str.append(__rhs, __len);
236 template<typename _CharT, typename _Traits, typename _Alloc,
237 template <typename, typename, typename> class _Base>
238 __versa_string<_CharT, _Traits, _Alloc, _Base>
239 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
242 __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
243 __str.reserve(__lhs.size() + 1);
245 __str.push_back(__rhs);
249 template<typename _CharT, typename _Traits, typename _Alloc,
250 template <typename, typename, typename> class _Base>
251 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
252 __versa_string<_CharT, _Traits, _Alloc, _Base>::
253 copy(_CharT* __s, size_type __n, size_type __pos) const
255 _M_check(__pos, "__versa_string::copy");
256 __n = _M_limit(__pos, __n);
257 __glibcxx_requires_string_len(__s, __n);
259 this->_S_copy(__s, this->_M_data() + __pos, __n);
260 // 21.3.5.7 par 3: do not append null. (good.)
264 template<typename _CharT, typename _Traits, typename _Alloc,
265 template <typename, typename, typename> class _Base>
266 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
267 __versa_string<_CharT, _Traits, _Alloc, _Base>::
268 find(const _CharT* __s, size_type __pos, size_type __n) const
270 __glibcxx_requires_string_len(__s, __n);
271 const size_type __size = this->size();
272 const _CharT* __data = this->_M_data();
275 return __pos <= __size ? __pos : npos;
279 for (; __pos <= __size - __n; ++__pos)
280 if (traits_type::eq(__data[__pos], __s[0])
281 && traits_type::compare(__data + __pos + 1,
282 __s + 1, __n - 1) == 0)
288 template<typename _CharT, typename _Traits, typename _Alloc,
289 template <typename, typename, typename> class _Base>
290 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
291 __versa_string<_CharT, _Traits, _Alloc, _Base>::
292 find(_CharT __c, size_type __pos) const
294 size_type __ret = npos;
295 const size_type __size = this->size();
298 const _CharT* __data = this->_M_data();
299 const size_type __n = __size - __pos;
300 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
302 __ret = __p - __data;
307 template<typename _CharT, typename _Traits, typename _Alloc,
308 template <typename, typename, typename> class _Base>
309 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
310 __versa_string<_CharT, _Traits, _Alloc, _Base>::
311 rfind(const _CharT* __s, size_type __pos, size_type __n) const
313 __glibcxx_requires_string_len(__s, __n);
314 const size_type __size = this->size();
317 __pos = std::min(size_type(__size - __n), __pos);
318 const _CharT* __data = this->_M_data();
321 if (traits_type::compare(__data + __pos, __s, __n) == 0)
329 template<typename _CharT, typename _Traits, typename _Alloc,
330 template <typename, typename, typename> class _Base>
331 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
332 __versa_string<_CharT, _Traits, _Alloc, _Base>::
333 rfind(_CharT __c, size_type __pos) const
335 size_type __size = this->size();
338 if (--__size > __pos)
340 for (++__size; __size-- > 0; )
341 if (traits_type::eq(this->_M_data()[__size], __c))
347 template<typename _CharT, typename _Traits, typename _Alloc,
348 template <typename, typename, typename> class _Base>
349 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
350 __versa_string<_CharT, _Traits, _Alloc, _Base>::
351 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
353 __glibcxx_requires_string_len(__s, __n);
354 for (; __n && __pos < this->size(); ++__pos)
356 const _CharT* __p = traits_type::find(__s, __n,
357 this->_M_data()[__pos]);
364 template<typename _CharT, typename _Traits, typename _Alloc,
365 template <typename, typename, typename> class _Base>
366 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
367 __versa_string<_CharT, _Traits, _Alloc, _Base>::
368 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
370 __glibcxx_requires_string_len(__s, __n);
371 size_type __size = this->size();
374 if (--__size > __pos)
378 if (traits_type::find(__s, __n, this->_M_data()[__size]))
381 while (__size-- != 0);
386 template<typename _CharT, typename _Traits, typename _Alloc,
387 template <typename, typename, typename> class _Base>
388 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
389 __versa_string<_CharT, _Traits, _Alloc, _Base>::
390 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
392 __glibcxx_requires_string_len(__s, __n);
393 for (; __pos < this->size(); ++__pos)
394 if (!traits_type::find(__s, __n, this->_M_data()[__pos]))
399 template<typename _CharT, typename _Traits, typename _Alloc,
400 template <typename, typename, typename> class _Base>
401 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
402 __versa_string<_CharT, _Traits, _Alloc, _Base>::
403 find_first_not_of(_CharT __c, size_type __pos) const
405 for (; __pos < this->size(); ++__pos)
406 if (!traits_type::eq(this->_M_data()[__pos], __c))
411 template<typename _CharT, typename _Traits, typename _Alloc,
412 template <typename, typename, typename> class _Base>
413 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
414 __versa_string<_CharT, _Traits, _Alloc, _Base>::
415 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
417 __glibcxx_requires_string_len(__s, __n);
418 size_type __size = this->size();
421 if (--__size > __pos)
425 if (!traits_type::find(__s, __n, this->_M_data()[__size]))
433 template<typename _CharT, typename _Traits, typename _Alloc,
434 template <typename, typename, typename> class _Base>
435 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
436 __versa_string<_CharT, _Traits, _Alloc, _Base>::
437 find_last_not_of(_CharT __c, size_type __pos) const
439 size_type __size = this->size();
442 if (--__size > __pos)
446 if (!traits_type::eq(this->_M_data()[__size], __c))
454 template<typename _CharT, typename _Traits, typename _Alloc,
455 template <typename, typename, typename> class _Base>
457 __versa_string<_CharT, _Traits, _Alloc, _Base>::
458 compare(size_type __pos, size_type __n, const __versa_string& __str) const
460 _M_check(__pos, "__versa_string::compare");
461 __n = _M_limit(__pos, __n);
462 const size_type __osize = __str.size();
463 const size_type __len = std::min(__n, __osize);
464 int __r = traits_type::compare(this->_M_data() + __pos,
465 __str.data(), __len);
467 __r = _S_compare(__n, __osize);
471 template<typename _CharT, typename _Traits, typename _Alloc,
472 template <typename, typename, typename> class _Base>
474 __versa_string<_CharT, _Traits, _Alloc, _Base>::
475 compare(size_type __pos1, size_type __n1, const __versa_string& __str,
476 size_type __pos2, size_type __n2) const
478 _M_check(__pos1, "__versa_string::compare");
479 __str._M_check(__pos2, "__versa_string::compare");
480 __n1 = _M_limit(__pos1, __n1);
481 __n2 = __str._M_limit(__pos2, __n2);
482 const size_type __len = std::min(__n1, __n2);
483 int __r = traits_type::compare(this->_M_data() + __pos1,
484 __str.data() + __pos2, __len);
486 __r = _S_compare(__n1, __n2);
490 template<typename _CharT, typename _Traits, typename _Alloc,
491 template <typename, typename, typename> class _Base>
493 __versa_string<_CharT, _Traits, _Alloc, _Base>::
494 compare(const _CharT* __s) const
496 __glibcxx_requires_string(__s);
497 const size_type __size = this->size();
498 const size_type __osize = traits_type::length(__s);
499 const size_type __len = std::min(__size, __osize);
500 int __r = traits_type::compare(this->_M_data(), __s, __len);
502 __r = _S_compare(__size, __osize);
506 template<typename _CharT, typename _Traits, typename _Alloc,
507 template <typename, typename, typename> class _Base>
509 __versa_string <_CharT, _Traits, _Alloc, _Base>::
510 compare(size_type __pos, size_type __n1, const _CharT* __s) const
512 __glibcxx_requires_string(__s);
513 _M_check(__pos, "__versa_string::compare");
514 __n1 = _M_limit(__pos, __n1);
515 const size_type __osize = traits_type::length(__s);
516 const size_type __len = std::min(__n1, __osize);
517 int __r = traits_type::compare(this->_M_data() + __pos, __s, __len);
519 __r = _S_compare(__n1, __osize);
523 template<typename _CharT, typename _Traits, typename _Alloc,
524 template <typename, typename, typename> class _Base>
526 __versa_string <_CharT, _Traits, _Alloc, _Base>::
527 compare(size_type __pos, size_type __n1, const _CharT* __s,
528 size_type __n2) const
530 __glibcxx_requires_string_len(__s, __n2);
531 _M_check(__pos, "__versa_string::compare");
532 __n1 = _M_limit(__pos, __n1);
533 const size_type __len = std::min(__n1, __n2);
534 int __r = traits_type::compare(this->_M_data() + __pos, __s, __len);
536 __r = _S_compare(__n1, __n2);
540 _GLIBCXX_END_NAMESPACE
542 _GLIBCXX_BEGIN_NAMESPACE(std)
544 template<typename _CharT, typename _Traits, typename _Alloc,
545 template <typename, typename, typename> class _Base>
546 basic_istream<_CharT, _Traits>&
547 operator>>(basic_istream<_CharT, _Traits>& __in,
548 __gnu_cxx::__versa_string<_CharT, _Traits,
549 _Alloc, _Base>& __str)
551 typedef basic_istream<_CharT, _Traits> __istream_type;
552 typedef typename __istream_type::ios_base __ios_base;
553 typedef __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>
555 typedef typename __istream_type::int_type __int_type;
556 typedef typename __string_type::size_type __size_type;
557 typedef ctype<_CharT> __ctype_type;
558 typedef typename __ctype_type::ctype_base __ctype_base;
560 __size_type __extracted = 0;
561 typename __ios_base::iostate __err = __ios_base::goodbit;
562 typename __istream_type::sentry __cerb(__in, false);
567 // Avoid reallocation for common case.
570 __size_type __len = 0;
571 const streamsize __w = __in.width();
572 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
574 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
575 const __int_type __eof = _Traits::eof();
576 __int_type __c = __in.rdbuf()->sgetc();
578 while (__extracted < __n
579 && !_Traits::eq_int_type(__c, __eof)
580 && !__ct.is(__ctype_base::space,
581 _Traits::to_char_type(__c)))
583 if (__len == sizeof(__buf) / sizeof(_CharT))
585 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
588 __buf[__len++] = _Traits::to_char_type(__c);
590 __c = __in.rdbuf()->snextc();
592 __str.append(__buf, __len);
594 if (_Traits::eq_int_type(__c, __eof))
595 __err |= __ios_base::eofbit;
598 __catch(__cxxabiv1::__forced_unwind&)
600 __in._M_setstate(__ios_base::badbit);
601 __throw_exception_again;
605 // _GLIBCXX_RESOLVE_LIB_DEFECTS
606 // 91. Description of operator>> and getline() for string<>
607 // might cause endless loop
608 __in._M_setstate(__ios_base::badbit);
611 // 211. operator>>(istream&, string&) doesn't set failbit
613 __err |= __ios_base::failbit;
615 __in.setstate(__err);
619 template<typename _CharT, typename _Traits, typename _Alloc,
620 template <typename, typename, typename> class _Base>
621 basic_istream<_CharT, _Traits>&
622 getline(basic_istream<_CharT, _Traits>& __in,
623 __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str,
626 typedef basic_istream<_CharT, _Traits> __istream_type;
627 typedef typename __istream_type::ios_base __ios_base;
628 typedef __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>
630 typedef typename __istream_type::int_type __int_type;
631 typedef typename __string_type::size_type __size_type;
633 __size_type __extracted = 0;
634 const __size_type __n = __str.max_size();
635 typename __ios_base::iostate __err = __ios_base::goodbit;
636 typename __istream_type::sentry __cerb(__in, true);
641 // Avoid reallocation for common case.
644 __size_type __len = 0;
645 const __int_type __idelim = _Traits::to_int_type(__delim);
646 const __int_type __eof = _Traits::eof();
647 __int_type __c = __in.rdbuf()->sgetc();
649 while (__extracted < __n
650 && !_Traits::eq_int_type(__c, __eof)
651 && !_Traits::eq_int_type(__c, __idelim))
653 if (__len == sizeof(__buf) / sizeof(_CharT))
655 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
658 __buf[__len++] = _Traits::to_char_type(__c);
660 __c = __in.rdbuf()->snextc();
662 __str.append(__buf, __len);
664 if (_Traits::eq_int_type(__c, __eof))
665 __err |= __ios_base::eofbit;
666 else if (_Traits::eq_int_type(__c, __idelim))
669 __in.rdbuf()->sbumpc();
672 __err |= __ios_base::failbit;
674 __catch(__cxxabiv1::__forced_unwind&)
676 __in._M_setstate(__ios_base::badbit);
677 __throw_exception_again;
681 // _GLIBCXX_RESOLVE_LIB_DEFECTS
682 // 91. Description of operator>> and getline() for string<>
683 // might cause endless loop
684 __in._M_setstate(__ios_base::badbit);
688 __err |= __ios_base::failbit;
690 __in.setstate(__err);
694 _GLIBCXX_END_NAMESPACE
696 #endif // _VSTRING_TCC