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 is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{ext/vstring.h}
31 #define _VSTRING_TCC 1
33 #pragma GCC system_header
35 #include <cxxabi-forced.h>
37 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
39 template<typename _CharT, typename _Traits, typename _Alloc,
40 template <typename, typename, typename> class _Base>
41 const typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
42 __versa_string<_CharT, _Traits, _Alloc, _Base>::npos;
44 template<typename _CharT, typename _Traits, typename _Alloc,
45 template <typename, typename, typename> class _Base>
47 __versa_string<_CharT, _Traits, _Alloc, _Base>::
48 resize(size_type __n, _CharT __c)
50 const size_type __size = this->size();
52 this->append(__n - __size, __c);
53 else if (__n < __size)
54 this->_M_erase(__n, __size - __n);
57 template<typename _CharT, typename _Traits, typename _Alloc,
58 template <typename, typename, typename> class _Base>
59 __versa_string<_CharT, _Traits, _Alloc, _Base>&
60 __versa_string<_CharT, _Traits, _Alloc, _Base>::
61 _M_append(const _CharT* __s, size_type __n)
63 const size_type __len = __n + this->size();
65 if (__len <= this->capacity() && !this->_M_is_shared())
68 this->_S_copy(this->_M_data() + this->size(), __s, __n);
71 this->_M_mutate(this->size(), size_type(0), __s, __n);
73 this->_M_set_length(__len);
77 template<typename _CharT, typename _Traits, typename _Alloc,
78 template <typename, typename, typename> class _Base>
79 template<typename _InputIterator>
80 __versa_string<_CharT, _Traits, _Alloc, _Base>&
81 __versa_string<_CharT, _Traits, _Alloc, _Base>::
82 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
83 _InputIterator __k2, std::__false_type)
85 const __versa_string __s(__k1, __k2);
86 const size_type __n1 = __i2 - __i1;
87 return _M_replace(__i1 - _M_ibegin(), __n1, __s._M_data(),
91 template<typename _CharT, typename _Traits, typename _Alloc,
92 template <typename, typename, typename> class _Base>
93 __versa_string<_CharT, _Traits, _Alloc, _Base>&
94 __versa_string<_CharT, _Traits, _Alloc, _Base>::
95 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
98 _M_check_length(__n1, __n2, "__versa_string::_M_replace_aux");
100 const size_type __old_size = this->size();
101 const size_type __new_size = __old_size + __n2 - __n1;
103 if (__new_size <= this->capacity() && !this->_M_is_shared())
105 _CharT* __p = this->_M_data() + __pos1;
107 const size_type __how_much = __old_size - __pos1 - __n1;
108 if (__how_much && __n1 != __n2)
109 this->_S_move(__p + __n2, __p + __n1, __how_much);
112 this->_M_mutate(__pos1, __n1, 0, __n2);
115 this->_S_assign(this->_M_data() + __pos1, __n2, __c);
117 this->_M_set_length(__new_size);
121 template<typename _CharT, typename _Traits, typename _Alloc,
122 template <typename, typename, typename> class _Base>
123 __versa_string<_CharT, _Traits, _Alloc, _Base>&
124 __versa_string<_CharT, _Traits, _Alloc, _Base>::
125 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
126 const size_type __len2)
128 _M_check_length(__len1, __len2, "__versa_string::_M_replace");
130 const size_type __old_size = this->size();
131 const size_type __new_size = __old_size + __len2 - __len1;
133 if (__new_size <= this->capacity() && !this->_M_is_shared())
135 _CharT* __p = this->_M_data() + __pos;
137 const size_type __how_much = __old_size - __pos - __len1;
138 if (_M_disjunct(__s))
140 if (__how_much && __len1 != __len2)
141 this->_S_move(__p + __len2, __p + __len1, __how_much);
143 this->_S_copy(__p, __s, __len2);
148 if (__len2 && __len2 <= __len1)
149 this->_S_move(__p, __s, __len2);
150 if (__how_much && __len1 != __len2)
151 this->_S_move(__p + __len2, __p + __len1, __how_much);
154 if (__s + __len2 <= __p + __len1)
155 this->_S_move(__p, __s, __len2);
156 else if (__s >= __p + __len1)
157 this->_S_copy(__p, __s + __len2 - __len1, __len2);
160 const size_type __nleft = (__p + __len1) - __s;
161 this->_S_move(__p, __s, __nleft);
162 this->_S_copy(__p + __nleft, __p + __len2,
169 this->_M_mutate(__pos, __len1, __s, __len2);
171 this->_M_set_length(__new_size);
175 template<typename _CharT, typename _Traits, typename _Alloc,
176 template <typename, typename, typename> class _Base>
177 __versa_string<_CharT, _Traits, _Alloc, _Base>
178 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
179 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
181 __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
182 __str.reserve(__lhs.size() + __rhs.size());
188 template<typename _CharT, typename _Traits, typename _Alloc,
189 template <typename, typename, typename> class _Base>
190 __versa_string<_CharT, _Traits, _Alloc, _Base>
191 operator+(const _CharT* __lhs,
192 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
194 __glibcxx_requires_string(__lhs);
195 typedef __versa_string<_CharT, _Traits, _Alloc, _Base> __string_type;
196 typedef typename __string_type::size_type __size_type;
197 const __size_type __len = _Traits::length(__lhs);
199 __str.reserve(__len + __rhs.size());
200 __str.append(__lhs, __len);
205 template<typename _CharT, typename _Traits, typename _Alloc,
206 template <typename, typename, typename> class _Base>
207 __versa_string<_CharT, _Traits, _Alloc, _Base>
208 operator+(_CharT __lhs,
209 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
211 __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
212 __str.reserve(__rhs.size() + 1);
213 __str.push_back(__lhs);
218 template<typename _CharT, typename _Traits, typename _Alloc,
219 template <typename, typename, typename> class _Base>
220 __versa_string<_CharT, _Traits, _Alloc, _Base>
221 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
224 __glibcxx_requires_string(__rhs);
225 typedef __versa_string<_CharT, _Traits, _Alloc, _Base> __string_type;
226 typedef typename __string_type::size_type __size_type;
227 const __size_type __len = _Traits::length(__rhs);
229 __str.reserve(__lhs.size() + __len);
231 __str.append(__rhs, __len);
235 template<typename _CharT, typename _Traits, typename _Alloc,
236 template <typename, typename, typename> class _Base>
237 __versa_string<_CharT, _Traits, _Alloc, _Base>
238 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
241 __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
242 __str.reserve(__lhs.size() + 1);
244 __str.push_back(__rhs);
248 template<typename _CharT, typename _Traits, typename _Alloc,
249 template <typename, typename, typename> class _Base>
250 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
251 __versa_string<_CharT, _Traits, _Alloc, _Base>::
252 copy(_CharT* __s, size_type __n, size_type __pos) const
254 _M_check(__pos, "__versa_string::copy");
255 __n = _M_limit(__pos, __n);
256 __glibcxx_requires_string_len(__s, __n);
258 this->_S_copy(__s, this->_M_data() + __pos, __n);
259 // 21.3.5.7 par 3: do not append null. (good.)
263 template<typename _CharT, typename _Traits, typename _Alloc,
264 template <typename, typename, typename> class _Base>
265 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
266 __versa_string<_CharT, _Traits, _Alloc, _Base>::
267 find(const _CharT* __s, size_type __pos, size_type __n) const
269 __glibcxx_requires_string_len(__s, __n);
270 const size_type __size = this->size();
271 const _CharT* __data = this->_M_data();
274 return __pos <= __size ? __pos : npos;
278 for (; __pos <= __size - __n; ++__pos)
279 if (traits_type::eq(__data[__pos], __s[0])
280 && traits_type::compare(__data + __pos + 1,
281 __s + 1, __n - 1) == 0)
287 template<typename _CharT, typename _Traits, typename _Alloc,
288 template <typename, typename, typename> class _Base>
289 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
290 __versa_string<_CharT, _Traits, _Alloc, _Base>::
291 find(_CharT __c, size_type __pos) const
293 size_type __ret = npos;
294 const size_type __size = this->size();
297 const _CharT* __data = this->_M_data();
298 const size_type __n = __size - __pos;
299 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
301 __ret = __p - __data;
306 template<typename _CharT, typename _Traits, typename _Alloc,
307 template <typename, typename, typename> class _Base>
308 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
309 __versa_string<_CharT, _Traits, _Alloc, _Base>::
310 rfind(const _CharT* __s, size_type __pos, size_type __n) const
312 __glibcxx_requires_string_len(__s, __n);
313 const size_type __size = this->size();
316 __pos = std::min(size_type(__size - __n), __pos);
317 const _CharT* __data = this->_M_data();
320 if (traits_type::compare(__data + __pos, __s, __n) == 0)
328 template<typename _CharT, typename _Traits, typename _Alloc,
329 template <typename, typename, typename> class _Base>
330 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
331 __versa_string<_CharT, _Traits, _Alloc, _Base>::
332 rfind(_CharT __c, size_type __pos) const
334 size_type __size = this->size();
337 if (--__size > __pos)
339 for (++__size; __size-- > 0; )
340 if (traits_type::eq(this->_M_data()[__size], __c))
346 template<typename _CharT, typename _Traits, typename _Alloc,
347 template <typename, typename, typename> class _Base>
348 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
349 __versa_string<_CharT, _Traits, _Alloc, _Base>::
350 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
352 __glibcxx_requires_string_len(__s, __n);
353 for (; __n && __pos < this->size(); ++__pos)
355 const _CharT* __p = traits_type::find(__s, __n,
356 this->_M_data()[__pos]);
363 template<typename _CharT, typename _Traits, typename _Alloc,
364 template <typename, typename, typename> class _Base>
365 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
366 __versa_string<_CharT, _Traits, _Alloc, _Base>::
367 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
369 __glibcxx_requires_string_len(__s, __n);
370 size_type __size = this->size();
373 if (--__size > __pos)
377 if (traits_type::find(__s, __n, this->_M_data()[__size]))
380 while (__size-- != 0);
385 template<typename _CharT, typename _Traits, typename _Alloc,
386 template <typename, typename, typename> class _Base>
387 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
388 __versa_string<_CharT, _Traits, _Alloc, _Base>::
389 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
391 __glibcxx_requires_string_len(__s, __n);
392 for (; __pos < this->size(); ++__pos)
393 if (!traits_type::find(__s, __n, this->_M_data()[__pos]))
398 template<typename _CharT, typename _Traits, typename _Alloc,
399 template <typename, typename, typename> class _Base>
400 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
401 __versa_string<_CharT, _Traits, _Alloc, _Base>::
402 find_first_not_of(_CharT __c, size_type __pos) const
404 for (; __pos < this->size(); ++__pos)
405 if (!traits_type::eq(this->_M_data()[__pos], __c))
410 template<typename _CharT, typename _Traits, typename _Alloc,
411 template <typename, typename, typename> class _Base>
412 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
413 __versa_string<_CharT, _Traits, _Alloc, _Base>::
414 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
416 __glibcxx_requires_string_len(__s, __n);
417 size_type __size = this->size();
420 if (--__size > __pos)
424 if (!traits_type::find(__s, __n, this->_M_data()[__size]))
432 template<typename _CharT, typename _Traits, typename _Alloc,
433 template <typename, typename, typename> class _Base>
434 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
435 __versa_string<_CharT, _Traits, _Alloc, _Base>::
436 find_last_not_of(_CharT __c, size_type __pos) const
438 size_type __size = this->size();
441 if (--__size > __pos)
445 if (!traits_type::eq(this->_M_data()[__size], __c))
453 template<typename _CharT, typename _Traits, typename _Alloc,
454 template <typename, typename, typename> class _Base>
456 __versa_string<_CharT, _Traits, _Alloc, _Base>::
457 compare(size_type __pos, size_type __n, const __versa_string& __str) const
459 _M_check(__pos, "__versa_string::compare");
460 __n = _M_limit(__pos, __n);
461 const size_type __osize = __str.size();
462 const size_type __len = std::min(__n, __osize);
463 int __r = traits_type::compare(this->_M_data() + __pos,
464 __str.data(), __len);
466 __r = _S_compare(__n, __osize);
470 template<typename _CharT, typename _Traits, typename _Alloc,
471 template <typename, typename, typename> class _Base>
473 __versa_string<_CharT, _Traits, _Alloc, _Base>::
474 compare(size_type __pos1, size_type __n1, const __versa_string& __str,
475 size_type __pos2, size_type __n2) const
477 _M_check(__pos1, "__versa_string::compare");
478 __str._M_check(__pos2, "__versa_string::compare");
479 __n1 = _M_limit(__pos1, __n1);
480 __n2 = __str._M_limit(__pos2, __n2);
481 const size_type __len = std::min(__n1, __n2);
482 int __r = traits_type::compare(this->_M_data() + __pos1,
483 __str.data() + __pos2, __len);
485 __r = _S_compare(__n1, __n2);
489 template<typename _CharT, typename _Traits, typename _Alloc,
490 template <typename, typename, typename> class _Base>
492 __versa_string<_CharT, _Traits, _Alloc, _Base>::
493 compare(const _CharT* __s) const
495 __glibcxx_requires_string(__s);
496 const size_type __size = this->size();
497 const size_type __osize = traits_type::length(__s);
498 const size_type __len = std::min(__size, __osize);
499 int __r = traits_type::compare(this->_M_data(), __s, __len);
501 __r = _S_compare(__size, __osize);
505 template<typename _CharT, typename _Traits, typename _Alloc,
506 template <typename, typename, typename> class _Base>
508 __versa_string <_CharT, _Traits, _Alloc, _Base>::
509 compare(size_type __pos, size_type __n1, const _CharT* __s) const
511 __glibcxx_requires_string(__s);
512 _M_check(__pos, "__versa_string::compare");
513 __n1 = _M_limit(__pos, __n1);
514 const size_type __osize = traits_type::length(__s);
515 const size_type __len = std::min(__n1, __osize);
516 int __r = traits_type::compare(this->_M_data() + __pos, __s, __len);
518 __r = _S_compare(__n1, __osize);
522 template<typename _CharT, typename _Traits, typename _Alloc,
523 template <typename, typename, typename> class _Base>
525 __versa_string <_CharT, _Traits, _Alloc, _Base>::
526 compare(size_type __pos, size_type __n1, const _CharT* __s,
527 size_type __n2) const
529 __glibcxx_requires_string_len(__s, __n2);
530 _M_check(__pos, "__versa_string::compare");
531 __n1 = _M_limit(__pos, __n1);
532 const size_type __len = std::min(__n1, __n2);
533 int __r = traits_type::compare(this->_M_data() + __pos, __s, __len);
535 __r = _S_compare(__n1, __n2);
539 _GLIBCXX_END_NAMESPACE
541 _GLIBCXX_BEGIN_NAMESPACE(std)
543 template<typename _CharT, typename _Traits, typename _Alloc,
544 template <typename, typename, typename> class _Base>
545 basic_istream<_CharT, _Traits>&
546 operator>>(basic_istream<_CharT, _Traits>& __in,
547 __gnu_cxx::__versa_string<_CharT, _Traits,
548 _Alloc, _Base>& __str)
550 typedef basic_istream<_CharT, _Traits> __istream_type;
551 typedef typename __istream_type::ios_base __ios_base;
552 typedef __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>
554 typedef typename __istream_type::int_type __int_type;
555 typedef typename __string_type::size_type __size_type;
556 typedef ctype<_CharT> __ctype_type;
557 typedef typename __ctype_type::ctype_base __ctype_base;
559 __size_type __extracted = 0;
560 typename __ios_base::iostate __err = __ios_base::goodbit;
561 typename __istream_type::sentry __cerb(__in, false);
566 // Avoid reallocation for common case.
569 __size_type __len = 0;
570 const streamsize __w = __in.width();
571 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
573 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
574 const __int_type __eof = _Traits::eof();
575 __int_type __c = __in.rdbuf()->sgetc();
577 while (__extracted < __n
578 && !_Traits::eq_int_type(__c, __eof)
579 && !__ct.is(__ctype_base::space,
580 _Traits::to_char_type(__c)))
582 if (__len == sizeof(__buf) / sizeof(_CharT))
584 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
587 __buf[__len++] = _Traits::to_char_type(__c);
589 __c = __in.rdbuf()->snextc();
591 __str.append(__buf, __len);
593 if (_Traits::eq_int_type(__c, __eof))
594 __err |= __ios_base::eofbit;
597 __catch(__cxxabiv1::__forced_unwind&)
599 __in._M_setstate(__ios_base::badbit);
600 __throw_exception_again;
604 // _GLIBCXX_RESOLVE_LIB_DEFECTS
605 // 91. Description of operator>> and getline() for string<>
606 // might cause endless loop
607 __in._M_setstate(__ios_base::badbit);
610 // 211. operator>>(istream&, string&) doesn't set failbit
612 __err |= __ios_base::failbit;
614 __in.setstate(__err);
618 template<typename _CharT, typename _Traits, typename _Alloc,
619 template <typename, typename, typename> class _Base>
620 basic_istream<_CharT, _Traits>&
621 getline(basic_istream<_CharT, _Traits>& __in,
622 __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str,
625 typedef basic_istream<_CharT, _Traits> __istream_type;
626 typedef typename __istream_type::ios_base __ios_base;
627 typedef __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>
629 typedef typename __istream_type::int_type __int_type;
630 typedef typename __string_type::size_type __size_type;
632 __size_type __extracted = 0;
633 const __size_type __n = __str.max_size();
634 typename __ios_base::iostate __err = __ios_base::goodbit;
635 typename __istream_type::sentry __cerb(__in, true);
640 // Avoid reallocation for common case.
643 __size_type __len = 0;
644 const __int_type __idelim = _Traits::to_int_type(__delim);
645 const __int_type __eof = _Traits::eof();
646 __int_type __c = __in.rdbuf()->sgetc();
648 while (__extracted < __n
649 && !_Traits::eq_int_type(__c, __eof)
650 && !_Traits::eq_int_type(__c, __idelim))
652 if (__len == sizeof(__buf) / sizeof(_CharT))
654 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
657 __buf[__len++] = _Traits::to_char_type(__c);
659 __c = __in.rdbuf()->snextc();
661 __str.append(__buf, __len);
663 if (_Traits::eq_int_type(__c, __eof))
664 __err |= __ios_base::eofbit;
665 else if (_Traits::eq_int_type(__c, __idelim))
668 __in.rdbuf()->sbumpc();
671 __err |= __ios_base::failbit;
673 __catch(__cxxabiv1::__forced_unwind&)
675 __in._M_setstate(__ios_base::badbit);
676 __throw_exception_again;
680 // _GLIBCXX_RESOLVE_LIB_DEFECTS
681 // 91. Description of operator>> and getline() for string<>
682 // might cause endless loop
683 __in._M_setstate(__ios_base::badbit);
687 __err |= __ios_base::failbit;
689 __in.setstate(__err);
693 _GLIBCXX_END_NAMESPACE
695 #endif // _VSTRING_TCC