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 | |
| parent | upgrade to std::optional (diff) | |
| download | winapi-common-3333f7cc6e5bee683ae7349ed86304e20db5a675.tar.gz winapi-common-3333f7cc6e5bee683ae7349ed86304e20db5a675.zip | |
upgrade to std::format
| -rw-r--r-- | include/winapi/buffer.hpp | 9 | ||||
| -rw-r--r-- | include/winapi/error.hpp | 6 | ||||
| -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 |
6 files changed, 16 insertions, 27 deletions
diff --git a/include/winapi/buffer.hpp b/include/winapi/buffer.hpp index bebd05f..1009ebb 100644 --- a/include/winapi/buffer.hpp +++ b/include/winapi/buffer.hpp @@ -7,8 +7,8 @@ #include <cstddef> #include <cstring> +#include <format> #include <initializer_list> -#include <sstream> #include <stdexcept> #include <string> #include <string_view> @@ -67,11 +67,8 @@ public: std::wstring as_utf16() const { const auto c_str = reinterpret_cast<const wchar_t*>(data()); const auto nb = size(); - if (nb % 2 != 0) { - std::ostringstream oss; - oss << "Buffer size invalid at " << nb << " bytes"; - throw std::runtime_error{oss.str()}; - } + if (nb % 2 != 0) + throw std::runtime_error{std::format("Buffer size invalid at {} bytes", nb)}; const auto nch = nb / 2; return {c_str, nch}; } diff --git a/include/winapi/error.hpp b/include/winapi/error.hpp index 35e10f0..30237e3 100644 --- a/include/winapi/error.hpp +++ b/include/winapi/error.hpp @@ -13,7 +13,7 @@ #include <windows.h> #include <cstdint> -#include <sstream> +#include <format> #include <stdexcept> #include <string> #include <system_error> @@ -44,9 +44,7 @@ std::system_error windows(DWORD code, const char* function); template <typename Ret> std::runtime_error custom(Ret ret, const char* function) { - std::ostringstream oss; - oss << "Function " << function << " failed with error code " << ret; - return std::runtime_error{oss.str()}; + return std::runtime_error{std::format("Function {} failed with error code {}", function, ret)}; } } // namespace error 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); |
