1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "winapi-utf8" project.
// For details, see https://github.com/egor-tensin/winapi-utf8.
// Distributed under the MIT License.
#include <winapi/utf8.hpp>
#include <windows.h>
#include <cstddef>
#include <cstdint>
#include <format>
#include <limits>
#include <stdexcept>
#include <string>
#include <vector>
namespace winapi {
namespace {
std::runtime_error error(const char* function, DWORD code) {
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) {
if (src > static_cast<uint32_t>(std::numeric_limits<int32_t>::max()))
return false;
dest = static_cast<int32_t>(src);
return true;
}
bool int_to_size_t(int32_t src, std::size_t& dest) {
if (src < 0 || static_cast<uint32_t>(src) > std::numeric_limits<std::size_t>::max())
return false;
dest = static_cast<std::size_t>(src);
return true;
}
int32_t convert_input_bytes_to_bytes(std::size_t nb) {
int32_t real_nb = 0;
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)
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))
throw std::runtime_error{std::format("Input buffer is too large at {} characters", nch)};
return real_nch;
}
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))
throw std::runtime_error{std::format("Buffer size invalid at {} bytes", size)};
std::vector<CharT> buffer;
buffer.resize(real_size);
return buffer;
}
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)
throw std::runtime_error{
std::format("Expected output length {}, got {}", expected.size(), _actual_size)};
}
} // namespace
std::wstring widen(std::string_view src) {
return widen(src.data(), src.length());
}
std::wstring widen(std::u8string_view src) {
return widen(src.data(), src.length());
}
std::wstring widen(const void* src, std::size_t in_nb) {
const DWORD flags = MB_ERR_INVALID_CHARS;
const auto in_data = reinterpret_cast<const char*>(src);
const auto real_in_nb = convert_input_bytes_to_bytes(in_nb);
auto out_nch = ::MultiByteToWideChar(CP_UTF8, flags, in_data, real_in_nb, NULL, 0);
if (out_nch == 0) {
throw error("MultiByteToWideChar", GetLastError());
}
static_assert(sizeof(wchar_t) == sizeof(WCHAR), "wchar_t != WCHAR");
auto out = output_buffer<wchar_t>(out_nch);
out_nch = ::MultiByteToWideChar(CP_UTF8, flags, in_data, real_in_nb, out.data(), out_nch);
if (out_nch == 0) {
throw error("MultiByteToWideChar", GetLastError());
}
verify_output(out, out_nch);
return {out.data(), out.size()};
}
std::string narrow(std::wstring_view src) {
static_assert(sizeof(wchar_t) == sizeof(WCHAR), "wchar_t != WCHAR");
return narrow(src.data(), src.length() * sizeof(std::wstring_view::value_type));
}
std::string narrow(std::u16string_view src) {
return narrow(src.data(), src.length() * sizeof(std::u16string_view::value_type));
}
std::string narrow(const void* src, std::size_t in_nb) {
const DWORD flags = WC_ERR_INVALID_CHARS;
const auto in_data = reinterpret_cast<const wchar_t*>(src);
const auto in_nch = convert_input_bytes_to_chars(in_nb);
auto out_nb = ::WideCharToMultiByte(CP_UTF8, flags, in_data, in_nch, NULL, 0, NULL, NULL);
if (out_nb == 0) {
throw error("WideCharToMultiByte", GetLastError());
}
auto out = output_buffer<char>(out_nb);
out_nb = ::WideCharToMultiByte(CP_UTF8, flags, in_data, in_nch, out.data(), out_nb, NULL, NULL);
if (out_nb == 0) {
throw error("WideCharToMultiByte", GetLastError());
}
verify_output(out, out_nb);
return {out.data(), out.size()};
}
} // namespace winapi
|