📄 string.cc
字号:
/***************************************************************************
*
* string.cc - Definitions for the Standard Library string classes
*
* $Id: string.cc,v 1.1.1.1 2002/01/10 17:38:30 vkorstan Exp $
*
***************************************************************************
*
* Copyright (c) 1994-2001 Rogue Wave Software, Inc. All Rights Reserved.
*
* This computer software is owned by Rogue Wave Software, Inc. and is
* protected by U.S. copyright laws and other laws and by international
* treaties. This computer software is furnished by Rogue Wave Software,
* Inc. pursuant to a written license agreement and may be used, copied,
* transmitted, and stored only in accordance with the terms of such
* license and with the inclusion of the above copyright notice. This
* computer software or any other copies thereof may not be provided or
* otherwise made available to any other person.
*
* U.S. Government Restricted Rights. This computer software is provided
* with Restricted Rights. Use, duplication, or disclosure by the
* Government is subject to restrictions as set forth in subparagraph (c)
* (1) (ii) of The Rights in Technical Data and Computer Software clause
* at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
* Commercial Computer Software--Restricted Rights at 48 CFR 52.227-19,
* as applicable. Manufacturer is Rogue Wave Software, Inc., 5500
* Flatiron Parkway, Boulder, Colorado 80301 USA.
*
**************************************************************************/
_RWSTD_NAMESPACE_BEGIN (std)
#if defined (_RWSTD_LLP64_ARCHITECTURE) \
&& defined (_RWSTD_NO_STATIC_CONST_MEMBER_INIT) \
&& defined (_RWBUILD_std)
template <class _CharT, class _Traits, class _Allocator>
const _TYPENAME basic_string<_CharT, _Traits, _Allocator>::size_type
basic_string<_CharT, _Traits, _Allocator>::npos = size_t(-1);
#elif !defined(_RWSTD_NO_STATIC_CONST_MEMBER_INIT)
template <class _CharT, class _Traits, class _Allocator>
const _TYPENAME basic_string<_CharT, _Traits, _Allocator>::size_type
basic_string<_CharT, _Traits, _Allocator>::npos;
#endif
#ifndef _RWSTD_NO_COLLAPSE_TEMPLATE_STATICS
template <class _CharT, class _Traits, class _Allocator>
_RW::__null_ref<_CharT, _Traits, _Allocator>
basic_string<_CharT, _Traits, _Allocator>::__nullref;
#endif // _RWSTD_NO_STRING_REF_COUNT
template <class _CharT, class _Traits, class _Allocator>
_TYPENAME basic_string<_CharT, _Traits, _Allocator>::_C_string_ref_type *
basic_string<_CharT, _Traits, _Allocator>::
_C_getRep (size_type __cap, size_type __len)
{
_RWSTD_REQUIRES (__cap <= max_size (),
(_RWSTD_ERROR_LENGTH_ERROR,
_RWSTD_FUNC ("basic_string::_C_getRep(size_type, "
"size_type)"), __cap, max_size ()));
_RWSTD_REQUIRES (__len <= __cap,
(_RWSTD_ERROR_LENGTH_ERROR,
_RWSTD_FUNC ("basic_string::_C_getRep(size_type, "
"size_type)"), __len, __cap));
if (!__cap) {
_RWSTD_ASSERT (!__len);
#ifndef _RWSTD_NO_COLLAPSE_TEMPLATE_STATICS
return &__nullref;
#else // if defined (_RWSTD_NO_COLLAPSE_TEMPLATE_STATICS)
return _RWSTD_REINTERPRET_CAST (_C_string_ref_type*, &_RW::__nullref);
#endif // _RWSTD_NO_COLLAPSE_TEMPLATE_STATICS
}
// allocate, initialize the __string_ref, and initialize each character
_C_string_ref_type * __ret =
_RWSTD_REINTERPRET_CAST (_C_string_ref_type*,
_RWSTD_VALUE_ALLOC (_C_value_alloc_type,
allocate (__cap + sizeof (_C_string_ref_type) /
sizeof (value_type) + 2)));
// avoid copy construction (mutex isn't copy-constructible)
// _C_ref_alloc_type (*this).construct (__ret, _C_string_ref_type ());
new (__ret) _C_string_ref_type ();
#ifndef _RWSTD_NO_STRING_REF_COUNT
// set initial reference count to 1
__ret->_C_init (1, __cap, __len);
#else // if defined (_RWSTD_NO_STRING_REF_COUNT)
// initial reference count is 0 (ref counting disabled)
__ret->_C_init (0, __cap, __len);
#endif // _RWSTD_NO_STRING_REF_COUNT
_RWSTD_VALUE_ALLOC (_C_value_alloc_type, construct (__ret->data () + __len,
value_type ()));
return __ret;
}
template <class _CharT, class _Traits, class _Allocator>
basic_string<_CharT, _Traits, _Allocator>::
basic_string (const basic_string &__s,
size_type __pos, size_type __n, const allocator_type& __alloc)
: allocator_type (__alloc)
{
_RWSTD_REQUIRES (__pos <= __s.size (),
(_RWSTD_ERROR_OUT_OF_RANGE,
_RWSTD_FUNC ("basic_string::basic_string(const "
"basic_string&, size_type, size_type)"),
__pos, __s.size ()));
size_type __slen = __s.size() - __pos;
size_type __rlen = __n < __slen ? __n : __slen;
size_type __nlen = __n == npos ? 0 : __n;
size_type __maxlen = __nlen > __rlen ? __nlen : __rlen;
if (__maxlen)
_C_data = _C_allocate (0, __maxlen, __rlen);
else
_C_data = _C_getRep (__maxlen,__rlen)->data();
traits_type::copy(_C_data, &__s._C_data[__pos], __rlen);
}
template <class _CharT, class _Traits, class _Allocator>
void basic_string<_CharT, _Traits, _Allocator>::
_C_initn (size_type __n, value_type __c)
{
_RWSTD_REQUIRES (__n <= max_size (),
(_RWSTD_ERROR_LENGTH_ERROR,
_RWSTD_FUNC ("basic_string::_C_initn(size_type, "
"value_type)"), __n, max_size ()));
_C_data = __n ? _C_allocate (0, __n, __n) : _C_null ();
while (__n--)
traits_type::assign (_C_data [__n], __c);
}
template <class _CharT, class _Traits, class _Allocator>
basic_string<_CharT, _Traits, _Allocator>::
basic_string (const_pointer __s, size_type __n, const allocator_type& __alloc)
: allocator_type (__alloc)
{
// extension: if `s' is 0 then `n' unitialized elements are allocated
_RWSTD_REQUIRES (__n <= max_size (),
(_RWSTD_ERROR_LENGTH_ERROR,
_RWSTD_FUNC ("basic_string::basic_string(const_pointer,"
"size_type, const allocator_type&)"),
__n, max_size ()));
_C_data = __n ? _C_allocate (0, __n, __n) : _C_null ();
if (__s)
traits_type::copy (_C_data, __s, __n);
}
template <class _CharT, class _Traits, class _Allocator>
basic_string<_CharT, _Traits, _Allocator>::
basic_string (const_pointer __s, const allocator_type& __alloc)
: allocator_type(__alloc)
{
_RWSTD_ASSERT(__s != 0);
size_type __n = traits_type::length (__s);
_C_data = __n ? _C_allocate (0, __n, __n) : _C_null ();
traits_type::copy (_C_data, __s, __n);
}
#ifndef _RWSTD_NO_MEMBER_TEMPLATES
template <class _CharT, class _Traits, class _Allocator>
template <class _InputIterator>
basic_string<_CharT, _Traits, _Allocator>::
basic_string (_InputIterator __first, _InputIterator __last,
const allocator_type &__alloc)
: allocator_type (__alloc),
_C_data (_C_null ())
{
_RWSTD_ASSERT_RANGE (__first, __last);
replace (_C_make_iter (_C_data), _C_make_iter (_C_data), __first, __last);
}
#endif // _RWSTD_NO_MEMBER_TEMPLATES
template <class _CharT, class _Traits, class _Allocator>
basic_string<_CharT, _Traits, _Allocator>::
basic_string (const_pointer __first, const_pointer __last,
const allocator_type& __alloc)
: allocator_type (__alloc)
{
_RWSTD_ASSERT_RANGE (__first, __last);
_RWSTD_ASSERT (__first <= __last);
size_type __n = __last - __first;
_C_data = __n ? _C_allocate (0, __n, __n) : _C_null ();
traits_type::copy (_C_data, __first, __n);
}
template <class _CharT, class _Traits, class _Allocator>
basic_string<_CharT, _Traits, _Allocator>&
basic_string<_CharT, _Traits, _Allocator>::operator= (const basic_string &__rhs)
{
if (__rhs._C_pref ()->_C_ref_count () > 0) {
__rhs._C_pref ()->_C_inc_ref ();
_C_unlink ();
_C_data = __rhs._C_data;
}
else if (this != &__rhs)
replace (0, size (), __rhs.data (), __rhs.size ());
return *this;
}
template <class _CharT, class _Traits, class _Allocator>
basic_string<_CharT, _Traits, _Allocator>&
basic_string<_CharT, _Traits, _Allocator>::operator= (const_pointer __rhs)
{
_RWSTD_ASSERT (__rhs != 0);
size_type __len = traits_type::length (__rhs);
if (0 == __len) {
if (_C_pref ()->_C_ref_count () == 1) {
_C_pref ()->_C_size = 0;
traits_type::assign (_C_data [0], value_type ());
}
else {
_C_unlink();
_C_data = _C_null ();
}
return *this;
}
return replace (0, size (), __rhs, __len);
}
template <class _CharT, class _Traits, class _Allocator>
basic_string<_CharT, _Traits, _Allocator>&
basic_string<_CharT, _Traits, _Allocator>::
append (const basic_string &__str, size_type __pos, size_type __n)
{
_RWSTD_REQUIRES (__pos <= __str.size (),
(_RWSTD_ERROR_OUT_OF_RANGE,
_RWSTD_FUNC ("basic_string::append(const basic_string&,"
" size_type, size_type)"),
__pos, __str.size ()));
size_type __slen = __str.size() - __pos;
size_type __rlen = __n < __slen ? __n : __slen;
_RWSTD_REQUIRES (size () <= max_size () - __rlen,
(_RWSTD_ERROR_LENGTH_ERROR,
_RWSTD_FUNC ("basic_string::append(const basic_string&,"
" size_type, size_type)"),
size (), max_size () - __rlen));
replace(size(), 0, __str.c_str(), __str.size(), __pos, __n);
return *this;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -