Skip to content

Commit 182d9c0

Browse files
caseqV8 LUCI CQ
authored andcommitted
Define UChar as char16_t
We used to have UChar defined as uint16_t which does not go along with STL these days if you try to have an std::basic_string<> of it, as there are no standard std::char_traits<> specialization for uint16_t. This switches UChar to char16_t where practical, introducing a few compatibility shims to keep CL size small, as (1) this would likely have to be back-ported and (2) crdtp extensively uses uint16_t for wide chars. Bug: b:296390693 Change-Id: I66a32d8f0050915225b187de56896c26dd76163d Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4789966 Reviewed-by: Jaroslav Sevcik <[email protected]> Commit-Queue: Jaroslav Sevcik <[email protected]> Auto-Submit: Andrey Kosyakov <[email protected]> Cr-Commit-Position: refs/heads/main@{#89559}
1 parent 25933a9 commit 182d9c0

5 files changed

Lines changed: 28 additions & 11 deletions

File tree

src/inspector/string-16.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ bool isSpaceOrNewLine(UChar c) {
2727
return isASCII(c) && c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9));
2828
}
2929

30-
int64_t charactersToInteger(const UChar* characters, size_t length,
30+
int64_t charactersToInteger(const uint16_t* characters, size_t length,
3131
bool* ok = nullptr) {
3232
std::vector<char> buffer;
3333
buffer.reserve(length + 1);
@@ -50,6 +50,8 @@ int64_t charactersToInteger(const UChar* characters, size_t length,
5050

5151
String16::String16(const UChar* characters, size_t size)
5252
: m_impl(characters, size) {}
53+
String16::String16(const uint16_t* characters, size_t size)
54+
: m_impl(reinterpret_cast<const UChar*>(characters), size) {}
5355

5456
String16::String16(const UChar* characters) : m_impl(characters) {}
5557

@@ -241,6 +243,10 @@ String16 String16::fromUTF16LE(const UChar* stringStart, size_t length) {
241243
#endif // V8_TARGET_BIG_ENDIAN
242244
}
243245

246+
String16 String16::fromUTF16LE(const uint16_t* stringStart, size_t length) {
247+
return fromUTF16LE(reinterpret_cast<const UChar*>(stringStart), length);
248+
}
249+
244250
std::string String16::utf8() const {
245251
return UTF16ToUTF8(m_impl.data(), m_impl.size());
246252
}

src/inspector/string-16.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define V8_INSPECTOR_STRING_16_H_
77

88
#include <stdint.h>
9+
#include <uchar.h>
910

1011
#include <cctype>
1112
#include <climits>
@@ -18,7 +19,7 @@
1819

1920
namespace v8_inspector {
2021

21-
using UChar = uint16_t;
22+
using UChar = char16_t;
2223

2324
class String16 {
2425
public:
@@ -28,6 +29,7 @@ class String16 {
2829
String16(const String16&) V8_NOEXCEPT = default;
2930
String16(String16&&) V8_NOEXCEPT = default;
3031
String16(const UChar* characters, size_t size);
32+
String16(const uint16_t* characters, size_t size);
3133
V8_EXPORT String16(const UChar* characters);
3234
V8_EXPORT String16(const char* characters);
3335
String16(const char* characters, size_t size);
@@ -49,7 +51,9 @@ class String16 {
4951
int toInteger(bool* ok = nullptr) const;
5052
std::pair<size_t, size_t> getTrimmedOffsetAndLength() const;
5153
String16 stripWhiteSpace() const;
52-
const UChar* characters16() const { return m_impl.c_str(); }
54+
const uint16_t* characters16() const {
55+
return reinterpret_cast<const uint16_t*>(m_impl.c_str());
56+
}
5357
size_t length() const { return m_impl.length(); }
5458
bool isEmpty() const { return !m_impl.length(); }
5559
UChar operator[](size_t index) const { return m_impl[index]; }
@@ -79,6 +83,8 @@ class String16 {
7983
// On Big endian architectures, byte order needs to be flipped.
8084
V8_EXPORT static String16 fromUTF16LE(const UChar* stringStart,
8185
size_t length);
86+
V8_EXPORT static String16 fromUTF16LE(const uint16_t* stringStart,
87+
size_t length);
8288

8389
std::size_t hash() const {
8490
if (!hash_code) {

src/inspector/v8-string-conversions.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace v8_inspector {
1414
namespace {
15-
using UChar = uint16_t;
15+
using UChar = char16_t;
1616
using UChar32 = uint32_t;
1717

1818
bool isASCII(UChar c) { return !(c & ~0x7F); }
@@ -386,7 +386,7 @@ std::string UTF16ToUTF8(const UChar* stringStart, size_t length) {
386386

387387
std::basic_string<UChar> UTF8ToUTF16(const char* stringStart, size_t length) {
388388
if (!stringStart || !length) return std::basic_string<UChar>();
389-
std::vector<uint16_t> buffer(length);
389+
std::vector<UChar> buffer(length);
390390
UChar* bufferStart = buffer.data();
391391

392392
UChar* bufferCurrent = bufferStart;
@@ -395,7 +395,7 @@ std::basic_string<UChar> UTF8ToUTF16(const char* stringStart, size_t length) {
395395
reinterpret_cast<const char*>(stringStart + length),
396396
&bufferCurrent, bufferCurrent + buffer.size(), nullptr,
397397
true) != conversionOK)
398-
return std::basic_string<uint16_t>();
398+
return std::basic_string<UChar>();
399399
size_t utf16Length = bufferCurrent - bufferStart;
400400
return std::basic_string<UChar>(bufferStart, bufferStart + utf16Length);
401401
}

src/inspector/v8-string-conversions.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
#ifndef V8_INSPECTOR_V8_STRING_CONVERSIONS_H_
66
#define V8_INSPECTOR_V8_STRING_CONVERSIONS_H_
77

8+
#include <uchar.h>
9+
810
#include <cstdint>
911
#include <string>
1012

1113
// Conversion routines between UT8 and UTF16, used by string-16.{h,cc}. You may
1214
// want to use string-16.h directly rather than these.
1315
namespace v8_inspector {
14-
std::basic_string<uint16_t> UTF8ToUTF16(const char* stringStart, size_t length);
15-
std::string UTF16ToUTF8(const uint16_t* stringStart, size_t length);
16+
std::basic_string<char16_t> UTF8ToUTF16(const char* stringStart, size_t length);
17+
std::string UTF16ToUTF8(const char16_t* stringStart, size_t length);
1618
} // namespace v8_inspector
1719

1820
#endif // V8_INSPECTOR_V8_STRING_CONVERSIONS_H_

third_party/inspector_protocol/crdtp/test_platform_v8.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
namespace v8_crdtp {
1212

1313
std::string UTF16ToUTF8(span<uint16_t> in) {
14-
return v8_inspector::UTF16ToUTF8(in.data(), in.size());
14+
return v8_inspector::UTF16ToUTF8(reinterpret_cast<const char16_t*>(in.data()),
15+
in.size());
1516
}
1617

1718
std::vector<uint16_t> UTF8ToUTF16(span<uint8_t> in) {
18-
std::basic_string<uint16_t> utf16 = v8_inspector::UTF8ToUTF16(
19+
std::basic_string<char16_t> utf16 = v8_inspector::UTF8ToUTF16(
1920
reinterpret_cast<const char*>(in.data()), in.size());
20-
return std::vector<uint16_t>(utf16.begin(), utf16.end());
21+
return std::vector<uint16_t>(
22+
reinterpret_cast<const uint16_t*>(utf16.data()),
23+
reinterpret_cast<const uint16_t*>(utf16.data()) + utf16.size());
2124
}
2225

2326
} // namespace v8_crdtp

0 commit comments

Comments
 (0)