From patchwork Tue Oct 4 21:35:50 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 117702 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id A68A31007D1 for ; Wed, 5 Oct 2011 08:36:10 +1100 (EST) Received: (qmail 20734 invoked by alias); 4 Oct 2011 21:36:08 -0000 Received: (qmail 20477 invoked by uid 22791); 4 Oct 2011 21:36:06 -0000 X-SWARE-Spam-Status: No, hits=-2.5 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW X-Spam-Check-By: sourceware.org Received: from mail-pz0-f47.google.com (HELO mail-pz0-f47.google.com) (209.85.210.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 04 Oct 2011 21:35:51 +0000 Received: by pzk4 with SMTP id 4so2643406pzk.6 for ; Tue, 04 Oct 2011 14:35:50 -0700 (PDT) MIME-Version: 1.0 Received: by 10.68.17.4 with SMTP id k4mr12791852pbd.8.1317764150543; Tue, 04 Oct 2011 14:35:50 -0700 (PDT) Received: by 10.143.30.6 with HTTP; Tue, 4 Oct 2011 14:35:50 -0700 (PDT) Date: Tue, 4 Oct 2011 22:35:50 +0100 Message-ID: Subject: [v3] define string::pop_back() From: Jonathan Wakely To: "libstdc++" , gcc-patches Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org This adds the new C++11 pop_back() member to , and , 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 +// . + +// 21.4.6.5 basic_string::pop_back +// { dg-options "-std=gnu++0x" } + +#include +#include + +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 +// . + +// 21.4.6.5 basic_string::pop_back +// { dg-options "-std=gnu++0x" } + +#include +#include + +template +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; +}