This adds the new C++11 pop_back() member to <string>, <debug/string> and <ext/vstring.h>, as added by LWG 534.
* include/bits/basic_string.h (basic_string::pop_back): Define. * include/ext/vstring.h (versa_string::pop_back): Define. * include/debug/string (basic_string::pop_back): Define. * testsuite/21_strings/basic_string/pop_back.cc: New. * testsuite/ext/vstring/pop_back.cc: New. I also moved the non-const at() overload adjacent to the const at() overload, they had got separated. Tested x86_64-linux, any objections to committing? Is there a more efficient implementation than erase(size()-1, 1) ?
Index: include/bits/basic_string.h =================================================================== --- include/bits/basic_string.h (revision 179472) +++ include/bits/basic_string.h (working copy) @@ -863,6 +863,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return _M_data()[__n]; } + /** + * @brief Provides access to the data contained in the %string. + * @param __n The index of the character to access. + * @return Read/write reference to the character. + * @throw std::out_of_range If @a n is an invalid index. + * + * This function provides for safer data access. The parameter is + * first checked that it is in the range of the string. The function + * throws out_of_range if the check fails. Success results in + * unsharing the string. + */ + reference + at(size_type __n) + { + if (__n >= size()) + __throw_out_of_range(__N("basic_string::at")); + _M_leak(); + return _M_data()[__n]; + } + #ifdef __GXX_EXPERIMENTAL_CXX0X__ /** * Returns a read/write reference to the data at the first @@ -897,26 +917,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return operator[](this->size() - 1); } #endif - /** - * @brief Provides access to the data contained in the %string. - * @param __n The index of the character to access. - * @return Read/write reference to the character. - * @throw std::out_of_range If @a n is an invalid index. - * - * This function provides for safer data access. The parameter is - * first checked that it is in the range of the string. The function - * throws out_of_range if the check fails. Success results in - * unsharing the string. - */ - reference - at(size_type __n) - { - if (__n >= size()) - __throw_out_of_range(__N("basic_string::at")); - _M_leak(); - return _M_data()[__n]; - } - // Modifiers: /** * @brief Append a string to this string. @@ -1392,6 +1392,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION iterator erase(iterator __first, iterator __last); +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + /** + * @brief Remove the last character. + * + * The string must be non-empty. + */ + void + pop_back() + { erase(size()-1, 1); } +#endif // __GXX_EXPERIMENTAL_CXX0X__ + /** * @brief Replace characters with value from another string. * @param __pos Index of first character to replace. Index: include/debug/string =================================================================== --- include/debug/string (revision 179472) +++ include/debug/string (working copy) @@ -580,6 +580,16 @@ namespace __gnu_debug return iterator(__res, this); } +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + void + pop_back() + { + __glibcxx_check_nonempty(); + _Base::pop_back(); + this->_M_invalidate_all(); + } +#endif // __GXX_EXPERIMENTAL_CXX0X__ + basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str) { Index: include/ext/vstring.h =================================================================== --- include/ext/vstring.h (revision 179472) +++ include/ext/vstring.h (working copy) @@ -1137,6 +1137,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return iterator(this->_M_data() + __pos); } +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + /** + * @brief Remove the last character. + * + * The string must be non-empty. + */ + void + pop_back() + { this->_M_erase(size()-1, 1); } +#endif // __GXX_EXPERIMENTAL_CXX0X__ + /** * @brief Replace characters with value from another string. * @param __pos Index of first character to replace. Index: testsuite/21_strings/basic_string/pop_back.cc =================================================================== --- testsuite/21_strings/basic_string/pop_back.cc (revision 0) +++ testsuite/21_strings/basic_string/pop_back.cc (revision 0) @@ -0,0 +1,41 @@ +// Copyright (C) 2011 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 3, 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 COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// 21.4.6.5 basic_string::pop_back +// { dg-options "-std=gnu++0x" } + +#include <string> +#include <testsuite_hooks.h> + +void test01() +{ + bool test __attribute__((unused)) = true; + + const std::string cstr("Badger"); + std::string str = cstr; + str.pop_back(); + VERIFY( str.size() == cstr.size() - 1 ); + + str += cstr.back(); + VERIFY( str == cstr ); +} + +int main() +{ + test01(); + return 0; +} Index: testsuite/ext/vstring/pop_back.cc =================================================================== --- testsuite/ext/vstring/pop_back.cc (revision 0) +++ testsuite/ext/vstring/pop_back.cc (revision 0) @@ -0,0 +1,45 @@ +// Copyright (C) 2011 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 3, 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 COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// 21.4.6.5 basic_string::pop_back +// { dg-options "-std=gnu++0x" } + +#include <ext/vstring.h> +#include <testsuite_hooks.h> + +template<typename StrT> +int test01() +{ + bool test __attribute__((unused)) = true; + + const StrT cstr("Badger"); + StrT str = cstr; + str.pop_back(); + VERIFY( str.size() == cstr.size() - 1 ); + + str += cstr.back(); + VERIFY( str == cstr ); + + return test; +} + +int main() +{ + test01<__gnu_cxx::__sso_string>(); + test01<__gnu_cxx::__rc_string>(); + return 0; +}