1 // Versatile string -*- C++ -*-
3 // Copyright (C) 2005 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
43 template<typename _CharT, typename _Traits, typename _Alloc,
44 template <typename, typename, typename> class _Base>
45 const typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
46 __versa_string<_CharT, _Traits, _Alloc, _Base>::npos;
48 template<typename _CharT, typename _Traits, typename _Alloc,
49 template <typename, typename, typename> class _Base>
51 __versa_string<_CharT, _Traits, _Alloc, _Base>::
52 resize(size_type __n, _CharT __c)
54 const size_type __size = this->size();
56 this->append(__n - __size, __c);
57 else if (__n < __size)
58 this->_M_erase(__n, __size - __n);
61 template<typename _CharT, typename _Traits, typename _Alloc,
62 template <typename, typename, typename> class _Base>
63 __versa_string<_CharT, _Traits, _Alloc, _Base>&
64 __versa_string<_CharT, _Traits, _Alloc, _Base>::
65 _M_append(const _CharT* __s, size_type __n)
67 const size_type __len = __n + this->size();
69 if (__len <= this->capacity() && !this->_M_is_shared())
72 this->_S_copy(this->_M_data() + this->size(), __s, __n);
75 this->_M_mutate(this->size(), size_type(0), __s, __n);
77 this->_M_set_length(__len);
81 template<typename _CharT, typename _Traits, typename _Alloc,
82 template <typename, typename, typename> class _Base>
83 template<typename _InputIterator>
84 __versa_string<_CharT, _Traits, _Alloc, _Base>&
85 __versa_string<_CharT, _Traits, _Alloc, _Base>::
86 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
87 _InputIterator __k2, __false_type)
89 const __versa_string __s(__k1, __k2);
90 const size_type __n1 = __i2 - __i1;
91 return _M_replace(__i1 - _M_ibegin(), __n1, __s._M_data(),
95 template<typename _CharT, typename _Traits, typename _Alloc,
96 template <typename, typename, typename> class _Base>
97 __versa_string<_CharT, _Traits, _Alloc, _Base>&
98 __versa_string<_CharT, _Traits, _Alloc, _Base>::
99 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
102 _M_check_length(__n1, __n2, "__versa_string::_M_replace_aux");
104 const size_type __old_size = this->size();
105 const size_type __new_size = __old_size + __n2 - __n1;
107 if (__new_size <= this->capacity() && !this->_M_is_shared())
109 _CharT* __p = this->_M_data() + __pos1;
111 const size_type __how_much = __old_size - __pos1 - __n1;
112 if (__how_much && __n1 != __n2)
113 this->_S_move(__p + __n2, __p + __n1, __how_much);
116 this->_M_mutate(__pos1, __n1, 0, __n2);
119 this->_S_assign(this->_M_data() + __pos1, __n2, __c);
121 this->_M_set_length(__new_size);
125 template<typename _CharT, typename _Traits, typename _Alloc,
126 template <typename, typename, typename> class _Base>
127 __versa_string<_CharT, _Traits, _Alloc, _Base>&
128 __versa_string<_CharT, _Traits, _Alloc, _Base>::
129 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
130 const size_type __len2)
132 _M_check_length(__len1, __len2, "__versa_string::_M_replace");
134 const size_type __old_size = this->size();
135 const size_type __new_size = __old_size + __len2 - __len1;
137 if (__new_size <= this->capacity() && !this->_M_is_shared())
139 _CharT* __p = this->_M_data() + __pos;
141 const size_type __how_much = __old_size - __pos - __len1;
142 if (_M_disjunct(__s))
144 if (__how_much && __len1 != __len2)
145 this->_S_move(__p + __len2, __p + __len1, __how_much);
147 this->_S_copy(__p, __s, __len2);
152 if (__len2 && __len2 <= __len1)
153 this->_S_move(__p, __s, __len2);
154 if (__how_much && __len1 != __len2)
155 this->_S_move(__p + __len2, __p + __len1, __how_much);
158 if (__s + __len2 <= __p + __len1)
159 this->_S_move(__p, __s, __len2);
160 else if (__s >= __p + __len1)
161 this->_S_copy(__p, __s + __len2 - __len1, __len2);
164 const size_type __nleft = (__p + __len1) - __s;
165 this->_S_move(__p, __s, __nleft);
166 this->_S_copy(__p + __nleft, __p + __len2,
173 this->_M_mutate(__pos, __len1, __s, __len2);
175 this->_M_set_length(__new_size);
179 template<typename _CharT, typename _Traits, typename _Alloc,
180 template <typename, typename, typename> class _Base>
181 __versa_string<_CharT, _Traits, _Alloc, _Base>
182 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
183 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
185 __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
186 __str.reserve(__lhs.size() + __rhs.size());
192 template<typename _CharT, typename _Traits, typename _Alloc,
193 template <typename, typename, typename> class _Base>
194 __versa_string<_CharT, _Traits, _Alloc, _Base>
195 operator+(const _CharT* __lhs,
196 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
198 __glibcxx_requires_string(__lhs);
199 typedef __versa_string<_CharT, _Traits, _Alloc, _Base> __string_type;
200 typedef typename __string_type::size_type __size_type;
201 const __size_type __len = _Traits::length(__lhs);
203 __str.reserve(__len + __rhs.size());
204 __str.append(__lhs, __len);
209 template<typename _CharT, typename _Traits, typename _Alloc,
210 template <typename, typename, typename> class _Base>
211 __versa_string<_CharT, _Traits, _Alloc, _Base>
212 operator+(_CharT __lhs,
213 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
215 __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
216 __str.reserve(__rhs.size() + 1);
217 __str.push_back(__lhs);
222 template<typename _CharT, typename _Traits, typename _Alloc,
223 template <typename, typename, typename> class _Base>
224 __versa_string<_CharT, _Traits, _Alloc, _Base>
225 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
228 __glibcxx_requires_string(__rhs);
229 typedef __versa_string<_CharT, _Traits, _Alloc, _Base> __string_type;
230 typedef typename __string_type::size_type __size_type;
231 const __size_type __len = _Traits::length(__rhs);
233 __str.reserve(__lhs.size() + __len);
235 __str.append(__rhs, __len);
239 template<typename _CharT, typename _Traits, typename _Alloc,
240 template <typename, typename, typename> class _Base>
241 __versa_string<_CharT, _Traits, _Alloc, _Base>
242 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
245 __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
246 __str.reserve(__lhs.size() + 1);
248 __str.push_back(__rhs);
252 template<typename _CharT, typename _Traits, typename _Alloc,
253 template <typename, typename, typename> class _Base>
254 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
255 __versa_string<_CharT, _Traits, _Alloc, _Base>::
256 copy(_CharT* __s, size_type __n, size_type __pos) const
258 _M_check(__pos, "__versa_string::copy");
259 __n = _M_limit(__pos, __n);
260 __glibcxx_requires_string_len(__s, __n);
262 this->_S_copy(__s, this->_M_data() + __pos, __n);
263 // 21.3.5.7 par 3: do not append null. (good.)
267 template<typename _CharT, typename _Traits, typename _Alloc,
268 template <typename, typename, typename> class _Base>
269 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type
270 __versa_string<_CharT, _Traits, _Alloc, _Base>::
271 find(const _CharT* __s, size_type __pos, size_type __n) const
273 __glibcxx_requires_string_len(__s, __n);
274 size_type __ret = npos;
275 const size_type __size = this->size();
276 if (__pos + __n <= __size)
278 const _CharT* __data = this->_M_data();
279 const _CharT* __p = std::search(__data + __pos, __data + __size,
280 __s, __s + __n, traits_type::eq);
281 if (__p != __data + __size || __n == 0)
282 __ret = __p - __data;
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);
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);
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 = __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 = __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);
539 } // namespace __gnu_cxx
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::int_type __int_type;
552 typedef typename __istream_type::__streambuf_type __streambuf_type;
553 typedef typename __istream_type::__ctype_type __ctype_type;
554 typedef __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>
556 typedef typename __string_type::size_type __size_type;
558 __size_type __extracted = 0;
559 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
560 typename __istream_type::sentry __cerb(__in, false);
565 // Avoid reallocation for common case.
568 __size_type __len = 0;
569 const streamsize __w = __in.width();
570 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
572 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
573 const __int_type __eof = _Traits::eof();
574 __streambuf_type* __sb = __in.rdbuf();
575 __int_type __c = __sb->sgetc();
577 while (__extracted < __n
578 && !_Traits::eq_int_type(__c, __eof)
579 && !__ct.is(ctype_base::space, _Traits::to_char_type(__c)))
581 if (__len == sizeof(__buf) / sizeof(_CharT))
583 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
586 __buf[__len++] = _Traits::to_char_type(__c);
588 __c = __sb->snextc();
590 __str.append(__buf, __len);
592 if (_Traits::eq_int_type(__c, __eof))
593 __err |= ios_base::eofbit;
598 // _GLIBCXX_RESOLVE_LIB_DEFECTS
599 // 91. Description of operator>> and getline() for string<>
600 // might cause endless loop
601 __in._M_setstate(ios_base::badbit);
604 // 211. operator>>(istream&, string&) doesn't set failbit
606 __err |= ios_base::failbit;
608 __in.setstate(__err);
612 template<typename _CharT, typename _Traits, typename _Alloc,
613 template <typename, typename, typename> class _Base>
614 basic_ostream<_CharT, _Traits>&
615 operator<<(basic_ostream<_CharT, _Traits>& __out,
616 const __gnu_cxx::__versa_string<_CharT, _Traits,
617 _Alloc, _Base>& __str)
619 typedef basic_ostream<_CharT, _Traits> __ostream_type;
621 typename __ostream_type::sentry __cerb(__out);
624 const streamsize __w = __out.width();
625 streamsize __len = static_cast<streamsize>(__str.size());
626 const _CharT* __s = __str.data();
628 // _GLIBCXX_RESOLVE_LIB_DEFECTS
629 // 25. String operator<< uses width() value wrong
632 _CharT* __cs = (static_cast<
633 _CharT*>(__builtin_alloca(sizeof(_CharT) * __w)));
634 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs,
635 __s, __w, __len, false);
639 __out._M_write(__s, __len);
645 template<typename _CharT, typename _Traits, typename _Alloc,
646 template <typename, typename, typename> class _Base>
647 basic_istream<_CharT, _Traits>&
648 getline(basic_istream<_CharT, _Traits>& __in,
649 __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str,
652 typedef basic_istream<_CharT, _Traits> __istream_type;
653 typedef typename __istream_type::int_type __int_type;
654 typedef typename __istream_type::__streambuf_type __streambuf_type;
655 typedef typename __istream_type::__ctype_type __ctype_type;
656 typedef __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>
658 typedef typename __string_type::size_type __size_type;
660 __size_type __extracted = 0;
661 const __size_type __n = __str.max_size();
662 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
663 typename __istream_type::sentry __cerb(__in, true);
668 // Avoid reallocation for common case.
671 __size_type __len = 0;
672 const __int_type __idelim = _Traits::to_int_type(__delim);
673 const __int_type __eof = _Traits::eof();
674 __streambuf_type* __sb = __in.rdbuf();
675 __int_type __c = __sb->sgetc();
677 while (__extracted < __n
678 && !_Traits::eq_int_type(__c, __eof)
679 && !_Traits::eq_int_type(__c, __idelim))
681 if (__len == sizeof(__buf) / sizeof(_CharT))
683 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
686 __buf[__len++] = _Traits::to_char_type(__c);
688 __c = __sb->snextc();
690 __str.append(__buf, __len);
692 if (_Traits::eq_int_type(__c, __eof))
693 __err |= ios_base::eofbit;
694 else if (_Traits::eq_int_type(__c, __idelim))
700 __err |= ios_base::failbit;
704 // _GLIBCXX_RESOLVE_LIB_DEFECTS
705 // 91. Description of operator>> and getline() for string<>
706 // might cause endless loop
707 __in._M_setstate(ios_base::badbit);
711 __err |= ios_base::failbit;
713 __in.setstate(__err);
719 #endif // _VSTRING_TCC