string.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 1,794 行 · 第 1/5 页

HPP
1,794
字号
   void priv_addr(pointer addr)   {  this->members_.m_repr.long_repr().start = addr;  }   size_type priv_storage() const   {  return this->is_short() ? InternalBufferChars : this->members_.m_repr.long_repr().storage;  }   void priv_storage(size_type storage)   {        if(!this->is_short())         this->members_.m_repr.long_repr().storage = storage;   }   size_type priv_size() const   {  return this->is_short() ? this->members_.m_repr.short_repr().h.length : this->members_.m_repr.long_repr().length;  }   void priv_size(size_type sz)   {        if(this->is_short())         this->members_.m_repr.s.h.length = (unsigned char)sz;      else         this->members_.m_repr.long_repr().length = static_cast<typename A::size_type>(sz);   }   void swap(basic_string_base& other)    {      if(this->is_short()){         if(other.is_short()){            std::swap(this->members_.m_repr, other.members_.m_repr);         }         else{            repr_t copied(this->members_.m_repr);            this->members_.m_repr.long_repr() = other.members_.m_repr.long_repr();            other.members_.m_repr = copied;         }      }      else{         if(other.is_short()){            repr_t copied(other.members_.m_repr);            other.members_.m_repr.long_repr() = this->members_.m_repr.long_repr();            this->members_.m_repr = copied;         }         else{            std::swap(this->members_.m_repr.long_repr(), other.members_.m_repr.long_repr());         }      }      allocator_type & this_al = this->alloc(), &other_al = other.alloc();      if(this_al != other_al){         detail::do_swap(this_al, other_al);      }   }};/// @endcond}  //namespace detail {//! The basic_string class represents a Sequence of characters. It contains all the //! usual operations of a Sequence, and, additionally, it contains standard string //! operations such as search and concatenation.//!//! The basic_string class is parameterized by character type, and by that type's //! Character Traits.//! //! This class has performance characteristics very much like vector<>, meaning, //! for example, that it does not perform reference-count or copy-on-write, and that//! concatenation of two strings is an O(N) operation. //! //! Some of basic_string's member functions use an unusual method of specifying positions //! and ranges. In addition to the conventional method using iterators, many of //! basic_string's member functions use a single value pos of type size_type to represent a //! position (in which case the position is begin() + pos, and many of basic_string's //! member functions use two values, pos and n, to represent a range. In that case pos is //! the beginning of the range and n is its size. That is, the range is //! [begin() + pos, begin() + pos + n). //! //! Note that the C++ standard does not specify the complexity of basic_string operations. //! In this implementation, basic_string has performance characteristics very similar to //! those of vector: access to a single character is O(1), while copy and concatenation //! are O(N).//! //! In this implementation, begin(), //! end(), rbegin(), rend(), operator[], c_str(), and data() do not invalidate iterators.//! In this implementation, iterators are only invalidated by member functions that//! explicitly change the string's contents. template <class CharT, class Traits, class A> class basic_string   :  private detail::basic_string_base<A> {   /// @cond   private:   typedef detail::basic_string_base<A> base_t;   static const typename base_t::size_type InternalBufferChars = base_t::InternalBufferChars;   protected:   // A helper class to use a char_traits as a function object.   template <class Tr>   struct Eq_traits      : public std::binary_function<typename Tr::char_type,                                    typename Tr::char_type,                                    bool>   {      bool operator()(const typename Tr::char_type& x,                      const typename Tr::char_type& y) const         { return Tr::eq(x, y); }   };   template <class Tr>   struct Not_within_traits      : public std::unary_function<typename Tr::char_type, bool>   {      typedef const typename Tr::char_type* Pointer;      const Pointer m_first;      const Pointer m_last;      Not_within_traits(Pointer f, Pointer l)          : m_first(f), m_last(l) {}      bool operator()(const typename Tr::char_type& x) const       {         return std::find_if(m_first, m_last,                         std::bind1st(Eq_traits<Tr>(), x)) == m_last;      }   };   /// @endcond   public:   //! The allocator type   typedef A                                       allocator_type;   //! The stored allocator type   typedef allocator_type                          stored_allocator_type;   //! The type of object, CharT, stored in the string   typedef CharT                                   value_type;   //! The second template parameter Traits   typedef Traits                                  traits_type;   //! Pointer to CharT   typedef typename A::pointer                     pointer;   //! Const pointer to CharT    typedef typename A::const_pointer               const_pointer;   //! Reference to CharT    typedef typename A::reference                   reference;   //! Const reference to CharT    typedef typename A::const_reference             const_reference;   //! An unsigned integral type   typedef typename A::size_type                   size_type;   //! A signed integral type   typedef typename A::difference_type             difference_type;   //! Iterator used to iterate through a string. It's a Random Access Iterator   typedef pointer                                 iterator;   //! Const iterator used to iterate through a string. It's a Random Access Iterator   typedef const_pointer                           const_iterator;   //! Iterator used to iterate backwards through a string   typedef std::reverse_iterator<iterator>       reverse_iterator;   //! Const iterator used to iterate backwards through a string   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;   //! The largest possible value of type size_type. That is, size_type(-1).    static const size_type npos;   /// @cond   private:   typedef constant_iterator<CharT, difference_type> cvalue_iterator;   /// @endcond   public:                         // Constructor, destructor, assignment.   /// @cond   struct reserve_t {};   /// @endcond   basic_string(reserve_t, std::size_t n,               const allocator_type& a = allocator_type())      : base_t(a, n + 1)   { this->priv_terminate_string(); }   //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter.   //!    //! <b>Throws</b>: If allocator_type's copy constructor throws.   explicit basic_string(const allocator_type& a = allocator_type())      : base_t(a, InternalBufferChars)   { this->priv_terminate_string(); }   //! <b>Effects</b>: Copy constructs a basic_string.   //!   //! <b>Postcondition</b>: x == *this.   //!    //! <b>Throws</b>: If allocator_type's default constructor or copy constructor throws.   basic_string(const basic_string& s)       : base_t(s.alloc())    { this->priv_range_initialize(s.begin(), s.end()); }   //! <b>Effects</b>: Move constructor. Moves mx's resources to *this.   //!   //! <b>Throws</b>: If allocator_type's copy constructor throws.   //!    //! <b>Complexity</b>: Constant.   #ifndef BOOST_INTERPROCESS_RVALUE_REFERENCE   basic_string(const detail::moved_object<basic_string>& s)       : base_t(detail::move_impl((base_t&)s.get()))   {}   #else   basic_string(basic_string && s)       : base_t(detail::move_impl((base_t&)s))   {}   #endif   //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,   //!   and is initialized by a specific number of characters of the s string.    basic_string(const basic_string& s, size_type pos, size_type n = npos,               const allocator_type& a = allocator_type())       : base_t(a)    {      if (pos > s.size())         this->throw_out_of_range();      else         this->priv_range_initialize            (s.begin() + pos, s.begin() + pos + min_value(n, s.size() - pos));   }   //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,   //!   and is initialized by a specific number of characters of the s c-string.   basic_string(const CharT* s, size_type n,               const allocator_type& a = allocator_type())       : base_t(a)    { this->priv_range_initialize(s, s + n); }   //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,   //!   and is initialized by the null-terminated s c-string.   basic_string(const CharT* s,                const allocator_type& a = allocator_type())      : base_t(a)    { this->priv_range_initialize(s, s + Traits::length(s)); }   //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,   //!   and is initialized by n copies of c.   basic_string(size_type n, CharT c,                const allocator_type& a = allocator_type())      : base_t(a)   {        this->priv_range_initialize(cvalue_iterator(c, n),                                  cvalue_iterator());   }   //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,   //!   and a range of iterators.   template <class InputIterator>   basic_string(InputIterator f, InputIterator l,               const allocator_type& a = allocator_type())      : base_t(a)   {      //Dispatch depending on integer/iterator      const bool aux_boolean = detail::is_convertible<InputIterator, std::size_t>::value;      typedef detail::bool_<aux_boolean> Result;      this->priv_initialize_dispatch(f, l, Result());   }   //! <b>Effects</b>: Destroys the basic_string. All used memory is deallocated.   //!   //! <b>Throws</b>: Nothing.   //!   //! <b>Complexity</b>: Constant.   ~basic_string()    {}         //! <b>Effects</b>: Copy constructs a string.   //!   //! <b>Postcondition</b>: x == *this.   //!    //! <b>Complexity</b>: Linear to the elements x contains.   basic_string& operator=(const basic_string& s)   {      if (&s != this)          this->assign(s.begin(), s.end());      return *this;   }   //! <b>Effects</b>: Move constructor. Moves mx's resources to *this.   //!   //! <b>Throws</b>: If allocator_type's copy constructor throws.   //!    //! <b>Complexity</b>: Constant.   #ifndef BOOST_INTERPROCESS_RVALUE_REFERENCE   basic_string& operator=(const detail::moved_object<basic_string>& ms)   {      basic_string &s = ms.get();      if (&s != this){         this->swap(s);      }      return *this;   }   #else   basic_string& operator=(basic_string && ms)   {      basic_string &s = ms;      if (&s != this){         this->swap(s);      }      return *this;   }   #endif   //! <b>Effects</b>: Assignment from a null-terminated c-string.   basic_string& operator=(const CharT* s)    { return this->assign(s, s + Traits::length(s)); }   //! <b>Effects</b>: Assignment from character.   basic_string& operator=(CharT c)   { return this->assign(static_cast<size_type>(1), c); }   //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.   //!    //! <b>Throws</b>: Nothing.   //!    //! <b>Complexity</b>: Constant.   iterator begin()   { return this->priv_addr(); }   //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.   //!    //! <b>Throws</b>: Nothing.   //!    //! <b>Complexity</b>: Constant.   const_iterator begin() const   { return this->priv_addr(); }   //! <b>Effects</b>: Returns an iterator to the end of the vector.   //!    //! <b>Throws</b>: Nothing.   //!    //! <b>Complexity</b>: Constant.   iterator end()   { return this->priv_addr() + this->priv_size(); }   //! <b>Effects</b>: Returns a const_iterator to the end of the vector.   //!    //! <b>Throws</b>: Nothing.   //!    //! <b>Complexity</b>: Constant.   const_iterator end() const    { return this->priv_addr() + this->priv_size(); }     //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning    //! of the reversed vector.    //!    //! <b>Throws</b>: Nothing.   //!    //! <b>Complexity</b>: Constant.   reverse_iterator rbegin()                { return reverse_iterator(this->priv_addr() + this->priv_size()); }   //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning    //! of the reversed vector.    //!    //! <b>Throws</b>: Nothing.   //!    //! <b>Complexity</b>: Constant.   const_reverse_iterator rbegin() const    { return const_reverse_iterator(this->priv_addr() + this->priv_size()); }   //! <b>Effects</b>: Returns a reverse_iterator pointing to the end

⌨️ 快捷键说明

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