Import a stripped down version of gcc-4.1.1
[dragonfly.git] / contrib / gcc-4.1 / libstdc++-v3 / include / ext / vstring.tcc
1 // Versatile string -*- C++ -*-
2
3 // Copyright (C) 2005 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 2, or (at your option)
9 // any later version.
10
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.
15
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,
19 // USA.
20
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.
29
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.
34  */
35
36 #ifndef _VSTRING_TCC
37 #define _VSTRING_TCC 1
38
39 #pragma GCC system_header
40
41 namespace __gnu_cxx
42 {
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;
47
48   template<typename _CharT, typename _Traits, typename _Alloc,
49            template <typename, typename, typename> class _Base>
50     void
51     __versa_string<_CharT, _Traits, _Alloc, _Base>::
52     resize(size_type __n, _CharT __c)
53     {
54       const size_type __size = this->size();
55       if (__size < __n)
56         this->append(__n - __size, __c);
57       else if (__n < __size)
58         this->_M_erase(__n, __size - __n);
59     }
60
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)
66     {
67       const size_type __len = __n + this->size();
68
69       if (__len <= this->capacity() && !this->_M_is_shared())
70         {
71           if (__n)
72             this->_S_copy(this->_M_data() + this->size(), __s, __n);
73         }
74       else
75         this->_M_mutate(this->size(), size_type(0), __s, __n);
76
77       this->_M_set_length(__len);
78       return *this;
79     }
80
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)
88       {
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(),
92                           __s.size());
93       }
94
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,
100                    _CharT __c)
101     {
102       _M_check_length(__n1, __n2, "__versa_string::_M_replace_aux");
103
104       const size_type __old_size = this->size();
105       const size_type __new_size = __old_size + __n2 - __n1;
106
107       if (__new_size <= this->capacity() && !this->_M_is_shared())
108         {
109           _CharT* __p = this->_M_data() + __pos1;
110
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);
114         }
115       else
116         this->_M_mutate(__pos1, __n1, 0, __n2);
117
118       if (__n2)
119         this->_S_assign(this->_M_data() + __pos1, __n2, __c);
120
121       this->_M_set_length(__new_size);
122       return *this;
123     }
124
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)
131     {
132       _M_check_length(__len1, __len2, "__versa_string::_M_replace");
133
134       const size_type __old_size = this->size();
135       const size_type __new_size = __old_size + __len2 - __len1;
136       
137       if (__new_size <= this->capacity() && !this->_M_is_shared())
138         {
139           _CharT* __p = this->_M_data() + __pos;
140
141           const size_type __how_much = __old_size - __pos - __len1;
142           if (_M_disjunct(__s))
143             {
144               if (__how_much && __len1 != __len2)
145                 this->_S_move(__p + __len2, __p + __len1, __how_much);
146               if (__len2)
147                 this->_S_copy(__p, __s, __len2);
148             }
149           else
150             {
151               // Work in-place.
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);
156               if (__len2 > __len1)
157                 {
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);
162                   else
163                     {
164                       const size_type __nleft = (__p + __len1) - __s;
165                       this->_S_move(__p, __s, __nleft);
166                       this->_S_copy(__p + __nleft, __p + __len2,
167                                     __len2 - __nleft);
168                     }
169                 }
170             }
171         }
172       else
173         this->_M_mutate(__pos, __len1, __s, __len2);
174
175       this->_M_set_length(__new_size);
176       return *this;
177     }
178   
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)
184     {
185       __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
186       __str.reserve(__lhs.size() + __rhs.size());
187       __str.append(__lhs);
188       __str.append(__rhs);
189       return __str;
190     }
191
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)
197     {
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);
202       __string_type __str;
203       __str.reserve(__len + __rhs.size());
204       __str.append(__lhs, __len);
205       __str.append(__rhs);
206       return __str;
207     }
208
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)
214     {
215       __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
216       __str.reserve(__rhs.size() + 1);
217       __str.push_back(__lhs);
218       __str.append(__rhs);
219       return __str;
220     }
221
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,
226               const _CharT* __rhs)
227     {
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);
232       __string_type __str;
233       __str.reserve(__lhs.size() + __len);
234       __str.append(__lhs);
235       __str.append(__rhs, __len);
236       return __str;
237     }
238
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,
243               _CharT __rhs)
244     {
245       __versa_string<_CharT, _Traits, _Alloc, _Base> __str;
246       __str.reserve(__lhs.size() + 1);
247       __str.append(__lhs);
248       __str.push_back(__rhs);
249       return __str;
250     }
251
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
257     {
258       _M_check(__pos, "__versa_string::copy");
259       __n = _M_limit(__pos, __n);
260       __glibcxx_requires_string_len(__s, __n);
261       if (__n)
262         this->_S_copy(__s, this->_M_data() + __pos, __n);
263       // 21.3.5.7 par 3: do not append null.  (good.)
264       return __n;
265     }
266
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
272     {
273       __glibcxx_requires_string_len(__s, __n);
274       size_type __ret = npos;
275       const size_type __size = this->size();
276       if (__pos + __n <= __size)
277         {
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;
283         }
284       return __ret;
285     }
286
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     }
305
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     }
327
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     }
345
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     }
362
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     }
384
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     }
397
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     }
409
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     }
431
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     }
452
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 = __n - __osize;
467       return __r;
468     }
469
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 = __n1 - __n2;
486       return __r;
487     }
488
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 = __size - __osize;
502       return __r;
503     }
504
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 = __n1 - __osize;
519       return __r;
520     }
521
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 = __n1 - __n2;
536       return __r;
537     }
538
539 } // namespace __gnu_cxx
540
541 namespace std
542 {
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::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>
555                                                         __string_type;
556       typedef typename __string_type::size_type         __size_type;
557
558       __size_type __extracted = 0;
559       ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
560       typename __istream_type::sentry __cerb(__in, false);
561       if (__cerb)
562         {
563           try
564             {
565               // Avoid reallocation for common case.
566               __str.erase();
567               _CharT __buf[128];
568               __size_type __len = 0;
569               const streamsize __w = __in.width();
570               const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
571                                               : __str.max_size();
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();
576
577               while (__extracted < __n
578                      && !_Traits::eq_int_type(__c, __eof)
579                      && !__ct.is(ctype_base::space, _Traits::to_char_type(__c)))
580                 {
581                   if (__len == sizeof(__buf) / sizeof(_CharT))
582                     {
583                       __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
584                       __len = 0;
585                     }
586                   __buf[__len++] = _Traits::to_char_type(__c);
587                   ++__extracted;
588                   __c = __sb->snextc();
589                 }
590               __str.append(__buf, __len);
591
592               if (_Traits::eq_int_type(__c, __eof))
593                 __err |= ios_base::eofbit;
594               __in.width(0);
595             }
596           catch(...)
597             {
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);
602             }
603         }
604       // 211.  operator>>(istream&, string&) doesn't set failbit
605       if (!__extracted)
606         __err |= ios_base::failbit;
607       if (__err)
608         __in.setstate(__err);
609       return __in;
610     }      
611
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)
618     {
619       typedef basic_ostream<_CharT, _Traits>            __ostream_type;
620
621       typename __ostream_type::sentry __cerb(__out);
622       if (__cerb)
623         {
624           const streamsize __w = __out.width();
625           streamsize __len = static_cast<streamsize>(__str.size());
626           const _CharT* __s = __str.data();
627
628           // _GLIBCXX_RESOLVE_LIB_DEFECTS
629           // 25. String operator<< uses width() value wrong
630           if (__w > __len)
631             {
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);
636               __s = __cs;
637               __len = __w;
638             }
639           __out._M_write(__s, __len);
640           __out.width(0);
641         }
642       return __out;
643     }
644
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,
650             _CharT __delim)
651     {
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>
657                                                         __string_type;
658       typedef typename __string_type::size_type         __size_type;
659
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);
664       if (__cerb)
665         {
666           try
667             {
668               // Avoid reallocation for common case.
669               __str.erase();
670               _CharT __buf[128];
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();
676
677               while (__extracted < __n
678                      && !_Traits::eq_int_type(__c, __eof)
679                      && !_Traits::eq_int_type(__c, __idelim))
680                 {
681                   if (__len == sizeof(__buf) / sizeof(_CharT))
682                     {
683                       __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
684                       __len = 0;
685                     }
686                   __buf[__len++] = _Traits::to_char_type(__c);
687                   ++__extracted;
688                   __c = __sb->snextc();
689                 }
690               __str.append(__buf, __len);
691
692               if (_Traits::eq_int_type(__c, __eof))
693                 __err |= ios_base::eofbit;
694               else if (_Traits::eq_int_type(__c, __idelim))
695                 {
696                   ++__extracted;                  
697                   __sb->sbumpc();
698                 }
699               else
700                 __err |= ios_base::failbit;
701             }
702           catch(...)
703             {
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);
708             }
709         }
710       if (!__extracted)
711         __err |= ios_base::failbit;
712       if (__err)
713         __in.setstate(__err);
714       return __in;
715     }      
716   
717 } // namespace std
718
719 #endif // _VSTRING_TCC