diff --git a/include/boost/container/string.hpp b/include/boost/container/string.hpp index 65e8859..33f5f66 100644 --- a/include/boost/container/string.hpp +++ b/include/boost/container/string.hpp @@ -1,6 +1,6 @@ ////////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// (C) Copyright Ion Gaztanaga 2005-2015. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // @@ -1124,6 +1124,62 @@ class basic_string // ////////////////////////////////////////////// + //! Requires: !empty() + //! + //! Effects: Returns a reference to the first + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference front() BOOST_NOEXCEPT_OR_NOTHROW + { + BOOST_ASSERT(!this->empty()); + return *this->priv_addr(); + } + + //! Requires: !empty() + //! + //! Effects: Returns a const reference to the first + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW + { + BOOST_ASSERT(!this->empty()); + return *this->priv_addr(); + } + + //! Requires: !empty() + //! + //! Effects: Returns a reference to the last + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference back() BOOST_NOEXCEPT_OR_NOTHROW + { + BOOST_ASSERT(!this->empty()); + return *(this->priv_addr() + (this->size() - 1u) ); + } + + //! Requires: !empty() + //! + //! Effects: Returns a const reference to the last + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW + { + BOOST_ASSERT(!this->empty()); + return *(this->priv_addr() + (this->size() - 1u) ); + } + //! Requires: size() > n. //! //! Effects: Returns a reference to the nth element @@ -1133,7 +1189,10 @@ class basic_string //! //! Complexity: Constant. reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW - { return *(this->priv_addr() + n); } + { + BOOST_ASSERT(this->size() > n); + return *(this->priv_addr() + n); + } //! Requires: size() > n. //! @@ -1144,7 +1203,10 @@ class basic_string //! //! Complexity: Constant. const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW - { return *(this->priv_addr() + n); } + { + BOOST_ASSERT(this->size() > n); + return *(this->priv_addr() + n); + } //! Requires: size() > n. //! @@ -1626,6 +1688,18 @@ class basic_string } #endif + //! Effects: Removes the last element from the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant time. + void pop_back() BOOST_NOEXCEPT_OR_NOTHROW + { + BOOST_ASSERT(!this->empty()); + iterator p = this->end(); + this->erase(--p); + } + //! Requires: pos <= size() //! //! Effects: Determines the effective length xlen of the string to be removed as the smaller of n and size() - pos. @@ -1687,18 +1761,6 @@ class basic_string return iterator(f); } - //! Requires: !empty() - //! - //! Throws: Nothing - //! - //! Effects: Equivalent to erase(size() - 1, 1). - void pop_back() BOOST_NOEXCEPT_OR_NOTHROW - { - const size_type old_size = this->priv_size(); - Traits::assign(this->priv_addr()[old_size-1], CharT(0)); - this->priv_size(old_size-1);; - } - //! Effects: Erases all the elements of the vector. //! //! Throws: Nothing. diff --git a/test/string_test.cpp b/test/string_test.cpp index 098c5f3..75c61a1 100644 --- a/test/string_test.cpp +++ b/test/string_test.cpp @@ -440,6 +440,37 @@ int string_test() if(!StringEqual()(bs4, ss4)){ return 1; } + + //Check front/back/begin/end + + if(bs4.front() != *ss4.begin()) + return 1; + + if(bs4.back() != *(ss4.end()-1)) + return 1; + + bs4.pop_back(); + ss4.erase(ss4.end()-1); + if(!StringEqual()(bs4, ss4)){ + return 1; + } + + if(*bs4.begin() != *ss4.begin()) + return 1; + if(*bs4.cbegin() != *ss4.begin()) + return 1; + if(*bs4.rbegin() != *ss4.rbegin()) + return 1; + if(*bs4.crbegin() != *ss4.rbegin()) + return 1; + if(*(bs4.end()-1) != *(ss4.end()-1)) + return 1; + if(*(bs4.cend()-1) != *(ss4.end()-1)) + return 1; + if(*(bs4.rend()-1) != *(ss4.rend()-1)) + return 1; + if(*(bs4.crend()-1) != *(ss4.rend()-1)) + return 1; } //When done, delete vector