basic_string.tcc
来自「ARM Linux Tool 各种代码包括MTD」· TCC 代码 · 共 863 行 · 第 1/2 页
TCC
863 行
basic_string<_CharT, _Traits, _Alloc>& basic_string<_CharT, _Traits, _Alloc>:: _M_replace(iterator __i1, iterator __i2, _InputIter __k1, _InputIter __k2, input_iterator_tag) { basic_string __s(__k1, __k2); return this->replace(__i1, __i2, __s._M_ibegin(), __s._M_iend()); } template<typename _CharT, typename _Traits, typename _Alloc> template<typename _ForwardIter> basic_string<_CharT, _Traits, _Alloc>& basic_string<_CharT, _Traits, _Alloc>:: _M_replace(iterator __i1, iterator __i2, _ForwardIter __k1, _ForwardIter __k2, forward_iterator_tag) { size_type __dold = __i2 - __i1; size_type __dmax = this->max_size(); size_type __dnew = static_cast<size_type>(distance(__k1, __k2)); if (__dmax <= __dnew) __throw_length_error("basic_string::_M_replace"); size_type __off = __i1 - _M_ibegin(); _M_mutate(__off, __dold, __dnew); // Invalidated __i1, __i2 if (__dnew) _S_copy_chars(_M_data() + __off, __k1, __k2); return *this; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>& basic_string<_CharT, _Traits, _Alloc>:: replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2) { return this->replace(_M_check(__pos1), _M_fold(__pos1, __n1), __str._M_check(__pos2), __str._M_fold(__pos2, __n2)); } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT,_Traits,_Alloc>& basic_string<_CharT,_Traits,_Alloc>:: append(const basic_string& __str) { // Iff appending itself, string needs to pre-reserve the // correct size so that _M_mutate does not clobber the // iterators formed here. size_type __size = __str.size(); size_type __len = __size + this->size(); if (__len > this->capacity()) this->reserve(__len); return this->replace(_M_iend(), _M_iend(), __str._M_ibegin(), __str._M_iend()); } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT,_Traits,_Alloc>& basic_string<_CharT,_Traits,_Alloc>:: append(const basic_string& __str, size_type __pos, size_type __n) { // Iff appending itself, string needs to pre-reserve the // correct size so that _M_mutate does not clobber the // iterators formed here. size_type __len = min(__str.size() - __pos, __n) + this->size(); if (__len > this->capacity()) this->reserve(__len); return this->replace(_M_iend(), _M_iend(), __str._M_check(__pos), __str._M_fold(__pos, __n)); } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT,_Traits,_Alloc>& basic_string<_CharT,_Traits,_Alloc>:: append(const _CharT* __s, size_type __n) { size_type __len = __n + this->size(); if (__len > this->capacity()) this->reserve(__len); return this->replace(_M_iend(), _M_iend(), __s, __s + __n); } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT,_Traits,_Alloc>& basic_string<_CharT,_Traits,_Alloc>:: append(size_type __n, _CharT __c) { size_type __len = __n + this->size(); if (__len > this->capacity()) this->reserve(__len); return this->replace(_M_iend(), _M_iend(), __n, __c); } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT,_Traits,_Alloc> operator+(const _CharT* __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs) { typedef basic_string<_CharT,_Traits,_Alloc> __string_type; typedef typename __string_type::size_type __size_type; __size_type __len = _Traits::length(__lhs); __string_type __str; __str.reserve(__len + __rhs.size()); __str.append(__lhs, __lhs + __len); __str.append(__rhs); return __str; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT,_Traits,_Alloc> operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs) { typedef basic_string<_CharT,_Traits,_Alloc> __string_type; typedef typename __string_type::size_type __size_type; __string_type __str; __size_type __len = __rhs.size(); __str.reserve(__len + 1); __str.append(__size_type(1), __lhs); __str.append(__rhs); return __str; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>& basic_string<_CharT, _Traits, _Alloc>:: replace(iterator __i1, iterator __i2, size_type __n2, _CharT __c) { size_type __n1 = __i2 - __i1; size_type __off1 = __i1 - _M_ibegin(); if (max_size() - (this->size() - __n1) <= __n2) __throw_length_error("basic_string::replace"); _M_mutate (__off1, __n1, __n2); // Invalidated __i1, __i2 if (__n2) traits_type::assign(_M_data() + __off1, __n2, __c); return *this; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>:: copy(_CharT* __s, size_type __n, size_type __pos) const { if (__pos > this->size()) __throw_out_of_range("basic_string::copy"); if (__n > this->size() - __pos) __n = this->size() - __pos; traits_type::copy(__s, _M_data() + __pos, __n); // 21.3.5.7 par 3: do not append null. (good.) return __n; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>:: find(const _CharT* __s, size_type __pos, size_type __n) const { size_type __size = this->size(); size_t __xpos = __pos; const _CharT* __data = _M_data(); for (; __xpos + __n <= __size; ++__xpos) if (traits_type::compare(__data + __xpos, __s, __n) == 0) return __xpos; return npos; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>:: find(_CharT __c, size_type __pos) const { size_type __size = this->size(); size_type __ret = npos; if (__pos < __size) { const _CharT* __data = _M_data(); size_type __n = __size - __pos; const _CharT* __p = traits_type::find(__data + __pos, __n, __c); if (__p) __ret = __p - __data; } return __ret; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>:: rfind(const _CharT* __s, size_type __pos, size_type __n) const { size_type __size = this->size(); if (__n <= __size) { __pos = std::min(__size - __n ,__pos); const _CharT* __data = _M_data(); do { if (traits_type::compare(__data + __pos, __s, __n) == 0) return __pos; } while (__pos-- > 0); } return npos; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>:: rfind(_CharT __c, size_type __pos) const { size_type __size = this->size(); if (__size) { size_t __xpos = __size - 1; if (__xpos > __pos) __xpos = __pos; for (++__xpos; __xpos-- > 0; ) if (traits_type::eq(_M_data()[__xpos], __c)) return __xpos; } return npos; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>:: find_first_of(const _CharT* __s, size_type __pos, size_type __n) const { for (; __n && __pos < this->size(); ++__pos) { const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]); if (__p) return __pos; } return npos; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>:: find_last_of(const _CharT* __s, size_type __pos, size_type __n) const { size_type __size = this->size(); if (__size && __n) { if (--__size > __pos) __size = __pos; do { if (traits_type::find(__s, __n, _M_data()[__size])) return __size; } while (__size-- != 0); } return npos; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>:: find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const { size_t __xpos = __pos; for (; __n && __xpos < this->size(); ++__xpos) if (!traits_type::find(__s, __n, _M_data()[__xpos])) return __xpos; return npos; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>:: find_first_not_of(_CharT __c, size_type __pos) const { size_t __xpos = __pos; for (; __xpos < this->size(); ++__xpos) if (!traits_type::eq(_M_data()[__xpos], __c)) return __xpos; return npos; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>:: find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const { size_type __size = this->size(); if (__size && __n) { if (--__size > __pos) __size = __pos; do { if (!traits_type::find(__s, __n, _M_data()[__size])) return __size; } while (__size--); } return npos; } template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc>::size_type basic_string<_CharT, _Traits, _Alloc>:: find_last_not_of(_CharT __c, size_type __pos) const { size_type __size = this->size(); if (__size) { if (--__size > __pos) __size = __pos; do { if (!traits_type::eq(_M_data()[__size], __c)) return __size; } while (__size--); } return npos; } template<typename _CharT, typename _Traits, typename _Alloc> int basic_string<_CharT, _Traits, _Alloc>:: compare(size_type __pos, size_type __n, const basic_string& __str) const { size_type __size = this->size(); size_type __osize = __str.size(); if (__pos > __size) __throw_out_of_range("basic_string::compare"); size_type __rsize= min(__size - __pos, __n); size_type __len = min(__rsize, __osize); int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len); if (!__r) __r = __rsize - __osize; return __r; } template<typename _CharT, typename _Traits, typename _Alloc> int basic_string<_CharT, _Traits, _Alloc>:: compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2) const { size_type __size = this->size(); size_type __osize = __str.size(); if (__pos1 > __size || __pos2 > __osize) __throw_out_of_range("basic_string::compare"); size_type __rsize = min(__size - __pos1, __n1); size_type __rosize = min(__osize - __pos2, __n2); size_type __len = min(__rsize, __rosize); int __r = traits_type::compare(_M_data() + __pos1, __str.data() + __pos2, __len); if (!__r) __r = __rsize - __rosize; return __r; } template<typename _CharT, typename _Traits, typename _Alloc> int basic_string<_CharT, _Traits, _Alloc>:: compare(const _CharT* __s) const { size_type __size = this->size(); int __r = traits_type::compare(_M_data(), __s, __size); if (!__r) __r = __size - traits_type::length(__s); return __r; } template<typename _CharT, typename _Traits, typename _Alloc> int basic_string <_CharT,_Traits,_Alloc>:: compare(size_type __pos, size_type __n1, const _CharT* __s) const { size_type __size = this->size(); if (__pos > __size) __throw_out_of_range("basic_string::compare"); size_type __osize = traits_type::length(__s); size_type __rsize = min(__size - __pos, __n1); size_type __len = min(__rsize, __osize); int __r = traits_type::compare(_M_data() + __pos, __s, __len); if (!__r) __r = __rsize - __osize; return __r; } template<typename _CharT, typename _Traits, typename _Alloc> int basic_string <_CharT,_Traits,_Alloc>:: compare(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::compare"); size_type __osize = min(traits_type::length(__s), __n2); size_type __rsize = min(__size - __pos, __n1); size_type __len = min(__rsize, __osize); int __r = traits_type::compare(_M_data() + __pos, __s, __len); if (!__r) __r = __rsize - __osize; return __r; } template <class _CharT, class _Traits, class _Alloc> void _S_string_copy(const basic_string<_CharT, _Traits, _Alloc>& __str, _CharT* __buf, typename _Alloc::size_type __bufsiz) { typedef typename _Alloc::size_type size_type; size_type __strsize = __str.size(); size_type __bytes = min(__strsize, __bufsiz - 1); _Traits::copy(__buf, __str.data(), __bytes); __buf[__bytes] = _CharT(); }} // namespace std#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?