diff options
Diffstat (limited to 'src/util.cc')
-rw-r--r-- | src/util.cc | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/src/util.cc b/src/util.cc index 5dbdd72..4bf3550 100644 --- a/src/util.cc +++ b/src/util.cc @@ -283,6 +283,44 @@ std::string StrError(int errnum) { os << str << " Error #" << errnum; return os.str(); } - } // namespace util + +#ifdef OS_WIN +namespace win32 { +std::wstring Utf8ToWide(const std::string &input) { + int output_length = + ::MultiByteToWideChar(CP_UTF8, 0, input.c_str(), -1, nullptr, 0); + output_length = output_length <= 0 ? 0 : output_length - 1; + if (output_length == 0) { + return L""; + } + std::unique_ptr<wchar_t[]> input_wide(new wchar_t[output_length + 1]); + const int result = ::MultiByteToWideChar(CP_UTF8, 0, input.c_str(), -1, + input_wide.get(), output_length + 1); + std::wstring output; + if (result > 0) { + output.assign(input_wide.get()); + } + return output; +} + +std::string WideToUtf8(const std::wstring &input) { + const int output_length = ::WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1, + nullptr, 0, nullptr, nullptr); + if (output_length == 0) { + return ""; + } + + std::unique_ptr<char[]> input_encoded(new char[output_length + 1]); + const int result = + ::WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1, input_encoded.get(), + output_length + 1, nullptr, nullptr); + std::string output; + if (result > 0) { + output.assign(input_encoded.get()); + } + return output; +} +} // namespace win32 +#endif } // namespace sentencepiece |