From 69bb47a5f181b52f45d73f03b5e244601cdd5a5b Mon Sep 17 00:00:00 2001 From: Fini Jastrow Date: Wed, 26 Feb 2025 10:32:29 +0100 Subject: [PATCH] Enable new NRVO warning [why] While RVO is guaranteed and we will get warnings about pessimizations the c++17 new optional NRVO is warned on only on request (except on mcvc). To get optimal code we of course want to benefit from NRVO optimizations. Signed-off-by: Fini Jastrow --- src/escape.cc | 9 +++++---- src/meson.build | 3 +++ src/replace.cc | 7 ++++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/escape.cc b/src/escape.cc index d9c45c72..19426270 100644 --- a/src/escape.cc +++ b/src/escape.cc @@ -83,13 +83,14 @@ std::string unescape(string_view in) auto rit = std::regex_iterator{ in.cbegin(), in.cend(), re }; auto const rend = std::regex_iterator{ }; auto last = decltype(*rit){ }; - - if (rit == rend) - return std::string{ in }; - auto unescaped = ""s; unescaped.reserve(in.length()); + if (rit == rend) { + unescaped.assign(in.data(), in.size()); + return unescaped; + } + for (; rit != rend; ++rit) { last = *rit; unescaped += rit->prefix(); diff --git a/src/meson.build b/src/meson.build index a2692485..bcb164db 100644 --- a/src/meson.build +++ b/src/meson.build @@ -99,6 +99,9 @@ endif if meson.get_compiler('cpp').has_argument('-Wconversion') # msvc doesnt have add_cpp_args += [ '-Wconversion' ] endif +if meson.get_compiler('cpp').has_argument('-Wnrvo') # msvc: included in permissive- + add_cpp_args += [ '-Wnrvo' ] +endif deps = dependency('threads') diff --git a/src/replace.cc b/src/replace.cc index 68449c21..b3374afd 100644 --- a/src/replace.cc +++ b/src/replace.cc @@ -28,11 +28,12 @@ namespace gul14 { std::string replace(string_view haystack, string_view needle, string_view hammer) { - if (needle.empty()) - return std::string(haystack); - auto result = ""s; result.reserve(haystack.length()); + if (needle.empty()) { + result.assign(haystack.data(), haystack.length()); + return result; + } std::size_t pos = 0; std::size_t last_pos = 0;