diff options
| author | Egor Tensin <egor@tensin.name> | 2026-06-04 21:14:05 +0200 |
|---|---|---|
| committer | Egor Tensin <egor@tensin.name> | 2026-06-04 21:15:38 +0200 |
| commit | d656c4124f9e796d6e80c6a1baa9c5c3fdf977a5 (patch) | |
| tree | 3f0fba428421f2c0ccf466acf98ee45a83b0cc3c | |
| parent | upgrade cmake-common (diff) | |
| download | winapi-utf8-d656c4124f9e796d6e80c6a1baa9c5c3fdf977a5.tar.gz winapi-utf8-d656c4124f9e796d6e80c6a1baa9c5c3fdf977a5.zip | |
upgrade to std::format
| -rw-r--r-- | CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/convert.cpp | 42 |
2 files changed, 16 insertions, 29 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a9fd29..2d8b301 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,9 @@ cmake_minimum_required(VERSION 3.25) project(winapi_utf8 CXX) enable_testing() +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED TRUE) + if(MSVC) add_compile_options(/MP /W4) else() diff --git a/src/convert.cpp b/src/convert.cpp index c0a8e98..0f96767 100644 --- a/src/convert.cpp +++ b/src/convert.cpp @@ -9,7 +9,7 @@ #include <cstddef> #include <cstdint> -#include <sstream> +#include <format> #include <stdexcept> #include <string> #include <vector> @@ -18,9 +18,7 @@ namespace winapi { namespace { std::runtime_error error(const char* function, DWORD code) { - std::ostringstream oss; - oss << "Function " << function << " failed with error code " << code; - return std::runtime_error{oss.str()}; + return std::runtime_error{std::format("Function {} failed with error code {}", function, code)}; } bool size_t_to_int(std::size_t src, int32_t& dest) { @@ -40,30 +38,21 @@ bool int_to_size_t(int32_t src, std::size_t& dest) { int32_t convert_input_bytes_to_bytes(std::size_t nb) { int32_t real_nb = 0; - if (!size_t_to_int(nb, real_nb)) { - std::ostringstream oss; - oss << "Input buffer is too large at " << nb << " bytes"; - throw std::runtime_error{oss.str()}; - } + if (!size_t_to_int(nb, real_nb)) + throw std::runtime_error{std::format("Input buffer is too large at {} bytes", nb)}; return real_nb; } int32_t convert_input_bytes_to_chars(std::size_t nb) { - if (nb % sizeof(WCHAR) != 0) { - std::ostringstream oss; - oss << "Buffer size invalid at " << nb << " bytes"; - throw std::runtime_error{oss.str()}; - } + if (nb % sizeof(WCHAR) != 0) + throw std::runtime_error{std::format("Buffer size invalid at {} bytes", nb)}; const std::size_t nch = nb / sizeof(WCHAR); int32_t real_nch = 0; - if (!size_t_to_int(nch, real_nch)) { - std::ostringstream oss; - oss << "Input buffer is too large at " << nch << " characters"; - throw std::runtime_error{oss.str()}; - } + if (!size_t_to_int(nch, real_nch)) + throw std::runtime_error{std::format("Input buffer is too large at {} characters", nch)}; return real_nch; } @@ -72,11 +61,8 @@ template <typename CharT> std::vector<CharT> output_buffer(int32_t size) { std::size_t real_size = 0; - if (!int_to_size_t(size, real_size)) { - std::ostringstream oss; - oss << "Buffer size invalid at " << size << " bytes"; - throw std::runtime_error{oss.str()}; - } + if (!int_to_size_t(size, real_size)) + throw std::runtime_error{std::format("Buffer size invalid at {} bytes", size)}; std::vector<CharT> buffer; buffer.resize(real_size); @@ -87,11 +73,9 @@ template <typename CharT> void verify_output(const std::vector<CharT>& expected, int32_t _actual_size) { std::size_t actual_size = 0; - if (!int_to_size_t(_actual_size, actual_size) || expected.size() != actual_size) { - std::ostringstream oss; - oss << "Expected output length " << expected.size() << ", got " << _actual_size; - throw std::runtime_error{oss.str()}; - } + if (!int_to_size_t(_actual_size, actual_size) || expected.size() != actual_size) + throw std::runtime_error{ + std::format("Expected output length {}, got {}", expected.size(), _actual_size)}; } } // namespace |
