1 // 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
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_SCANNER_CHARACTER_STREAMS_H_
29 #define V8_SCANNER_CHARACTER_STREAMS_H_
36 // A buffered character stream based on a random access character
37 // source (ReadBlock can be called with pos_ pointing to any position,
38 // even positions before the current).
39 class BufferedUC16CharacterStream: public UC16CharacterStream {
41 BufferedUC16CharacterStream();
42 virtual ~BufferedUC16CharacterStream();
44 virtual void PushBack(uc32 character);
47 static const unsigned kBufferSize = 512;
48 static const unsigned kPushBackStepSize = 16;
50 virtual unsigned SlowSeekForward(unsigned delta);
51 virtual bool ReadBlock();
52 virtual void SlowPushBack(uc16 character);
54 virtual unsigned BufferSeekForward(unsigned delta) = 0;
55 virtual unsigned FillBuffer(unsigned position, unsigned length) = 0;
57 const uc16* pushback_limit_;
58 uc16 buffer_[kBufferSize];
62 // Generic string stream.
63 class GenericStringUC16CharacterStream: public BufferedUC16CharacterStream {
65 GenericStringUC16CharacterStream(Handle<String> data,
66 unsigned start_position,
67 unsigned end_position);
68 virtual ~GenericStringUC16CharacterStream();
71 virtual unsigned BufferSeekForward(unsigned delta);
72 virtual unsigned FillBuffer(unsigned position, unsigned length);
74 Handle<String> string_;
75 unsigned start_position_;
80 // UC16 stream based on a literal UTF-8 string.
81 class Utf8ToUC16CharacterStream: public BufferedUC16CharacterStream {
83 Utf8ToUC16CharacterStream(const byte* data, unsigned length);
84 virtual ~Utf8ToUC16CharacterStream();
87 virtual unsigned BufferSeekForward(unsigned delta);
88 virtual unsigned FillBuffer(unsigned char_position, unsigned length);
89 void SetRawPosition(unsigned char_position);
91 const byte* raw_data_;
92 unsigned raw_data_length_; // Measured in bytes, not characters.
93 unsigned raw_data_pos_;
94 // The character position of the character at raw_data[raw_data_pos_].
95 // Not necessarily the same as pos_.
96 unsigned raw_character_position_;
100 // UTF16 buffer to read characters from an external string.
101 class ExternalTwoByteStringUC16CharacterStream: public UC16CharacterStream {
103 ExternalTwoByteStringUC16CharacterStream(Handle<ExternalTwoByteString> data,
106 virtual ~ExternalTwoByteStringUC16CharacterStream();
108 virtual void PushBack(uc32 character) {
109 ASSERT(buffer_cursor_ > raw_data_);
115 virtual unsigned SlowSeekForward(unsigned delta) {
116 // Fast case always handles seeking.
119 virtual bool ReadBlock() {
120 // Entire string is read at start.
123 Handle<ExternalTwoByteString> source_;
124 const uc16* raw_data_; // Pointer to the actual array of characters.
127 } } // namespace v8::internal
129 #endif // V8_SCANNER_CHARACTER_STREAMS_H_