2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/utils/port/wstring_convert.h"
|
|
|
|
|
|
|
|
char disable_linker_warning_about_empty_file_wstring_convert_cpp TD_UNUSED;
|
|
|
|
|
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
|
2018-05-18 17:55:40 +02:00
|
|
|
#include "td/utils/utf8.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-05-18 17:55:40 +02:00
|
|
|
#include <cwchar>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2018-05-18 16:22:18 +02:00
|
|
|
Result<std::wstring> to_wstring(CSlice slice) {
|
2018-05-18 17:55:40 +02:00
|
|
|
if (!check_utf8(slice)) {
|
2021-04-08 13:43:47 +02:00
|
|
|
return Status::Error("Wrong string encoding");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-05-18 17:55:40 +02:00
|
|
|
|
2019-10-07 02:41:04 +02:00
|
|
|
size_t wstring_len = utf8_utf16_length(slice);
|
2018-05-18 17:55:40 +02:00
|
|
|
|
|
|
|
std::wstring result(wstring_len, static_cast<wchar_t>(0));
|
|
|
|
if (wstring_len) {
|
|
|
|
wchar_t *res = &result[0];
|
|
|
|
for (size_t i = 0; i < slice.size();) {
|
|
|
|
unsigned int a = static_cast<unsigned char>(slice[i++]);
|
|
|
|
if (a >= 0x80) {
|
|
|
|
unsigned int b = static_cast<unsigned char>(slice[i++]);
|
|
|
|
if (a >= 0xe0) {
|
|
|
|
unsigned int c = static_cast<unsigned char>(slice[i++]);
|
|
|
|
if (a >= 0xf0) {
|
|
|
|
unsigned int d = static_cast<unsigned char>(slice[i++]);
|
|
|
|
unsigned int val = ((a & 0x07) << 18) + ((b & 0x3f) << 12) + ((c & 0x3f) << 6) + (d & 0x3f) - 0x10000;
|
|
|
|
*res++ = static_cast<wchar_t>(0xD800 + (val >> 10));
|
|
|
|
*res++ = static_cast<wchar_t>(0xDC00 + (val & 0x3ff));
|
|
|
|
} else {
|
|
|
|
*res++ = static_cast<wchar_t>(((a & 0x0f) << 12) + ((b & 0x3f) << 6) + (c & 0x3f));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*res++ = static_cast<wchar_t>(((a & 0x1f) << 6) + (b & 0x3f));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*res++ = static_cast<wchar_t>(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(res == &result[0] + wstring_len);
|
|
|
|
}
|
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<string> from_wstring(const wchar_t *begin, size_t size) {
|
2018-05-18 17:55:40 +02:00
|
|
|
size_t result_len = 0;
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
unsigned int cur = begin[i];
|
|
|
|
if ((cur & 0xF800) == 0xD800) {
|
|
|
|
if (i < size) {
|
|
|
|
unsigned int next = begin[++i];
|
|
|
|
if ((next & 0xFC00) == 0xDC00 && (cur & 0x400) == 0) {
|
|
|
|
result_len += 4;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 13:43:47 +02:00
|
|
|
return Status::Error("Wrong wstring encoding");
|
2018-05-18 17:55:40 +02:00
|
|
|
}
|
|
|
|
result_len += 1 + (cur >= 0x80) + (cur >= 0x800);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string result(result_len, '\0');
|
|
|
|
if (result_len) {
|
|
|
|
char *res = &result[0];
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
unsigned int cur = begin[i];
|
|
|
|
// TODO conversion unsigned int -> signed char is implementation defined
|
|
|
|
if (cur <= 0x7f) {
|
|
|
|
*res++ = static_cast<char>(cur);
|
|
|
|
} else if (cur <= 0x7ff) {
|
|
|
|
*res++ = static_cast<char>(0xc0 | (cur >> 6));
|
|
|
|
*res++ = static_cast<char>(0x80 | (cur & 0x3f));
|
|
|
|
} else if ((cur & 0xF800) != 0xD800) {
|
|
|
|
*res++ = static_cast<char>(0xe0 | (cur >> 12));
|
|
|
|
*res++ = static_cast<char>(0x80 | ((cur >> 6) & 0x3f));
|
|
|
|
*res++ = static_cast<char>(0x80 | (cur & 0x3f));
|
|
|
|
} else {
|
|
|
|
unsigned int next = begin[++i];
|
|
|
|
unsigned int val = ((cur - 0xD800) << 10) + next - 0xDC00 + 0x10000;
|
|
|
|
|
|
|
|
*res++ = static_cast<char>(0xf0 | (val >> 18));
|
|
|
|
*res++ = static_cast<char>(0x80 | ((val >> 12) & 0x3f));
|
|
|
|
*res++ = static_cast<char>(0x80 | ((val >> 6) & 0x3f));
|
|
|
|
*res++ = static_cast<char>(0x80 | (val & 0x3f));
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-05-18 17:55:40 +02:00
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<string> from_wstring(const std::wstring &str) {
|
|
|
|
return from_wstring(str.data(), str.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<string> from_wstring(const wchar_t *begin) {
|
2018-05-18 17:55:40 +02:00
|
|
|
return from_wstring(begin, std::wcslen(begin));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
#endif
|