v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
unicode.h
Go to the documentation of this file.
1// Copyright 2011 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_STRINGS_UNICODE_H_
6#define V8_STRINGS_UNICODE_H_
7
8#include <sys/types.h>
9
10#include "src/base/bit-field.h"
11#include "src/base/vector.h"
12#include "src/common/globals.h"
13#include "third_party/utf8-decoder/utf8-decoder.h"
19namespace unibrow {
20
21using uchar = unsigned int;
22
27const int kMaxMappingSize = 4;
28
29#ifndef V8_INTL_SUPPORT
30template <class T, int size = 256>
31class Predicate {
32 public:
33 inline Predicate() = default;
34 inline bool get(uchar c);
35
36 private:
37 friend class Test;
38 bool CalculateValue(uchar c);
39 class CacheEntry {
40 public:
41 inline CacheEntry()
42 : bit_field_(CodePointField::encode(0) | ValueField::encode(0)) {}
43 inline CacheEntry(uchar code_point, bool value)
44 : bit_field_(
46 ValueField::encode(value)) {
48 code_point == static_cast<uchar>(-1));
49 }
50
52 bool value() const { return ValueField::decode(bit_field_); }
53
54 private:
57
58 uint32_t bit_field_;
59 };
60 static const int kSize = size;
61 static const int kMask = kSize - 1;
63};
64
65// A cache used in case conversion. It caches the value for characters
66// that either have no mapping or map to a single character independent
67// of context. Characters that map to more than one character or that
68// map differently depending on context are always looked up.
69template <class T, int size = 256>
70class Mapping {
71 public:
72 inline Mapping() = default;
73 inline int get(uchar c, uchar n, uchar* result);
74
75 private:
76 friend class Test;
78 struct CacheEntry {
80 inline CacheEntry(uchar code_point, signed offset)
81 : code_point_(code_point), offset_(offset) {}
83 signed offset_;
84 static const int kNoChar = (1 << 21) - 1;
85 };
86 static const int kSize = size;
87 static const int kMask = kSize - 1;
89};
90
92 private:
93 friend class Test;
94 static int GetByteCount();
95 static const uchar kMaxCodePoint;
96};
97
98#endif // !V8_INTL_SUPPORT
99
100class Utf16 {
101 public:
102 static const int kNoPreviousCharacter = -1;
103 static inline bool IsSurrogatePair(int lead, int trail) {
104 return IsLeadSurrogate(lead) && IsTrailSurrogate(trail);
105 }
106 static inline bool IsLeadSurrogate(int code) {
107 return (code & 0x1ffc00) == 0xd800;
108 }
109 static inline bool IsTrailSurrogate(int code) {
110 return (code & 0x1ffc00) == 0xdc00;
111 }
112
113 static inline int CombineSurrogatePair(uchar lead, uchar trail) {
114 return 0x10000 + ((lead & 0x3ff) << 10) + (trail & 0x3ff);
115 }
116 static const uchar kMaxNonSurrogateCharCode = 0xffff;
117 // Encoding a single UTF-16 code unit will produce 1, 2 or 3 bytes
118 // of UTF-8 data. The special case where the unit is a surrogate
119 // trail produces 1 byte net, because the encoding of the pair is
120 // 4 bytes and the 3 bytes that were used to encode the lead surrogate
121 // can be reclaimed.
123 // One UTF-16 surrogate is encoded (illegally) as 3 UTF-8 bytes.
124 // The illegality stems from the surrogate not being part of a pair.
125 static const int kUtf8BytesToCodeASurrogate = 3;
126 static inline uint16_t LeadSurrogate(uint32_t char_code) {
127 return 0xd800 + (((char_code - 0x10000) >> 10) & 0x3ff);
128 }
129 static inline uint16_t TrailSurrogate(uint32_t char_code) {
130 return 0xdc00 + (char_code & 0x3ff);
131 }
132 static inline bool HasUnpairedSurrogate(const uint16_t* code_units,
133 size_t length);
134
135 static void ReplaceUnpairedSurrogates(const uint16_t* source_code_units,
136 uint16_t* dest_code_units,
137 size_t length);
138};
139
140class Latin1 {
141 public:
142 static const uint16_t kMaxChar = 0xff;
143};
144
145enum class Utf8Variant : uint8_t {
146#if V8_ENABLE_WEBASSEMBLY
147 kUtf8, // UTF-8. Decoding an invalid byte sequence or encoding a
148 // surrogate codepoint signals an error.
149 kUtf8NoTrap, // UTF-8. Decoding an invalid byte sequence or encoding a
150 // surrogate codepoint returns null.
151 kWtf8, // WTF-8: like UTF-8, but allows isolated (but not paired)
152 // surrogate codepoints to be encoded and decoded.
153#endif
154 kLossyUtf8, // Lossy UTF-8: Any byte sequence can be decoded without
155 // error, replacing invalid UTF-8 with the replacement
156 // character (U+FFFD). Any sequence of codepoints can be
157 // encoded without error, replacing surrogates with U+FFFD.
159};
160
162 public:
163 using State = Utf8DfaDecoder::State;
164
165 static inline unsigned LengthOneByte(uint8_t chr);
166 static inline unsigned Length(uchar chr, int previous);
167 static inline unsigned EncodeOneByte(char* out, uint8_t c);
168 static inline unsigned Encode(char* out, uchar c, int previous,
169 bool replace_invalid = false);
170 static uchar CalculateValue(const uint8_t* str, size_t length,
171 size_t* cursor);
172
173 // The unicode replacement character, used to signal invalid unicode
174 // sequences (e.g. an orphan surrogate) when converting to a UTF-8 encoding.
175 static const uchar kBadChar = 0xFFFD;
176 static const uchar kBufferEmpty = 0x0;
177 static const uchar kIncomplete = 0xFFFFFFFC; // any non-valid code point.
178 static const unsigned kMaxEncodedSize = 4;
179 static const unsigned kMaxOneByteChar = 0x7f;
180 static const unsigned kMaxTwoByteChar = 0x7ff;
181 static const unsigned kMaxThreeByteChar = 0xffff;
182 static const unsigned kMaxFourByteChar = 0x1fffff;
183
184 // A single surrogate is coded as a 3 byte UTF-8 sequence, but two together
185 // that match are coded as a 4 byte UTF-8 sequence.
186 static const unsigned kBytesSavedByCombiningSurrogates = 2;
187 static const unsigned kSizeOfUnmatchedSurrogate = 3;
188 // The maximum size a single UTF-16 code unit may take up when encoded as
189 // UTF-8.
190 static const unsigned kMax16BitCodeUnitSize = 3;
191 // The maximum size a single UTF-16 code unit known to be in the range
192 // [0,0xff] may take up when encoded as UTF-8.
193 static const unsigned kMax8BitCodeUnitSize = 2;
194 static inline uchar ValueOf(const uint8_t* str, size_t length,
195 size_t* cursor);
196
197 using Utf8IncrementalBuffer = uint32_t;
198 static inline uchar ValueOfIncremental(const uint8_t** cursor, State* state,
199 Utf8IncrementalBuffer* buffer);
200 static uchar ValueOfIncrementalFinish(State* state);
201
202 // Excludes non-characters from the set of valid code points.
203 static inline bool IsValidCharacter(uchar c);
204
205 // Validate if the input has a valid utf-8 encoding. Unlike JS source code
206 // this validation function will accept any unicode code point, including
207 // kBadChar and BOMs.
208 //
209 // This method checks for:
210 // - valid utf-8 endcoding (e.g. no over-long encodings),
211 // - absence of surrogates,
212 // - valid code point range.
213 static bool ValidateEncoding(const uint8_t* str, size_t length);
214
215 // Encode the given characters as Utf8 into the provided output buffer.
220 template <typename Char>
221 static EncodingResult Encode(v8::base::Vector<const Char> string,
222 char* buffer, size_t capacity, bool write_null,
223 bool replace_invalid_utf8);
224};
225
226#if V8_ENABLE_WEBASSEMBLY
227class V8_EXPORT_PRIVATE Wtf8 {
228 public:
229 // Validate that the input has a valid WTF-8 encoding.
230 //
231 // This method checks for:
232 // - valid utf-8 endcoding (e.g. no over-long encodings),
233 // - absence of surrogate pairs,
234 // - valid code point range.
235 //
236 // In terms of the WTF-8 specification (https://simonsapin.github.io/wtf-8/),
237 // this function checks for a valid "generalized UTF-8" sequence, with the
238 // additional constraint that surrogate pairs are not allowed.
239 static bool ValidateEncoding(const uint8_t* str, size_t length);
240
241 static void ScanForSurrogates(v8::base::Vector<const uint8_t> wtf8,
242 std::vector<size_t>* surrogate_offsets);
243};
244#endif // V8_ENABLE_WEBASSEMBLY
245
246struct Uppercase {
247 static bool Is(uchar c);
248};
249struct Letter {
250 static bool Is(uchar c);
251};
252#ifndef V8_INTL_SUPPORT
254 static bool Is(uchar c);
255};
257 static bool Is(uchar c);
258};
260 static bool Is(uchar c);
261};
262#endif // !V8_INTL_SUPPORT
263
264// LineTerminator: 'JS_Line_Terminator' in point.properties
265// ES#sec-line-terminators lists exactly 4 code points:
266// LF (U+000A), CR (U+000D), LS(U+2028), PS(U+2029)
268 return c == 0x000A || c == 0x000D || c == 0x2028 || c == 0x2029;
269}
270
272 return c == 0x000A || c == 0x000D;
273}
274
275#ifdef V8_INTL_SUPPORT
276struct V8_EXPORT_PRIVATE ToLowercase {
277 static const bool kIsToLower = true;
278};
279struct V8_EXPORT_PRIVATE ToUppercase {
280 static const bool kIsToLower = false;
281};
282#else
284 static const int kMaxWidth = 3;
285 static const bool kIsToLower = true;
286 static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr);
287};
289 static const int kMaxWidth = 3;
290 static const bool kIsToLower = false;
291 static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr);
292};
294 static const int kMaxWidth = 1;
295 static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr);
296};
298 static const int kMaxWidth = 4;
299 static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr);
300};
302 static const int kMaxWidth = 1;
303 static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr);
304};
305#endif // !V8_INTL_SUPPORT
306
307} // namespace unibrow
308
309#endif // V8_STRINGS_UNICODE_H_
static const uint16_t kMaxChar
Definition unicode.h:142
friend class Test
Definition unicode.h:76
static const int kMask
Definition unicode.h:87
int CalculateValue(uchar c, uchar n, uchar *result)
Definition unicode-inl.h:47
CacheEntry entries_[kSize]
Definition unicode.h:88
static const int kSize
Definition unicode.h:86
Mapping()=default
int get(uchar c, uchar n, uchar *result)
Definition unicode-inl.h:32
CacheEntry(uchar code_point, bool value)
Definition unicode.h:43
friend class Test
Definition unicode.h:37
bool get(uchar c)
Definition unicode-inl.h:18
bool CalculateValue(uchar c)
Definition unicode-inl.h:25
CacheEntry entries_[kSize]
Definition unicode.h:62
static const int kMask
Definition unicode.h:61
static const int kSize
Definition unicode.h:60
static const uchar kMaxCodePoint
Definition unicode.h:95
friend class Test
Definition unicode.h:93
static int GetByteCount()
Definition unicode.cc:2634
static uint16_t LeadSurrogate(uint32_t char_code)
Definition unicode.h:126
static const int kNoPreviousCharacter
Definition unicode.h:102
static const uchar kMaxNonSurrogateCharCode
Definition unicode.h:116
static const int kMaxExtraUtf8BytesForOneUtf16CodeUnit
Definition unicode.h:122
static uint16_t TrailSurrogate(uint32_t char_code)
Definition unicode.h:129
static bool IsSurrogatePair(int lead, int trail)
Definition unicode.h:103
static void ReplaceUnpairedSurrogates(const uint16_t *source_code_units, uint16_t *dest_code_units, size_t length)
Definition unicode.cc:244
static int CombineSurrogatePair(uchar lead, uchar trail)
Definition unicode.h:113
static bool IsTrailSurrogate(int code)
Definition unicode.h:109
static bool HasUnpairedSurrogate(const uint16_t *code_units, size_t length)
Definition unicode-inl.h:64
static const int kUtf8BytesToCodeASurrogate
Definition unicode.h:125
static bool IsLeadSurrogate(int code)
Definition unicode.h:106
uint32_t Utf8IncrementalBuffer
Definition unicode.h:197
Utf8DfaDecoder::State State
Definition unicode.h:163
static constexpr T decode(U value)
Definition bit-field.h:66
static constexpr U kMask
Definition bit-field.h:41
LineAndColumn previous
int32_t offset
ZoneVector< RpoNumber > & result
Utf8Variant
Definition unicode.h:145
const int kMaxMappingSize
Definition unicode.h:27
V8_INLINE bool IsStringLiteralLineTerminator(uchar c)
Definition unicode.h:271
V8_INLINE bool IsLineTerminator(uchar c)
Definition unicode.h:267
unsigned int uchar
Definition unicode.h:21
unsigned short uint16_t
Definition unicode.cc:39
#define DCHECK_IMPLIES(v1, v2)
Definition logging.h:493
#define V8_EXPORT_PRIVATE
Definition macros.h:460
static bool Is(uchar c)
Definition unicode.cc:583
static const int kNoChar
Definition unicode.h:84
CacheEntry(uchar code_point, signed offset)
Definition unicode.h:80
static bool Is(uchar c)
Definition unicode.cc:433
#define V8_INLINE
Definition v8config.h:500