Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/c++/15/ext/vstring.tcc
1 // Versatile string -*- C++ -*- 2 3 // Copyright (C) 2005-2025 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 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 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 /** @file ext/vstring.tcc 26 * This is an internal header file, included by other library headers. 27 * Do not attempt to use it directly. @headername{ext/vstring.h} 28 */ 29 30 #ifndef _VSTRING_TCC 31 #define _VSTRING_TCC 1 32 33 #ifdef _GLIBCXX_SYSHDR 34 #pragma GCC system_header 35 #endif 36 37 #include <bits/requires_hosted.h> // GNU extensions are currently omitted 38 39 #include <bits/cxxabi_forced.h> 40 41 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 42 { 43 _GLIBCXX_BEGIN_NAMESPACE_VERSION 44 45 template<typename _CharT, typename _Traits, typename _Alloc, 46 template <typename, typename, typename> class _Base> 47 const typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type 48 __versa_string<_CharT, _Traits, _Alloc, _Base>::npos; 49 50 template<typename _CharT, typename _Traits, typename _Alloc, 51 template <typename, typename, typename> class _Base> 52 void 53 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 54 resize(size_type __n, _CharT __c) 55 { 56 const size_type __size = this->size(); 57 if (__size < __n) 58 this->append(__n - __size, __c); 59 else if (__n < __size) 60 this->_M_erase(__n, __size - __n); 61 } 62 63 template<typename _CharT, typename _Traits, typename _Alloc, 64 template <typename, typename, typename> class _Base> 65 __versa_string<_CharT, _Traits, _Alloc, _Base>& 66 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 67 _M_append(const _CharT* __s, size_type __n) 68 { 69 const size_type __len = __n + this->size(); 70 71 if (__len <= this->capacity() && !this->_M_is_shared()) 72 { 73 if (__n) 74 this->_S_copy(this->_M_data() + this->size(), __s, __n); 75 } 76 else 77 this->_M_mutate(this->size(), size_type(0), __s, __n); 78 79 this->_M_set_length(__len); 80 return *this; 81 } 82 83 template<typename _CharT, typename _Traits, typename _Alloc, 84 template <typename, typename, typename> class _Base> 85 template<typename _InputIterator> 86 __versa_string<_CharT, _Traits, _Alloc, _Base>& 87 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 88 _M_replace_dispatch(const_iterator __i1, const_iterator __i2, 89 _InputIterator __k1, _InputIterator __k2, 90 std::__false_type) 91 { 92 const __versa_string __s(__k1, __k2); 93 const size_type __n1 = __i2 - __i1; 94 return _M_replace(__i1 - _M_ibegin(), __n1, __s._M_data(), 95 __s.size()); 96 } 97 98 template<typename _CharT, typename _Traits, typename _Alloc, 99 template <typename, typename, typename> class _Base> 100 __versa_string<_CharT, _Traits, _Alloc, _Base>& 101 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 102 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, 103 _CharT __c) 104 { 105 _M_check_length(__n1, __n2, "__versa_string::_M_replace_aux"); 106 107 const size_type __old_size = this->size(); 108 const size_type __new_size = __old_size + __n2 - __n1; 109 110 if (__new_size <= this->capacity() && !this->_M_is_shared()) 111 { 112 _CharT* __p = this->_M_data() + __pos1; 113 114 const size_type __how_much = __old_size - __pos1 - __n1; 115 if (__how_much && __n1 != __n2) 116 this->_S_move(__p + __n2, __p + __n1, __how_much); 117 } 118 else 119 this->_M_mutate(__pos1, __n1, 0, __n2); 120 121 if (__n2) 122 this->_S_assign(this->_M_data() + __pos1, __n2, __c); 123 124 this->_M_set_length(__new_size); 125 return *this; 126 } 127 128 template<typename _CharT, typename _Traits, typename _Alloc, 129 template <typename, typename, typename> class _Base> 130 __versa_string<_CharT, _Traits, _Alloc, _Base>& 131 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 132 _M_replace(size_type __pos, size_type __len1, const _CharT* __s, 133 const size_type __len2) 134 { 135 _M_check_length(__len1, __len2, "__versa_string::_M_replace"); 136 137 const size_type __old_size = this->size(); 138 const size_type __new_size = __old_size + __len2 - __len1; 139 140 if (__new_size <= this->capacity() && !this->_M_is_shared()) 141 { 142 _CharT* __p = this->_M_data() + __pos; 143 144 const size_type __how_much = __old_size - __pos - __len1; 145 if (_M_disjunct(__s)) 146 { 147 if (__how_much && __len1 != __len2) 148 this->_S_move(__p + __len2, __p + __len1, __how_much); 149 if (__len2) 150 this->_S_copy(__p, __s, __len2); 151 } 152 else 153 { 154 // Work in-place. 155 if (__len2 && __len2 <= __len1) 156 this->_S_move(__p, __s, __len2); 157 if (__how_much && __len1 != __len2) 158 this->_S_move(__p + __len2, __p + __len1, __how_much); 159 if (__len2 > __len1) 160 { 161 if (__s + __len2 <= __p + __len1) 162 this->_S_move(__p, __s, __len2); 163 else if (__s >= __p + __len1) 164 this->_S_copy(__p, __s + __len2 - __len1, __len2); 165 else 166 { 167 const size_type __nleft = (__p + __len1) - __s; 168 this->_S_move(__p, __s, __nleft); 169 this->_S_copy(__p + __nleft, __p + __len2, 170 __len2 - __nleft); 171 } 172 } 173 } 174 } 175 else 176 this->_M_mutate(__pos, __len1, __s, __len2); 177 178 this->_M_set_length(__new_size); 179 return *this; 180 } 181 182 template<typename _CharT, typename _Traits, typename _Alloc, 183 template <typename, typename, typename> class _Base> 184 __versa_string<_CharT, _Traits, _Alloc, _Base> 185 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 186 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 187 { 188 __versa_string<_CharT, _Traits, _Alloc, _Base> __str; 189 __str.reserve(__lhs.size() + __rhs.size()); 190 __str.append(__lhs); 191 __str.append(__rhs); 192 return __str; 193 } 194 195 template<typename _CharT, typename _Traits, typename _Alloc, 196 template <typename, typename, typename> class _Base> 197 __versa_string<_CharT, _Traits, _Alloc, _Base> 198 operator+(const _CharT* __lhs, 199 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 200 { 201 __glibcxx_requires_string(__lhs); 202 typedef __versa_string<_CharT, _Traits, _Alloc, _Base> __string_type; 203 typedef typename __string_type::size_type __size_type; 204 const __size_type __len = _Traits::length(__lhs); 205 __string_type __str; 206 __str.reserve(__len + __rhs.size()); 207 __str.append(__lhs, __len); 208 __str.append(__rhs); 209 return __str; 210 } 211 212 template<typename _CharT, typename _Traits, typename _Alloc, 213 template <typename, typename, typename> class _Base> 214 __versa_string<_CharT, _Traits, _Alloc, _Base> 215 operator+(_CharT __lhs, 216 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 217 { 218 __versa_string<_CharT, _Traits, _Alloc, _Base> __str; 219 __str.reserve(__rhs.size() + 1); 220 __str.push_back(__lhs); 221 __str.append(__rhs); 222 return __str; 223 } 224 225 template<typename _CharT, typename _Traits, typename _Alloc, 226 template <typename, typename, typename> class _Base> 227 __versa_string<_CharT, _Traits, _Alloc, _Base> 228 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 229 const _CharT* __rhs) 230 { 231 __glibcxx_requires_string(__rhs); 232 typedef __versa_string<_CharT, _Traits, _Alloc, _Base> __string_type; 233 typedef typename __string_type::size_type __size_type; 234 const __size_type __len = _Traits::length(__rhs); 235 __string_type __str; 236 __str.reserve(__lhs.size() + __len); 237 __str.append(__lhs); 238 __str.append(__rhs, __len); 239 return __str; 240 } 241 242 template<typename _CharT, typename _Traits, typename _Alloc, 243 template <typename, typename, typename> class _Base> 244 __versa_string<_CharT, _Traits, _Alloc, _Base> 245 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 246 _CharT __rhs) 247 { 248 __versa_string<_CharT, _Traits, _Alloc, _Base> __str; 249 __str.reserve(__lhs.size() + 1); 250 __str.append(__lhs); 251 __str.push_back(__rhs); 252 return __str; 253 } 254 255 template<typename _CharT, typename _Traits, typename _Alloc, 256 template <typename, typename, typename> class _Base> 257 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type 258 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 259 copy(_CharT* __s, size_type __n, size_type __pos) const 260 { 261 _M_check(__pos, "__versa_string::copy"); 262 __n = _M_limit(__pos, __n); 263 __glibcxx_requires_string_len(__s, __n); 264 if (__n) 265 this->_S_copy(__s, this->_M_data() + __pos, __n); 266 // 21.3.5.7 par 3: do not append null. (good.) 267 return __n; 268 } 269 270 template<typename _CharT, typename _Traits, typename _Alloc, 271 template <typename, typename, typename> class _Base> 272 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type 273 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 274 find(const _CharT* __s, size_type __pos, size_type __n) const 275 { 276 __glibcxx_requires_string_len(__s, __n); 277 const size_type __size = this->size(); 278 const _CharT* __data = this->_M_data(); 279 280 if (__n == 0) 281 return __pos <= __size ? __pos : npos; 282 283 if (__n <= __size) 284 { 285 for (; __pos <= __size - __n; ++__pos) 286 if (traits_type::eq(__data[__pos], __s[0]) 287 && traits_type::compare(__data + __pos + 1, 288 __s + 1, __n - 1) == 0) 289 return __pos; 290 } 291 return npos; 292 } 293 294 template<typename _CharT, typename _Traits, typename _Alloc, 295 template <typename, typename, typename> class _Base> 296 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type 297 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 298 find(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT 299 { 300 size_type __ret = npos; 301 const size_type __size = this->size(); 302 if (__pos < __size) 303 { 304 const _CharT* __data = this->_M_data(); 305 const size_type __n = __size - __pos; 306 const _CharT* __p = traits_type::find(__data + __pos, __n, __c); 307 if (__p) 308 __ret = __p - __data; 309 } 310 return __ret; 311 } 312 313 template<typename _CharT, typename _Traits, typename _Alloc, 314 template <typename, typename, typename> class _Base> 315 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type 316 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 317 rfind(const _CharT* __s, size_type __pos, size_type __n) const 318 { 319 __glibcxx_requires_string_len(__s, __n); 320 const size_type __size = this->size(); 321 if (__n <= __size) 322 { 323 __pos = std::min(size_type(__size - __n), __pos); 324 const _CharT* __data = this->_M_data(); 325 do 326 { 327 if (traits_type::compare(__data + __pos, __s, __n) == 0) 328 return __pos; 329 } 330 while (__pos-- > 0); 331 } 332 return npos; 333 } 334 335 template<typename _CharT, typename _Traits, typename _Alloc, 336 template <typename, typename, typename> class _Base> 337 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type 338 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 339 rfind(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT 340 { 341 size_type __size = this->size(); 342 if (__size) 343 { 344 if (--__size > __pos) 345 __size = __pos; 346 for (++__size; __size-- > 0; ) 347 if (traits_type::eq(this->_M_data()[__size], __c)) 348 return __size; 349 } 350 return npos; 351 } 352 353 template<typename _CharT, typename _Traits, typename _Alloc, 354 template <typename, typename, typename> class _Base> 355 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type 356 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 357 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const 358 { 359 __glibcxx_requires_string_len(__s, __n); 360 for (; __n && __pos < this->size(); ++__pos) 361 { 362 const _CharT* __p = traits_type::find(__s, __n, 363 this->_M_data()[__pos]); 364 if (__p) 365 return __pos; 366 } 367 return npos; 368 } 369 370 template<typename _CharT, typename _Traits, typename _Alloc, 371 template <typename, typename, typename> class _Base> 372 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type 373 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 374 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const 375 { 376 __glibcxx_requires_string_len(__s, __n); 377 size_type __size = this->size(); 378 if (__size && __n) 379 { 380 if (--__size > __pos) 381 __size = __pos; 382 do 383 { 384 if (traits_type::find(__s, __n, this->_M_data()[__size])) 385 return __size; 386 } 387 while (__size-- != 0); 388 } 389 return npos; 390 } 391 392 template<typename _CharT, typename _Traits, typename _Alloc, 393 template <typename, typename, typename> class _Base> 394 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type 395 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 396 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const 397 { 398 __glibcxx_requires_string_len(__s, __n); 399 for (; __pos < this->size(); ++__pos) 400 if (!traits_type::find(__s, __n, this->_M_data()[__pos])) 401 return __pos; 402 return npos; 403 } 404 405 template<typename _CharT, typename _Traits, typename _Alloc, 406 template <typename, typename, typename> class _Base> 407 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type 408 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 409 find_first_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT 410 { 411 for (; __pos < this->size(); ++__pos) 412 if (!traits_type::eq(this->_M_data()[__pos], __c)) 413 return __pos; 414 return npos; 415 } 416 417 template<typename _CharT, typename _Traits, typename _Alloc, 418 template <typename, typename, typename> class _Base> 419 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type 420 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 421 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const 422 { 423 __glibcxx_requires_string_len(__s, __n); 424 size_type __size = this->size(); 425 if (__size) 426 { 427 if (--__size > __pos) 428 __size = __pos; 429 do 430 { 431 if (!traits_type::find(__s, __n, this->_M_data()[__size])) 432 return __size; 433 } 434 while (__size--); 435 } 436 return npos; 437 } 438 439 template<typename _CharT, typename _Traits, typename _Alloc, 440 template <typename, typename, typename> class _Base> 441 typename __versa_string<_CharT, _Traits, _Alloc, _Base>::size_type 442 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 443 find_last_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT 444 { 445 size_type __size = this->size(); 446 if (__size) 447 { 448 if (--__size > __pos) 449 __size = __pos; 450 do 451 { 452 if (!traits_type::eq(this->_M_data()[__size], __c)) 453 return __size; 454 } 455 while (__size--); 456 } 457 return npos; 458 } 459 460 template<typename _CharT, typename _Traits, typename _Alloc, 461 template <typename, typename, typename> class _Base> 462 int 463 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 464 compare(size_type __pos, size_type __n, const __versa_string& __str) const 465 { 466 _M_check(__pos, "__versa_string::compare"); 467 __n = _M_limit(__pos, __n); 468 const size_type __osize = __str.size(); 469 const size_type __len = std::min(__n, __osize); 470 int __r = traits_type::compare(this->_M_data() + __pos, 471 __str.data(), __len); 472 if (!__r) 473 __r = this->_S_compare(__n, __osize); 474 return __r; 475 } 476 477 template<typename _CharT, typename _Traits, typename _Alloc, 478 template <typename, typename, typename> class _Base> 479 int 480 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 481 compare(size_type __pos1, size_type __n1, const __versa_string& __str, 482 size_type __pos2, size_type __n2) const 483 { 484 _M_check(__pos1, "__versa_string::compare"); 485 __str._M_check(__pos2, "__versa_string::compare"); 486 __n1 = _M_limit(__pos1, __n1); 487 __n2 = __str._M_limit(__pos2, __n2); 488 const size_type __len = std::min(__n1, __n2); 489 int __r = traits_type::compare(this->_M_data() + __pos1, 490 __str.data() + __pos2, __len); 491 if (!__r) 492 __r = this->_S_compare(__n1, __n2); 493 return __r; 494 } 495 496 template<typename _CharT, typename _Traits, typename _Alloc, 497 template <typename, typename, typename> class _Base> 498 int 499 __versa_string<_CharT, _Traits, _Alloc, _Base>:: 500 compare(const _CharT* __s) const 501 { 502 __glibcxx_requires_string(__s); 503 const size_type __size = this->size(); 504 const size_type __osize = traits_type::length(__s); 505 const size_type __len = std::min(__size, __osize); 506 int __r = traits_type::compare(this->_M_data(), __s, __len); 507 if (!__r) 508 __r = this->_S_compare(__size, __osize); 509 return __r; 510 } 511 512 template<typename _CharT, typename _Traits, typename _Alloc, 513 template <typename, typename, typename> class _Base> 514 int 515 __versa_string <_CharT, _Traits, _Alloc, _Base>:: 516 compare(size_type __pos, size_type __n1, const _CharT* __s) const 517 { 518 __glibcxx_requires_string(__s); 519 _M_check(__pos, "__versa_string::compare"); 520 __n1 = _M_limit(__pos, __n1); 521 const size_type __osize = traits_type::length(__s); 522 const size_type __len = std::min(__n1, __osize); 523 int __r = traits_type::compare(this->_M_data() + __pos, __s, __len); 524 if (!__r) 525 __r = this->_S_compare(__n1, __osize); 526 return __r; 527 } 528 529 template<typename _CharT, typename _Traits, typename _Alloc, 530 template <typename, typename, typename> class _Base> 531 int 532 __versa_string <_CharT, _Traits, _Alloc, _Base>:: 533 compare(size_type __pos, size_type __n1, const _CharT* __s, 534 size_type __n2) const 535 { 536 __glibcxx_requires_string_len(__s, __n2); 537 _M_check(__pos, "__versa_string::compare"); 538 __n1 = _M_limit(__pos, __n1); 539 const size_type __len = std::min(__n1, __n2); 540 int __r = traits_type::compare(this->_M_data() + __pos, __s, __len); 541 if (!__r) 542 __r = this->_S_compare(__n1, __n2); 543 return __r; 544 } 545 546 _GLIBCXX_END_NAMESPACE_VERSION 547 } // namespace 548 549 namespace std _GLIBCXX_VISIBILITY(default) 550 { 551 _GLIBCXX_BEGIN_NAMESPACE_VERSION 552 553 template<typename _CharT, typename _Traits, typename _Alloc, 554 template <typename, typename, typename> class _Base> 555 basic_istream<_CharT, _Traits>& 556 operator>>(basic_istream<_CharT, _Traits>& __in, 557 __gnu_cxx::__versa_string<_CharT, _Traits, 558 _Alloc, _Base>& __str) 559 { 560 typedef basic_istream<_CharT, _Traits> __istream_type; 561 typedef typename __istream_type::ios_base __ios_base; 562 typedef __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base> 563 __string_type; 564 typedef typename __istream_type::int_type __int_type; 565 typedef typename __string_type::size_type __size_type; 566 typedef ctype<_CharT> __ctype_type; 567 typedef typename __ctype_type::ctype_base __ctype_base; 568 569 __size_type __extracted = 0; 570 typename __ios_base::iostate __err = __ios_base::goodbit; 571 typename __istream_type::sentry __cerb(__in, false); 572 if (__cerb) 573 { 574 __try 575 { 576 // Avoid reallocation for common case. 577 __str.erase(); 578 _CharT __buf[128]; 579 __size_type __len = 0; 580 const streamsize __w = __in.width(); 581 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w) 582 : __str.max_size(); 583 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc()); 584 const __int_type __eof = _Traits::eof(); 585 __int_type __c = __in.rdbuf()->sgetc(); 586 587 while (__extracted < __n 588 && !_Traits::eq_int_type(__c, __eof) 589 && !__ct.is(__ctype_base::space, 590 _Traits::to_char_type(__c))) 591 { 592 if (__len == sizeof(__buf) / sizeof(_CharT)) 593 { 594 __str.append(__buf, sizeof(__buf) / sizeof(_CharT)); 595 __len = 0; 596 } 597 __buf[__len++] = _Traits::to_char_type(__c); 598 ++__extracted; 599 __c = __in.rdbuf()->snextc(); 600 } 601 __str.append(__buf, __len); 602 603 if (_Traits::eq_int_type(__c, __eof)) 604 __err |= __ios_base::eofbit; 605 __in.width(0); 606 } 607 __catch(__cxxabiv1::__forced_unwind&) 608 { 609 __in._M_setstate(__ios_base::badbit); 610 __throw_exception_again; 611 } 612 __catch(...) 613 { 614 // _GLIBCXX_RESOLVE_LIB_DEFECTS 615 // 91. Description of operator>> and getline() for string<> 616 // might cause endless loop 617 __in._M_setstate(__ios_base::badbit); 618 } 619 } 620 // 211. operator>>(istream&, string&) doesn't set failbit 621 if (!__extracted) 622 __err |= __ios_base::failbit; 623 if (__err) 624 __in.setstate(__err); 625 return __in; 626 } 627 628 template<typename _CharT, typename _Traits, typename _Alloc, 629 template <typename, typename, typename> class _Base> 630 basic_istream<_CharT, _Traits>& 631 getline(basic_istream<_CharT, _Traits>& __in, 632 __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str, 633 _CharT __delim) 634 { 635 typedef basic_istream<_CharT, _Traits> __istream_type; 636 typedef typename __istream_type::ios_base __ios_base; 637 typedef __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base> 638 __string_type; 639 typedef typename __istream_type::int_type __int_type; 640 typedef typename __string_type::size_type __size_type; 641 642 __size_type __extracted = 0; 643 const __size_type __n = __str.max_size(); 644 typename __ios_base::iostate __err = __ios_base::goodbit; 645 typename __istream_type::sentry __cerb(__in, true); 646 if (__cerb) 647 { 648 __try 649 { 650 // Avoid reallocation for common case. 651 __str.erase(); 652 _CharT __buf[128]; 653 __size_type __len = 0; 654 const __int_type __idelim = _Traits::to_int_type(__delim); 655 const __int_type __eof = _Traits::eof(); 656 __int_type __c = __in.rdbuf()->sgetc(); 657 658 while (__extracted < __n 659 && !_Traits::eq_int_type(__c, __eof) 660 && !_Traits::eq_int_type(__c, __idelim)) 661 { 662 if (__len == sizeof(__buf) / sizeof(_CharT)) 663 { 664 __str.append(__buf, sizeof(__buf) / sizeof(_CharT)); 665 __len = 0; 666 } 667 __buf[__len++] = _Traits::to_char_type(__c); 668 ++__extracted; 669 __c = __in.rdbuf()->snextc(); 670 } 671 __str.append(__buf, __len); 672 673 if (_Traits::eq_int_type(__c, __eof)) 674 __err |= __ios_base::eofbit; 675 else if (_Traits::eq_int_type(__c, __idelim)) 676 { 677 ++__extracted; 678 __in.rdbuf()->sbumpc(); 679 } 680 else 681 __err |= __ios_base::failbit; 682 } 683 __catch(__cxxabiv1::__forced_unwind&) 684 { 685 __in._M_setstate(__ios_base::badbit); 686 __throw_exception_again; 687 } 688 __catch(...) 689 { 690 // _GLIBCXX_RESOLVE_LIB_DEFECTS 691 // 91. Description of operator>> and getline() for string<> 692 // might cause endless loop 693 __in._M_setstate(__ios_base::badbit); 694 } 695 } 696 if (!__extracted) 697 __err |= __ios_base::failbit; 698 if (__err) 699 __in.setstate(__err); 700 return __in; 701 } 702 703 _GLIBCXX_END_NAMESPACE_VERSION 704 } // namespace 705 706 #endif // _VSTRING_TCC