Branch data Line data Source code
1 : : // Components for manipulating sequences of characters -*- C++ -*-
2 : :
3 : : // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 : : // 2006, 2007
5 : : // Free Software Foundation, Inc.
6 : : //
7 : : // This file is part of the GNU ISO C++ Library. This library is free
8 : : // software; you can redistribute it and/or modify it under the
9 : : // terms of the GNU General Public License as published by the
10 : : // Free Software Foundation; either version 2, or (at your option)
11 : : // any later version.
12 : :
13 : : // This library is distributed in the hope that it will be useful,
14 : : // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : : // GNU General Public License for more details.
17 : :
18 : : // You should have received a copy of the GNU General Public License along
19 : : // with this library; see the file COPYING. If not, write to the Free
20 : : // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 : : // USA.
22 : :
23 : : // As a special exception, you may use this file as part of a free software
24 : : // library without restriction. Specifically, if other files instantiate
25 : : // templates or use macros or inline functions from this file, or you compile
26 : : // this file and link it with other files to produce an executable, this
27 : : // file does not by itself cause the resulting executable to be covered by
28 : : // the GNU General Public License. This exception does not however
29 : : // invalidate any other reasons why the executable file might be covered by
30 : : // the GNU General Public License.
31 : :
32 : : /** @file basic_string.tcc
33 : : * This is an internal header file, included by other library headers.
34 : : * You should not attempt to use it directly.
35 : : */
36 : :
37 : : //
38 : : // ISO C++ 14882: 21 Strings library
39 : : //
40 : :
41 : : // Written by Jason Merrill based upon the specification by Takanori Adachi
42 : : // in ANSI X3J16/94-0013R2. Rewritten by Nathan Myers to ISO-14882.
43 : :
44 : : #ifndef _BASIC_STRING_TCC
45 : : #define _BASIC_STRING_TCC 1
46 : :
47 : : #pragma GCC system_header
48 : :
49 : : #include <cxxabi-forced.h>
50 : :
51 : : _GLIBCXX_BEGIN_NAMESPACE(std)
52 : :
53 : : template<typename _CharT, typename _Traits, typename _Alloc>
54 : : const typename basic_string<_CharT, _Traits, _Alloc>::size_type
55 : : basic_string<_CharT, _Traits, _Alloc>::
56 : : _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
57 : :
58 : : template<typename _CharT, typename _Traits, typename _Alloc>
59 : : const _CharT
60 : : basic_string<_CharT, _Traits, _Alloc>::
61 : : _Rep::_S_terminal = _CharT();
62 : :
63 : : template<typename _CharT, typename _Traits, typename _Alloc>
64 : : const typename basic_string<_CharT, _Traits, _Alloc>::size_type
65 : : basic_string<_CharT, _Traits, _Alloc>::npos;
66 : :
67 : : // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
68 : : // at static init time (before static ctors are run).
69 : : template<typename _CharT, typename _Traits, typename _Alloc>
70 : : typename basic_string<_CharT, _Traits, _Alloc>::size_type
71 : : basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
72 : : (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
73 : : sizeof(size_type)];
74 : :
75 : : // NB: This is the special case for Input Iterators, used in
76 : : // istreambuf_iterators, etc.
77 : : // Input Iterators have a cost structure very different from
78 : : // pointers, calling for a different coding style.
79 : : template<typename _CharT, typename _Traits, typename _Alloc>
80 : : template<typename _InIterator>
81 : : _CharT*
82 : : basic_string<_CharT, _Traits, _Alloc>::
83 : : _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
84 : : input_iterator_tag)
85 : : {
86 : : if (__beg == __end && __a == _Alloc())
87 : : return _S_empty_rep()._M_refcopy();
88 : :
89 : : // Avoid reallocation for common case.
90 : : _CharT __buf[128];
91 : : size_type __len = 0;
92 : : while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
93 : : {
94 : : __buf[__len++] = *__beg;
95 : : ++__beg;
96 : : }
97 : : _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
98 : : _M_copy(__r->_M_refdata(), __buf, __len);
99 : : try
100 : : {
101 : : while (__beg != __end)
102 : : {
103 : : if (__len == __r->_M_capacity)
104 : : {
105 : : // Allocate more space.
106 : : _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
107 : : _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
108 : : __r->_M_destroy(__a);
109 : : __r = __another;
110 : : }
111 : : __r->_M_refdata()[__len++] = *__beg;
112 : : ++__beg;
113 : : }
114 : : }
115 : : catch(...)
116 : : {
117 : : __r->_M_destroy(__a);
118 : : __throw_exception_again;
119 : : }
120 : : __r->_M_set_length_and_sharable(__len);
121 : : return __r->_M_refdata();
122 : : }
123 : :
124 : : template<typename _CharT, typename _Traits, typename _Alloc>
125 : : template <typename _InIterator>
126 : : _CharT*
127 : : basic_string<_CharT, _Traits, _Alloc>::
128 : : _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
129 : : forward_iterator_tag)
130 : : {
131 [ + + ][ + - ]: 272999 : if (__beg == __end && __a == _Alloc())
[ + + ]
[ + + # # ]
[ # # ][ # # ]
[ # # ][ + + ]
[ + - ][ + + ]
[ + + ]
132 : 4194 : return _S_empty_rep()._M_refcopy();
133 : :
134 : : // NB: Not required, but considered best practice.
135 [ - + ][ # # ]: 264611 : if (__builtin_expect(__gnu_cxx::__is_null_pointer(__beg)
[ - + ][ - + ]
[ # # ][ - + ]
136 : : && __beg != __end, 0))
137 : 0 : __throw_logic_error(__N("basic_string::_S_construct NULL not valid"));
138 : :
139 : : const size_type __dnew = static_cast<size_type>(std::distance(__beg,
140 : 529222 : __end));
141 : : // Check for out_of_range and length_error exceptions.
142 : 264611 : _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
143 : : try
144 : 264611 : { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
145 : : catch(...)
146 : : {
147 : : __r->_M_destroy(__a);
148 : : __throw_exception_again;
149 : : }
150 : : __r->_M_set_length_and_sharable(__dnew);
151 : 264611 : return __r->_M_refdata();
152 : : }
153 : :
154 : : template<typename _CharT, typename _Traits, typename _Alloc>
155 : : _CharT*
156 : : basic_string<_CharT, _Traits, _Alloc>::
157 : : _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
158 : : {
159 : : if (__n == 0 && __a == _Alloc())
160 : : return _S_empty_rep()._M_refcopy();
161 : : // Check for out_of_range and length_error exceptions.
162 : : _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
163 : : if (__n)
164 : : _M_assign(__r->_M_refdata(), __n, __c);
165 : :
166 : : __r->_M_set_length_and_sharable(__n);
167 : : return __r->_M_refdata();
168 : : }
169 : :
170 : : template<typename _CharT, typename _Traits, typename _Alloc>
171 : : basic_string<_CharT, _Traits, _Alloc>::
172 : 73172 : basic_string(const basic_string& __str)
173 : : : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
174 : : __str.get_allocator()),
175 : 73172 : __str.get_allocator())
176 : 73172 : { }
177 : :
178 : : template<typename _CharT, typename _Traits, typename _Alloc>
179 : : basic_string<_CharT, _Traits, _Alloc>::
180 : : basic_string(const _Alloc& __a)
181 : : : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
182 : : { }
183 : :
184 : : template<typename _CharT, typename _Traits, typename _Alloc>
185 : : basic_string<_CharT, _Traits, _Alloc>::
186 : 912 : basic_string(const basic_string& __str, size_type __pos, size_type __n)
187 : : : _M_dataplus(_S_construct(__str._M_data()
188 : : + __str._M_check(__pos,
189 : : "basic_string::basic_string"),
190 : : __str._M_data() + __str._M_limit(__pos, __n)
191 : 912 : + __pos, _Alloc()), _Alloc())
192 : 912 : { }
193 : :
194 : : template<typename _CharT, typename _Traits, typename _Alloc>
195 : : basic_string<_CharT, _Traits, _Alloc>::
196 : : basic_string(const basic_string& __str, size_type __pos,
197 : : size_type __n, const _Alloc& __a)
198 : : : _M_dataplus(_S_construct(__str._M_data()
199 : : + __str._M_check(__pos,
200 : : "basic_string::basic_string"),
201 : : __str._M_data() + __str._M_limit(__pos, __n)
202 : : + __pos, __a), __a)
203 : : { }
204 : :
205 : : // TBD: DPG annotate
206 : : template<typename _CharT, typename _Traits, typename _Alloc>
207 : : basic_string<_CharT, _Traits, _Alloc>::
208 : 4001 : basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
209 : 4001 : : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
210 : 4001 : { }
211 : :
212 : : // TBD: DPG annotate
213 : : template<typename _CharT, typename _Traits, typename _Alloc>
214 : : basic_string<_CharT, _Traits, _Alloc>::
215 : 13509 : basic_string(const _CharT* __s, const _Alloc& __a)
216 : : : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
217 [ + - ]: 27018 : __s + npos, __a), __a)
218 : 13509 : { }
219 : :
220 : : template<typename _CharT, typename _Traits, typename _Alloc>
221 : : basic_string<_CharT, _Traits, _Alloc>::
222 : : basic_string(size_type __n, _CharT __c, const _Alloc& __a)
223 : : : _M_dataplus(_S_construct(__n, __c, __a), __a)
224 : : { }
225 : :
226 : : // TBD: DPG annotate
227 : : template<typename _CharT, typename _Traits, typename _Alloc>
228 : : template<typename _InputIterator>
229 : : basic_string<_CharT, _Traits, _Alloc>::
230 : 250383 : basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a)
231 : 250383 : : _M_dataplus(_S_construct(__beg, __end, __a), __a)
232 : 250383 : { }
233 : :
234 : : template<typename _CharT, typename _Traits, typename _Alloc>
235 : : basic_string<_CharT, _Traits, _Alloc>&
236 : : basic_string<_CharT, _Traits, _Alloc>::
237 : 7 : assign(const basic_string& __str)
238 : : {
239 [ + - ]: 7 : if (_M_rep() != __str._M_rep())
240 : : {
241 : : // XXX MT
242 : : const allocator_type __a = this->get_allocator();
243 : 7 : _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
244 : 7 : _M_rep()->_M_dispose(__a);
245 : 7 : _M_data(__tmp);
246 : : }
247 : 7 : return *this;
248 : : }
249 : :
250 : : template<typename _CharT, typename _Traits, typename _Alloc>
251 : : basic_string<_CharT, _Traits, _Alloc>&
252 : : basic_string<_CharT, _Traits, _Alloc>::
253 : 0 : assign(const _CharT* __s, size_type __n)
254 : : {
255 : : __glibcxx_requires_string_len(__s, __n);
256 : 0 : _M_check_length(this->size(), __n, "basic_string::assign");
257 [ # # ][ # # ]: 0 : if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
[ # # ]
258 : 0 : return _M_replace_safe(size_type(0), this->size(), __s, __n);
259 : : else
260 : : {
261 : : // Work in-place.
262 : 0 : const size_type __pos = __s - _M_data();
263 [ # # ]: 0 : if (__pos >= __n)
264 : : _M_copy(_M_data(), __s, __n);
265 [ # # ]: 0 : else if (__pos)
266 : : _M_move(_M_data(), __s, __n);
267 : : _M_rep()->_M_set_length_and_sharable(__n);
268 : 0 : return *this;
269 : : }
270 : : }
271 : :
272 : : template<typename _CharT, typename _Traits, typename _Alloc>
273 : : basic_string<_CharT, _Traits, _Alloc>&
274 : : basic_string<_CharT, _Traits, _Alloc>::
275 : : append(size_type __n, _CharT __c)
276 : : {
277 : : if (__n)
278 : : {
279 : : _M_check_length(size_type(0), __n, "basic_string::append");
280 : : const size_type __len = __n + this->size();
281 : : if (__len > this->capacity() || _M_rep()->_M_is_shared())
282 : : this->reserve(__len);
283 : : _M_assign(_M_data() + this->size(), __n, __c);
284 : : _M_rep()->_M_set_length_and_sharable(__len);
285 : : }
286 : : return *this;
287 : : }
288 : :
289 : : template<typename _CharT, typename _Traits, typename _Alloc>
290 : : basic_string<_CharT, _Traits, _Alloc>&
291 : : basic_string<_CharT, _Traits, _Alloc>::
292 : 0 : append(const _CharT* __s, size_type __n)
293 : : {
294 : : __glibcxx_requires_string_len(__s, __n);
295 [ # # ]: 0 : if (__n)
296 : : {
297 : 0 : _M_check_length(size_type(0), __n, "basic_string::append");
298 : 0 : const size_type __len = __n + this->size();
299 [ # # ][ # # ]: 0 : if (__len > this->capacity() || _M_rep()->_M_is_shared())
[ # # ]
300 : : {
301 [ # # ]: 0 : if (_M_disjunct(__s))
302 : 0 : this->reserve(__len);
303 : : else
304 : : {
305 : 0 : const size_type __off = __s - _M_data();
306 : 0 : this->reserve(__len);
307 : 0 : __s = _M_data() + __off;
308 : : }
309 : : }
310 : 0 : _M_copy(_M_data() + this->size(), __s, __n);
311 : : _M_rep()->_M_set_length_and_sharable(__len);
312 : : }
313 : 0 : return *this;
314 : : }
315 : :
316 : : template<typename _CharT, typename _Traits, typename _Alloc>
317 : : basic_string<_CharT, _Traits, _Alloc>&
318 : : basic_string<_CharT, _Traits, _Alloc>::
319 : 126680 : append(const basic_string& __str)
320 : : {
321 : 126680 : const size_type __size = __str.size();
322 [ + + ]: 126680 : if (__size)
323 : : {
324 : 125045 : const size_type __len = __size + this->size();
325 [ + + ][ + + ]: 125045 : if (__len > this->capacity() || _M_rep()->_M_is_shared())
[ + + ]
326 : 34144 : this->reserve(__len);
327 : 125045 : _M_copy(_M_data() + this->size(), __str._M_data(), __size);
328 : : _M_rep()->_M_set_length_and_sharable(__len);
329 : : }
330 : 126680 : return *this;
331 : : }
332 : :
333 : : template<typename _CharT, typename _Traits, typename _Alloc>
334 : : basic_string<_CharT, _Traits, _Alloc>&
335 : : basic_string<_CharT, _Traits, _Alloc>::
336 : : append(const basic_string& __str, size_type __pos, size_type __n)
337 : : {
338 : : __str._M_check(__pos, "basic_string::append");
339 : : __n = __str._M_limit(__pos, __n);
340 : : if (__n)
341 : : {
342 : : const size_type __len = __n + this->size();
343 : : if (__len > this->capacity() || _M_rep()->_M_is_shared())
344 : : this->reserve(__len);
345 : : _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
346 : : _M_rep()->_M_set_length_and_sharable(__len);
347 : : }
348 : : return *this;
349 : : }
350 : :
351 : : template<typename _CharT, typename _Traits, typename _Alloc>
352 : : basic_string<_CharT, _Traits, _Alloc>&
353 : : basic_string<_CharT, _Traits, _Alloc>::
354 : : insert(size_type __pos, const _CharT* __s, size_type __n)
355 : : {
356 : : __glibcxx_requires_string_len(__s, __n);
357 : : _M_check(__pos, "basic_string::insert");
358 : : _M_check_length(size_type(0), __n, "basic_string::insert");
359 : : if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
360 : : return _M_replace_safe(__pos, size_type(0), __s, __n);
361 : : else
362 : : {
363 : : // Work in-place.
364 : : const size_type __off = __s - _M_data();
365 : : _M_mutate(__pos, 0, __n);
366 : : __s = _M_data() + __off;
367 : : _CharT* __p = _M_data() + __pos;
368 : : if (__s + __n <= __p)
369 : : _M_copy(__p, __s, __n);
370 : : else if (__s >= __p)
371 : : _M_copy(__p, __s + __n, __n);
372 : : else
373 : : {
374 : : const size_type __nleft = __p - __s;
375 : : _M_copy(__p, __s, __nleft);
376 : : _M_copy(__p + __nleft, __p + __n, __n - __nleft);
377 : : }
378 : : return *this;
379 : : }
380 : : }
381 : :
382 : : template<typename _CharT, typename _Traits, typename _Alloc>
383 : : basic_string<_CharT, _Traits, _Alloc>&
384 : : basic_string<_CharT, _Traits, _Alloc>::
385 : : replace(size_type __pos, size_type __n1, const _CharT* __s,
386 : 0 : size_type __n2)
387 : : {
388 : : __glibcxx_requires_string_len(__s, __n2);
389 : 0 : _M_check(__pos, "basic_string::replace");
390 : 0 : __n1 = _M_limit(__pos, __n1);
391 : 0 : _M_check_length(__n1, __n2, "basic_string::replace");
392 : : bool __left;
393 [ # # ][ # # ]: 0 : if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
[ # # ]
394 : 0 : return _M_replace_safe(__pos, __n1, __s, __n2);
395 [ # # ][ # # ]: 0 : else if ((__left = __s + __n2 <= _M_data() + __pos)
[ # # ]
396 : : || _M_data() + __pos + __n1 <= __s)
397 : : {
398 : : // Work in-place: non-overlapping case.
399 : 0 : size_type __off = __s - _M_data();
400 [ # # ]: 0 : __left ? __off : (__off += __n2 - __n1);
401 : 0 : _M_mutate(__pos, __n1, __n2);
402 : 0 : _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
403 : 0 : return *this;
404 : : }
405 : : else
406 : : {
407 : : // Todo: overlapping case.
408 : 0 : const basic_string __tmp(__s, __n2);
409 : 0 : return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
410 : : }
411 : : }
412 : :
413 : : template<typename _CharT, typename _Traits, typename _Alloc>
414 : : void
415 : : basic_string<_CharT, _Traits, _Alloc>::_Rep::
416 : : _M_destroy(const _Alloc& __a) throw ()
417 : : {
418 : : const size_type __size = sizeof(_Rep_base) +
419 : 224898 : (this->_M_capacity + 1) * sizeof(_CharT);
420 : 449796 : _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
421 : : }
422 : :
423 : : template<typename _CharT, typename _Traits, typename _Alloc>
424 : : void
425 : : basic_string<_CharT, _Traits, _Alloc>::
426 : 30792 : _M_leak_hard()
427 : : {
428 [ + + ]: 30792 : if (_M_rep()->_M_is_shared())
429 : 965 : _M_mutate(0, 0, 0);
430 : : _M_rep()->_M_set_leaked();
431 : 30792 : }
432 : :
433 : : template<typename _CharT, typename _Traits, typename _Alloc>
434 : : void
435 : : basic_string<_CharT, _Traits, _Alloc>::
436 : 0 : _M_mutate(size_type __pos, size_type __len1, size_type __len2)
437 : : {
438 : 0 : const size_type __old_size = this->size();
439 : 0 : const size_type __new_size = __old_size + __len2 - __len1;
440 : 0 : const size_type __how_much = __old_size - __pos - __len1;
441 : :
442 [ # # ][ # # ]: 0 : if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
[ # # ]
443 : : {
444 : : // Must reallocate.
445 : : const allocator_type __a = get_allocator();
446 : 0 : _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
447 : :
448 [ # # ]: 0 : if (__pos)
449 : : _M_copy(__r->_M_refdata(), _M_data(), __pos);
450 [ # # ]: 0 : if (__how_much)
451 : 0 : _M_copy(__r->_M_refdata() + __pos + __len2,
452 : : _M_data() + __pos + __len1, __how_much);
453 : :
454 : 0 : _M_rep()->_M_dispose(__a);
455 : 0 : _M_data(__r->_M_refdata());
456 : : }
457 [ # # ][ # # ]: 0 : else if (__how_much && __len1 != __len2)
458 : : {
459 : : // Work in-place.
460 : 0 : _M_move(_M_data() + __pos + __len2,
461 : : _M_data() + __pos + __len1, __how_much);
462 : : }
463 : : _M_rep()->_M_set_length_and_sharable(__new_size);
464 : 0 : }
465 : :
466 : : template<typename _CharT, typename _Traits, typename _Alloc>
467 : : void
468 : : basic_string<_CharT, _Traits, _Alloc>::
469 : 0 : reserve(size_type __res)
470 : : {
471 [ # # ][ # # ]: 0 : if (__res != this->capacity() || _M_rep()->_M_is_shared())
[ # # ]
472 : : {
473 : : // Make sure we don't shrink below the current size
474 [ # # ]: 0 : if (__res < this->size())
475 : 0 : __res = this->size();
476 : : const allocator_type __a = get_allocator();
477 : 0 : _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
478 : 0 : _M_rep()->_M_dispose(__a);
479 : 0 : _M_data(__tmp);
480 : : }
481 : 0 : }
482 : :
483 : : template<typename _CharT, typename _Traits, typename _Alloc>
484 : : void
485 : : basic_string<_CharT, _Traits, _Alloc>::
486 : : swap(basic_string& __s)
487 : : {
488 : : if (_M_rep()->_M_is_leaked())
489 : : _M_rep()->_M_set_sharable();
490 : : if (__s._M_rep()->_M_is_leaked())
491 : : __s._M_rep()->_M_set_sharable();
492 : : if (this->get_allocator() == __s.get_allocator())
493 : : {
494 : : _CharT* __tmp = _M_data();
495 : : _M_data(__s._M_data());
496 : : __s._M_data(__tmp);
497 : : }
498 : : // The code below can usually be optimized away.
499 : : else
500 : : {
501 : : const basic_string __tmp1(_M_ibegin(), _M_iend(),
502 : : __s.get_allocator());
503 : : const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
504 : : this->get_allocator());
505 : : *this = __tmp2;
506 : : __s = __tmp1;
507 : : }
508 : : }
509 : :
510 : : template<typename _CharT, typename _Traits, typename _Alloc>
511 : : typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
512 : : basic_string<_CharT, _Traits, _Alloc>::_Rep::
513 : : _S_create(size_type __capacity, size_type __old_capacity,
514 : 0 : const _Alloc& __alloc)
515 : : {
516 : : // _GLIBCXX_RESOLVE_LIB_DEFECTS
517 : : // 83. String::npos vs. string::max_size()
518 [ # # ]: 0 : if (__capacity > _S_max_size)
519 : 0 : __throw_length_error(__N("basic_string::_S_create"));
520 : :
521 : : // The standard places no restriction on allocating more memory
522 : : // than is strictly needed within this layer at the moment or as
523 : : // requested by an explicit application call to reserve().
524 : :
525 : : // Many malloc implementations perform quite poorly when an
526 : : // application attempts to allocate memory in a stepwise fashion
527 : : // growing each allocation size by only 1 char. Additionally,
528 : : // it makes little sense to allocate less linear memory than the
529 : : // natural blocking size of the malloc implementation.
530 : : // Unfortunately, we would need a somewhat low-level calculation
531 : : // with tuned parameters to get this perfect for any particular
532 : : // malloc implementation. Fortunately, generalizations about
533 : : // common features seen among implementations seems to suffice.
534 : :
535 : : // __pagesize need not match the actual VM page size for good
536 : : // results in practice, thus we pick a common value on the low
537 : : // side. __malloc_header_size is an estimate of the amount of
538 : : // overhead per memory allocation (in practice seen N * sizeof
539 : : // (void*) where N is 0, 2 or 4). According to folklore,
540 : : // picking this value on the high side is better than
541 : : // low-balling it (especially when this algorithm is used with
542 : : // malloc implementations that allocate memory blocks rounded up
543 : : // to a size which is a power of 2).
544 : 0 : const size_type __pagesize = 4096;
545 : 0 : const size_type __malloc_header_size = 4 * sizeof(void*);
546 : :
547 : : // The below implements an exponential growth policy, necessary to
548 : : // meet amortized linear time requirements of the library: see
549 : : // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
550 : : // It's active for allocations requiring an amount of memory above
551 : : // system pagesize. This is consistent with the requirements of the
552 : : // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
553 [ # # ][ # # ]: 0 : if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
554 : 0 : __capacity = 2 * __old_capacity;
555 : :
556 : : // NB: Need an array of char_type[__capacity], plus a terminating
557 : : // null char_type() element, plus enough for the _Rep data structure.
558 : : // Whew. Seemingly so needy, yet so elemental.
559 : 0 : size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
560 : :
561 : 0 : const size_type __adj_size = __size + __malloc_header_size;
562 [ # # ][ # # ]: 0 : if (__adj_size > __pagesize && __capacity > __old_capacity)
563 : : {
564 : 0 : const size_type __extra = __pagesize - __adj_size % __pagesize;
565 : 0 : __capacity += __extra / sizeof(_CharT);
566 : : // Never allocate a string bigger than _S_max_size.
567 [ # # ]: 0 : if (__capacity > _S_max_size)
568 : 0 : __capacity = _S_max_size;
569 : 0 : __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
570 : : }
571 : :
572 : : // NB: Might throw, but no worries about a leak, mate: _Rep()
573 : : // does not throw.
574 : 0 : void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
575 : 0 : _Rep *__p = new (__place) _Rep;
576 : 0 : __p->_M_capacity = __capacity;
577 : : // ABI compatibility - 3.4.x set in _S_create both
578 : : // _M_refcount and _M_length. All callers of _S_create
579 : : // in basic_string.tcc then set just _M_length.
580 : : // In 4.0.x and later both _M_refcount and _M_length
581 : : // are initialized in the callers, unfortunately we can
582 : : // have 3.4.x compiled code with _S_create callers inlined
583 : : // calling 4.0.x+ _S_create.
584 : : __p->_M_set_sharable();
585 : 0 : return __p;
586 : : }
587 : :
588 : : template<typename _CharT, typename _Traits, typename _Alloc>
589 : : _CharT*
590 : : basic_string<_CharT, _Traits, _Alloc>::_Rep::
591 : 837 : _M_clone(const _Alloc& __alloc, size_type __res)
592 : : {
593 : : // Requested capacity of the clone.
594 : 837 : const size_type __requested_cap = this->_M_length + __res;
595 : : _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
596 : 837 : __alloc);
597 [ + - ]: 837 : if (this->_M_length)
598 : 837 : _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
599 : :
600 : 837 : __r->_M_set_length_and_sharable(this->_M_length);
601 : 837 : return __r->_M_refdata();
602 : : }
603 : :
604 : : template<typename _CharT, typename _Traits, typename _Alloc>
605 : : void
606 : : basic_string<_CharT, _Traits, _Alloc>::
607 : : resize(size_type __n, _CharT __c)
608 : : {
609 : : const size_type __size = this->size();
610 : : _M_check_length(__size, __n, "basic_string::resize");
611 : : if (__size < __n)
612 : : this->append(__n - __size, __c);
613 : : else if (__n < __size)
614 : : this->erase(__n);
615 : : // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
616 : : }
617 : :
618 : : template<typename _CharT, typename _Traits, typename _Alloc>
619 : : template<typename _InputIterator>
620 : : basic_string<_CharT, _Traits, _Alloc>&
621 : : basic_string<_CharT, _Traits, _Alloc>::
622 : : _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
623 : : _InputIterator __k2, __false_type)
624 : : {
625 : : const basic_string __s(__k1, __k2);
626 : : const size_type __n1 = __i2 - __i1;
627 : : _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
628 : : return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
629 : : __s.size());
630 : : }
631 : :
632 : : template<typename _CharT, typename _Traits, typename _Alloc>
633 : : basic_string<_CharT, _Traits, _Alloc>&
634 : : basic_string<_CharT, _Traits, _Alloc>::
635 : : _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
636 : : _CharT __c)
637 : : {
638 : : _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
639 : : _M_mutate(__pos1, __n1, __n2);
640 : : if (__n2)
641 : : _M_assign(_M_data() + __pos1, __n2, __c);
642 : : return *this;
643 : : }
644 : :
645 : : template<typename _CharT, typename _Traits, typename _Alloc>
646 : : basic_string<_CharT, _Traits, _Alloc>&
647 : : basic_string<_CharT, _Traits, _Alloc>::
648 : : _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
649 : 0 : size_type __n2)
650 : : {
651 : 0 : _M_mutate(__pos1, __n1, __n2);
652 [ # # ]: 0 : if (__n2)
653 : 0 : _M_copy(_M_data() + __pos1, __s, __n2);
654 : 0 : return *this;
655 : : }
656 : :
657 : : template<typename _CharT, typename _Traits, typename _Alloc>
658 : : basic_string<_CharT, _Traits, _Alloc>
659 : : operator+(const _CharT* __lhs,
660 : 62 : const basic_string<_CharT, _Traits, _Alloc>& __rhs)
661 : : {
662 : : __glibcxx_requires_string(__lhs);
663 : : typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
664 : : typedef typename __string_type::size_type __size_type;
665 : 62 : const __size_type __len = _Traits::length(__lhs);
666 : 62 : __string_type __str;
667 : 62 : __str.reserve(__len + __rhs.size());
668 : 62 : __str.append(__lhs, __len);
669 : 62 : __str.append(__rhs);
670 : 62 : return __str;
671 : : }
672 : :
673 : : template<typename _CharT, typename _Traits, typename _Alloc>
674 : : basic_string<_CharT, _Traits, _Alloc>
675 : : operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
676 : : {
677 : : typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
678 : : typedef typename __string_type::size_type __size_type;
679 : : __string_type __str;
680 : : const __size_type __len = __rhs.size();
681 : : __str.reserve(__len + 1);
682 : : __str.append(__size_type(1), __lhs);
683 : : __str.append(__rhs);
684 : : return __str;
685 : : }
686 : :
687 : : template<typename _CharT, typename _Traits, typename _Alloc>
688 : : typename basic_string<_CharT, _Traits, _Alloc>::size_type
689 : : basic_string<_CharT, _Traits, _Alloc>::
690 : : copy(_CharT* __s, size_type __n, size_type __pos) const
691 : : {
692 : : _M_check(__pos, "basic_string::copy");
693 : : __n = _M_limit(__pos, __n);
694 : : __glibcxx_requires_string_len(__s, __n);
695 : : if (__n)
696 : : _M_copy(__s, _M_data() + __pos, __n);
697 : : // 21.3.5.7 par 3: do not append null. (good.)
698 : : return __n;
699 : : }
700 : :
701 : : template<typename _CharT, typename _Traits, typename _Alloc>
702 : : typename basic_string<_CharT, _Traits, _Alloc>::size_type
703 : : basic_string<_CharT, _Traits, _Alloc>::
704 : 249 : find(const _CharT* __s, size_type __pos, size_type __n) const
705 : : {
706 : : __glibcxx_requires_string_len(__s, __n);
707 : 249 : const size_type __size = this->size();
708 : 249 : const _CharT* __data = _M_data();
709 : :
710 [ - + ]: 249 : if (__n == 0)
711 [ # # ]: 0 : return __pos <= __size ? __pos : npos;
712 : :
713 [ + + ]: 249 : if (__n <= __size)
714 : : {
715 [ + + ]: 1642 : for (; __pos <= __size - __n; ++__pos)
716 [ + + + + ]: 1706 : if (traits_type::eq(__data[__pos], __s[0])
[ + + ]
717 : : && traits_type::compare(__data + __pos + 1,
718 : : __s + 1, __n - 1) == 0)
719 : 86 : return __pos;
720 : : }
721 : 249 : return npos;
722 : : }
723 : :
724 : : template<typename _CharT, typename _Traits, typename _Alloc>
725 : : typename basic_string<_CharT, _Traits, _Alloc>::size_type
726 : : basic_string<_CharT, _Traits, _Alloc>::
727 : 1826 : find(_CharT __c, size_type __pos) const
728 : : {
729 : 1826 : size_type __ret = npos;
730 : 1826 : const size_type __size = this->size();
731 [ + + ]: 1826 : if (__pos < __size)
732 : : {
733 : 1806 : const _CharT* __data = _M_data();
734 : 1806 : const size_type __n = __size - __pos;
735 : 3612 : const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
736 [ + + ]: 1806 : if (__p)
737 : 908 : __ret = __p - __data;
738 : : }
739 : 1826 : return __ret;
740 : : }
741 : :
742 : : template<typename _CharT, typename _Traits, typename _Alloc>
743 : : typename basic_string<_CharT, _Traits, _Alloc>::size_type
744 : : basic_string<_CharT, _Traits, _Alloc>::
745 : 0 : rfind(const _CharT* __s, size_type __pos, size_type __n) const
746 : : {
747 : : __glibcxx_requires_string_len(__s, __n);
748 : 0 : const size_type __size = this->size();
749 [ # # ]: 0 : if (__n <= __size)
750 : : {
751 : 0 : __pos = std::min(size_type(__size - __n), __pos);
752 : 0 : const _CharT* __data = _M_data();
753 [ # # ]: 0 : do
754 : : {
755 [ # # ]: 0 : if (traits_type::compare(__data + __pos, __s, __n) == 0)
756 : 0 : return __pos;
757 : : }
758 : : while (__pos-- > 0);
759 : : }
760 : 0 : return npos;
761 : : }
762 : :
763 : : template<typename _CharT, typename _Traits, typename _Alloc>
764 : : typename basic_string<_CharT, _Traits, _Alloc>::size_type
765 : : basic_string<_CharT, _Traits, _Alloc>::
766 : : rfind(_CharT __c, size_type __pos) const
767 : : {
768 : 0 : size_type __size = this->size();
769 [ # # ]: 0 : if (__size)
770 : : {
771 [ # # ]: 0 : if (--__size > __pos)
772 : 0 : __size = __pos;
773 [ # # ]: 0 : for (++__size; __size-- > 0; )
774 [ # # ]: 0 : if (traits_type::eq(_M_data()[__size], __c))
775 : 0 : return __size;
776 : : }
777 : 0 : return npos;
778 : : }
779 : :
780 : : template<typename _CharT, typename _Traits, typename _Alloc>
781 : : typename basic_string<_CharT, _Traits, _Alloc>::size_type
782 : : basic_string<_CharT, _Traits, _Alloc>::
783 : 4 : find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
784 : : {
785 : : __glibcxx_requires_string_len(__s, __n);
786 [ + - ][ + + ]: 34 : for (; __n && __pos < this->size(); ++__pos)
[ + + ]
787 : : {
788 : 64 : const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
789 [ + + ]: 32 : if (__p)
790 : 2 : return __pos;
791 : : }
792 : 4 : return npos;
793 : : }
794 : :
795 : : template<typename _CharT, typename _Traits, typename _Alloc>
796 : : typename basic_string<_CharT, _Traits, _Alloc>::size_type
797 : : basic_string<_CharT, _Traits, _Alloc>::
798 : : find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
799 : : {
800 : : __glibcxx_requires_string_len(__s, __n);
801 : : size_type __size = this->size();
802 : : if (__size && __n)
803 : : {
804 : : if (--__size > __pos)
805 : : __size = __pos;
806 : : do
807 : : {
808 : : if (traits_type::find(__s, __n, _M_data()[__size]))
809 : : return __size;
810 : : }
811 : : while (__size-- != 0);
812 : : }
813 : : return npos;
814 : : }
815 : :
816 : : template<typename _CharT, typename _Traits, typename _Alloc>
817 : : typename basic_string<_CharT, _Traits, _Alloc>::size_type
818 : : basic_string<_CharT, _Traits, _Alloc>::
819 : 1810 : find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
820 : : {
821 : : __glibcxx_requires_string_len(__s, __n);
822 [ + + ]: 2078 : for (; __pos < this->size(); ++__pos)
823 [ + + ]: 1968 : if (!traits_type::find(__s, __n, _M_data()[__pos]))
824 : 1700 : return __pos;
825 : 1810 : return npos;
826 : : }
827 : :
828 : : template<typename _CharT, typename _Traits, typename _Alloc>
829 : : typename basic_string<_CharT, _Traits, _Alloc>::size_type
830 : : basic_string<_CharT, _Traits, _Alloc>::
831 : : find_first_not_of(_CharT __c, size_type __pos) const
832 : : {
833 : : for (; __pos < this->size(); ++__pos)
834 : : if (!traits_type::eq(_M_data()[__pos], __c))
835 : : return __pos;
836 : : return npos;
837 : : }
838 : :
839 : : template<typename _CharT, typename _Traits, typename _Alloc>
840 : : typename basic_string<_CharT, _Traits, _Alloc>::size_type
841 : : basic_string<_CharT, _Traits, _Alloc>::
842 : : find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
843 : : {
844 : : __glibcxx_requires_string_len(__s, __n);
845 : : size_type __size = this->size();
846 : : if (__size)
847 : : {
848 : : if (--__size > __pos)
849 : : __size = __pos;
850 : : do
851 : : {
852 : : if (!traits_type::find(__s, __n, _M_data()[__size]))
853 : : return __size;
854 : : }
855 : : while (__size--);
856 : : }
857 : : return npos;
858 : : }
859 : :
860 : : template<typename _CharT, typename _Traits, typename _Alloc>
861 : : typename basic_string<_CharT, _Traits, _Alloc>::size_type
862 : : basic_string<_CharT, _Traits, _Alloc>::
863 : : find_last_not_of(_CharT __c, size_type __pos) const
864 : : {
865 : : size_type __size = this->size();
866 : : if (__size)
867 : : {
868 : : if (--__size > __pos)
869 : : __size = __pos;
870 : : do
871 : : {
872 : : if (!traits_type::eq(_M_data()[__size], __c))
873 : : return __size;
874 : : }
875 : : while (__size--);
876 : : }
877 : : return npos;
878 : : }
879 : :
880 : : template<typename _CharT, typename _Traits, typename _Alloc>
881 : : int
882 : : basic_string<_CharT, _Traits, _Alloc>::
883 : : compare(size_type __pos, size_type __n, const basic_string& __str) const
884 : : {
885 : : _M_check(__pos, "basic_string::compare");
886 : : __n = _M_limit(__pos, __n);
887 : : const size_type __osize = __str.size();
888 : : const size_type __len = std::min(__n, __osize);
889 : : int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
890 : : if (!__r)
891 : : __r = _S_compare(__n, __osize);
892 : : return __r;
893 : : }
894 : :
895 : : template<typename _CharT, typename _Traits, typename _Alloc>
896 : : int
897 : : basic_string<_CharT, _Traits, _Alloc>::
898 : : compare(size_type __pos1, size_type __n1, const basic_string& __str,
899 : : size_type __pos2, size_type __n2) const
900 : : {
901 : : _M_check(__pos1, "basic_string::compare");
902 : : __str._M_check(__pos2, "basic_string::compare");
903 : : __n1 = _M_limit(__pos1, __n1);
904 : : __n2 = __str._M_limit(__pos2, __n2);
905 : : const size_type __len = std::min(__n1, __n2);
906 : : int __r = traits_type::compare(_M_data() + __pos1,
907 : : __str.data() + __pos2, __len);
908 : : if (!__r)
909 : : __r = _S_compare(__n1, __n2);
910 : : return __r;
911 : : }
912 : :
913 : : template<typename _CharT, typename _Traits, typename _Alloc>
914 : : int
915 : : basic_string<_CharT, _Traits, _Alloc>::
916 : 397420 : compare(const _CharT* __s) const
917 : : {
918 : : __glibcxx_requires_string(__s);
919 : 397420 : const size_type __size = this->size();
920 : 397420 : const size_type __osize = traits_type::length(__s);
921 : 397420 : const size_type __len = std::min(__size, __osize);
922 : 397420 : int __r = traits_type::compare(_M_data(), __s, __len);
923 [ + + ]: 397420 : if (!__r)
924 : 383099 : __r = _S_compare(__size, __osize);
925 : 397420 : return __r;
926 : : }
927 : :
928 : : template<typename _CharT, typename _Traits, typename _Alloc>
929 : : int
930 : : basic_string <_CharT, _Traits, _Alloc>::
931 : : compare(size_type __pos, size_type __n1, const _CharT* __s) const
932 : : {
933 : : __glibcxx_requires_string(__s);
934 : : _M_check(__pos, "basic_string::compare");
935 : : __n1 = _M_limit(__pos, __n1);
936 : : const size_type __osize = traits_type::length(__s);
937 : : const size_type __len = std::min(__n1, __osize);
938 : : int __r = traits_type::compare(_M_data() + __pos, __s, __len);
939 : : if (!__r)
940 : : __r = _S_compare(__n1, __osize);
941 : : return __r;
942 : : }
943 : :
944 : : template<typename _CharT, typename _Traits, typename _Alloc>
945 : : int
946 : : basic_string <_CharT, _Traits, _Alloc>::
947 : : compare(size_type __pos, size_type __n1, const _CharT* __s,
948 : : size_type __n2) const
949 : : {
950 : : __glibcxx_requires_string_len(__s, __n2);
951 : : _M_check(__pos, "basic_string::compare");
952 : : __n1 = _M_limit(__pos, __n1);
953 : : const size_type __len = std::min(__n1, __n2);
954 : : int __r = traits_type::compare(_M_data() + __pos, __s, __len);
955 : : if (!__r)
956 : : __r = _S_compare(__n1, __n2);
957 : : return __r;
958 : : }
959 : :
960 : : // 21.3.7.9 basic_string::getline and operators
961 : : template<typename _CharT, typename _Traits, typename _Alloc>
962 : : basic_istream<_CharT, _Traits>&
963 : : operator>>(basic_istream<_CharT, _Traits>& __in,
964 : : basic_string<_CharT, _Traits, _Alloc>& __str)
965 : : {
966 : : typedef basic_istream<_CharT, _Traits> __istream_type;
967 : : typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
968 : : typedef typename __istream_type::ios_base __ios_base;
969 : : typedef typename __istream_type::int_type __int_type;
970 : : typedef typename __string_type::size_type __size_type;
971 : : typedef ctype<_CharT> __ctype_type;
972 : : typedef typename __ctype_type::ctype_base __ctype_base;
973 : :
974 : : __size_type __extracted = 0;
975 : : typename __ios_base::iostate __err = __ios_base::goodbit;
976 : : typename __istream_type::sentry __cerb(__in, false);
977 : : if (__cerb)
978 : : {
979 : : try
980 : : {
981 : : // Avoid reallocation for common case.
982 : : __str.erase();
983 : : _CharT __buf[128];
984 : : __size_type __len = 0;
985 : : const streamsize __w = __in.width();
986 : : const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
987 : : : __str.max_size();
988 : : const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
989 : : const __int_type __eof = _Traits::eof();
990 : : __int_type __c = __in.rdbuf()->sgetc();
991 : :
992 : : while (__extracted < __n
993 : : && !_Traits::eq_int_type(__c, __eof)
994 : : && !__ct.is(__ctype_base::space,
995 : : _Traits::to_char_type(__c)))
996 : : {
997 : : if (__len == sizeof(__buf) / sizeof(_CharT))
998 : : {
999 : : __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
1000 : : __len = 0;
1001 : : }
1002 : : __buf[__len++] = _Traits::to_char_type(__c);
1003 : : ++__extracted;
1004 : : __c = __in.rdbuf()->snextc();
1005 : : }
1006 : : __str.append(__buf, __len);
1007 : :
1008 : : if (_Traits::eq_int_type(__c, __eof))
1009 : : __err |= __ios_base::eofbit;
1010 : : __in.width(0);
1011 : : }
1012 : : catch(__cxxabiv1::__forced_unwind&)
1013 : : {
1014 : : __in._M_setstate(__ios_base::badbit);
1015 : : __throw_exception_again;
1016 : : }
1017 : : catch(...)
1018 : : {
1019 : : // _GLIBCXX_RESOLVE_LIB_DEFECTS
1020 : : // 91. Description of operator>> and getline() for string<>
1021 : : // might cause endless loop
1022 : : __in._M_setstate(__ios_base::badbit);
1023 : : }
1024 : : }
1025 : : // 211. operator>>(istream&, string&) doesn't set failbit
1026 : : if (!__extracted)
1027 : : __err |= __ios_base::failbit;
1028 : : if (__err)
1029 : : __in.setstate(__err);
1030 : : return __in;
1031 : : }
1032 : :
1033 : : template<typename _CharT, typename _Traits, typename _Alloc>
1034 : : basic_istream<_CharT, _Traits>&
1035 : : getline(basic_istream<_CharT, _Traits>& __in,
1036 : : basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
1037 : : {
1038 : : typedef basic_istream<_CharT, _Traits> __istream_type;
1039 : : typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1040 : : typedef typename __istream_type::ios_base __ios_base;
1041 : : typedef typename __istream_type::int_type __int_type;
1042 : : typedef typename __string_type::size_type __size_type;
1043 : :
1044 : : __size_type __extracted = 0;
1045 : : const __size_type __n = __str.max_size();
1046 : : typename __ios_base::iostate __err = __ios_base::goodbit;
1047 : : typename __istream_type::sentry __cerb(__in, true);
1048 : : if (__cerb)
1049 : : {
1050 : : try
1051 : : {
1052 : : __str.erase();
1053 : : const __int_type __idelim = _Traits::to_int_type(__delim);
1054 : : const __int_type __eof = _Traits::eof();
1055 : : __int_type __c = __in.rdbuf()->sgetc();
1056 : :
1057 : : while (__extracted < __n
1058 : : && !_Traits::eq_int_type(__c, __eof)
1059 : : && !_Traits::eq_int_type(__c, __idelim))
1060 : : {
1061 : : __str += _Traits::to_char_type(__c);
1062 : : ++__extracted;
1063 : : __c = __in.rdbuf()->snextc();
1064 : : }
1065 : :
1066 : : if (_Traits::eq_int_type(__c, __eof))
1067 : : __err |= __ios_base::eofbit;
1068 : : else if (_Traits::eq_int_type(__c, __idelim))
1069 : : {
1070 : : ++__extracted;
1071 : : __in.rdbuf()->sbumpc();
1072 : : }
1073 : : else
1074 : : __err |= __ios_base::failbit;
1075 : : }
1076 : : catch(__cxxabiv1::__forced_unwind&)
1077 : : {
1078 : : __in._M_setstate(__ios_base::badbit);
1079 : : __throw_exception_again;
1080 : : }
1081 : : catch(...)
1082 : : {
1083 : : // _GLIBCXX_RESOLVE_LIB_DEFECTS
1084 : : // 91. Description of operator>> and getline() for string<>
1085 : : // might cause endless loop
1086 : : __in._M_setstate(__ios_base::badbit);
1087 : : }
1088 : : }
1089 : : if (!__extracted)
1090 : : __err |= __ios_base::failbit;
1091 : : if (__err)
1092 : : __in.setstate(__err);
1093 : : return __in;
1094 : : }
1095 : :
1096 : : // Inhibit implicit instantiations for required instantiations,
1097 : : // which are defined via explicit instantiations elsewhere.
1098 : : // NB: This syntax is a GNU extension.
1099 : : #if _GLIBCXX_EXTERN_TEMPLATE
1100 : : extern template class basic_string<char>;
1101 : : extern template
1102 : : basic_istream<char>&
1103 : : operator>>(basic_istream<char>&, string&);
1104 : : extern template
1105 : : basic_ostream<char>&
1106 : : operator<<(basic_ostream<char>&, const string&);
1107 : : extern template
1108 : : basic_istream<char>&
1109 : : getline(basic_istream<char>&, string&, char);
1110 : : extern template
1111 : : basic_istream<char>&
1112 : : getline(basic_istream<char>&, string&);
1113 : :
1114 : : #ifdef _GLIBCXX_USE_WCHAR_T
1115 : : extern template class basic_string<wchar_t>;
1116 : : extern template
1117 : : basic_istream<wchar_t>&
1118 : : operator>>(basic_istream<wchar_t>&, wstring&);
1119 : : extern template
1120 : : basic_ostream<wchar_t>&
1121 : : operator<<(basic_ostream<wchar_t>&, const wstring&);
1122 : : extern template
1123 : : basic_istream<wchar_t>&
1124 : : getline(basic_istream<wchar_t>&, wstring&, wchar_t);
1125 : : extern template
1126 : : basic_istream<wchar_t>&
1127 : : getline(basic_istream<wchar_t>&, wstring&);
1128 : : #endif
1129 : : #endif
1130 : :
1131 : : _GLIBCXX_END_NAMESPACE
1132 : :
1133 : : #endif
|