⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basic_string.tcc

📁 gcc3.2.1源代码
💻 TCC
📖 第 1 页 / 共 3 页
字号:
// Components for manipulating sequences of characters -*- C++ -*-// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002// 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)      {	size_type __dnew = static_cast<size_type>(distance(__beg, __end));	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");		// 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>    void    basic_string<_CharT, _Traits, _Alloc>::_Rep::    _M_destroy(const _Alloc& __a) throw ()    {      size_type __size = sizeof(_Rep) + (_M_capacity + 1) * sizeof(_CharT);      _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);    }  template<typename _CharT, typename _Traits, typename _Alloc>    void    basic_string<_CharT, _Traits, _Alloc>::_M_leak_hard()    {      if (_M_rep()->_M_is_shared()) 	_M_mutate(0, 0, 0);      _M_rep()->_M_set_leaked();    }  // _M_mutate and, below, _M_clone, include, in the same form, an exponential  // growth policy, necessary to meet amortized linear time requirements of  // the library: see http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.  // The policy is active for allocations requiring an amount of memory above  // system pagesize. This is consistent with the requirements of the standard:  // see, f.i., http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html  template<typename _CharT, typename _Traits, typename _Alloc>    void    basic_string<_CharT, _Traits, _Alloc>::    _M_mutate(size_type __pos, size_type __len1, size_type __len2)    {      size_type       __old_size = this->size();      const size_type __new_size = __old_size + __len2 - __len1;      const _CharT*        __src = _M_data()  + __pos + __len1;      const size_type __how_much = __old_size - __pos - __len1;            if (_M_rep()->_M_is_shared() || __new_size > capacity())	{	  // Must reallocate.	  allocator_type __a = get_allocator();	  // See below (_S_create) for the meaning and value of these	  // constants.	  const size_type __pagesize = 4096;	  const size_type __malloc_header_size = 4 * sizeof (void*);	  // The biggest string which fits in a memory page	  const size_type __page_capacity = (__pagesize - __malloc_header_size					     - sizeof(_Rep) - sizeof(_CharT)) 	    				     / sizeof(_CharT);	  _Rep* __r;	  if (__new_size > capacity() && __new_size > __page_capacity)	    // Growing exponentially.	    __r = _Rep::_S_create(__new_size > 2*capacity() ?				  __new_size : 2*capacity(), __a);	  else	    __r = _Rep::_S_create(__new_size, __a);	  try 	    {	      if (__pos)		traits_type::copy(__r->_M_refdata(), _M_data(), __pos);	      if (__how_much)		traits_type::copy(__r->_M_refdata() + __pos + __len2, 				  __src, __how_much);	    }	  catch(...) 	    { 	      __r->_M_dispose(get_allocator()); 	      __throw_exception_again;	    }	  _M_rep()->_M_dispose(__a);	  _M_data(__r->_M_refdata());      }      else if (__how_much && __len1 != __len2)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -