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.
9 #include "src/globals.h"
12 * Definitions and convenience functions for working with unicode.
17 typedef unsigned int uchar;
18 typedef unsigned char byte;
21 * The max length of the result of converting the case of a single
24 const int kMaxMappingSize = 4;
26 template <class T, int size = 256>
29 inline Predicate() { }
30 inline bool get(uchar c);
33 bool CalculateValue(uchar c);
35 inline CacheEntry() : code_point_(0), value_(0) { }
36 inline CacheEntry(uchar code_point, bool value)
37 : code_point_(code_point),
39 uchar code_point_ : 21;
42 static const int kSize = size;
43 static const int kMask = kSize - 1;
44 CacheEntry entries_[kSize];
48 // A cache used in case conversion. It caches the value for characters
49 // that either have no mapping or map to a single character independent
50 // of context. Characters that map to more than one character or that
51 // map differently depending on context are always looked up.
52 template <class T, int size = 256>
56 inline int get(uchar c, uchar n, uchar* result);
59 int CalculateValue(uchar c, uchar n, uchar* result);
61 inline CacheEntry() : code_point_(kNoChar), offset_(0) { }
62 inline CacheEntry(uchar code_point, signed offset)
63 : code_point_(code_point),
67 static const int kNoChar = (1 << 21) - 1;
69 static const int kSize = size;
70 static const int kMask = kSize - 1;
71 CacheEntry entries_[kSize];
78 static int GetByteCount();
79 static const uchar kMaxCodePoint;
85 static inline bool IsSurrogatePair(int lead, int trail) {
86 return IsLeadSurrogate(lead) && IsTrailSurrogate(trail);
88 static inline bool IsLeadSurrogate(int code) {
89 if (code == kNoPreviousCharacter) return false;
90 return (code & 0xfc00) == 0xd800;
92 static inline bool IsTrailSurrogate(int code) {
93 if (code == kNoPreviousCharacter) return false;
94 return (code & 0xfc00) == 0xdc00;
97 static inline int CombineSurrogatePair(uchar lead, uchar trail) {
98 return 0x10000 + ((lead & 0x3ff) << 10) + (trail & 0x3ff);
100 static const int kNoPreviousCharacter = -1;
101 static const uchar kMaxNonSurrogateCharCode = 0xffff;
102 // Encoding a single UTF-16 code unit will produce 1, 2 or 3 bytes
103 // of UTF-8 data. The special case where the unit is a surrogate
104 // trail produces 1 byte net, because the encoding of the pair is
105 // 4 bytes and the 3 bytes that were used to encode the lead surrogate
107 static const int kMaxExtraUtf8BytesForOneUtf16CodeUnit = 3;
108 // One UTF-16 surrogate is endoded (illegally) as 3 UTF-8 bytes.
109 // The illegality stems from the surrogate not being part of a pair.
110 static const int kUtf8BytesToCodeASurrogate = 3;
111 static inline uint16_t LeadSurrogate(uint32_t char_code) {
112 return 0xd800 + (((char_code - 0x10000) >> 10) & 0x3ff);
114 static inline uint16_t TrailSurrogate(uint32_t char_code) {
115 return 0xdc00 + (char_code & 0x3ff);
122 static inline uchar Length(uchar chr, int previous);
123 static inline unsigned EncodeOneByte(char* out, uint8_t c);
124 static inline unsigned Encode(char* out,
127 bool replace_invalid = false);
128 static uchar CalculateValue(const byte* str,
132 // The unicode replacement character, used to signal invalid unicode
133 // sequences (e.g. an orphan surrogate) when converting to a UTF-8 encoding.
134 static const uchar kBadChar = 0xFFFD;
135 static const unsigned kMaxEncodedSize = 4;
136 static const unsigned kMaxOneByteChar = 0x7f;
137 static const unsigned kMaxTwoByteChar = 0x7ff;
138 static const unsigned kMaxThreeByteChar = 0xffff;
139 static const unsigned kMaxFourByteChar = 0x1fffff;
141 // A single surrogate is coded as a 3 byte UTF-8 sequence, but two together
142 // that match are coded as a 4 byte UTF-8 sequence.
143 static const unsigned kBytesSavedByCombiningSurrogates = 2;
144 static const unsigned kSizeOfUnmatchedSurrogate = 3;
145 // The maximum size a single UTF-16 code unit may take up when encoded as
147 static const unsigned kMax16BitCodeUnitSize = 3;
148 static inline uchar ValueOf(const byte* str,
154 static bool Is(uchar c);
157 static bool Is(uchar c);
160 static bool Is(uchar c);
163 static bool Is(uchar c);
166 static bool Is(uchar c);
169 static bool Is(uchar c);
171 struct LineTerminator {
172 static bool Is(uchar c);
175 static const int kMaxWidth = 3;
176 static const bool kIsToLower = true;
177 static int Convert(uchar c,
180 bool* allow_caching_ptr);
183 static const int kMaxWidth = 3;
184 static const bool kIsToLower = false;
185 static int Convert(uchar c,
188 bool* allow_caching_ptr);
190 struct Ecma262Canonicalize {
191 static const int kMaxWidth = 1;
192 static int Convert(uchar c,
195 bool* allow_caching_ptr);
197 struct Ecma262UnCanonicalize {
198 static const int kMaxWidth = 4;
199 static int Convert(uchar c,
202 bool* allow_caching_ptr);
204 struct CanonicalizationRange {
205 static const int kMaxWidth = 1;
206 static int Convert(uchar c,
209 bool* allow_caching_ptr);
212 } // namespace unibrow
214 #endif // V8_UNICODE_H_