📄 stl_string.h
字号:
void _M_construct_null(_CharT* __p) {
_M_construct_null_aux(__p, _Char_Is_Integral());
}
private:
// Helper functions used by constructors. It is a severe error for
// any of them to be called anywhere except from within constructors.
void _M_terminate_string_aux(__false_type) {
__STL_TRY {
_M_construct_null(_M_finish);
}
__STL_UNWIND(destroy(_M_start, _M_finish));
}
void _M_terminate_string_aux(__true_type) {
*_M_finish=0;
}
void _M_terminate_string() {
_M_terminate_string_aux(_Char_Is_Integral());
}
#ifdef __STL_MEMBER_TEMPLATES
template <class _InputIter>
void _M_range_initialize(_InputIter __f, _InputIter __l,
input_iterator_tag) {
_M_allocate_block(8);
_M_construct_null(_M_finish);
__STL_TRY {
append(__f, __l);
}
__STL_UNWIND(destroy(_M_start, _M_finish + 1));
}
template <class _ForwardIter>
void _M_range_initialize(_ForwardIter __f, _ForwardIter __l,
forward_iterator_tag) {
difference_type __n = 0;
distance(__f, __l, __n);
_M_allocate_block(__n + 1);
_M_finish = uninitialized_copy(__f, __l, _M_start);
_M_terminate_string();
}
template <class _InputIter>
void _M_range_initialize(_InputIter __f, _InputIter __l) {
__stl_debug_do(__check_range(__f, __l));
# ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
typedef __STLPORT_STD::iterator_traits<_InputIter> _Iter_traits;
typedef typename _Iter_traits::iterator_category _Category;
_M_range_initialize(__f, __l, _Category());
# else
_M_range_initialize(__f, __l, __ITERATOR_CATEGORY(__f));
# endif
__stl_debug_do(_M_iter_list._Safe_init(&_M_start));
}
template <class _Integer>
void _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) {
_M_allocate_block(__n + 1);
_M_finish = uninitialized_fill_n(_M_start, __n, __x);
_M_terminate_string();
__stl_debug_do(_M_iter_list._Safe_init(&_M_start));
}
template <class _InputIter>
void _M_initialize_dispatch(_InputIter __f, _InputIter __l, __false_type) {
_M_range_initialize(__f, __l);
}
#else /* __STL_MEMBER_TEMPLATES */
void _M_range_initialize(const _CharT* __f, const _CharT* __l) {
__stl_debug_do(__check_range(__f, __l));
ptrdiff_t __n = __l - __f;
_M_allocate_block(__n + 1);
_M_finish = uninitialized_copy(__f, __l, _M_start);
_M_terminate_string();
__stl_debug_do(_M_iter_list._Safe_init(&_M_start));
}
#endif /* __STL_MEMBER_TEMPLATES */
public: // Iterators.
# if defined (__STL_DEBUG)
iterator begin() { return _Make_iterator(_M_start); }
const_iterator begin() const { return _Make_const_iterator(_M_start); }
iterator end() { return _Make_iterator(_M_finish); }
const_iterator end() const { return _Make_const_iterator(_M_finish); }
void _M_deallocate_block() {
__stl_debug_do(_M_iter_list._Invalidate_all());
_Base::_M_deallocate_block();
}
# else
iterator begin() { return _M_start; }
iterator end() { return _M_finish; }
const_iterator begin() const { return _M_start; }
const_iterator end() const { return _M_finish; }
# endif
reverse_iterator rbegin()
{ return reverse_iterator(_Make_iterator(_M_finish)); }
reverse_iterator rend()
{ return reverse_iterator(_Make_iterator(_M_start)); }
const_reverse_iterator rbegin() const
{ return const_reverse_iterator(_Make_const_iterator(_M_finish)); }
const_reverse_iterator rend() const
{ return const_reverse_iterator(_Make_const_iterator(_M_start)); }
public: // Size, capacity, etc.
size_type size() const { return _M_finish - _M_start; }
size_type length() const { return size(); }
size_t max_size() const { return _Base::max_size(); }
void resize(size_type __n, _CharT __c) {
if (__n <= size())
erase(begin() + __n, end());
else
append(__n - size(), __c);
}
void resize(size_type __n) { resize(__n, _M_null()); }
void reserve(size_type = 0);
size_type capacity() const { return (_M_end_of_storage._M_data - _M_start) - 1; }
void clear() {
if (!empty()) {
__stl_debug_do(_M_iter_list._Invalidate_all());
_Traits::assign(*_M_start, _M_null());
destroy(_M_start+1, _M_finish+1);
_M_finish = _M_start;
}
}
bool empty() const { return _M_start == _M_finish; }
public: // Element access.
const_reference operator[](size_type __n) const
{ return *(_M_start + __n); }
reference operator[](size_type __n)
{ return *(_M_start + __n); }
const_reference at(size_type __n) const {
if (__n >= size())
_M_throw_out_of_range();
return *(_M_start + __n);
}
reference at(size_type __n) {
if (__n >= size())
_M_throw_out_of_range();
return *(_M_start + __n);
}
public: // Append, operator+=, push_back.
_Self& operator+=(const _Self& __s) { return append(__s); }
_Self& operator+=(const _CharT* __s) { __STL_FIX_LITERAL_BUG(__s) return append(__s); }
_Self& operator+=(_CharT __c) { push_back(__c); return *this; }
_Self& append(const _Self& __s)
{ return append(__s._M_start, __s._M_finish); }
_Self& append(const _Self& __s,
size_type __pos, size_type __n)
{
if (__pos > __s.size())
_M_throw_out_of_range();
return append(__s._M_start + __pos,
__s._M_start + __pos + min(__n, __s.size() - __pos));
}
_Self& append(const _CharT* __s, size_type __n)
{ __STL_FIX_LITERAL_BUG(__s) return append(__s, __s+__n); }
_Self& append(const _CharT* __s)
{ __STL_FIX_LITERAL_BUG(__s) return append(__s, __s + _Traits::length(__s)); }
_Self& append(size_type __n, _CharT __c);
#ifdef __STL_MEMBER_TEMPLATES
// Check to see if _InputIterator is an integer type. If so, then
// it can't be an iterator.
template <class _InputIter>
_Self& append(_InputIter __first, _InputIter __last) {
typedef typename _Is_integer<_InputIter>::_Integral _Integral;
return _M_append_dispatch(__first, __last, _Integral());
}
#else /* __STL_MEMBER_TEMPLATES */
_Self& append(const _CharT* __first, const _CharT* __last);
#endif /* __STL_MEMBER_TEMPLATES */
void push_back(_CharT __c) {
if (_M_finish + 1 == _M_end_of_storage._M_data)
reserve(size() + max(size(), __STATIC_CAST(size_type,1)));
_M_construct_null(_M_finish + 1);
_Traits::assign(*_M_finish, __c);
++_M_finish;
}
void pop_back() {
__stl_debug_do(__invalidate_iterator(&_M_iter_list,end()));
_Traits::assign(*(_M_finish - 1), _M_null());
destroy(_M_finish);
--_M_finish;
}
private: // Helper functions for append.
#ifdef __STL_MEMBER_TEMPLATES
template <class _InputIter>
_Self& append(_InputIter __f, _InputIter __last, input_iterator_tag)
{
for ( ; __first != __last ; ++__first)
push_back(*__first);
return *this;
}
template <class _ForwardIter>
_Self& append(_ForwardIter __first, _ForwardIter __last,
forward_iterator_tag)
# ifndef __STL_INLINE_MEMBER_TEMPLATES
;
# else
{
__stl_debug_do(__check_range(__first, __last));
if (__first != __last) {
const size_type __old_size = size();
difference_type __n = 0;
distance(__first, __last, __n);
if (__STATIC_CAST(size_type,__n) > max_size() || __old_size > max_size() - __STATIC_CAST(size_type,__n))
_M_throw_length_error();
if (__old_size + __n > capacity()) {
const size_type __len = __old_size +
max(__old_size, __STATIC_CAST(size_type,__n)) + 1;
pointer __new_start = _M_end_of_storage.allocate(__len);
pointer __new_finish = __new_start;
__STL_TRY {
__new_finish = uninitialized_copy(_M_start, _M_finish, __new_start);
__new_finish = uninitialized_copy(__first, __last, __new_finish);
_M_construct_null(__new_finish);
}
__STL_UNWIND((destroy(__new_start,__new_finish),
_M_end_of_storage.deallocate(__new_start,__len)));
destroy(_M_start, _M_finish + 1);
_M_deallocate_block();
_M_start = __new_start;
_M_finish = __new_finish;
_M_end_of_storage._M_data = __new_start + __len;
}
else {
_ForwardIter __f1 = __first;
++__f1;
uninitialized_copy(__f1, __last, _M_finish + 1);
__STL_TRY {
_M_construct_null(_M_finish + __n);
}
__STL_UNWIND(destroy(_M_finish + 1, _M_finish + __n));
_Traits::assign(*_M_finish, *__first);
_M_finish += __n;
}
}
return *this;
}
# endif /* __STL_INLINE_MEMBER_TEMPLATES */
template <class _Integer>
_Self& _M_append_dispatch(_Integer __n, _Integer __x, __true_type) {
return append((size_type) __n, (_CharT) __x);
}
template <class _InputIter>
_Self& _M_append_dispatch(_InputIter __f, _InputIter __l,
__false_type) {
# if defined ( __STL_CLASS_PARTIAL_SPECIALIZATION )
typedef typename iterator_traits<_InputIter>::iterator_category _Category;
return append(__f, __l, _Category());
# else
return append(__f, __l, __ITERATOR_CATEGORY(__f));
# endif
}
#endif /* __STL_MEMBER_TEMPLATES */
public: // Assign
_Self& assign(const _Self& __s)
{ return assign(__s._M_start, __s._M_finish); }
_Self& assign(const _Self& __s,
size_type __pos, size_type __n) {
if (__pos > __s.size())
_M_throw_out_of_range();
return assign(__s._M_start + __pos,
__s._M_start + __pos + min(__n, __s.size() - __pos));
}
_Self& assign(const _CharT* __s, size_type __n)
{ return assign(__s, __s + __n); }
_Self& assign(const _CharT* __s)
{ return assign(__s, __s + _Traits::length(__s)); }
_Self& assign(size_type __n, _CharT __c);
#ifdef __STL_MEMBER_TEMPLATES
// Check to see if _InputIterator is an integer type. If so, then
// it can't be an iterator.
template <class _InputIter>
_Self& assign(_InputIter __first, _InputIter __last) {
typedef typename _Is_integer<_InputIter>::_Integral _Integral;
return _M_assign_dispatch(__first, __last, _Integral());
}
#endif /* __STL_MEMBER_TEMPLATES */
_Self& assign(const _CharT* __f, const _CharT* __l);
private: // Helper functions for assign.
#ifdef __STL_MEMBER_TEMPLATES
template <class _Integer>
_Self& _M_assign_dispatch(_Integer __n, _Integer __x, __true_type) {
return assign((size_type) __n, (_CharT) __x);
}
template <class _InputIter>
_Self& _M_assign_dispatch(_InputIter __f, _InputIter __l,
__false_type) {
__stl_debug_do(__check_range(__f, __l));
pointer __cur = _M_start;
while (__f != __l && __cur != _M_finish) {
_Traits::assign(*__cur, *__f);
++__f;
++__cur;
}
if (__f == __l)
erase(_Make_iterator(__cur), end());
else
append(__f, __l);
return *this;
}
#endif /* __STL_MEMBER_TEMPLATES */
public: // Insert
_Self& insert(size_type __pos, const _Self& __s) {
if (__pos > size())
_M_throw_out_of_range();
if (size() > max_size() - __s.size())
_M_throw_length_error();
insert(begin() + __pos, __s._M_start, __s._M_finish);
return *this;
}
_Self& insert(size_type __pos, const _Self& __s,
size_type __beg, size_type __n) {
if (__pos > size() || __beg > __s.size())
_M_throw_out_of_range();
size_type __len = min(__n, __s.size() - __beg);
if (size() > max_size() - __len)
_M_throw_length_error();
insert(begin() + __pos,
__s._M_start + __beg, __s._M_start + __beg + __len);
return *this;
}
_Self& insert(size_type __pos, const _CharT* __s, size_type __n) {
__STL_FIX_LITERAL_BUG(__s)
if (__pos > size())
_M_throw_out_of_range();
if (size() > max_size() - __n)
_M_throw_length_error();
insert(begin() + __pos, __s, __s + __n);
return *this;
}
_Self& insert(size_type __pos, const _CharT* __s) {
__STL_FIX_LITERAL_BUG(__s)
if (__pos > size())
_M_throw_out_of_range();
size_type __len = _Traits::length(__s);
if (size() > max_size() - __len)
_M_throw_length_error();
insert(_Make_iterator(_M_start + __pos), __s, __s + __len);
return *this;
}
_Self& insert(size_type __pos, size_type __n, _CharT __c) {
if (__pos > size())
_M_throw_out_of_range();
if (size() > max_size() - __n)
_M_throw_length_error();
insert(begin() + __pos, __n, __c);
return *this;
}
iterator insert(iterator __p, _CharT __c) {
__STL_FIX_LITERAL_BUG(__p)
if (__p == end()) {
push_back(__c);
return _Make_iterator(_M_finish - 1);
}
else
return _Make_iterator(_M_insert_aux(_Make_ptr(__p), __c));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -