2010-07-30 07:10:22 +00:00
|
|
|
// Copyright 2007-2010 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-04 13:36:43 +00:00
|
|
|
#ifndef V8_UNICODE_INL_H_
|
|
|
|
#define V8_UNICODE_INL_H_
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/unicode.h"
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/logging.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/utils.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
namespace unibrow {
|
|
|
|
|
|
|
|
template <class T, int s> bool Predicate<T, s>::get(uchar code_point) {
|
|
|
|
CacheEntry entry = entries_[code_point & kMask];
|
2014-11-05 12:40:56 +00:00
|
|
|
if (entry.code_point() == code_point) return entry.value();
|
2008-07-03 15:10:15 +00:00
|
|
|
return CalculateValue(code_point);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, int s> bool Predicate<T, s>::CalculateValue(
|
|
|
|
uchar code_point) {
|
|
|
|
bool result = T::Is(code_point);
|
|
|
|
entries_[code_point & kMask] = CacheEntry(code_point, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, int s> int Mapping<T, s>::get(uchar c, uchar n,
|
|
|
|
uchar* result) {
|
|
|
|
CacheEntry entry = entries_[c & kMask];
|
|
|
|
if (entry.code_point_ == c) {
|
|
|
|
if (entry.offset_ == 0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
result[0] = c + entry.offset_;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return CalculateValue(c, n, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, int s> int Mapping<T, s>::CalculateValue(uchar c, uchar n,
|
|
|
|
uchar* result) {
|
|
|
|
bool allow_caching = true;
|
|
|
|
int length = T::Convert(c, n, result, &allow_caching);
|
|
|
|
if (allow_caching) {
|
|
|
|
if (length == 1) {
|
|
|
|
entries_[c & kMask] = CacheEntry(c, result[0] - c);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
entries_[c & kMask] = CacheEntry(c, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-13 19:43:45 +00:00
|
|
|
unsigned Utf8::EncodeOneByte(char* str, uint8_t c) {
|
|
|
|
static const int kMask = ~(1 << 6);
|
|
|
|
if (c <= kMaxOneByteChar) {
|
|
|
|
str[0] = c;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
str[0] = 0xC0 | (c >> 6);
|
|
|
|
str[1] = 0x80 | (c & kMask);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2014-01-20 09:52:54 +00:00
|
|
|
// Encode encodes the UTF-16 code units c and previous into the given str
|
|
|
|
// buffer, and combines surrogate code units into single code points. If
|
|
|
|
// replace_invalid is set to true, orphan surrogate code units will be replaced
|
|
|
|
// with kBadChar.
|
|
|
|
unsigned Utf8::Encode(char* str,
|
|
|
|
uchar c,
|
|
|
|
int previous,
|
|
|
|
bool replace_invalid) {
|
2008-07-03 15:10:15 +00:00
|
|
|
static const int kMask = ~(1 << 6);
|
|
|
|
if (c <= kMaxOneByteChar) {
|
|
|
|
str[0] = c;
|
|
|
|
return 1;
|
|
|
|
} else if (c <= kMaxTwoByteChar) {
|
|
|
|
str[0] = 0xC0 | (c >> 6);
|
|
|
|
str[1] = 0x80 | (c & kMask);
|
|
|
|
return 2;
|
|
|
|
} else if (c <= kMaxThreeByteChar) {
|
2014-01-20 09:52:54 +00:00
|
|
|
if (Utf16::IsSurrogatePair(previous, c)) {
|
2012-03-12 12:35:28 +00:00
|
|
|
const int kUnmatchedSize = kSizeOfUnmatchedSurrogate;
|
|
|
|
return Encode(str - kUnmatchedSize,
|
|
|
|
Utf16::CombineSurrogatePair(previous, c),
|
2014-01-20 09:52:54 +00:00
|
|
|
Utf16::kNoPreviousCharacter,
|
|
|
|
replace_invalid) - kUnmatchedSize;
|
|
|
|
} else if (replace_invalid &&
|
|
|
|
(Utf16::IsLeadSurrogate(c) ||
|
|
|
|
Utf16::IsTrailSurrogate(c))) {
|
|
|
|
c = kBadChar;
|
2012-03-12 12:35:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
str[0] = 0xE0 | (c >> 12);
|
|
|
|
str[1] = 0x80 | ((c >> 6) & kMask);
|
|
|
|
str[2] = 0x80 | (c & kMask);
|
|
|
|
return 3;
|
|
|
|
} else {
|
|
|
|
str[0] = 0xF0 | (c >> 18);
|
|
|
|
str[1] = 0x80 | ((c >> 12) & kMask);
|
|
|
|
str[2] = 0x80 | ((c >> 6) & kMask);
|
|
|
|
str[3] = 0x80 | (c & kMask);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uchar Utf8::ValueOf(const byte* bytes, unsigned length, unsigned* cursor) {
|
|
|
|
if (length <= 0) return kBadChar;
|
|
|
|
byte first = bytes[0];
|
|
|
|
// Characters between 0000 and 0007F are encoded as a single character
|
|
|
|
if (first <= kMaxOneByteChar) {
|
|
|
|
*cursor += 1;
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
return CalculateValue(bytes, length, cursor);
|
|
|
|
}
|
|
|
|
|
2012-03-12 12:35:28 +00:00
|
|
|
unsigned Utf8::Length(uchar c, int previous) {
|
2008-07-03 15:10:15 +00:00
|
|
|
if (c <= kMaxOneByteChar) {
|
|
|
|
return 1;
|
|
|
|
} else if (c <= kMaxTwoByteChar) {
|
|
|
|
return 2;
|
|
|
|
} else if (c <= kMaxThreeByteChar) {
|
2012-03-12 12:35:28 +00:00
|
|
|
if (Utf16::IsTrailSurrogate(c) &&
|
|
|
|
Utf16::IsLeadSurrogate(previous)) {
|
|
|
|
return kSizeOfUnmatchedSurrogate - kBytesSavedByCombiningSurrogates;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
return 3;
|
|
|
|
} else {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace unibrow
|
|
|
|
|
2009-05-04 13:36:43 +00:00
|
|
|
#endif // V8_UNICODE_INL_H_
|