diff options
| author | Egor Tensin <egor@tensin.name> | 2026-06-04 23:00:02 +0200 |
|---|---|---|
| committer | Egor Tensin <egor@tensin.name> | 2026-06-04 23:00:02 +0200 |
| commit | 3333f7cc6e5bee683ae7349ed86304e20db5a675 (patch) | |
| tree | 81d2d574b9a21af6f9e56325fba8dc418ad7b3e3 /src | |
| parent | upgrade to std::optional (diff) | |
| download | winapi-common-3333f7cc6e5bee683ae7349ed86304e20db5a675.tar.gz winapi-common-3333f7cc6e5bee683ae7349ed86304e20db5a675.zip | |
upgrade to std::format
Diffstat (limited to '')
| -rw-r--r-- | src/cmd_line.cpp | 6 | ||||
| -rw-r--r-- | src/error.cpp | 6 | ||||
| -rw-r--r-- | src/handle.cpp | 6 | ||||
| -rw-r--r-- | src/process.cpp | 10 |
4 files changed, 11 insertions, 17 deletions
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 <cstddef> +#include <format> #include <memory> #include <sstream> #include <stdexcept> @@ -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 <windows.h> #include <cstdint> -#include <sstream> +#include <format> #include <string> #include <string_view> #include <system_error> @@ -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 <cassert> #include <cstddef> #include <limits> -#include <sstream> #include <stdexcept> #include <utility> @@ -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 <cstddef> #include <cstring> +#include <format> #include <limits> -#include <sstream> #include <stdexcept> #include <string> #include <utility> @@ -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); |
