From 3333f7cc6e5bee683ae7349ed86304e20db5a675 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 4 Jun 2026 23:00:02 +0200 Subject: upgrade to std::format --- src/cmd_line.cpp | 6 +++--- src/error.cpp | 6 ++---- src/handle.cpp | 6 ++---- src/process.cpp | 10 ++++------ 4 files changed, 11 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/cmd_line.cpp b/src/cmd_line.cpp index 46281e2..1f20933 100644 --- a/src/cmd_line.cpp +++ b/src/cmd_line.cpp @@ -16,6 +16,7 @@ // clang-format on #include +#include #include #include #include @@ -129,9 +130,8 @@ std::string CommandLine::escape_cmd(std::string_view arg) { auto safe = escape(arg); for (const auto danger : dangerous_symbols) { - std::ostringstream replacement; - replacement << escape_symbol << danger; - boost::replace_all(safe, std::string{danger}, replacement.str()); + const auto replacement = std::format("{}{}", escape_symbol, danger); + boost::replace_all(safe, std::string{danger}, replacement.data()); } return safe; } diff --git a/src/error.cpp b/src/error.cpp index 3fd18ea..afc33f3 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include #include #include @@ -27,9 +27,7 @@ std::wstring trim_trailing_newline(std::wstring_view s) { } std::string build_what(DWORD code, std::string_view function) { - std::ostringstream what; - what << "Function " << function << " failed with error code " << code; - return what.str(); + return std::format("Function {} failed with error code {}", function, code); } std::string format_message(int32_t code) { diff --git a/src/handle.cpp b/src/handle.cpp index 2260aa3..916b8ae 100644 --- a/src/handle.cpp +++ b/src/handle.cpp @@ -13,7 +13,6 @@ #include #include #include -#include #include #include @@ -21,9 +20,8 @@ namespace winapi { namespace { std::runtime_error write_file_incomplete(std::size_t expected, std::size_t actual) { - std::ostringstream oss; - oss << "WriteFile could only write " << actual << " bytes instead of " << expected; - return std::runtime_error{oss.str()}; + return std::runtime_error{ + std::format("WriteFile could only write {} bytes instead of {}", actual, expected)}; } bool is_std_handle(HANDLE handle) { diff --git a/src/process.cpp b/src/process.cpp index e0799d5..5886c41 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -18,8 +18,8 @@ #include #include +#include #include -#include #include #include #include @@ -384,11 +384,9 @@ Resource Process::get_resource(uint32_t id) { const auto data = ::LockResource(resource); - if (data == NULL) { - std::ostringstream oss; - oss << "Couldn't get data pointer for resource with ID " << id; - throw std::runtime_error{oss.str()}; - } + if (data == NULL) + throw std::runtime_error{ + std::format("Couldn't get data pointer for resource with ID {}", id)}; const auto nb = ::SizeofResource(module, src); -- cgit v1.3