mirror of
https://git.suyu.dev/suyu/suyu.git
synced 2024-11-27 01:02:48 +01:00
string_util: Get rid of separate resize() in CPToUTF16(), UTF16ToUTF8(), CodeToUTF8() and UTF8ToUTF16()
There's no need to perform the resize separately here, since the constructor allows presizing the buffer. Also move the empty string check before the construction of the string to make the early out more straightforward.
This commit is contained in:
parent
26a157cd31
commit
9d33122197
1 changed files with 22 additions and 20 deletions
|
@ -220,31 +220,37 @@ std::u16string UTF8ToUTF16(const std::string& input) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::wstring CPToUTF16(u32 code_page, const std::string& input) {
|
static std::wstring CPToUTF16(u32 code_page, const std::string& input) {
|
||||||
auto const size =
|
const auto size =
|
||||||
MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
|
MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
|
||||||
|
|
||||||
std::wstring output;
|
if (size == 0) {
|
||||||
output.resize(size);
|
return L"";
|
||||||
|
}
|
||||||
|
|
||||||
if (size == 0 ||
|
std::wstring output(size, L'\0');
|
||||||
size != MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()),
|
|
||||||
&output[0], static_cast<int>(output.size())))
|
if (size != MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()),
|
||||||
|
&output[0], static_cast<int>(output.size()))) {
|
||||||
output.clear();
|
output.clear();
|
||||||
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string UTF16ToUTF8(const std::wstring& input) {
|
std::string UTF16ToUTF8(const std::wstring& input) {
|
||||||
auto const size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
|
const auto size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
|
||||||
nullptr, 0, nullptr, nullptr);
|
nullptr, 0, nullptr, nullptr);
|
||||||
|
if (size == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
std::string output;
|
std::string output(size, '\0');
|
||||||
output.resize(size);
|
|
||||||
|
|
||||||
if (size == 0 ||
|
if (size != WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
|
||||||
size != WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
|
&output[0], static_cast<int>(output.size()), nullptr,
|
||||||
&output[0], static_cast<int>(output.size()), nullptr, nullptr))
|
nullptr)) {
|
||||||
output.clear();
|
output.clear();
|
||||||
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
@ -265,8 +271,6 @@ std::string CP1252ToUTF8(const std::string& input) {
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>& input) {
|
static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>& input) {
|
||||||
std::string result;
|
|
||||||
|
|
||||||
iconv_t const conv_desc = iconv_open("UTF-8", fromcode);
|
iconv_t const conv_desc = iconv_open("UTF-8", fromcode);
|
||||||
if ((iconv_t)(-1) == conv_desc) {
|
if ((iconv_t)(-1) == conv_desc) {
|
||||||
LOG_ERROR(Common, "Iconv initialization failure [{}]: {}", fromcode, strerror(errno));
|
LOG_ERROR(Common, "Iconv initialization failure [{}]: {}", fromcode, strerror(errno));
|
||||||
|
@ -278,8 +282,7 @@ static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>&
|
||||||
// Multiply by 4, which is the max number of bytes to encode a codepoint
|
// Multiply by 4, which is the max number of bytes to encode a codepoint
|
||||||
const size_t out_buffer_size = 4 * in_bytes;
|
const size_t out_buffer_size = 4 * in_bytes;
|
||||||
|
|
||||||
std::string out_buffer;
|
std::string out_buffer(out_buffer_size, '\0');
|
||||||
out_buffer.resize(out_buffer_size);
|
|
||||||
|
|
||||||
auto src_buffer = &input[0];
|
auto src_buffer = &input[0];
|
||||||
size_t src_bytes = in_bytes;
|
size_t src_bytes = in_bytes;
|
||||||
|
@ -304,6 +307,7 @@ static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>&
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string result;
|
||||||
out_buffer.resize(out_buffer_size - dst_bytes);
|
out_buffer.resize(out_buffer_size - dst_bytes);
|
||||||
out_buffer.swap(result);
|
out_buffer.swap(result);
|
||||||
|
|
||||||
|
@ -313,8 +317,6 @@ static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>&
|
||||||
}
|
}
|
||||||
|
|
||||||
std::u16string UTF8ToUTF16(const std::string& input) {
|
std::u16string UTF8ToUTF16(const std::string& input) {
|
||||||
std::u16string result;
|
|
||||||
|
|
||||||
iconv_t const conv_desc = iconv_open("UTF-16LE", "UTF-8");
|
iconv_t const conv_desc = iconv_open("UTF-16LE", "UTF-8");
|
||||||
if ((iconv_t)(-1) == conv_desc) {
|
if ((iconv_t)(-1) == conv_desc) {
|
||||||
LOG_ERROR(Common, "Iconv initialization failure [UTF-8]: {}", strerror(errno));
|
LOG_ERROR(Common, "Iconv initialization failure [UTF-8]: {}", strerror(errno));
|
||||||
|
@ -326,8 +328,7 @@ std::u16string UTF8ToUTF16(const std::string& input) {
|
||||||
// Multiply by 4, which is the max number of bytes to encode a codepoint
|
// Multiply by 4, which is the max number of bytes to encode a codepoint
|
||||||
const size_t out_buffer_size = 4 * sizeof(char16_t) * in_bytes;
|
const size_t out_buffer_size = 4 * sizeof(char16_t) * in_bytes;
|
||||||
|
|
||||||
std::u16string out_buffer;
|
std::u16string out_buffer(out_buffer_size, char16_t{});
|
||||||
out_buffer.resize(out_buffer_size);
|
|
||||||
|
|
||||||
char* src_buffer = const_cast<char*>(&input[0]);
|
char* src_buffer = const_cast<char*>(&input[0]);
|
||||||
size_t src_bytes = in_bytes;
|
size_t src_bytes = in_bytes;
|
||||||
|
@ -352,6 +353,7 @@ std::u16string UTF8ToUTF16(const std::string& input) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::u16string result;
|
||||||
out_buffer.resize(out_buffer_size - dst_bytes);
|
out_buffer.resize(out_buffer_size - dst_bytes);
|
||||||
out_buffer.swap(result);
|
out_buffer.swap(result);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue