blob: fb503459f7f4cdb756e11773b739c84585c4f5d0 [file] [log] [blame]
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001// Copyright 2011 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#include "v8.h"
29
30#include "scanner-character-streams.h"
31
ricow@chromium.org55ee8072011-09-08 16:33:10 +000032#include "handles.h"
33#include "unicode-inl.h"
34
35namespace v8 {
36namespace internal {
37
38// ----------------------------------------------------------------------------
yangguo@chromium.org154ff992012-03-13 08:09:54 +000039// BufferedUtf16CharacterStreams
ricow@chromium.org55ee8072011-09-08 16:33:10 +000040
yangguo@chromium.org154ff992012-03-13 08:09:54 +000041BufferedUtf16CharacterStream::BufferedUtf16CharacterStream()
42 : Utf16CharacterStream(),
ricow@chromium.org55ee8072011-09-08 16:33:10 +000043 pushback_limit_(NULL) {
44 // Initialize buffer as being empty. First read will fill the buffer.
45 buffer_cursor_ = buffer_;
46 buffer_end_ = buffer_;
47}
48
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +000049
yangguo@chromium.org154ff992012-03-13 08:09:54 +000050BufferedUtf16CharacterStream::~BufferedUtf16CharacterStream() { }
ricow@chromium.org55ee8072011-09-08 16:33:10 +000051
yangguo@chromium.org154ff992012-03-13 08:09:54 +000052void BufferedUtf16CharacterStream::PushBack(uc32 character) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +000053 if (character == kEndOfInput) {
54 pos_--;
55 return;
56 }
57 if (pushback_limit_ == NULL && buffer_cursor_ > buffer_) {
58 // buffer_ is writable, buffer_cursor_ is const pointer.
59 buffer_[--buffer_cursor_ - buffer_] = static_cast<uc16>(character);
60 pos_--;
61 return;
62 }
63 SlowPushBack(static_cast<uc16>(character));
64}
65
66
yangguo@chromium.org154ff992012-03-13 08:09:54 +000067void BufferedUtf16CharacterStream::SlowPushBack(uc16 character) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +000068 // In pushback mode, the end of the buffer contains pushback,
69 // and the start of the buffer (from buffer start to pushback_limit_)
70 // contains valid data that comes just after the pushback.
71 // We NULL the pushback_limit_ if pushing all the way back to the
72 // start of the buffer.
73
74 if (pushback_limit_ == NULL) {
75 // Enter pushback mode.
76 pushback_limit_ = buffer_end_;
77 buffer_end_ = buffer_ + kBufferSize;
78 buffer_cursor_ = buffer_end_;
79 }
80 // Ensure that there is room for at least one pushback.
81 ASSERT(buffer_cursor_ > buffer_);
82 ASSERT(pos_ > 0);
83 buffer_[--buffer_cursor_ - buffer_] = character;
84 if (buffer_cursor_ == buffer_) {
85 pushback_limit_ = NULL;
86 } else if (buffer_cursor_ < pushback_limit_) {
87 pushback_limit_ = buffer_cursor_;
88 }
89 pos_--;
90}
91
92
yangguo@chromium.org154ff992012-03-13 08:09:54 +000093bool BufferedUtf16CharacterStream::ReadBlock() {
ricow@chromium.org55ee8072011-09-08 16:33:10 +000094 buffer_cursor_ = buffer_;
95 if (pushback_limit_ != NULL) {
96 // Leave pushback mode.
97 buffer_end_ = pushback_limit_;
98 pushback_limit_ = NULL;
99 // If there were any valid characters left at the
100 // start of the buffer, use those.
101 if (buffer_cursor_ < buffer_end_) return true;
102 // Otherwise read a new block.
103 }
104 unsigned length = FillBuffer(pos_, kBufferSize);
105 buffer_end_ = buffer_ + length;
106 return length > 0;
107}
108
109
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000110unsigned BufferedUtf16CharacterStream::SlowSeekForward(unsigned delta) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000111 // Leave pushback mode (i.e., ignore that there might be valid data
112 // in the buffer before the pushback_limit_ point).
113 pushback_limit_ = NULL;
114 return BufferSeekForward(delta);
115}
116
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000117
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000118// ----------------------------------------------------------------------------
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000119// GenericStringUtf16CharacterStream
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000120
121
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000122GenericStringUtf16CharacterStream::GenericStringUtf16CharacterStream(
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000123 Handle<String> data,
124 unsigned start_position,
125 unsigned end_position)
126 : string_(data),
127 length_(end_position) {
128 ASSERT(end_position >= start_position);
129 buffer_cursor_ = buffer_;
130 buffer_end_ = buffer_;
131 pos_ = start_position;
132}
133
134
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000135GenericStringUtf16CharacterStream::~GenericStringUtf16CharacterStream() { }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000136
137
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000138unsigned GenericStringUtf16CharacterStream::BufferSeekForward(unsigned delta) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000139 unsigned old_pos = pos_;
140 pos_ = Min(pos_ + delta, length_);
141 ReadBlock();
142 return pos_ - old_pos;
143}
144
145
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000146unsigned GenericStringUtf16CharacterStream::FillBuffer(unsigned from_pos,
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000147 unsigned length) {
148 if (from_pos >= length_) return 0;
149 if (from_pos + length > length_) {
150 length = length_ - from_pos;
151 }
152 String::WriteToFlat<uc16>(*string_, buffer_, from_pos, from_pos + length);
153 return length;
154}
155
156
157// ----------------------------------------------------------------------------
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000158// Utf8ToUtf16CharacterStream
159Utf8ToUtf16CharacterStream::Utf8ToUtf16CharacterStream(const byte* data,
160 unsigned length)
161 : BufferedUtf16CharacterStream(),
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000162 raw_data_(data),
163 raw_data_length_(length),
164 raw_data_pos_(0),
165 raw_character_position_(0) {
166 ReadBlock();
167}
168
169
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000170Utf8ToUtf16CharacterStream::~Utf8ToUtf16CharacterStream() { }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000171
172
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000173unsigned Utf8ToUtf16CharacterStream::BufferSeekForward(unsigned delta) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000174 unsigned old_pos = pos_;
175 unsigned target_pos = pos_ + delta;
176 SetRawPosition(target_pos);
177 pos_ = raw_character_position_;
178 ReadBlock();
179 return pos_ - old_pos;
180}
181
182
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000183unsigned Utf8ToUtf16CharacterStream::FillBuffer(unsigned char_position,
184 unsigned length) {
185 static const unibrow::uchar kMaxUtf16Character = 0xffff;
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000186 SetRawPosition(char_position);
187 if (raw_character_position_ != char_position) {
188 // char_position was not a valid position in the stream (hit the end
189 // while spooling to it).
190 return 0u;
191 }
192 unsigned i = 0;
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000193 while (i < length - 1) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000194 if (raw_data_pos_ == raw_data_length_) break;
195 unibrow::uchar c = raw_data_[raw_data_pos_];
196 if (c <= unibrow::Utf8::kMaxOneByteChar) {
197 raw_data_pos_++;
198 } else {
199 c = unibrow::Utf8::CalculateValue(raw_data_ + raw_data_pos_,
200 raw_data_length_ - raw_data_pos_,
201 &raw_data_pos_);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000202 }
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000203 if (c > kMaxUtf16Character) {
204 buffer_[i++] = unibrow::Utf16::LeadSurrogate(c);
205 buffer_[i++] = unibrow::Utf16::TrailSurrogate(c);
206 } else {
207 buffer_[i++] = static_cast<uc16>(c);
208 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000209 }
210 raw_character_position_ = char_position + i;
211 return i;
212}
213
214
215static const byte kUtf8MultiByteMask = 0xC0;
216static const byte kUtf8MultiByteCharStart = 0xC0;
217static const byte kUtf8MultiByteCharFollower = 0x80;
218
219
220#ifdef DEBUG
221static bool IsUtf8MultiCharacterStart(byte first_byte) {
222 return (first_byte & kUtf8MultiByteMask) == kUtf8MultiByteCharStart;
223}
224#endif
225
226
227static bool IsUtf8MultiCharacterFollower(byte later_byte) {
228 return (later_byte & kUtf8MultiByteMask) == kUtf8MultiByteCharFollower;
229}
230
231
232// Move the cursor back to point at the preceding UTF-8 character start
233// in the buffer.
234static inline void Utf8CharacterBack(const byte* buffer, unsigned* cursor) {
235 byte character = buffer[--*cursor];
236 if (character > unibrow::Utf8::kMaxOneByteChar) {
237 ASSERT(IsUtf8MultiCharacterFollower(character));
238 // Last byte of a multi-byte character encoding. Step backwards until
239 // pointing to the first byte of the encoding, recognized by having the
240 // top two bits set.
241 while (IsUtf8MultiCharacterFollower(buffer[--*cursor])) { }
242 ASSERT(IsUtf8MultiCharacterStart(buffer[*cursor]));
243 }
244}
245
246
247// Move the cursor forward to point at the next following UTF-8 character start
248// in the buffer.
249static inline void Utf8CharacterForward(const byte* buffer, unsigned* cursor) {
250 byte character = buffer[(*cursor)++];
251 if (character > unibrow::Utf8::kMaxOneByteChar) {
252 // First character of a multi-byte character encoding.
253 // The number of most-significant one-bits determines the length of the
254 // encoding:
255 // 110..... - (0xCx, 0xDx) one additional byte (minimum).
256 // 1110.... - (0xEx) two additional bytes.
257 // 11110... - (0xFx) three additional bytes (maximum).
258 ASSERT(IsUtf8MultiCharacterStart(character));
259 // Additional bytes is:
260 // 1 if value in range 0xC0 .. 0xDF.
261 // 2 if value in range 0xE0 .. 0xEF.
262 // 3 if value in range 0xF0 .. 0xF7.
263 // Encode that in a single value.
264 unsigned additional_bytes =
265 ((0x3211u) >> (((character - 0xC0) >> 2) & 0xC)) & 0x03;
266 *cursor += additional_bytes;
267 ASSERT(!IsUtf8MultiCharacterFollower(buffer[1 + additional_bytes]));
268 }
269}
270
271
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000272// This can't set a raw position between two surrogate pairs, since there
273// is no position in the UTF8 stream that corresponds to that. This assumes
274// that the surrogate pair is correctly coded as a 4 byte UTF-8 sequence. If
275// it is illegally coded as two 3 byte sequences then there is no problem here.
276void Utf8ToUtf16CharacterStream::SetRawPosition(unsigned target_position) {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000277 if (raw_character_position_ > target_position) {
278 // Spool backwards in utf8 buffer.
279 do {
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000280 int old_pos = raw_data_pos_;
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000281 Utf8CharacterBack(raw_data_, &raw_data_pos_);
282 raw_character_position_--;
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000283 ASSERT(old_pos - raw_data_pos_ <= 4);
284 // Step back over both code units for surrogate pairs.
285 if (old_pos - raw_data_pos_ == 4) raw_character_position_--;
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000286 } while (raw_character_position_ > target_position);
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000287 // No surrogate pair splitting.
288 ASSERT(raw_character_position_ == target_position);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000289 return;
290 }
291 // Spool forwards in the utf8 buffer.
292 while (raw_character_position_ < target_position) {
293 if (raw_data_pos_ == raw_data_length_) return;
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000294 int old_pos = raw_data_pos_;
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000295 Utf8CharacterForward(raw_data_, &raw_data_pos_);
296 raw_character_position_++;
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000297 ASSERT(raw_data_pos_ - old_pos <= 4);
298 if (raw_data_pos_ - old_pos == 4) raw_character_position_++;
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000299 }
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000300 // No surrogate pair splitting.
301 ASSERT(raw_character_position_ == target_position);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000302}
303
304
305// ----------------------------------------------------------------------------
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000306// ExternalTwoByteStringUtf16CharacterStream
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000307
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000308ExternalTwoByteStringUtf16CharacterStream::
309 ~ExternalTwoByteStringUtf16CharacterStream() { }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000310
311
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000312ExternalTwoByteStringUtf16CharacterStream
313 ::ExternalTwoByteStringUtf16CharacterStream(
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000314 Handle<ExternalTwoByteString> data,
315 int start_position,
316 int end_position)
yangguo@chromium.org154ff992012-03-13 08:09:54 +0000317 : Utf16CharacterStream(),
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000318 source_(data),
319 raw_data_(data->GetTwoByteData(start_position)) {
320 buffer_cursor_ = raw_data_,
321 buffer_end_ = raw_data_ + (end_position - start_position);
322 pos_ = start_position;
323}
324
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000325} } // namespace v8::internal