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