| 228 | } |
| 229 | |
| 230 | std::string UTF8ToConsoleCP(std::string utf8str) |
| 231 | { |
| 232 | #if defined(_WIN32) |
| 233 | if (utf8str.empty()) |
| 234 | return {}; |
| 235 | |
| 236 | int nCodePage = GetConsoleOutputCP(); |
| 237 | int wideSize = |
| 238 | MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), (int)utf8str.length(), nullptr, 0); |
| 239 | if (!wideSize) |
| 240 | return {}; // error |
| 241 | |
| 242 | std::wstring wstr(wideSize + 1, '\0'); |
| 243 | if (!MultiByteToWideChar(CP_UTF8, |
| 244 | 0, |
| 245 | utf8str.c_str(), |
| 246 | (int)utf8str.length(), |
| 247 | wstr.data(), |
| 248 | (int)wstr.size())) |
| 249 | return {}; // error |
| 250 | |
| 251 | int utf8Size = WideCharToMultiByte(nCodePage, |
| 252 | 0, |
| 253 | wstr.c_str(), |
| 254 | (int)wstr.length(), |
| 255 | nullptr, |
| 256 | 0, |
| 257 | nullptr, |
| 258 | nullptr); |
| 259 | if (!utf8Size) |
| 260 | return {}; // error |
| 261 | |
| 262 | std::string str(utf8Size, '\0'); |
| 263 | if (!WideCharToMultiByte(nCodePage, |
| 264 | 0, |
| 265 | wstr.c_str(), |
| 266 | (int)wstr.length(), |
| 267 | str.data(), |
| 268 | (int)str.size(), |
| 269 | nullptr, |
| 270 | nullptr)) |
| 271 | return {}; // error |
| 272 | |
| 273 | return str; |
| 274 | #else |
| 275 | return utf8str; |
| 276 | #endif |
| 277 | } |
| 278 | |
| 279 | // ------------------------------------------------- |
| 280 |
no test coverage detected