1 // Versatile string -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file ext/vstring.tcc
31 * This file is a GNU extension to the Standard C++ Library.
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
37 #define _VSTRING_TCC 1
39 #pragma GCC system_header
41 #include <cxxabi-forced.h>
43 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
45 template<typename _CharT, typename _Traits, typename _Alloc,
46 template <typename, typename, typename> class _Base>
47 const typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
48 __versa_string<_CharT, _Traits, _Alloc, _Base>::npos;
50 template<typename _CharT, typename _Traits, typename _Alloc,
51 template <typename, typename, typename> class _Base>
53 __versa_string<_CharT, _Traits, _Alloc, _Base>::
54 resize(size_type __n, _CharT __c)
56 const size_type __size = this->size();
58 this->append(__n - __size, __c);
59 else if (__n < __size)
60 this->_M_erase(__n, __size - __n);
63 template<typename _CharT, typename _Traits, typename _Alloc,
64 template <typename, typename, typename> class _Base>
65 __versa_string<_CharT, _Traits, _Alloc, _Base>&
66 __versa_string<_CharT, _Traits, _Alloc, _Base>::
67 _M_append(const _CharT* __s, size_type __n)
69 const size_type __len = __n + this->size();
71 if (__len <= this->capacity() && !this->_M_is_shared())
74 this->_S_copy(this->_M_data() + this->size(), __s, __n);
77 this->_M_mutate(this->size(), size_type(0), __s, __n);
79 this->_M_set_length(__len);
83 template<typename _CharT, typename _Traits, typename _Alloc,
84 template <typename, typename, typename> class _Base>
85 template<typename _InputIterator>
86 __versa_string<_CharT, _Traits, _Alloc, _Base>&
87 __versa_string<_CharT, _Traits, _Alloc, _Base>::
88 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
89 _InputIterator __k2, std::__false_type)
91 const __versa_string __s(__k1, __k2);
92 const size_type __n1 = __i2 - __i1;
93 return _M_replace(__i1 - _M_ibegin(), __n1, __s._M_data(),
97 template<typename _CharT, typename _Traits, typename _Alloc,
98 template <typename, typename, typename> class _Base>
99 __versa_string<_CharT, _Traits, _Alloc, _Base>&
100 __versa_string<_CharT, _Traits, _Alloc, _Base>::
101 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
104 _M_check_length(__n1, __n2, "__versa_string::_M_replace_aux");
106 const size_type __old_size = this->size();
107 const size_type __new_size = __old_size + __n2 - __n1;
109 if (__new_size <= this->capacity() && !this->_M_is_shared())
111 _CharT* __p = this->_M_data() + __pos1;
113 const size_type __how_much = __old_size - __pos1 - __n1;
114 if (__how_much && __n1 != __n2)
115 this->_S_move(__p + __n2, __p + __n1, __how_much);
118 this->_M_mutate(__pos1, __n1, 0, __n2);
121 this->_S_assign(this->_M_data() + __pos1, __n2, __c);
123 this->_M_set_length(__new_size);
127 template<typename _CharT, typename _Traits, typename _Alloc,
128 template <typename, typename, typename> class _Base>
129 __versa_string<_CharT, _Traits, _Alloc, _Base>&
130 __versa_string<_CharT, _Traits, _Alloc, _Base>::
131 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
132 const size_type __len2)
134 _M_check_length(__len1, __len2, "__versa_string::_M_replace");
136 const size_type __old_size = this->size();
137 const size_type __new_size = __old_size + __len2 - __len1;
139 if (__new_size <= this->capacity() && !this->_M_is_shared())
141 _CharT* __p = this->_M_data() + __pos;
143 const size_type __how_much = __old_size - __pos - __len1;
144 if (_M_disjunct(__s))
146 if (__how_much && __len1 != __len2)
147 this->_S_move(__p + __len2, __p + __len1, __how_much);
149 this->_S_copy(__p, __s, __len2);
154 if (__len2 && __len2 <= __len1)
155 this->_S_move(__p, __s, __len2);
156 if (__how_much && __len1 != __len2)
157 this->_S_move(__p + __len2, __p + __len1, __how_much);
160 if (__s + __len2 <= __p + __len1)
161 this->_S_move(__p, __s, __len2);
162 else if (__s >= __p + __len1)
163 this->_S_copy(__p, __s + __len2 - __len1, __len2);
166 const size_type __nleft = (__p + __len1) - __s;
167 this->_S_move(__p, __s, __nleft);
168 this->_S_copy(__p + __nleft, __p + __len2,
175 this->_M_mutate(__pos, __len1, __s, __len2);
177 this->_M_set_length(__new_size);
181 template<typename _CharT, typename _Traits, typename _Alloc,
182 template <typename, typename, typename> class _Base>
183 __versa_string<_CharT, _Traits, _Alloc, _Base>
184 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
185 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
187 __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
188 __str.reserve(__lhs.size() + __rhs.size());
194 template<typename _CharT, typename _Traits, typename _Alloc,
195 template <typename, typename, typename> class _Base>
196 __versa_string<_CharT, _Traits, _Alloc, _Base>
197 operator+(const _CharT* __lhs,
198 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
200 __glibcxx_requires_string(__lhs);
201 typedef __versa_string<_CharT, _Traits, _Alloc, _Base> __string_type;
202 typedef typename __string_type::size_type __size_type;
203 const __size_type __len = _Traits::length(__lhs);
205 __str.reserve(__len + __rhs.size());
206 __str.append(__lhs, __len);
211 template<typename _CharT, typename _Traits, typename _Alloc,
212 template <typename, typename, typename> class _Base>
213 __versa_string<_CharT, _Traits, _Alloc, _Base>
214 operator+(_CharT __lhs,
215 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
217 __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
218 __str.reserve(__rhs.size() + 1);
219 __str.push_back(__lhs);
224 template<typename _CharT, typename _Traits, typename _Alloc,
225 template <typename, typename, typename> class _Base>
226 __versa_string<_CharT, _Traits, _Alloc, _Base>
227 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
230 __glibcxx_requires_string(__rhs);
231 typedef __versa_string<_CharT, _Traits, _Alloc, _Base> __string_type;
232 typedef typename __string_type::size_type __size_type;
233 const __size_type __len = _Traits::length(__rhs);
235 __str.reserve(__lhs.size() + __len);
237 __str.append(__rhs, __len);
241 template<typename _CharT, typename _Traits, typename _Alloc,
242 template <typename, typename, typename> class _Base>
243 __versa_string<_CharT, _Traits, _Alloc, _Base>
244 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
247 __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
248 __str.reserve(__lhs.size() + 1);
250 __str.push_back(__rhs);
254 template<typename _CharT, typename _Traits, typename _Alloc,
255 template <typename, typename, typename> class _Base>
256 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
257 __versa_string<_CharT, _Traits, _Alloc, _Base>::
258 copy(_CharT* __s, size_type __n, size_type __pos) const
260 _M_check(__pos, "__versa_string::copy");
261 __n = _M_limit(__pos, __n);
262 __glibcxx_requires_string_len(__s, __n);
264 this->_S_copy(__s, this->_M_data() + __pos, __n);
265 // 21.3.5.7 par 3: do not append null. (good.)
269 template<typename _CharT, typename _Traits, typename _Alloc,
270 template <typename, typename, typename> class _Base>
271 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
272 __versa_string<_CharT, _Traits, _Alloc, _Base>::
273 find(const _CharT* __s, size_type __pos, size_type __n) const
275 __glibcxx_requires_string_len(__s, __n);
276 const size_type __size = this->size();
277 const _CharT* __data = this->_M_data();
280 return __pos <= __size ? __pos : npos;
284 for (; __pos <= __size - __n; ++__pos)
285 if (traits_type::eq(__data[__pos], __s[0])
286 && traits_type::compare(__data + __pos + 1,
287 __s + 1, __n - 1) == 0)
293 template<typename _CharT, typename _Traits, typename _Alloc,
294 template <typename, typename, typename> class _Base>
295 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
296 __versa_string<_CharT, _Traits, _Alloc, _Base>::
297 find(_CharT __c, size_type __pos) const
299 size_type __ret = npos;
300 const size_type __size = this->size();
303 const _CharT* __data = this->_M_data();
304 const size_type __n = __size - __pos;
305 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
307 __ret = __p - __data;
312 template<typename _CharT, typename _Traits, typename _Alloc,
313 template <typename, typename, typename> class _Base>
314 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
315 __versa_string<_CharT, _Traits, _Alloc, _Base>::
316 rfind(const _CharT* __s, size_type __pos, size_type __n) const
318 __glibcxx_requires_string_len(__s, __n);
319 const size_type __size = this->size();
322 __pos = std::min(size_type(__size - __n), __pos);
323 const _CharT* __data = this->_M_data();
326 if (traits_type::compare(__data + __pos, __s, __n) == 0)
334 template<typename _CharT, typename _Traits, typename _Alloc,
335 template <typename, typename, typename> class _Base>
336 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
337 __versa_string<_CharT, _Traits, _Alloc, _Base>::
338 rfind(_CharT __c, size_type __pos) const
340 size_type __size = this->size();
343 if (--__size > __pos)
345 for (++__size; __size-- > 0; )
346 if (traits_type::eq(this->_M_data()[__size], __c))
352 template<typename _CharT, typename _Traits, typename _Alloc,
353 template <typename, typename, typename> class _Base>
354 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
355 __versa_string<_CharT, _Traits, _Alloc, _Base>::
356 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
358 __glibcxx_requires_string_len(__s, __n);
359 for (; __n && __pos < this->size(); ++__pos)
361 const _CharT* __p = traits_type::find(__s, __n,
362 this->_M_data()[__pos]);
369 template<typename _CharT, typename _Traits, typename _Alloc,
370 template <typename, typename, typename> class _Base>
371 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
372 __versa_string<_CharT, _Traits, _Alloc, _Base>::
373 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
375 __glibcxx_requires_string_len(__s, __n);
376 size_type __size = this->size();
379 if (--__size > __pos)
383 if (traits_type::find(__s, __n, this->_M_data()[__size]))
386 while (__size-- != 0);
391 template<typename _CharT, typename _Traits, typename _Alloc,
392 template <typename, typename, typename> class _Base>
393 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
394 __versa_string<_CharT, _Traits, _Alloc, _Base>::
395 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
397 __glibcxx_requires_string_len(__s, __n);
398 for (; __pos < this->size(); ++__pos)
399 if (!traits_type::find(__s, __n, this->_M_data()[__pos]))
404 template<typename _CharT, typename _Traits, typename _Alloc,
405 template <typename, typename, typename> class _Base>
406 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
407 __versa_string<_CharT, _Traits, _Alloc, _Base>::
408 find_first_not_of(_CharT __c, size_type __pos) const
410 for (; __pos < this->size(); ++__pos)
411 if (!traits_type::eq(this->_M_data()[__pos], __c))
416 template<typename _CharT, typename _Traits, typename _Alloc,
417 template <typename, typename, typename> class _Base>
418 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
419 __versa_string<_CharT, _Traits, _Alloc, _Base>::
420 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
422 __glibcxx_requires_string_len(__s, __n);
423 size_type __size = this->size();
426 if (--__size > __pos)
430 if (!traits_type::find(__s, __n, this->_M_data()[__size]))
438 template<typename _CharT, typename _Traits, typename _Alloc,
439 template <typename, typename, typename> class _Base>
440 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
441 __versa_string<_CharT, _Traits, _Alloc, _Base>::
442 find_last_not_of(_CharT __c, size_type __pos) const
444 size_type __size = this->size();
447 if (--__size > __pos)
451 if (!traits_type::eq(this->_M_data()[__size], __c))
459 template<typename _CharT, typename _Traits, typename _Alloc,
460 template <typename, typename, typename> class _Base>
462 __versa_string<_CharT, _Traits, _Alloc, _Base>::
463 compare(size_type __pos, size_type __n, const __versa_string& __str) const
465 _M_check(__pos, "__versa_string::compare");
466 __n = _M_limit(__pos, __n);
467 const size_type __osize = __str.size();
468 const size_type __len = std::min(__n, __osize);
469 int __r = traits_type::compare(this->_M_data() + __pos,
470 __str.data(), __len);
472 __r = _S_compare(__n, __osize);
476 template<typename _CharT, typename _Traits, typename _Alloc,
477 template <typename, typename, typename> class _Base>
479 __versa_string<_CharT, _Traits, _Alloc, _Base>::
480 compare(size_type __pos1, size_type __n1, const __versa_string& __str,
481 size_type __pos2, size_type __n2) const
483 _M_check(__pos1, "__versa_string::compare");
484 __str._M_check(__pos2, "__versa_string::compare");
485 __n1 = _M_limit(__pos1, __n1);
486 __n2 = __str._M_limit(__pos2, __n2);
487 const size_type __len = std::min(__n1, __n2);
488 int __r = traits_type::compare(this->_M_data() + __pos1,
489 __str.data() + __pos2, __len);
491 __r = _S_compare(__n1, __n2);
495 template<typename _CharT, typename _Traits, typename _Alloc,
496 template <typename, typename, typename> class _Base>
498 __versa_string<_CharT, _Traits, _Alloc, _Base>::
499 compare(const _CharT* __s) const
501 __glibcxx_requires_string(__s);
502 const size_type __size = this->size();
503 const size_type __osize = traits_type::length(__s);
504 const size_type __len = std::min(__size, __osize);
505 int __r = traits_type::compare(this->_M_data(), __s, __len);
507 __r = _S_compare(__size, __osize);
511 template<typename _CharT, typename _Traits, typename _Alloc,
512 template <typename, typename, typename> class _Base>
514 __versa_string <_CharT, _Traits, _Alloc, _Base>::
515 compare(size_type __pos, size_type __n1, const _CharT* __s) const
517 __glibcxx_requires_string(__s);
518 _M_check(__pos, "__versa_string::compare");
519 __n1 = _M_limit(__pos, __n1);
520 const size_type __osize = traits_type::length(__s);
521 const size_type __len = std::min(__n1, __osize);
522 int __r = traits_type::compare(this->_M_data() + __pos, __s, __len);
524 __r = _S_compare(__n1, __osize);
528 template<typename _CharT, typename _Traits, typename _Alloc,
529 template <typename, typename, typename> class _Base>
531 __versa_string <_CharT, _Traits, _Alloc, _Base>::
532 compare(size_type __pos, size_type __n1, const _CharT* __s,
533 size_type __n2) const
535 __glibcxx_requires_string_len(__s, __n2);
536 _M_check(__pos, "__versa_string::compare");
537 __n1 = _M_limit(__pos, __n1);
538 const size_type __len = std::min(__n1, __n2);
539 int __r = traits_type::compare(this->_M_data() + __pos, __s, __len);
541 __r = _S_compare(__n1, __n2);
545 _GLIBCXX_END_NAMESPACE
547 _GLIBCXX_BEGIN_NAMESPACE(std)
549 template<typename _CharT, typename _Traits, typename _Alloc,
550 template <typename, typename, typename> class _Base>
551 basic_istream<_CharT, _Traits>&
552 operator>>(basic_istream<_CharT, _Traits>& __in,
553 __gnu_cxx::__versa_string<_CharT, _Traits,
554 _Alloc, _Base>& __str)
556 typedef basic_istream<_CharT, _Traits> __istream_type;
557 typedef typename __istream_type::ios_base __ios_base;
558 typedef __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>
560 typedef typename __istream_type::int_type __int_type;
561 typedef typename __string_type::size_type __size_type;
562 typedef ctype<_CharT> __ctype_type;
563 typedef typename __ctype_type::ctype_base __ctype_base;
565 __size_type __extracted = 0;
566 typename __ios_base::iostate __err = __ios_base::goodbit;
567 typename __istream_type::sentry __cerb(__in, false);
572 // Avoid reallocation for common case.
575 __size_type __len = 0;
576 const streamsize __w = __in.width();
577 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
579 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
580 const __int_type __eof = _Traits::eof();
581 __int_type __c = __in.rdbuf()->sgetc();
583 while (__extracted < __n
584 && !_Traits::eq_int_type(__c, __eof)
585 && !__ct.is(__ctype_base::space,
586 _Traits::to_char_type(__c)))
588 if (__len == sizeof(__buf) / sizeof(_CharT))
590 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
593 __buf[__len++] = _Traits::to_char_type(__c);
595 __c = __in.rdbuf()->snextc();
597 __str.append(__buf, __len);
599 if (_Traits::eq_int_type(__c, __eof))
600 __err |= __ios_base::eofbit;
603 catch(__cxxabiv1::__forced_unwind&)
605 __in._M_setstate(__ios_base::badbit);
606 __throw_exception_again;
610 // _GLIBCXX_RESOLVE_LIB_DEFECTS
611 // 91. Description of operator>> and getline() for string<>
612 // might cause endless loop
613 __in._M_setstate(__ios_base::badbit);
616 // 211. operator>>(istream&, string&) doesn't set failbit
618 __err |= __ios_base::failbit;
620 __in.setstate(__err);
624 template<typename _CharT, typename _Traits, typename _Alloc,
625 template <typename, typename, typename> class _Base>
626 basic_istream<_CharT, _Traits>&
627 getline(basic_istream<_CharT, _Traits>& __in,
628 __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str,
631 typedef basic_istream<_CharT, _Traits> __istream_type;
632 typedef typename __istream_type::ios_base __ios_base;
633 typedef __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>
635 typedef typename __istream_type::int_type __int_type;
636 typedef typename __string_type::size_type __size_type;
638 __size_type __extracted = 0;
639 const __size_type __n = __str.max_size();
640 typename __ios_base::iostate __err = __ios_base::goodbit;
641 typename __istream_type::sentry __cerb(__in, true);
646 // Avoid reallocation for common case.
649 __size_type __len = 0;
650 const __int_type __idelim = _Traits::to_int_type(__delim);
651 const __int_type __eof = _Traits::eof();
652 __int_type __c = __in.rdbuf()->sgetc();
654 while (__extracted < __n
655 && !_Traits::eq_int_type(__c, __eof)
656 && !_Traits::eq_int_type(__c, __idelim))
658 if (__len == sizeof(__buf) / sizeof(_CharT))
660 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
663 __buf[__len++] = _Traits::to_char_type(__c);
665 __c = __in.rdbuf()->snextc();
667 __str.append(__buf, __len);
669 if (_Traits::eq_int_type(__c, __eof))
670 __err |= __ios_base::eofbit;
671 else if (_Traits::eq_int_type(__c, __idelim))
674 __in.rdbuf()->sbumpc();
677 __err |= __ios_base::failbit;
679 catch(__cxxabiv1::__forced_unwind&)
681 __in._M_setstate(__ios_base::badbit);
682 __throw_exception_again;
686 // _GLIBCXX_RESOLVE_LIB_DEFECTS
687 // 91. Description of operator>> and getline() for string<>
688 // might cause endless loop
689 __in._M_setstate(__ios_base::badbit);
693 __err |= __ios_base::failbit;
695 __in.setstate(__err);
699 _GLIBCXX_END_NAMESPACE
701 #endif // _VSTRING_TCC