1
0
Fork 0
forked from suyu/suyu

Merge pull request #9970 from bunnei/string-util-view

common: string_util: Use std::string_view for UTF16ToUTF8/UTF8ToUTF16W.
This commit is contained in:
bunnei 2023-03-19 11:10:16 -07:00 committed by GitHub
commit 3d4c113037
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 11 deletions

View file

@ -125,18 +125,18 @@ std::string ReplaceAll(std::string result, const std::string& src, const std::st
return result; return result;
} }
std::string UTF16ToUTF8(const std::u16string& input) { std::string UTF16ToUTF8(std::u16string_view input) {
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
return convert.to_bytes(input); return convert.to_bytes(input.data(), input.data() + input.size());
} }
std::u16string UTF8ToUTF16(const std::string& input) { std::u16string UTF8ToUTF16(std::string_view input) {
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
return convert.from_bytes(input); return convert.from_bytes(input.data(), input.data() + input.size());
} }
#ifdef _WIN32 #ifdef _WIN32
static std::wstring CPToUTF16(u32 code_page, const std::string& input) { static std::wstring CPToUTF16(u32 code_page, std::string_view input) {
const auto 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);
@ -154,7 +154,7 @@ static std::wstring CPToUTF16(u32 code_page, const std::string& input) {
return output; return output;
} }
std::string UTF16ToUTF8(const std::wstring& input) { std::string UTF16ToUTF8(std::wstring_view input) {
const auto 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) { if (size == 0) {
@ -172,7 +172,7 @@ std::string UTF16ToUTF8(const std::wstring& input) {
return output; return output;
} }
std::wstring UTF8ToUTF16W(const std::string& input) { std::wstring UTF8ToUTF16W(std::string_view input) {
return CPToUTF16(CP_UTF8, input); return CPToUTF16(CP_UTF8, input);
} }

View file

@ -36,12 +36,12 @@ bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _
[[nodiscard]] std::string ReplaceAll(std::string result, const std::string& src, [[nodiscard]] std::string ReplaceAll(std::string result, const std::string& src,
const std::string& dest); const std::string& dest);
[[nodiscard]] std::string UTF16ToUTF8(const std::u16string& input); [[nodiscard]] std::string UTF16ToUTF8(std::u16string_view input);
[[nodiscard]] std::u16string UTF8ToUTF16(const std::string& input); [[nodiscard]] std::u16string UTF8ToUTF16(std::string_view input);
#ifdef _WIN32 #ifdef _WIN32
[[nodiscard]] std::string UTF16ToUTF8(const std::wstring& input); [[nodiscard]] std::string UTF16ToUTF8(std::wstring_view input);
[[nodiscard]] std::wstring UTF8ToUTF16W(const std::string& str); [[nodiscard]] std::wstring UTF8ToUTF16W(std::string_view str);
#endif #endif