1
// Copyright 2007-2010 the V8 project authors. All rights reserved.
2
// Redistribution and use in source and binary forms, with or without
3
// modification, are permitted provided that the following conditions are
6
// * Redistributions of source code must retain the above copyright
7
// notice, this list of conditions and the following disclaimer.
8
// * Redistributions in binary form must reproduce the above
9
// copyright notice, this list of conditions and the following
10
// disclaimer in the documentation and/or other materials provided
11
// with the distribution.
12
// * Neither the name of Google Inc. nor the names of its
13
// contributors may be used to endorse or promote products derived
14
// from this software without specific prior written permission.
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#ifndef V8_UNICODE_INL_H_
29
#define V8_UNICODE_INL_H_
35
template <class T, int s> bool Predicate<T, s>::get(uchar code_point) {
36
CacheEntry entry = entries_[code_point & kMask];
37
if (entry.code_point_ == code_point) return entry.value_;
38
return CalculateValue(code_point);
41
template <class T, int s> bool Predicate<T, s>::CalculateValue(
43
bool result = T::Is(code_point);
44
entries_[code_point & kMask] = CacheEntry(code_point, result);
48
template <class T, int s> int Mapping<T, s>::get(uchar c, uchar n,
50
CacheEntry entry = entries_[c & kMask];
51
if (entry.code_point_ == c) {
52
if (entry.offset_ == 0) {
55
result[0] = c + entry.offset_;
59
return CalculateValue(c, n, result);
63
template <class T, int s> int Mapping<T, s>::CalculateValue(uchar c, uchar n,
65
bool allow_caching = true;
66
int length = T::Convert(c, n, result, &allow_caching);
69
entries_[c & kMask] = CacheEntry(c, result[0] - c);
72
entries_[c & kMask] = CacheEntry(c, 0);
81
unsigned Utf8::Encode(char* str, uchar c, int previous) {
82
static const int kMask = ~(1 << 6);
83
if (c <= kMaxOneByteChar) {
86
} else if (c <= kMaxTwoByteChar) {
87
str[0] = 0xC0 | (c >> 6);
88
str[1] = 0x80 | (c & kMask);
90
} else if (c <= kMaxThreeByteChar) {
91
if (Utf16::IsTrailSurrogate(c) &&
92
Utf16::IsLeadSurrogate(previous)) {
93
const int kUnmatchedSize = kSizeOfUnmatchedSurrogate;
94
return Encode(str - kUnmatchedSize,
95
Utf16::CombineSurrogatePair(previous, c),
96
Utf16::kNoPreviousCharacter) - kUnmatchedSize;
98
str[0] = 0xE0 | (c >> 12);
99
str[1] = 0x80 | ((c >> 6) & kMask);
100
str[2] = 0x80 | (c & kMask);
103
str[0] = 0xF0 | (c >> 18);
104
str[1] = 0x80 | ((c >> 12) & kMask);
105
str[2] = 0x80 | ((c >> 6) & kMask);
106
str[3] = 0x80 | (c & kMask);
112
uchar Utf8::ValueOf(const byte* bytes, unsigned length, unsigned* cursor) {
113
if (length <= 0) return kBadChar;
114
byte first = bytes[0];
115
// Characters between 0000 and 0007F are encoded as a single character
116
if (first <= kMaxOneByteChar) {
120
return CalculateValue(bytes, length, cursor);
123
unsigned Utf8::Length(uchar c, int previous) {
124
if (c <= kMaxOneByteChar) {
126
} else if (c <= kMaxTwoByteChar) {
128
} else if (c <= kMaxThreeByteChar) {
129
if (Utf16::IsTrailSurrogate(c) &&
130
Utf16::IsLeadSurrogate(previous)) {
131
return kSizeOfUnmatchedSurrogate - kBytesSavedByCombiningSurrogates;
139
uchar CharacterStream::GetNext() {
140
uchar result = DecodeCharacter(buffer_, &cursor_);
141
if (remaining_ == 1) {
150
#if __BYTE_ORDER == __LITTLE_ENDIAN
151
#define IF_LITTLE(expr) expr
152
#define IF_BIG(expr) ((void) 0)
153
#elif __BYTE_ORDER == __BIG_ENDIAN
154
#define IF_LITTLE(expr) ((void) 0)
155
#define IF_BIG(expr) expr
157
#warning Unknown byte ordering
160
bool CharacterStream::EncodeAsciiCharacter(uchar c, byte* buffer,
161
unsigned capacity, unsigned& offset) {
162
if (offset >= capacity) return false;
168
bool CharacterStream::EncodeNonAsciiCharacter(uchar c, byte* buffer,
169
unsigned capacity, unsigned& offset) {
170
unsigned aligned = (offset + 0x3) & ~0x3;
171
if ((aligned + sizeof(uchar)) > capacity)
173
if (offset == aligned) {
174
IF_LITTLE(*reinterpret_cast<uchar*>(buffer + aligned) = (c << 8) | 0x80);
175
IF_BIG(*reinterpret_cast<uchar*>(buffer + aligned) = c | (1 << 31));
177
buffer[offset] = 0x80;
178
IF_LITTLE(*reinterpret_cast<uchar*>(buffer + aligned) = c << 8);
179
IF_BIG(*reinterpret_cast<uchar*>(buffer + aligned) = c);
181
offset = aligned + sizeof(uchar);
185
bool CharacterStream::EncodeCharacter(uchar c, byte* buffer, unsigned capacity,
187
if (c <= Utf8::kMaxOneByteChar) {
188
return EncodeAsciiCharacter(c, buffer, capacity, offset);
190
return EncodeNonAsciiCharacter(c, buffer, capacity, offset);
194
uchar CharacterStream::DecodeCharacter(const byte* buffer, unsigned* offset) {
195
byte b = buffer[*offset];
196
if (b <= Utf8::kMaxOneByteChar) {
200
unsigned aligned = (*offset + 0x3) & ~0x3;
201
*offset = aligned + sizeof(uchar);
202
IF_LITTLE(return *reinterpret_cast<const uchar*>(buffer + aligned) >> 8);
203
IF_BIG(return *reinterpret_cast<const uchar*>(buffer + aligned) &
211
template <class R, class I, unsigned s>
212
void InputBuffer<R, I, s>::FillBuffer() {
213
buffer_ = R::ReadBlock(input_, util_buffer_, s, &remaining_, &offset_);
216
template <class R, class I, unsigned s>
217
void InputBuffer<R, I, s>::Rewind() {
221
template <class R, class I, unsigned s>
222
void InputBuffer<R, I, s>::Reset(unsigned position, I input) {
227
buffer_ = R::ReadBlock(input_, util_buffer_, s, &remaining_, &offset_);
230
template <class R, class I, unsigned s>
231
void InputBuffer<R, I, s>::Reset(I input) {
235
template <class R, class I, unsigned s>
236
void InputBuffer<R, I, s>::Seek(unsigned position) {
238
buffer_ = R::ReadBlock(input_, util_buffer_, s, &remaining_, &offset_);
241
template <unsigned s>
242
Utf8InputBuffer<s>::Utf8InputBuffer(const char* data, unsigned length)
243
: InputBuffer<Utf8, Buffer<const char*>, s>(Buffer<const char*>(data,
247
} // namespace unibrow
249
#endif // V8_UNICODE_INL_H_