10#if V8_ENABLE_WEBASSEMBLY
11#include "third_party/utf8-decoder/generalized-utf8-decoder.h"
18template <
class Decoder>
22struct DecoderTraits<Utf8Decoder> {
23 static bool IsInvalidSurrogatePair(uint32_t lead, uint32_t trail) {
30 static const bool kAllowIncompleteSequences =
true;
31 using DfaDecoder = Utf8DfaDecoder;
34#if V8_ENABLE_WEBASSEMBLY
36struct DecoderTraits<Wtf8Decoder> {
37 static bool IsInvalidSurrogatePair(uint32_t lead, uint32_t trail) {
40 static const bool kAllowIncompleteSequences =
false;
41 using DfaDecoder = GeneralizedUtf8DfaDecoder;
45struct DecoderTraits<StrictUtf8Decoder> {
46 static bool IsInvalidSurrogatePair(uint32_t lead, uint32_t trail) {
53 static const bool kAllowIncompleteSequences =
false;
54 using DfaDecoder = Utf8DfaDecoder;
59template <
class Decoder>
63 utf16_length_(non_ascii_start_) {
64 using Traits = DecoderTraits<Decoder>;
68 auto state = Traits::DfaDecoder::kAccept;
72 const uint8_t*
end = data.begin() + data.length();
74 while (cursor <
end) {
76 state == Traits::DfaDecoder::kAccept)) {
85 auto previous_state =
state;
86 Traits::DfaDecoder::Decode(*cursor, &state, ¤t);
87 if (state < Traits::DfaDecoder::kAccept) {
88 DCHECK_EQ(state, Traits::DfaDecoder::kReject);
89 if (Traits::kAllowIncompleteSequences) {
90 state = Traits::DfaDecoder::kAccept;
98 if (previous_state != Traits::DfaDecoder::kAccept)
continue;
103 }
else if (state == Traits::DfaDecoder::kAccept) {
104 if (Traits::IsInvalidSurrogatePair(
previous, current)) {
117 if (state == Traits::DfaDecoder::kAccept) {
119 }
else if (Traits::kAllowIncompleteSequences) {
128template <
class Decoder>
129template <
typename Char>
132 using Traits = DecoderTraits<Decoder>;
134 CopyChars(out, data.begin(), non_ascii_start_);
136 out += non_ascii_start_;
138 auto state = Traits::DfaDecoder::kAccept;
139 uint32_t current = 0;
140 const uint8_t* cursor = data.begin() + non_ascii_start_;
141 const uint8_t*
end = data.begin() + data.length();
143 while (cursor <
end) {
145 state == Traits::DfaDecoder::kAccept)) {
147 *(out++) =
static_cast<Char
>(*cursor);
152 auto previous_state =
state;
153 Traits::DfaDecoder::Decode(*cursor, &state, ¤t);
154 if (Traits::kAllowIncompleteSequences &&
155 state < Traits::DfaDecoder::kAccept) {
156 state = Traits::DfaDecoder::kAccept;
161 if (previous_state != Traits::DfaDecoder::kAccept)
continue;
162 }
else if (state == Traits::DfaDecoder::kAccept) {
163 if (
sizeof(Char) == 1 ||
165 *(out++) =
static_cast<Char
>(current);
175 if (Traits::kAllowIncompleteSequences &&
176 state != Traits::DfaDecoder::kAccept) {
179 DCHECK_EQ(state, Traits::DfaDecoder::kAccept);
183#define DEFINE_UNICODE_DECODER(Decoder) \
184 template V8_EXPORT_PRIVATE Utf8DecoderBase<Decoder>::Utf8DecoderBase( \
185 base::Vector<const uint8_t> data); \
186 template V8_EXPORT_PRIVATE void Utf8DecoderBase<Decoder>::Decode( \
187 uint8_t* out, base::Vector<const uint8_t> data); \
188 template V8_EXPORT_PRIVATE void Utf8DecoderBase<Decoder>::Decode( \
189 uint16_t* out, base::Vector<const uint8_t> data)
193#if V8_ENABLE_WEBASSEMBLY
198#undef DEFINE_UNICODE_DECODER
static const uint16_t kMaxChar
static uint16_t LeadSurrogate(uint32_t char_code)
static const uchar kMaxNonSurrogateCharCode
static uint16_t TrailSurrogate(uint32_t char_code)
static bool IsSurrogatePair(int lead, int trail)
static bool IsTrailSurrogate(int code)
static bool IsLeadSurrogate(int code)
static const unsigned kMaxOneByteChar
static const uchar kBadChar
void Decode(Char *out, base::Vector< const uint8_t > data)
Utf8DecoderBase(base::Vector< const uint8_t > data)
LiftoffAssembler::CacheState state
void CopyChars(DstType *dst, const SrcType *src, size_t count) V8_NONNULL(1
uint32_t NonAsciiStart(const uint8_t *chars, uint32_t length)
#define DCHECK(condition)
#define DCHECK_EQ(v1, v2)
#define DEFINE_UNICODE_DECODER(Decoder)
#define V8_LIKELY(condition)