diff options
author | 2025-02-06 15:41:54 -0800 | |
---|---|---|
committer | 2025-02-12 19:31:54 -0800 | |
commit | adac7b0340b826b8cbdc4532e9d861b32c94e9a7 (patch) | |
tree | 327791b2c94765ce92bf4d7574889a4fbf0b070c /libs/androidfw/Util.cpp | |
parent | 548c1ab0e8bdb1ee0375deecd64c01ab8633b801 (diff) |
Revert^2 "[res] Optimize few functions for dtoh() as a noop"
6e4275255688ab4e7a0bc5602f4ba1ee2958cff4
Change-Id: I4f9a92a21d7597eaca5ef69fc67350a25c9859bc
Diffstat (limited to 'libs/androidfw/Util.cpp')
-rw-r--r-- | libs/androidfw/Util.cpp | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/libs/androidfw/Util.cpp b/libs/androidfw/Util.cpp index be55fe8b4bb6..86c459fb4647 100644 --- a/libs/androidfw/Util.cpp +++ b/libs/androidfw/Util.cpp @@ -32,13 +32,18 @@ namespace android { namespace util { void ReadUtf16StringFromDevice(const uint16_t* src, size_t len, std::string* out) { - char buf[5]; - while (*src && len != 0) { - char16_t c = static_cast<char16_t>(dtohs(*src)); - utf16_to_utf8(&c, 1, buf, sizeof(buf)); - out->append(buf, strlen(buf)); - ++src; - --len; + static constexpr bool kDeviceEndiannessSame = dtohs(0x1001) == 0x1001; + if constexpr (kDeviceEndiannessSame) { + *out = Utf16ToUtf8({(const char16_t*)src, strnlen16((const char16_t*)src, len)}); + } else { + char buf[5]; + while (*src && len != 0) { + char16_t c = static_cast<char16_t>(dtohs(*src)); + utf16_to_utf8(&c, 1, buf, sizeof(buf)); + out->append(buf, strlen(buf)); + ++src; + --len; + } } } @@ -63,8 +68,10 @@ std::string Utf16ToUtf8(StringPiece16 utf16) { } std::string utf8; - utf8.resize(utf8_length); - utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1); + utf8.resize_and_overwrite(utf8_length, [&utf16](char* data, size_t size) { + utf16_to_utf8(utf16.data(), utf16.length(), data, size + 1); + return size; + }); return utf8; } |