Commit 9faa6943 authored by Junliang Yan's avatar Junliang Yan Committed by Commit Bot

[inspector] Add fromUTF16LE function to string-16

Bug: v8:9355
Change-Id: I317bb52952ad5b347d627d4f6096f251bca1a815
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1652558
Commit-Queue: Junliang Yan <jyan@ca.ibm.com>
Reviewed-by: 's avatarAlexei Filippov <alph@chromium.org>
Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62342}
parent 36eedb63
...@@ -200,6 +200,23 @@ String16 String16::fromUTF8(const char* stringStart, size_t length) { ...@@ -200,6 +200,23 @@ String16 String16::fromUTF8(const char* stringStart, size_t length) {
return String16(UTF8ToUTF16(stringStart, length)); return String16(UTF8ToUTF16(stringStart, 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 =
utf16le_str[i] << 8 | (utf16le_str[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 { std::string String16::utf8() const {
return UTF16ToUTF8(m_impl.data(), m_impl.size()); return UTF16ToUTF8(m_impl.data(), m_impl.size());
} }
......
...@@ -66,8 +66,13 @@ class String16 { ...@@ -66,8 +66,13 @@ class String16 {
} }
// Convenience methods. // Convenience methods.
std::string utf8() const; V8_EXPORT std::string utf8() const;
static String16 fromUTF8(const char* stringStart, size_t length); V8_EXPORT static String16 fromUTF8(const char* 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 { std::size_t hash() const {
if (!hash_code) { if (!hash_code) {
......
...@@ -74,6 +74,10 @@ class StringUtil { ...@@ -74,6 +74,10 @@ class StringUtil {
return String16(data, length); return String16(data, length);
} }
static String fromUTF16LE(const uint16_t* data, size_t length) {
return String16::fromUTF16LE(data, length);
}
static const uint8_t* CharactersLatin1(const String& s) { return nullptr; } static const uint8_t* CharactersLatin1(const String& s) { return nullptr; }
static const uint8_t* CharactersUTF8(const String& s) { return nullptr; } static const uint8_t* CharactersUTF8(const String& s) { return nullptr; }
static const uint16_t* CharactersUTF16(const String& s) { static const uint16_t* CharactersUTF16(const String& s) {
......
...@@ -9,7 +9,9 @@ ...@@ -9,7 +9,9 @@
#include "include/v8-inspector.h" #include "include/v8-inspector.h"
#include "include/v8.h" #include "include/v8.h"
#include "src/inspector/protocol/Runtime.h" #include "src/inspector/protocol/Runtime.h"
#include "src/inspector/string-16.h"
using v8_inspector::String16;
using v8_inspector::StringBuffer; using v8_inspector::StringBuffer;
using v8_inspector::StringView; using v8_inspector::StringView;
using v8_inspector::V8ContextInfo; using v8_inspector::V8ContextInfo;
...@@ -63,3 +65,16 @@ TEST(WrapInsideWrapOnInterrupt) { ...@@ -63,3 +65,16 @@ TEST(WrapInsideWrapOnInterrupt) {
isolate->RequestInterrupt(&WrapOnInterrupt, session.get()); isolate->RequestInterrupt(&WrapOnInterrupt, session.get());
session->wrapObject(env.local(), v8::Null(isolate), object_group_view, false); session->wrapObject(env.local(), v8::Null(isolate), object_group_view, false);
} }
TEST(String16EndianTest) {
const char* utf8 =
"Hello, \xf0\x9f\x8c\x8e."; // "Hello, earth." in utf8 encoding
const uint16_t* utf16le = reinterpret_cast<const uint16_t*>(
"H\0e\0l\0l\0o\0,\0 \0\x3c\xd8\x0e\xdf.\0"); // Same text in UTF16LE
// encoding
String16 utf16_str = String16::fromUTF16LE(utf16le, 10);
String16 utf8_str = String16::fromUTF8(utf8, strlen(utf8));
CHECK_EQ(utf16_str.utf8(), utf8_str.utf8());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment