#pragma once
#include <stdexcept>
#include <string>
#include <string_view>
#include <windows.h>
namespace day_webview {
inline std::string utf8(std::wstring_view text) {
if (text.empty())
return {};
const int count = static_cast<int>(text.size());
const int length =
WideCharToMultiByte(CP_UTF8, 0, text.data(), count, nullptr, 0, nullptr, nullptr);
if (!length)
throw std::runtime_error("WebView2 UTF-8 conversion failed");
std::string result(static_cast<size_t>(length), '\0');
if (WideCharToMultiByte(CP_UTF8, 0, text.data(), count, result.data(), length, nullptr,
nullptr) != length)
throw std::runtime_error("WebView2 UTF-8 conversion failed");
return result;
}
}