From patchwork Fri Oct 15 23:48:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 46297 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 759353857C7F for ; Fri, 15 Oct 2021 23:49:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 759353857C7F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1634341784; bh=wexX9FmyDXC/frTpDeVoQHBkN7dl+TTWg4CjwGtbPkE=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=tx37V/4W3DvttpNEoty+edVdiz/dQQ4BzPbIiDq1lwaYpQD/61OiHgqk58ureFJ3y 0tydyyCdp0aX1NYrBBUecSQ5AZiIZX7j55DQl9AkcL0GUWQaqik6cZ15zlXAJl4iET Yw9oGnWaDJIa4Wti+hdR+a7N08w49kaf17NERi3I= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 669053858429 for ; Fri, 15 Oct 2021 23:48:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 669053858429 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-195-lISqz0DqMZG-jhiQ03x-JA-1; Fri, 15 Oct 2021 19:48:38 -0400 X-MC-Unique: lISqz0DqMZG-jhiQ03x-JA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5B3775074B; Fri, 15 Oct 2021 23:48:37 +0000 (UTC) Received: from localhost (unknown [10.33.36.131]) by smtp.corp.redhat.com (Postfix) with ESMTP id E902EB854D; Fri, 15 Oct 2021 23:48:36 +0000 (UTC) Date: Sat, 16 Oct 2021 00:48:35 +0100 To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Define std::basic_string::resize_and_overwrite for C++23 (P1072R10) Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" A recently approved change for the C++23 working draft. libstdc++-v3/ChangeLog: * include/bits/basic_string.h (__cpp_lib_string_resize_and_overwrite): Define for C++23. (basic_string::resize_and_overwrite): Declare. * include/bits/basic_string.tcc (basic_string::resize_and_overwrite): Define. * include/std/version (__cpp_lib_resize_and_overwrite): Define for C++23. * testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc: New test. Tested powerpc64le-linux. Committed to trunk. commit 929abc7fe3ad4491ac412ca232e055618559f268 Author: Jonathan Wakely Date: Fri Oct 15 22:01:25 2021 libstdc++: Define std::basic_string::resize_and_overwrite for C++23 (P1072R10) A recently approved change for the C++23 working draft. libstdc++-v3/ChangeLog: * include/bits/basic_string.h (__cpp_lib_string_resize_and_overwrite): Define for C++23. (basic_string::resize_and_overwrite): Declare. * include/bits/basic_string.tcc (basic_string::resize_and_overwrite): Define. * include/std/version (__cpp_lib_resize_and_overwrite): Define for C++23. * testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc: New test. diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 59c84b1b6ad..a6575fa9e26 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -971,6 +971,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 #pragma GCC diagnostic pop #endif +#if __cplusplus > 202002L +#define __cpp_lib_string_resize_and_overwrite 202110L + template + constexpr void + resize_and_overwrite(size_type __n, _Operation __op); +#endif + /** * Returns the total number of characters that the %string can hold * before needing to allocate more memory. diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index 371f1c3ccee..98c386239f9 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -515,6 +515,37 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __n; } +#if __cplusplus > 202002L + template + template + constexpr void + basic_string<_CharT, _Traits, _Alloc>:: + resize_and_overwrite(size_type __n, _Operation __op) + { + const size_type __capacity = capacity(); + _CharT* __p; + if (__n > __capacity) + { + __p = _M_create(__n, __capacity); + this->_S_copy(__p, _M_data(), length()); // exclude trailing null + _M_dispose(); + _M_data(__p); + _M_capacity(__n); + } + else + __p = _M_data(); + struct _Terminator { + ~_Terminator() { _M_this->_M_set_length(_M_r); } + basic_string* _M_this; + size_type _M_r; + }; + _Terminator __term{this}; + const size_type __n2 [[maybe_unused]] = __n; + __term._M_r = std::move(__op)(__p, __n); + _GLIBCXX_DEBUG_ASSERT(__term._M_r >= 0 && __term._M_r <= __n2); + } +#endif // C++23 + #endif // _GLIBCXX_USE_CXX11_ABI template diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version index 0d7ae3bf857..2b118301da7 100644 --- a/libstdc++-v3/include/std/version +++ b/libstdc++-v3/include/std/version @@ -294,6 +294,9 @@ #define __cpp_lib_is_scoped_enum 202011L #define __cpp_lib_move_only_function 202110L #define __cpp_lib_string_contains 202011L +#if _GLIBCXX_USE_CXX11_ABI // Only supported with cxx11-abi +# define __cpp_lib_string_resize_and_overwrite 202110L +#endif #define __cpp_lib_to_underlying 202102L #endif // C++2b #endif // C++20 diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc b/libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc new file mode 100644 index 00000000000..f0e81126a41 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc @@ -0,0 +1,114 @@ +// { dg-options "-std=gnu++23" } +// { dg-do run { target { c++23 && cxx11-abi } } } + +#include + +#ifndef __cpp_lib_string_resize_and_overwrite +#error "Feature test macro for resize_and_overwrite is missing in " +#elif __cpp_lib_string_resize_and_overwrite != 202110L +# error "Feature test macro for resize_and_overwrite has wrong value in " +#endif + + +#include +#include + +// P1072R10 basic_string::resize_and_overwrite + +void +test01() +{ + std::string s = "foo"; + s.resize_and_overwrite(99, [](char* p, int n) { + VERIFY( n == 99 ); + VERIFY( !std::strncmp(p, "foo", 3) ); + std::strcpy(p, "monkey tennis"); + return 6; + }); + VERIFY( s == "monkey" ); + VERIFY( s.size() == 6 ); + VERIFY( s.capacity() >= 99 ); + VERIFY( s[6] == '\0' ); + + const auto str = s.data(); + + s.resize_and_overwrite(50, [](char* p, int n) -> unsigned { + VERIFY( n == 50 ); + VERIFY( !std::strncmp(p, "monkey", 3) ); + std::strcpy(p, "Partridge among the pidgeons"); + return 9; + }); + VERIFY( s.data() == str ); // No reallocation + VERIFY( s == "Partridge" ); + VERIFY( s[9] == '\0' ); +} + +void +test02() +{ + std::string s; + auto p = s.data(); + s.resize_and_overwrite(0, [](auto...) { return 0; }); + VERIFY( s.empty() ); + VERIFY( s[0] == '\0' ); + VERIFY( s.data() == p ); + + s = "short string"; + p = s.data(); + s.resize_and_overwrite(0, [](auto...) { return 0; }); + VERIFY( s.empty() ); + VERIFY( s[0] == '\0' ); + VERIFY( s.data() == p ); + + s = "a string that is long enough to not be a short string"; + p = s.data(); + s.resize_and_overwrite(0, [](auto...) { return 0; }); + VERIFY( s.empty() ); + VERIFY( s[0] == '\0' ); + VERIFY( s.data() == p ); +} + +void +test03() +{ + struct Op + { + int operator()(char*, int) & = delete; + int operator()(char*, int) const & = delete; + int operator()(char* p, int n) && { std::memset(p, 'a', n+1); return n; } + int operator()(char*, int) const && = delete; + }; + std::string s; + s.resize_and_overwrite(42, Op{}); + VERIFY( s.size() == 42 ); + VERIFY( s == std::string(42, 'a') ); + VERIFY( s[42] == '\0' ); + + s.resize_and_overwrite(0, [](auto&& p, auto&& n) { + static_assert( std::is_same_v ); + static_assert( std::is_same_v ); + return 0; + }); +} + +void +test04() +{ + std::string s = "this tests how the library copes with undefined behaviour"; + + try { + s.resize_and_overwrite(13, [](auto...) -> int { throw "undefined"; }); + } catch (...) { + // The standard doesn't require this, but we leave the string empty: + VERIFY( s.size() == 0 ); + VERIFY( s[0] == '\0' ); + } +} + +int main() +{ + test01(); + test02(); + test03(); + test04(); +}