@@ -566,13 +566,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Operation>
constexpr void
basic_string<_CharT, _Traits, _Alloc>::
- resize_and_overwrite(size_type __n, _Operation __op)
+ resize_and_overwrite(const size_type __n, _Operation __op)
{
const size_type __capacity = capacity();
_CharT* __p;
if (__n > __capacity)
{
- __p = _M_create(__n, __capacity);
+ auto __new_capacity = __n; // Must not allow _M_create to modify __n.
+ __p = _M_create(__new_capacity, __capacity);
this->_S_copy(__p, _M_data(), length()); // exclude trailing null
#if __cpp_lib_is_constant_evaluated
if (std::is_constant_evaluated())
@@ -580,7 +581,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
_M_dispose();
_M_data(__p);
- _M_capacity(__n);
+ _M_capacity(__new_capacity);
}
else
__p = _M_data();
@@ -120,6 +120,26 @@ test05()
return true;
}
+void
+test06()
+{
+ std::string s = "0123456789";
+ s.resize_and_overwrite(16, [](char* p, int n) {
+ VERIFY( n == 16 );
+ std::char_traits<char>::copy(p + 10, "0123456798", 6);
+ return n;
+ });
+ VERIFY( s.size() == 16 );
+ VERIFY( s == "0123456789012345" );
+
+ s.resize_and_overwrite(4, [](char* p, int n) {
+ VERIFY( n == 4 );
+ std::char_traits<char>::copy(p, "abcd", 4);
+ return n;
+ });
+ VERIFY( s.size() == 4 );
+}
+
int main()
{
test01();
@@ -127,4 +147,5 @@ int main()
test03();
test04();
static_assert( test05() );
+ test06();
}
@@ -256,6 +256,11 @@ test_wchar()
std::locale loc;
s = std::format(loc, L"{:L} {:.3s}{:Lc}", true, L"data"sv, '.');
VERIFY( s == L"true dat." );
+
+ s = std::format(L"{}", 0.0625);
+ VERIFY( s == L"0.0625" );
+ s = std::format(L"{}", 0.25);
+ VERIFY( s == L"0.25" );
}
void
new file mode 100644
@@ -0,0 +1,4 @@
+// { dg-do run { target c++23 } }
+// This test does not have -std=gnu++20 in dg-options so that format.cc
+// can be tested for e.g. -std=c++26
+#include "format.cc"