v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
unicode-inl.h
Go to the documentation of this file.
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 #include "checks.h"
33 
34 namespace unibrow {
35 
36 template <class T, int s> bool Predicate<T, s>::get(uchar code_point) {
37  CacheEntry entry = entries_[code_point & kMask];
38  if (entry.code_point_ == code_point) return entry.value_;
39  return CalculateValue(code_point);
40 }
41 
42 template <class T, int s> bool Predicate<T, s>::CalculateValue(
43  uchar code_point) {
44  bool result = T::Is(code_point);
45  entries_[code_point & kMask] = CacheEntry(code_point, result);
46  return result;
47 }
48 
49 template <class T, int s> int Mapping<T, s>::get(uchar c, uchar n,
50  uchar* result) {
51  CacheEntry entry = entries_[c & kMask];
52  if (entry.code_point_ == c) {
53  if (entry.offset_ == 0) {
54  return 0;
55  } else {
56  result[0] = c + entry.offset_;
57  return 1;
58  }
59  } else {
60  return CalculateValue(c, n, result);
61  }
62 }
63 
64 template <class T, int s> int Mapping<T, s>::CalculateValue(uchar c, uchar n,
65  uchar* result) {
66  bool allow_caching = true;
67  int length = T::Convert(c, n, result, &allow_caching);
68  if (allow_caching) {
69  if (length == 1) {
70  entries_[c & kMask] = CacheEntry(c, result[0] - c);
71  return 1;
72  } else {
73  entries_[c & kMask] = CacheEntry(c, 0);
74  return 0;
75  }
76  } else {
77  return length;
78  }
79 }
80 
81 
82 unsigned Utf8::Encode(char* str, uchar c, int previous) {
83  static const int kMask = ~(1 << 6);
84  if (c <= kMaxOneByteChar) {
85  str[0] = c;
86  return 1;
87  } else if (c <= kMaxTwoByteChar) {
88  str[0] = 0xC0 | (c >> 6);
89  str[1] = 0x80 | (c & kMask);
90  return 2;
91  } else if (c <= kMaxThreeByteChar) {
92  if (Utf16::IsTrailSurrogate(c) &&
93  Utf16::IsLeadSurrogate(previous)) {
94  const int kUnmatchedSize = kSizeOfUnmatchedSurrogate;
95  return Encode(str - kUnmatchedSize,
96  Utf16::CombineSurrogatePair(previous, c),
97  Utf16::kNoPreviousCharacter) - kUnmatchedSize;
98  }
99  str[0] = 0xE0 | (c >> 12);
100  str[1] = 0x80 | ((c >> 6) & kMask);
101  str[2] = 0x80 | (c & kMask);
102  return 3;
103  } else {
104  str[0] = 0xF0 | (c >> 18);
105  str[1] = 0x80 | ((c >> 12) & kMask);
106  str[2] = 0x80 | ((c >> 6) & kMask);
107  str[3] = 0x80 | (c & kMask);
108  return 4;
109  }
110 }
111 
112 
113 uchar Utf8::ValueOf(const byte* bytes, unsigned length, unsigned* cursor) {
114  if (length <= 0) return kBadChar;
115  byte first = bytes[0];
116  // Characters between 0000 and 0007F are encoded as a single character
117  if (first <= kMaxOneByteChar) {
118  *cursor += 1;
119  return first;
120  }
121  return CalculateValue(bytes, length, cursor);
122 }
123 
124 unsigned Utf8::Length(uchar c, int previous) {
125  if (c <= kMaxOneByteChar) {
126  return 1;
127  } else if (c <= kMaxTwoByteChar) {
128  return 2;
129  } else if (c <= kMaxThreeByteChar) {
130  if (Utf16::IsTrailSurrogate(c) &&
131  Utf16::IsLeadSurrogate(previous)) {
133  }
134  return 3;
135  } else {
136  return 4;
137  }
138 }
139 
141  uchar result = DecodeCharacter(buffer_, &cursor_);
142  if (remaining_ == 1) {
143  cursor_ = 0;
144  FillBuffer();
145  } else {
146  remaining_--;
147  }
149  return result;
150 }
151 
152 #if __BYTE_ORDER == __LITTLE_ENDIAN
153 #define IF_LITTLE(expr) expr
154 #define IF_BIG(expr) ((void) 0)
155 #elif __BYTE_ORDER == __BIG_ENDIAN
156 #define IF_LITTLE(expr) ((void) 0)
157 #define IF_BIG(expr) expr
158 #else
159 #warning Unknown byte ordering
160 #endif
161 
163  unsigned capacity, unsigned& offset) {
164  if (offset >= capacity) return false;
165  buffer[offset] = c;
166  offset += 1;
167  return true;
168 }
169 
171  unsigned capacity, unsigned& offset) {
172  unsigned aligned = (offset + 0x3) & ~0x3;
173  if ((aligned + sizeof(uchar)) > capacity)
174  return false;
175  if (offset == aligned) {
176  IF_LITTLE(*reinterpret_cast<uchar*>(buffer + aligned) = (c << 8) | 0x80);
177  IF_BIG(*reinterpret_cast<uchar*>(buffer + aligned) = c | (1 << 31));
178  } else {
179  buffer[offset] = 0x80;
180  IF_LITTLE(*reinterpret_cast<uchar*>(buffer + aligned) = c << 8);
181  IF_BIG(*reinterpret_cast<uchar*>(buffer + aligned) = c);
182  }
183  offset = aligned + sizeof(uchar);
184  return true;
185 }
186 
187 bool CharacterStream::EncodeCharacter(uchar c, byte* buffer, unsigned capacity,
188  unsigned& offset) {
189  if (c <= Utf8::kMaxOneByteChar) {
190  return EncodeAsciiCharacter(c, buffer, capacity, offset);
191  } else {
192  return EncodeNonAsciiCharacter(c, buffer, capacity, offset);
193  }
194 }
195 
196 uchar CharacterStream::DecodeCharacter(const byte* buffer, unsigned* offset) {
197  byte b = buffer[*offset];
198  if (b <= Utf8::kMaxOneByteChar) {
199  (*offset)++;
200  return b;
201  } else {
202  unsigned aligned = (*offset + 0x3) & ~0x3;
203  *offset = aligned + sizeof(uchar);
204  IF_LITTLE(return *reinterpret_cast<const uchar*>(buffer + aligned) >> 8);
205  IF_BIG(return *reinterpret_cast<const uchar*>(buffer + aligned) &
206  ~(1 << 31));
207  }
208 }
209 
210 #undef IF_LITTLE
211 #undef IF_BIG
212 
213 template <class R, class I, unsigned s>
215  buffer_ = R::ReadBlock(input_, util_buffer_, s, &remaining_, &offset_);
216 }
217 
218 template <class R, class I, unsigned s>
220  Reset(input_);
221 }
222 
223 template <class R, class I, unsigned s>
224 void InputBuffer<R, I, s>::Reset(unsigned position, I input) {
225  input_ = input;
226  remaining_ = 0;
227  cursor_ = 0;
228  offset_ = position;
229  buffer_ = R::ReadBlock(input_, util_buffer_, s, &remaining_, &offset_);
230 }
231 
232 template <class R, class I, unsigned s>
234  Reset(0, input);
235 }
236 
237 template <class R, class I, unsigned s>
238 void InputBuffer<R, I, s>::Seek(unsigned position) {
239  offset_ = position;
240  buffer_ = R::ReadBlock(input_, util_buffer_, s, &remaining_, &offset_);
241 }
242 
243 template <unsigned s>
244 Utf8InputBuffer<s>::Utf8InputBuffer(const char* data, unsigned length)
245  : InputBuffer<Utf8, Buffer<const char*>, s>(Buffer<const char*>(data,
246  length)) {
247 }
248 
249 } // namespace unibrow
250 
251 #endif // V8_UNICODE_INL_H_
bool get(uchar c)
Definition: unicode-inl.h:36
static int CombineSurrogatePair(uchar lead, uchar trail)
Definition: unicode.h:129
static const unsigned kSizeOfUnmatchedSurrogate
Definition: unicode.h:172
#define I(name, number_of_args, result_size)
Definition: runtime.cc:13224
static const unsigned kMaxTwoByteChar
Definition: unicode.h:165
static bool EncodeNonAsciiCharacter(uchar c, byte *buffer, unsigned capacity, unsigned &offset)
Definition: unicode-inl.h:170
static uchar DecodeCharacter(const byte *buffer, unsigned *offset)
Definition: unicode-inl.h:196
#define IF_BIG(expr)
Definition: unicode-inl.h:154
virtual bool BoundsCheck(unsigned offset)=0
#define ASSERT(condition)
Definition: checks.h:270
static uchar CalculateValue(const byte *str, unsigned length, unsigned *cursor)
Definition: unicode.cc:210
StringInputBuffer *const buffer_
int get(uchar c, uchar n, uchar *result)
Definition: unicode-inl.h:49
static uchar Length(uchar chr, int previous)
Definition: unicode-inl.h:124
#define IF_LITTLE(expr)
Definition: unicode-inl.h:153
void Seek(unsigned position)
Definition: unicode-inl.h:238
static unsigned Encode(char *out, uchar c, int previous)
Definition: unicode-inl.h:82
virtual void Rewind()
Definition: unicode-inl.h:219
virtual void FillBuffer()=0
static const unsigned kBytesSavedByCombiningSurrogates
Definition: unicode.h:171
static const uchar kBadChar
Definition: unicode.h:162
static const unsigned kMaxThreeByteChar
Definition: unicode.h:166
static const unsigned kMaxOneByteChar
Definition: unicode.h:164
void Reset(Input input)
Definition: unicode-inl.h:233
static bool IsLeadSurrogate(int code)
Definition: unicode.h:120
static bool IsTrailSurrogate(int code)
Definition: unicode.h:124
static bool EncodeAsciiCharacter(uchar c, byte *buffer, unsigned capacity, unsigned &offset)
Definition: unicode-inl.h:162
unsigned char byte
Definition: unicode.h:41
const byte * buffer_
Definition: unicode.h:210
virtual void FillBuffer()
Definition: unicode-inl.h:214
static bool EncodeCharacter(uchar c, byte *buffer, unsigned capacity, unsigned &offset)
Definition: unicode-inl.h:187
unsigned int uchar
Definition: unicode.h:40
static const int kNoPreviousCharacter
Definition: unicode.h:132