Merge from mainline (168000:168310).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / include / ext / vstring.tcc
blob6908e1f39794b68194792e155f94fcd98768fdba
1 // Versatile string -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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}
28  */
30 #ifndef _VSTRING_TCC
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>
46     void
47     __versa_string<_CharT, _Traits, _Alloc, _Base>::
48     resize(size_type __n, _CharT __c)
49     {
50       const size_type __size = this->size();
51       if (__size < __n)
52         this->append(__n - __size, __c);
53       else if (__n < __size)
54         this->_M_erase(__n, __size - __n);
55     }
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)
62     {
63       const size_type __len = __n + this->size();
65       if (__len <= this->capacity() && !this->_M_is_shared())
66         {
67           if (__n)
68             this->_S_copy(this->_M_data() + this->size(), __s, __n);
69         }
70       else
71         this->_M_mutate(this->size(), size_type(0), __s, __n);
73       this->_M_set_length(__len);
74       return *this;
75     }
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)
84       {
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(),
88                           __s.size());
89       }
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,
96                    _CharT __c)
97     {
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())
104         {
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);
110         }
111       else
112         this->_M_mutate(__pos1, __n1, 0, __n2);
114       if (__n2)
115         this->_S_assign(this->_M_data() + __pos1, __n2, __c);
117       this->_M_set_length(__new_size);
118       return *this;
119     }
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)
127     {
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;
132       
133       if (__new_size <= this->capacity() && !this->_M_is_shared())
134         {
135           _CharT* __p = this->_M_data() + __pos;
137           const size_type __how_much = __old_size - __pos - __len1;
138           if (_M_disjunct(__s))
139             {
140               if (__how_much && __len1 != __len2)
141                 this->_S_move(__p + __len2, __p + __len1, __how_much);
142               if (__len2)
143                 this->_S_copy(__p, __s, __len2);
144             }
145           else
146             {
147               // Work in-place.
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);
152               if (__len2 > __len1)
153                 {
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);
158                   else
159                     {
160                       const size_type __nleft = (__p + __len1) - __s;
161                       this->_S_move(__p, __s, __nleft);
162                       this->_S_copy(__p + __nleft, __p + __len2,
163                                     __len2 - __nleft);
164                     }
165                 }
166             }
167         }
168       else
169         this->_M_mutate(__pos, __len1, __s, __len2);
171       this->_M_set_length(__new_size);
172       return *this;
173     }
174   
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)
180     {
181       __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
182       __str.reserve(__lhs.size() + __rhs.size());
183       __str.append(__lhs);
184       __str.append(__rhs);
185       return __str;
186     }
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)
193     {
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);
198       __string_type __str;
199       __str.reserve(__len + __rhs.size());
200       __str.append(__lhs, __len);
201       __str.append(__rhs);
202       return __str;
203     }
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)
210     {
211       __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
212       __str.reserve(__rhs.size() + 1);
213       __str.push_back(__lhs);
214       __str.append(__rhs);
215       return __str;
216     }
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,
222               const _CharT* __rhs)
223     {
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);
228       __string_type __str;
229       __str.reserve(__lhs.size() + __len);
230       __str.append(__lhs);
231       __str.append(__rhs, __len);
232       return __str;
233     }
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,
239               _CharT __rhs)
240     {
241       __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
242       __str.reserve(__lhs.size() + 1);
243       __str.append(__lhs);
244       __str.push_back(__rhs);
245       return __str;
246     }
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
253     {
254       _M_check(__pos, "__versa_string::copy");
255       __n = _M_limit(__pos, __n);
256       __glibcxx_requires_string_len(__s, __n);
257       if (__n)
258         this->_S_copy(__s, this->_M_data() + __pos, __n);
259       // 21.3.5.7 par 3: do not append null.  (good.)
260       return __n;
261     }
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
268     {
269       __glibcxx_requires_string_len(__s, __n);
270       const size_type __size = this->size();
271       const _CharT* __data = this->_M_data();
273       if (__n == 0)
274         return __pos <= __size ? __pos : npos;
276       if (__n <= __size)
277         {
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)
282               return __pos;
283         }
284       return npos;
285     }
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
292     {
293       size_type __ret = npos;
294       const size_type __size = this->size();
295       if (__pos < __size)
296         {
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);
300           if (__p)
301             __ret = __p - __data;
302         }
303       return __ret;
304     }
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
311     {
312       __glibcxx_requires_string_len(__s, __n);
313       const size_type __size = this->size();
314       if (__n <= __size)
315         {
316           __pos = std::min(size_type(__size - __n), __pos);
317           const _CharT* __data = this->_M_data();
318           do
319             {
320               if (traits_type::compare(__data + __pos, __s, __n) == 0)
321                 return __pos;
322             }
323           while (__pos-- > 0);
324         }
325       return npos;
326     }
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
333     {
334       size_type __size = this->size();
335       if (__size)
336         {
337           if (--__size > __pos)
338             __size = __pos;
339           for (++__size; __size-- > 0; )
340             if (traits_type::eq(this->_M_data()[__size], __c))
341               return __size;
342         }
343       return npos;
344     }
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
351     {
352       __glibcxx_requires_string_len(__s, __n);
353       for (; __n && __pos < this->size(); ++__pos)
354         {
355           const _CharT* __p = traits_type::find(__s, __n,
356                                                 this->_M_data()[__pos]);
357           if (__p)
358             return __pos;
359         }
360       return npos;
361     }
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
368     {
369       __glibcxx_requires_string_len(__s, __n);
370       size_type __size = this->size();
371       if (__size && __n)
372         {
373           if (--__size > __pos)
374             __size = __pos;
375           do
376             {
377               if (traits_type::find(__s, __n, this->_M_data()[__size]))
378                 return __size;
379             }
380           while (__size-- != 0);
381         }
382       return npos;
383     }
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
390     {
391       __glibcxx_requires_string_len(__s, __n);
392       for (; __pos < this->size(); ++__pos)
393         if (!traits_type::find(__s, __n, this->_M_data()[__pos]))
394           return __pos;
395       return npos;
396     }
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
403     {
404       for (; __pos < this->size(); ++__pos)
405         if (!traits_type::eq(this->_M_data()[__pos], __c))
406           return __pos;
407       return npos;
408     }
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
415     {
416       __glibcxx_requires_string_len(__s, __n);
417       size_type __size = this->size();
418       if (__size)
419         {
420           if (--__size > __pos)
421             __size = __pos;
422           do
423             {
424               if (!traits_type::find(__s, __n, this->_M_data()[__size]))
425                 return __size;
426             }
427           while (__size--);
428         }
429       return npos;
430     }
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
437     {
438       size_type __size = this->size();
439       if (__size)
440         {
441           if (--__size > __pos)
442             __size = __pos;
443           do
444             {
445               if (!traits_type::eq(this->_M_data()[__size], __c))
446                 return __size;
447             }
448           while (__size--);
449         }
450       return npos;
451     }
453   template<typename _CharT, typename _Traits, typename _Alloc,
454            template <typename, typename, typename> class _Base>
455     int
456     __versa_string<_CharT, _Traits, _Alloc, _Base>::
457     compare(size_type __pos, size_type __n, const __versa_string& __str) const
458     {
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);
465       if (!__r)
466         __r = _S_compare(__n, __osize);
467       return __r;
468     }
470   template<typename _CharT, typename _Traits, typename _Alloc,
471            template <typename, typename, typename> class _Base>
472     int
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
476     {
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);
484       if (!__r)
485         __r = _S_compare(__n1, __n2);
486       return __r;
487     }
489   template<typename _CharT, typename _Traits, typename _Alloc,
490            template <typename, typename, typename> class _Base>
491     int
492     __versa_string<_CharT, _Traits, _Alloc, _Base>::
493     compare(const _CharT* __s) const
494     {
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);
500       if (!__r)
501         __r = _S_compare(__size, __osize);
502       return __r;
503     }
505   template<typename _CharT, typename _Traits, typename _Alloc,
506            template <typename, typename, typename> class _Base>
507     int
508     __versa_string <_CharT, _Traits, _Alloc, _Base>::
509     compare(size_type __pos, size_type __n1, const _CharT* __s) const
510     {
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);
517       if (!__r)
518         __r = _S_compare(__n1, __osize);
519       return __r;
520     }
522   template<typename _CharT, typename _Traits, typename _Alloc,
523            template <typename, typename, typename> class _Base>
524     int
525     __versa_string <_CharT, _Traits, _Alloc, _Base>::
526     compare(size_type __pos, size_type __n1, const _CharT* __s,
527             size_type __n2) const
528     {
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);
534       if (!__r)
535         __r = _S_compare(__n1, __n2);
536       return __r;
537     }
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)
549     {
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>
553                                                         __string_type;
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);
562       if (__cerb)
563         {
564           __try
565             {
566               // Avoid reallocation for common case.
567               __str.erase();
568               _CharT __buf[128];
569               __size_type __len = 0;
570               const streamsize __w = __in.width();
571               const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
572                                               : __str.max_size();
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)))
581                 {
582                   if (__len == sizeof(__buf) / sizeof(_CharT))
583                     {
584                       __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
585                       __len = 0;
586                     }
587                   __buf[__len++] = _Traits::to_char_type(__c);
588                   ++__extracted;
589                   __c = __in.rdbuf()->snextc();
590                 }
591               __str.append(__buf, __len);
593               if (_Traits::eq_int_type(__c, __eof))
594                 __err |= __ios_base::eofbit;
595               __in.width(0);
596             }
597           __catch(__cxxabiv1::__forced_unwind&)
598             {
599               __in._M_setstate(__ios_base::badbit);
600               __throw_exception_again;
601             }
602           __catch(...)
603             {
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);
608             }
609         }
610       // 211.  operator>>(istream&, string&) doesn't set failbit
611       if (!__extracted)
612         __err |= __ios_base::failbit;
613       if (__err)
614         __in.setstate(__err);
615       return __in;
616     }      
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,
623             _CharT __delim)
624     {
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>
628                                                         __string_type;
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);
636       if (__cerb)
637         {
638           __try
639             {
640               // Avoid reallocation for common case.
641               __str.erase();
642               _CharT __buf[128];
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))
651                 {
652                   if (__len == sizeof(__buf) / sizeof(_CharT))
653                     {
654                       __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
655                       __len = 0;
656                     }
657                   __buf[__len++] = _Traits::to_char_type(__c);
658                   ++__extracted;
659                   __c = __in.rdbuf()->snextc();
660                 }
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))
666                 {
667                   ++__extracted;                  
668                   __in.rdbuf()->sbumpc();
669                 }
670               else
671                 __err |= __ios_base::failbit;
672             }
673           __catch(__cxxabiv1::__forced_unwind&)
674             {
675               __in._M_setstate(__ios_base::badbit);
676               __throw_exception_again;
677             }
678           __catch(...)
679             {
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);
684             }
685         }
686       if (!__extracted)
687         __err |= __ios_base::failbit;
688       if (__err)
689         __in.setstate(__err);
690       return __in;
691     }      
693 _GLIBCXX_END_NAMESPACE
695 #endif // _VSTRING_TCC