[inspector] Add byte swapping on BE machines

With https://crrev.com/c/2277142 adding unified
(de)serialization support, "cbor ParseUTF16String" is no longer
being used and byte orders remain in LE format.

This CL essentially reverts some of the changes made here:
https://crrev.com/c/2038716 and re-adds byte swapping
on BE machines.

Change-Id: I3e7be6ba182e7faada3bf31dff9a89c1343abbbc
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2281082
Reviewed-by: Yang Guo <yangguo@chromium.org>
Commit-Queue: Milad Farazmand <miladfar@ca.ibm.com>
Cr-Commit-Position: refs/heads/master@{#68690}
This commit is contained in:
Milad Farazmand 2020-07-03 18:46:32 +00:00 committed by Commit Bot
parent b8fbf8ebb2
commit 4deef4d795
3 changed files with 20 additions and 3 deletions

View File

@ -208,8 +208,21 @@ String16 String16::fromUTF8(const char* stringStart, size_t length) {
return String16(UTF8ToUTF16(stringStart, length));
}
String16 String16::fromUTF16(const UChar* stringStart, size_t length) {
String16 String16::fromUTF16LE(const UChar* stringStart, size_t length) {
#ifdef V8_TARGET_BIG_ENDIAN
// Need to flip the byte order on big endian machines.
String16Builder builder;
builder.reserveCapacity(length);
for (size_t i = 0; i < length; i++) {
const UChar utf16be_char =
stringStart[i] << 8 | (stringStart[i] >> 8 & 0x00FF);
builder.append(utf16be_char);
}
return builder.toString();
#else
// No need to do anything on little endian machines.
return String16(stringStart, length);
#endif // V8_TARGET_BIG_ENDIAN
}
std::string String16::utf8() const {

View File

@ -70,7 +70,11 @@ class String16 {
// Convenience methods.
V8_EXPORT std::string utf8() const;
V8_EXPORT static String16 fromUTF8(const char* stringStart, size_t length);
V8_EXPORT static String16 fromUTF16(const UChar* stringStart, size_t length);
// Instantiates a String16 in native endianness from UTF16 LE.
// On Big endian architectures, byte order needs to be flipped.
V8_EXPORT static String16 fromUTF16LE(const UChar* stringStart,
size_t length);
std::size_t hash() const {
if (!hash_code) {

View File

@ -30,7 +30,7 @@ class StringUtil {
}
static String fromUTF16LE(const uint16_t* data, size_t length) {
return String16::fromUTF16(data, length);
return String16::fromUTF16LE(data, length);
}
static const uint8_t* CharactersLatin1(const String& s) { return nullptr; }