5 #ifndef V8_UNICODE_INL_H_ 6 #define V8_UNICODE_INL_H_ 9 #include "src/base/logging.h" 10 #include "src/utils.h" 14 template <
class T,
int s>
bool Predicate<T, s>::get(uchar code_point) {
15 CacheEntry entry = entries_[code_point & kMask];
16 if (entry.code_point() == code_point)
return entry.value();
17 return CalculateValue(code_point);
20 template <
class T,
int s>
bool Predicate<T, s>::CalculateValue(
22 bool result = T::Is(code_point);
23 entries_[code_point & kMask] = CacheEntry(code_point, result);
27 template <
class T,
int s>
int Mapping<T, s>::get(uchar c, uchar n,
29 CacheEntry entry = entries_[c & kMask];
30 if (entry.code_point_ == c) {
31 if (entry.offset_ == 0) {
34 result[0] = c + entry.offset_;
38 return CalculateValue(c, n, result);
42 template <
class T,
int s>
int Mapping<T, s>::CalculateValue(uchar c, uchar n,
44 bool allow_caching =
true;
45 int length = T::Convert(c, n, result, &allow_caching);
48 entries_[c & kMask] = CacheEntry(c, result[0] - c);
51 entries_[c & kMask] = CacheEntry(c, 0);
60 unsigned Utf8::EncodeOneByte(
char* str, uint8_t c) {
61 static const int kMask = ~(1 << 6);
62 if (c <= kMaxOneByteChar) {
66 str[0] = 0xC0 | (c >> 6);
67 str[1] = 0x80 | (c & kMask);
75 unsigned Utf8::Encode(
char* str,
78 bool replace_invalid) {
79 static const int kMask = ~(1 << 6);
80 if (c <= kMaxOneByteChar) {
83 }
else if (c <= kMaxTwoByteChar) {
84 str[0] = 0xC0 | (c >> 6);
85 str[1] = 0x80 | (c & kMask);
87 }
else if (c <= kMaxThreeByteChar) {
88 DCHECK(!Utf16::IsLeadSurrogate(Utf16::kNoPreviousCharacter));
89 if (Utf16::IsSurrogatePair(previous, c)) {
90 const int kUnmatchedSize = kSizeOfUnmatchedSurrogate;
91 return Encode(str - kUnmatchedSize,
92 Utf16::CombineSurrogatePair(previous, c),
93 Utf16::kNoPreviousCharacter,
94 replace_invalid) - kUnmatchedSize;
95 }
else if (replace_invalid &&
96 (Utf16::IsLeadSurrogate(c) ||
97 Utf16::IsTrailSurrogate(c))) {
100 str[0] = 0xE0 | (c >> 12);
101 str[1] = 0x80 | ((c >> 6) & kMask);
102 str[2] = 0x80 | (c & kMask);
105 str[0] = 0xF0 | (c >> 18);
106 str[1] = 0x80 | ((c >> 12) & kMask);
107 str[2] = 0x80 | ((c >> 6) & kMask);
108 str[3] = 0x80 | (c & kMask);
114 uchar Utf8::ValueOf(
const byte* bytes,
size_t length,
size_t* cursor) {
115 if (length <= 0)
return kBadChar;
116 byte first = bytes[0];
118 if (V8_LIKELY(first <= kMaxOneByteChar)) {
122 return CalculateValue(bytes, length, cursor);
125 unsigned Utf8::Length(uchar c,
int previous) {
126 if (c <= kMaxOneByteChar) {
128 }
else if (c <= kMaxTwoByteChar) {
130 }
else if (c <= kMaxThreeByteChar) {
131 DCHECK(!Utf16::IsLeadSurrogate(Utf16::kNoPreviousCharacter));
132 if (Utf16::IsSurrogatePair(previous, c)) {
133 return kSizeOfUnmatchedSurrogate - kBytesSavedByCombiningSurrogates;
141 bool Utf8::IsValidCharacter(uchar c) {
142 return c < 0xD800u || (c >= 0xE000u && c < 0xFDD0u) ||
143 (c > 0xFDEFu && c <= 0x10FFFFu && (c & 0xFFFEu) != 0xFFFEu &&
149 #endif // V8_UNICODE_INL_H_