Skip to content

Commit

Permalink
fix(core): fix convert bug
Browse files Browse the repository at this point in the history
  • Loading branch information
churchill-zhang authored and zoomchan-cxj committed May 12, 2022
1 parent 301f631 commit f43988f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 10 additions & 4 deletions core/include/core/base/string_view_utils.h
Expand Up @@ -141,10 +141,16 @@ class StringViewUtils {
unicode_string_view::Encoding src_encoding) {
switch (src_encoding) {
case unicode_string_view::Encoding::Latin1: {
const auto &str = str_view.latin1_value();
auto ptr =
reinterpret_cast<const unicode_string_view::char8_t_ *>(str.c_str());
return unicode_string_view(ptr, str.length());
u8string u8;
for (const auto& ch : str_view.latin1_value()){
if (ch < 0x80) {
u8 += ch;
} else {
u8 += (0xc0 | ch >> 6);
u8 += (0x80 | (ch & 0x3f));
}
}
return unicode_string_view(std::move(u8));
}
case unicode_string_view::Encoding::Utf16: {
return unicode_string_view(U16ToU8(str_view.utf16_value()));
Expand Down
12 changes: 10 additions & 2 deletions core/third_party/base/include/base/logging.h
Expand Up @@ -20,8 +20,16 @@ inline std::ostream& operator<<(std::ostream& stream, const unicode_string_view&
unicode_string_view::Encoding encoding = str_view.encoding();
switch (encoding) {
case unicode_string_view::Encoding::Latin1: {
const std::string& str = str_view.latin1_value();
stream << str;
std::string u8;
for (const auto& ch : str_view.latin1_value()){
if (ch < 0x80) {
u8 += ch;
} else {
u8 += (0xc0 | ch >> 6);
u8 += (0x80 | (ch & 0x3f));
}
}
stream << u8;
break;
}
case unicode_string_view::Encoding::Utf16: {
Expand Down

0 comments on commit f43988f

Please sign in to comment.