📄 basic_string.tcc
字号:
// Components for manipulating sequences of characters -*- C++ -*-// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003// Free Software Foundation, Inc.//// This file is part of the GNU ISO C++ Library. This library is free// software; you can redistribute it and/or modify it under the// terms of the GNU General Public License as published by the// Free Software Foundation; either version 2, or (at your option)// any later version.// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.// You should have received a copy of the GNU General Public License along// with this library; see the file COPYING. If not, write to the Free// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,// USA.// As a special exception, you may use this file as part of a free software// library without restriction. Specifically, if other files instantiate// templates or use macros or inline functions from this file, or you compile// this file and link it with other files to produce an executable, this// file does not by itself cause the resulting executable to be covered by// the GNU General Public License. This exception does not however// invalidate any other reasons why the executable file might be covered by// the GNU General Public License.//// ISO C++ 14882: 21 Strings library//// This file is included by <string>. It is not meant to be included// separately.// Written by Jason Merrill based upon the specification by Takanori Adachi// in ANSI X3J16/94-0013R2. Rewritten by Nathan Myers to ISO-14882.#ifndef _CPP_BITS_STRING_TCC#define _CPP_BITS_STRING_TCC 1#pragma GCC system_headernamespace std{ template<typename _CharT, typename _Traits, typename _Alloc> const typename basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>:: _Rep::_S_max_size = (((npos - sizeof(_Rep))/sizeof(_CharT)) - 1) / 4; template<typename _CharT, typename _Traits, typename _Alloc> const _CharT basic_string<_CharT, _Traits, _Alloc>:: _Rep::_S_terminal = _CharT(); template<typename _CharT, typename _Traits, typename _Alloc> const typename basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>::npos; // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string) // at static init time (before static ctors are run). template<typename _CharT, typename _Traits, typename _Alloc> typename basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>::_S_empty_rep_storage[ (sizeof(_Rep) + sizeof(_CharT) + sizeof(size_type) - 1)/sizeof(size_type)]; // NB: This is the special case for Input Iterators, used in // istreambuf_iterators, etc. // Input Iterators have a cost structure very different from // pointers, calling for a different coding style. template<typename _CharT, typename _Traits, typename _Alloc> template<typename _InIter> _CharT* basic_string<_CharT, _Traits, _Alloc>:: _S_construct(_InIter __beg, _InIter __end, const _Alloc& __a, input_iterator_tag) { if (__beg == __end && __a == _Alloc()) return _S_empty_rep()._M_refcopy(); // Avoid reallocation for common case. _CharT __buf[100]; size_type __i = 0; while (__beg != __end && __i < sizeof(__buf) / sizeof(_CharT)) { __buf[__i++] = *__beg; ++__beg; } _Rep* __r = _Rep::_S_create(__i, __a); traits_type::copy(__r->_M_refdata(), __buf, __i); __r->_M_length = __i; try { // NB: this loop looks precisely this way because // it avoids comparing __beg != __end any more // than strictly necessary; != might be expensive! for (;;) { _CharT* __p = __r->_M_refdata() + __r->_M_length; _CharT* __last = __r->_M_refdata() + __r->_M_capacity; for (;;) { if (__beg == __end) { __r->_M_length = __p - __r->_M_refdata(); *__p = _Rep::_S_terminal; // grrr. return __r->_M_refdata(); } if (__p == __last) break; *__p++ = *__beg; ++__beg; } // Allocate more space. size_type __len = __p - __r->_M_refdata(); _Rep* __another = _Rep::_S_create(__len + 1, __a); traits_type::copy(__another->_M_refdata(), __r->_M_refdata(), __len); __r->_M_destroy(__a); __r = __another; __r->_M_length = __len; } } catch(...) { __r->_M_destroy(__a); __throw_exception_again; } return 0; } template<typename _CharT, typename _Traits, typename _Alloc> template <class _InIter> _CharT* basic_string<_CharT, _Traits, _Alloc>:: _S_construct(_InIter __beg, _InIter __end, const _Alloc& __a, forward_iterator_tag) { if (__beg == __end && __a == _Alloc()) return _S_empty_rep()._M_refcopy(); // NB: Not required, but considered best practice. if (__builtin_expect(__beg == _InIter(), 0)) __throw_logic_error("attempt to create string with null pointer"); size_type __dnew = static_cast<size_type>(std::distance(__beg, __end)); // Check for out_of_range and length_error exceptions. _Rep* __r = _Rep::_S_create(__dnew, __a); try { _S_copy_chars(__r->_M_refdata(), __beg, __end); } catch(...) { __r->_M_destroy(__a); __throw_exception_again; } __r->_M_length = __dnew; __r->_M_refdata()[__dnew] = _Rep::_S_terminal; // grrr. return __r->_M_refdata(); } template<typename _CharT, typename _Traits, typename _Alloc> _CharT* basic_string<_CharT, _Traits, _Alloc>:: _S_construct(size_type __n, _CharT __c, const _Alloc& __a) { if (__n == 0 && __a == _Alloc()) return _S_empty_rep()._M_refcopy(); // Check for out_of_range and length_error exceptions. _Rep* __r = _Rep::_S_create(__n, __a); try { if (__n) traits_type::assign(__r->_M_refdata(), __n, __c); } catch(...) { __r->_M_destroy(__a); __throw_exception_again; } __r->_M_length = __n; __r->_M_refdata()[__n] = _Rep::_S_terminal; // grrr return __r->_M_refdata(); } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>:: basic_string(const basic_string& __str) : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(), __str.get_allocator()), __str.get_allocator()) { } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>:: basic_string(const _Alloc& __a) : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a) { } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>:: basic_string(const basic_string& __str, size_type __pos, size_type __n) : _M_dataplus(_S_construct(__str._M_check(__pos), __str._M_fold(__pos, __n), _Alloc()), _Alloc()) { } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>:: basic_string(const basic_string& __str, size_type __pos, size_type __n, const _Alloc& __a) : _M_dataplus(_S_construct(__str._M_check(__pos), __str._M_fold(__pos, __n), __a), __a) { } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>:: basic_string(const _CharT* __s, size_type __n, const _Alloc& __a) : _M_dataplus(_S_construct(__s, __s + __n, __a), __a) { } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>:: basic_string(const _CharT* __s, const _Alloc& __a) : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) : __s + npos, __a), __a) { } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>:: basic_string(size_type __n, _CharT __c, const _Alloc& __a) : _M_dataplus(_S_construct(__n, __c, __a), __a) { } template<typename _CharT, typename _Traits, typename _Alloc> template<typename _InputIter> basic_string<_CharT, _Traits, _Alloc>:: basic_string(_InputIter __beg, _InputIter __end, const _Alloc& __a) : _M_dataplus(_S_construct(__beg, __end, __a), __a) { } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>& basic_string<_CharT, _Traits, _Alloc>::assign(const basic_string& __str) { if (_M_rep() != __str._M_rep()) { // XXX MT allocator_type __a = this->get_allocator(); _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator()); _M_rep()->_M_dispose(__a); _M_data(__tmp); } return *this; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>& basic_string<_CharT, _Traits, _Alloc>:: assign(const basic_string& __str, size_type __pos, size_type __n) { const size_type __strsize = __str.size(); if (__pos > __strsize) __throw_out_of_range("basic_string::assign"); const bool __testn = __n < __strsize - __pos; const size_type __newsize = __testn ? __n : __strsize - __pos; return this->assign(__str._M_data() + __pos, __newsize); } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>& basic_string<_CharT, _Traits, _Alloc>:: assign(const _CharT* __s, size_type __n) { if (__n > this->max_size()) __throw_length_error("basic_string::assign"); if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data()) || less<const _CharT*>()(_M_data() + this->size(), __s)) return _M_replace_safe(_M_ibegin(), _M_iend(), __s, __s + __n); else { // Work in-place const size_type __pos = __s - _M_data(); if (__pos >= __n) traits_type::copy(_M_data(), __s, __n); else if (__pos) traits_type::move(_M_data(), __s, __n); _M_rep()->_M_length = __n; _M_data()[__n] = _Rep::_S_terminal; // grr. return *this; } } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>& basic_string<_CharT, _Traits, _Alloc>:: insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n) { const size_type __strsize = __str.size(); if (__pos2 > __strsize) __throw_out_of_range("basic_string::insert"); const bool __testn = __n < __strsize - __pos2; const size_type __newsize = __testn ? __n : __strsize - __pos2; return this->insert(__pos1, __str._M_data() + __pos2, __newsize); } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>& basic_string<_CharT, _Traits, _Alloc>:: insert(size_type __pos, const _CharT* __s, size_type __n) { const size_type __size = this->size(); if (__pos > __size) __throw_out_of_range("basic_string::insert"); if (__size > this->max_size() - __n) __throw_length_error("basic_string::insert"); if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data()) || less<const _CharT*>()(_M_data() + __size, __s)) return _M_replace_safe(_M_ibegin() + __pos, _M_ibegin() + __pos, __s, __s + __n); else { // Work in-place. If _M_mutate reallocates the string, __s // does not point anymore to valid data, therefore we save its // offset, then we restore it. const size_type __off = __s - _M_data(); _M_mutate(__pos, 0, __n); __s = _M_data() + __off; _CharT* __p = _M_data() + __pos; if (__s + __n <= __p) traits_type::copy(__p, __s, __n); else if (__s >= __p) traits_type::copy(__p, __s + __n, __n); else { traits_type::copy(__p, __s, __p - __s); traits_type::copy(__p + (__p-__s), __p + __n, __n - (__p-__s)); } return *this; } } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>& basic_string<_CharT, _Traits, _Alloc>:: replace(size_type __pos, size_type __n1, const _CharT* __s, size_type __n2) { const size_type __size = this->size(); if (__pos > __size) __throw_out_of_range("basic_string::replace"); const bool __testn1 = __n1 < __size - __pos; const size_type __foldn1 = __testn1 ? __n1 : __size - __pos; if (__size - __foldn1 > this->max_size() - __n2) __throw_length_error("basic_string::replace"); if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data()) || less<const _CharT*>()(_M_data() + __size, __s)) return _M_replace_safe(_M_ibegin() + __pos, _M_ibegin() + __pos + __foldn1, __s, __s + __n2); // Todo: optimized in-place replace. else return _M_replace(_M_ibegin() + __pos, _M_ibegin() + __pos + __foldn1, __s, __s + __n2,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -