📄 vstring.h
字号:
// Versatile string -*- C++ -*-// Copyright (C) 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,// 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./** @file ext/vstring.h * This file is a GNU extension to the Standard C++ Library. */#ifndef _VSTRING_H#define _VSTRING_H 1#pragma GCC system_header#include <ext/vstring_util.h>#include <ext/rc_string_base.h>#include <ext/sso_string_base.h>namespace __gnu_cxx{ /** * @class __versa_string vstring.h * @brief Managing sequences of characters and character-like objects. */ // Template class __versa_string template<typename _CharT, typename _Traits, typename _Alloc, template <typename, typename, typename> class _Base> class __versa_string : private _Base<_CharT, _Traits, _Alloc> { typedef _Base<_CharT, _Traits, _Alloc> __vstring_base; typedef typename __vstring_base::_CharT_alloc_type _CharT_alloc_type; // Types: public: typedef _Traits traits_type; typedef typename _Traits::char_type value_type; typedef _Alloc allocator_type; typedef typename _CharT_alloc_type::size_type size_type; typedef typename _CharT_alloc_type::difference_type difference_type; typedef typename _CharT_alloc_type::reference reference; typedef typename _CharT_alloc_type::const_reference const_reference; typedef typename _CharT_alloc_type::pointer pointer; typedef typename _CharT_alloc_type::const_pointer const_pointer; typedef __gnu_cxx::__normal_iterator<pointer, __versa_string> iterator; typedef __gnu_cxx::__normal_iterator<const_pointer, __versa_string> const_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; typedef std::reverse_iterator<iterator> reverse_iterator; // Data Member (public): // NB: This is an unsigned type, and thus represents the maximum // size that the allocator can hold. /// Value returned by various member functions when they fail. static const size_type npos = static_cast<size_type>(-1); private: size_type _M_check(size_type __pos, const char* __s) const { if (__pos > this->size()) std::__throw_out_of_range(__N(__s)); return __pos; } void _M_check_length(size_type __n1, size_type __n2, const char* __s) const { if (this->max_size() - (this->size() - __n1) < __n2) std::__throw_length_error(__N(__s)); } // NB: _M_limit doesn't check for a bad __pos value. size_type _M_limit(size_type __pos, size_type __off) const { const bool __testoff = __off < this->size() - __pos; return __testoff ? __off : this->size() - __pos; } // True if _Rep and source do not overlap. bool _M_disjunct(const _CharT* __s) const { return (std::less<const _CharT*>()(__s, this->_M_data()) || std::less<const _CharT*>()(this->_M_data() + this->size(), __s)); } // For the internal use we have functions similar to `begin'/`end' // but they do not call _M_leak. iterator _M_ibegin() const { return iterator(this->_M_data()); } iterator _M_iend() const { return iterator(this->_M_data() + this->_M_length()); } public: // Construct/copy/destroy: // NB: We overload ctors in some cases instead of using default // arguments, per 17.4.4.4 para. 2 item 2. /** * @brief Default constructor creates an empty string. */ __versa_string() : __vstring_base() { } /** * @brief Construct an empty string using allocator @a a. */ explicit __versa_string(const _Alloc& __a) : __vstring_base(__a) { } // NB: per LWG issue 42, semantics different from IS: /** * @brief Construct string with copy of value of @a str. * @param str Source string. */ __versa_string(const __versa_string& __str) : __vstring_base(__str) { } /** * @brief Construct string as copy of a substring. * @param str Source string. * @param pos Index of first character to copy from. * @param n Number of characters to copy (default remainder). */ __versa_string(const __versa_string& __str, size_type __pos, size_type __n = npos) : __vstring_base(__str._M_data() + __str._M_check(__pos, "__versa_string::__versa_string"), __str._M_data() + __str._M_limit(__pos, __n) + __pos, _Alloc()) { } /** * @brief Construct string as copy of a substring. * @param str Source string. * @param pos Index of first character to copy from. * @param n Number of characters to copy. * @param a Allocator to use. */ __versa_string(const __versa_string& __str, size_type __pos, size_type __n, const _Alloc& __a) : __vstring_base(__str._M_data() + __str._M_check(__pos, "__versa_string::__versa_string"), __str._M_data() + __str._M_limit(__pos, __n) + __pos, __a) { } /** * @brief Construct string initialized by a character array. * @param s Source character array. * @param n Number of characters to copy. * @param a Allocator to use (default is default allocator). * * NB: @a s must have at least @a n characters, '\0' has no special * meaning. */ __versa_string(const _CharT* __s, size_type __n, const _Alloc& __a = _Alloc()) : __vstring_base(__s, __s + __n, __a) { } /** * @brief Construct string as copy of a C string. * @param s Source C string. * @param a Allocator to use (default is default allocator). */ __versa_string(const _CharT* __s, const _Alloc& __a = _Alloc()) : __vstring_base(__s, __s ? __s + traits_type::length(__s) : __s + npos, __a) { } /** * @brief Construct string as multiple characters. * @param n Number of characters. * @param c Character to use. * @param a Allocator to use (default is default allocator). */ __versa_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) : __vstring_base(__n, __c, __a) { } /** * @brief Construct string as copy of a range. * @param beg Start of range. * @param end End of range. * @param a Allocator to use (default is default allocator). */ template<class _InputIterator> __versa_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a = _Alloc()) : __vstring_base(__beg, __end, __a) { } /** * @brief Destroy the string instance. */ ~__versa_string() { } /** * @brief Assign the value of @a str to this string. * @param str Source string. */ __versa_string& operator=(const __versa_string& __str) { return this->assign(__str); } /** * @brief Copy contents of @a s into this string. * @param s Source null-terminated string. */ __versa_string& operator=(const _CharT* __s) { return this->assign(__s); } /** * @brief Set value to string of length 1. * @param c Source character. * * Assigning to a character makes this string length 1 and * (*this)[0] == @a c. */ __versa_string& operator=(_CharT __c) { this->assign(1, __c); return *this; } // Iterators: /** * Returns a read/write iterator that points to the first character in * the %string. Unshares the string. */ iterator begin() { this->_M_leak(); return iterator(this->_M_data()); } /** * Returns a read-only (constant) iterator that points to the first * character in the %string. */ const_iterator begin() const { return const_iterator(this->_M_data()); } /** * Returns a read/write iterator that points one past the last * character in the %string. Unshares the string. */ iterator end() { this->_M_leak(); return iterator(this->_M_data() + this->size()); } /** * Returns a read-only (constant) iterator that points one past the * last character in the %string. */ const_iterator end() const { return const_iterator(this->_M_data() + this->size()); } /** * Returns a read/write reverse iterator that points to the last * character in the %string. Iteration is done in reverse element * order. Unshares the string. */ reverse_iterator rbegin() { return reverse_iterator(this->end()); } /** * Returns a read-only (constant) reverse iterator that points * to the last character in the %string. Iteration is done in * reverse element order. */ const_reverse_iterator rbegin() const { return const_reverse_iterator(this->end()); } /** * Returns a read/write reverse iterator that points to one before the * first character in the %string. Iteration is done in reverse * element order. Unshares the string. */ reverse_iterator rend() { return reverse_iterator(this->begin()); } /** * Returns a read-only (constant) reverse iterator that points * to one before the first character in the %string. Iteration * is done in reverse element order. */ const_reverse_iterator rend() const { return const_reverse_iterator(this->begin()); } public: // Capacity: /// Returns the number of characters in the string, not including any /// null-termination. size_type size() const { return this->_M_length(); } /// Returns the number of characters in the string, not including any /// null-termination. size_type
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -