Skip to content

Commit

Permalink
work around GCC bug affecting std::string::replace
Browse files Browse the repository at this point in the history
  • Loading branch information
grisumbras committed Aug 27, 2024
1 parent 6ebffc0 commit 46338ce
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions test/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@
#include "test.hpp"
#include "test_suite.hpp"

namespace
{

// std::string::replace is buggy on GCC 12+ with -std=c++20 -O3
// so we have to use a helper
std::string&
string_replace(
std::string& str, std::size_t pos, std::size_t count,
char const* sub, std::size_t count2)
{
std::string temp(sub, count2);
return str.replace(pos, count, temp);
}

} // namespace

namespace boost {
namespace json {

Expand Down Expand Up @@ -2266,8 +2282,10 @@ class string_test
{
std::string s1(t.v2.data(), t.v2.size());
string s2(t.v2, sp);
BOOST_TEST(s2.replace(1, 1, s2.subview(0)) ==
s1.replace(1, 1, s1.data(), s1.size()));
BOOST_TEST(
s2.replace( 1, 1, s2.subview(0) )
==
string_replace(s1, 1, 1, s1.data(), s1.size() ));
});

// inside, reallocate, split
Expand All @@ -2277,8 +2295,10 @@ class string_test
string s2(t.v2, sp);
s1.append(s1);
s2.append(s2);
BOOST_TEST(s2.replace(1, 1, s2.subview(0)) ==
s1.replace(1, 1, s1.data(), s1.size()));
BOOST_TEST(
s2.replace( 1, 1, s2.subview(0) )
==
string_replace( s1, 1, 1, s1.data(), s1.size() ));
});

// inside, same, split
Expand Down Expand Up @@ -2361,14 +2381,9 @@ class string_test
std::string s1(t.v2.data(), t.v2.size());
string s2(t.v2, sp);
BOOST_TEST(
s2.replace(
s2.begin() + 1,
s2.begin() + 2,
s2.subview(0)) ==
s1.replace(
1, 1,
s1.data(),
s1.size()));
s2.replace( s2.begin() + 1, s2.begin() + 2, s2.subview(0) )
==
string_replace( s1, 1, 1, s1.data(), s1.size() ));
});

// inside, same
Expand Down

0 comments on commit 46338ce

Please sign in to comment.