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
4 // met:
5 //
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.
15 //
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.
27
28 #ifndef V8_UNICODE_INL_H_
29 #define V8_UNICODE_INL_H_
30
31 #include "unicode.h"
32
33 namespace unibrow {
34
get(uchar code_point)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);
39 }
40
CalculateValue(uchar code_point)41 template <class T, int s> bool Predicate<T, s>::CalculateValue(
42 uchar code_point) {
43 bool result = T::Is(code_point);
44 entries_[code_point & kMask] = CacheEntry(code_point, result);
45 return result;
46 }
47
get(uchar c,uchar n,uchar * result)48 template <class T, int s> int Mapping<T, s>::get(uchar c, uchar n,
49 uchar* result) {
50 CacheEntry entry = entries_[c & kMask];
51 if (entry.code_point_ == c) {
52 if (entry.offset_ == 0) {
53 return 0;
54 } else {
55 result[0] = c + entry.offset_;
56 return 1;
57 }
58 } else {
59 return CalculateValue(c, n, result);
60 }
61 }
62
CalculateValue(uchar c,uchar n,uchar * result)63 template <class T, int s> int Mapping<T, s>::CalculateValue(uchar c, uchar n,
64 uchar* result) {
65 bool allow_caching = true;
66 int length = T::Convert(c, n, result, &allow_caching);
67 if (allow_caching) {
68 if (length == 1) {
69 entries_[c & kMask] = CacheEntry(c, result[0] - c);
70 return 1;
71 } else {
72 entries_[c & kMask] = CacheEntry(c, 0);
73 return 0;
74 }
75 } else {
76 return length;
77 }
78 }
79
80
Encode(char * str,uchar c)81 unsigned Utf8::Encode(char* str, uchar c) {
82 static const int kMask = ~(1 << 6);
83 if (c <= kMaxOneByteChar) {
84 str[0] = c;
85 return 1;
86 } else if (c <= kMaxTwoByteChar) {
87 str[0] = 0xC0 | (c >> 6);
88 str[1] = 0x80 | (c & kMask);
89 return 2;
90 } else if (c <= kMaxThreeByteChar) {
91 str[0] = 0xE0 | (c >> 12);
92 str[1] = 0x80 | ((c >> 6) & kMask);
93 str[2] = 0x80 | (c & kMask);
94 return 3;
95 } else {
96 str[0] = 0xF0 | (c >> 18);
97 str[1] = 0x80 | ((c >> 12) & kMask);
98 str[2] = 0x80 | ((c >> 6) & kMask);
99 str[3] = 0x80 | (c & kMask);
100 return 4;
101 }
102 }
103
104
ValueOf(const byte * bytes,unsigned length,unsigned * cursor)105 uchar Utf8::ValueOf(const byte* bytes, unsigned length, unsigned* cursor) {
106 if (length <= 0) return kBadChar;
107 byte first = bytes[0];
108 // Characters between 0000 and 0007F are encoded as a single character
109 if (first <= kMaxOneByteChar) {
110 *cursor += 1;
111 return first;
112 }
113 return CalculateValue(bytes, length, cursor);
114 }
115
Length(uchar c)116 unsigned Utf8::Length(uchar c) {
117 if (c <= kMaxOneByteChar) {
118 return 1;
119 } else if (c <= kMaxTwoByteChar) {
120 return 2;
121 } else if (c <= kMaxThreeByteChar) {
122 return 3;
123 } else {
124 return 4;
125 }
126 }
127
GetNext()128 uchar CharacterStream::GetNext() {
129 uchar result = DecodeCharacter(buffer_, &cursor_);
130 if (remaining_ == 1) {
131 cursor_ = 0;
132 FillBuffer();
133 } else {
134 remaining_--;
135 }
136 return result;
137 }
138
139 #if __BYTE_ORDER == __LITTLE_ENDIAN
140 #define IF_LITTLE(expr) expr
141 #define IF_BIG(expr) ((void) 0)
142 #elif __BYTE_ORDER == __BIG_ENDIAN
143 #define IF_LITTLE(expr) ((void) 0)
144 #define IF_BIG(expr) expr
145 #else
146 #warning Unknown byte ordering
147 #endif
148
EncodeAsciiCharacter(uchar c,byte * buffer,unsigned capacity,unsigned & offset)149 bool CharacterStream::EncodeAsciiCharacter(uchar c, byte* buffer,
150 unsigned capacity, unsigned& offset) {
151 if (offset >= capacity) return false;
152 buffer[offset] = c;
153 offset += 1;
154 return true;
155 }
156
EncodeNonAsciiCharacter(uchar c,byte * buffer,unsigned capacity,unsigned & offset)157 bool CharacterStream::EncodeNonAsciiCharacter(uchar c, byte* buffer,
158 unsigned capacity, unsigned& offset) {
159 unsigned aligned = (offset + 0x3) & ~0x3;
160 if ((aligned + sizeof(uchar)) > capacity)
161 return false;
162 if (offset == aligned) {
163 IF_LITTLE(*reinterpret_cast<uchar*>(buffer + aligned) = (c << 8) | 0x80);
164 IF_BIG(*reinterpret_cast<uchar*>(buffer + aligned) = c | (1 << 31));
165 } else {
166 buffer[offset] = 0x80;
167 IF_LITTLE(*reinterpret_cast<uchar*>(buffer + aligned) = c << 8);
168 IF_BIG(*reinterpret_cast<uchar*>(buffer + aligned) = c);
169 }
170 offset = aligned + sizeof(uchar);
171 return true;
172 }
173
EncodeCharacter(uchar c,byte * buffer,unsigned capacity,unsigned & offset)174 bool CharacterStream::EncodeCharacter(uchar c, byte* buffer, unsigned capacity,
175 unsigned& offset) {
176 if (c <= Utf8::kMaxOneByteChar) {
177 return EncodeAsciiCharacter(c, buffer, capacity, offset);
178 } else {
179 return EncodeNonAsciiCharacter(c, buffer, capacity, offset);
180 }
181 }
182
DecodeCharacter(const byte * buffer,unsigned * offset)183 uchar CharacterStream::DecodeCharacter(const byte* buffer, unsigned* offset) {
184 byte b = buffer[*offset];
185 if (b <= Utf8::kMaxOneByteChar) {
186 (*offset)++;
187 return b;
188 } else {
189 unsigned aligned = (*offset + 0x3) & ~0x3;
190 *offset = aligned + sizeof(uchar);
191 IF_LITTLE(return *reinterpret_cast<const uchar*>(buffer + aligned) >> 8);
192 IF_BIG(return *reinterpret_cast<const uchar*>(buffer + aligned) &
193 ~(1 << 31));
194 }
195 }
196
197 #undef IF_LITTLE
198 #undef IF_BIG
199
200 template <class R, class I, unsigned s>
FillBuffer()201 void InputBuffer<R, I, s>::FillBuffer() {
202 buffer_ = R::ReadBlock(input_, util_buffer_, s, &remaining_, &offset_);
203 }
204
205 template <class R, class I, unsigned s>
Rewind()206 void InputBuffer<R, I, s>::Rewind() {
207 Reset(input_);
208 }
209
210 template <class R, class I, unsigned s>
Reset(unsigned position,I input)211 void InputBuffer<R, I, s>::Reset(unsigned position, I input) {
212 input_ = input;
213 remaining_ = 0;
214 cursor_ = 0;
215 offset_ = position;
216 buffer_ = R::ReadBlock(input_, util_buffer_, s, &remaining_, &offset_);
217 }
218
219 template <class R, class I, unsigned s>
Reset(I input)220 void InputBuffer<R, I, s>::Reset(I input) {
221 Reset(0, input);
222 }
223
224 template <class R, class I, unsigned s>
Seek(unsigned position)225 void InputBuffer<R, I, s>::Seek(unsigned position) {
226 offset_ = position;
227 buffer_ = R::ReadBlock(input_, util_buffer_, s, &remaining_, &offset_);
228 }
229
230 template <unsigned s>
Utf8InputBuffer(const char * data,unsigned length)231 Utf8InputBuffer<s>::Utf8InputBuffer(const char* data, unsigned length)
232 : InputBuffer<Utf8, Buffer<const char*>, s>(Buffer<const char*>(data,
233 length)) {
234 }
235
236 } // namespace unibrow
237
238 #endif // V8_UNICODE_INL_H_
239