1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_SCANNER_CHARACTER_STREAMS_H_
6 #define V8_SCANNER_CHARACTER_STREAMS_H_
8 #include "src/scanner.h"
13 // A buffered character stream based on a random access character
14 // source (ReadBlock can be called with pos_ pointing to any position,
15 // even positions before the current).
16 class BufferedUtf16CharacterStream: public Utf16CharacterStream {
18 BufferedUtf16CharacterStream();
19 virtual ~BufferedUtf16CharacterStream();
21 virtual void PushBack(uc32 character);
24 static const unsigned kBufferSize = 512;
25 static const unsigned kPushBackStepSize = 16;
27 virtual unsigned SlowSeekForward(unsigned delta);
28 virtual bool ReadBlock();
29 virtual void SlowPushBack(uc16 character);
31 virtual unsigned BufferSeekForward(unsigned delta) = 0;
32 virtual unsigned FillBuffer(unsigned position) = 0;
34 const uc16* pushback_limit_;
35 uc16 buffer_[kBufferSize];
39 // Generic string stream.
40 class GenericStringUtf16CharacterStream: public BufferedUtf16CharacterStream {
42 GenericStringUtf16CharacterStream(Handle<String> data,
43 unsigned start_position,
44 unsigned end_position);
45 virtual ~GenericStringUtf16CharacterStream();
48 virtual unsigned BufferSeekForward(unsigned delta);
49 virtual unsigned FillBuffer(unsigned position);
51 Handle<String> string_;
56 // Utf16 stream based on a literal UTF-8 string.
57 class Utf8ToUtf16CharacterStream: public BufferedUtf16CharacterStream {
59 Utf8ToUtf16CharacterStream(const byte* data, unsigned length);
60 virtual ~Utf8ToUtf16CharacterStream();
63 virtual unsigned BufferSeekForward(unsigned delta);
64 virtual unsigned FillBuffer(unsigned char_position);
65 void SetRawPosition(unsigned char_position);
67 const byte* raw_data_;
68 unsigned raw_data_length_; // Measured in bytes, not characters.
69 unsigned raw_data_pos_;
70 // The character position of the character at raw_data[raw_data_pos_].
71 // Not necessarily the same as pos_.
72 unsigned raw_character_position_;
76 // UTF16 buffer to read characters from an external string.
77 class ExternalTwoByteStringUtf16CharacterStream: public Utf16CharacterStream {
79 ExternalTwoByteStringUtf16CharacterStream(Handle<ExternalTwoByteString> data,
82 virtual ~ExternalTwoByteStringUtf16CharacterStream();
84 virtual void PushBack(uc32 character) {
85 DCHECK(buffer_cursor_ > raw_data_);
91 virtual unsigned SlowSeekForward(unsigned delta) {
92 // Fast case always handles seeking.
95 virtual bool ReadBlock() {
96 // Entire string is read at start.
99 Handle<ExternalTwoByteString> source_;
100 const uc16* raw_data_; // Pointer to the actual array of characters.
103 } } // namespace v8::internal
105 #endif // V8_SCANNER_CHARACTER_STREAMS_H_