public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-4454] libstdc++: Define std::basic_string::resize_and_overwrite for C++23 (P1072R10) Date: Fri, 15 Oct 2021 23:48:25 +0000 (GMT) [thread overview] Message-ID: <20211015234825.679463858401@sourceware.org> (raw) https://gcc.gnu.org/g:929abc7fe3ad4491ac412ca232e055618559f268 commit r12-4454-g929abc7fe3ad4491ac412ca232e055618559f268 Author: Jonathan Wakely <jwakely@redhat.com> Date: Fri Oct 15 22:01:25 2021 +0100 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: --- libstdc++-v3/include/bits/basic_string.h | 7 ++ libstdc++-v3/include/bits/basic_string.tcc | 31 ++++++ libstdc++-v3/include/std/version | 3 + .../capacity/char/resize_and_overwrite.cc | 114 +++++++++++++++++++++ 4 files changed, 155 insertions(+) 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<typename _Operation> + 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<typename _CharT, typename _Traits, typename _Alloc> + template<typename _Operation> + 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<typename _CharT, typename _Traits, typename _Alloc> 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 <string> + +#ifndef __cpp_lib_string_resize_and_overwrite +#error "Feature test macro for resize_and_overwrite is missing in <string>" +#elif __cpp_lib_string_resize_and_overwrite != 202110L +# error "Feature test macro for resize_and_overwrite has wrong value in <string>" +#endif + + +#include <cstring> +#include <testsuite_hooks.h> + +// 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<decltype(p), char*&> ); + static_assert( std::is_same_v<decltype(n), std::string::size_type&> ); + 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(); +}
reply other threads:[~2021-10-15 23:48 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20211015234825.679463858401@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).