Commit cf0d7455 authored by lrn@chromium.org's avatar lrn@chromium.org

Remove unnecessary buffer doubling and content copying.

Review URL: http://codereview.chromium.org/377006


git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@3246 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent be8e9c0b
This diff is collapsed.
...@@ -41,6 +41,7 @@ class UTF8Buffer { ...@@ -41,6 +41,7 @@ class UTF8Buffer {
~UTF8Buffer(); ~UTF8Buffer();
void AddChar(uc32 c) { void AddChar(uc32 c) {
ASSERT_NOT_NULL(data_);
if (cursor_ <= limit_ && if (cursor_ <= limit_ &&
static_cast<unsigned>(c) <= unibrow::Utf8::kMaxOneByteChar) { static_cast<unsigned>(c) <= unibrow::Utf8::kMaxOneByteChar) {
*cursor_++ = static_cast<char>(c); *cursor_++ = static_cast<char>(c);
...@@ -49,16 +50,29 @@ class UTF8Buffer { ...@@ -49,16 +50,29 @@ class UTF8Buffer {
} }
} }
void Reset() { cursor_ = data_; } void Reset() {
int pos() const { return cursor_ - data_; } if (data_ == NULL) {
data_ = NewArray<char>(kInitialCapacity);
limit_ = ComputeLimit(data_, kInitialCapacity);
}
cursor_ = data_;
}
int pos() const {
ASSERT_NOT_NULL(data_);
return cursor_ - data_;
}
char* data() const { return data_; } char* data() const { return data_; }
private: private:
static const int kInitialCapacity = 256;
char* data_; char* data_;
char* cursor_; char* cursor_;
char* limit_; char* limit_;
int Capacity() const { int Capacity() const {
ASSERT_NOT_NULL(data_);
return (limit_ - data_) + unibrow::Utf8::kMaxEncodedSize; return (limit_ - data_) + unibrow::Utf8::kMaxEncodedSize;
} }
...@@ -278,26 +292,30 @@ class Scanner { ...@@ -278,26 +292,30 @@ class Scanner {
// token returned by Next()). The string is 0-terminated and in // token returned by Next()). The string is 0-terminated and in
// UTF-8 format; they may contain 0-characters. Literal strings are // UTF-8 format; they may contain 0-characters. Literal strings are
// collected for identifiers, strings, and numbers. // collected for identifiers, strings, and numbers.
// These functions only give the correct result if the literal
// was scanned between calls to StartLiteral() and TerminateLiteral().
const char* literal_string() const { const char* literal_string() const {
return &literals_.data()[current_.literal_pos]; return current_.literal_buffer->data();
} }
int literal_length() const { int literal_length() const {
return current_.literal_end - current_.literal_pos; // Excluding terminal '\0' added by TerminateLiteral().
} return current_.literal_buffer->pos() - 1;
Vector<const char> next_literal() const {
return Vector<const char>(next_literal_string(), next_literal_length());
} }
// Returns the literal string for the next token (the token that // Returns the literal string for the next token (the token that
// would be returned if Next() were called). // would be returned if Next() were called).
const char* next_literal_string() const { const char* next_literal_string() const {
return &literals_.data()[next_.literal_pos]; return next_.literal_buffer->data();
} }
// Returns the length of the next token (that would be returned if // Returns the length of the next token (that would be returned if
// Next() were called). // Next() were called).
int next_literal_length() const { int next_literal_length() const {
return next_.literal_end - next_.literal_pos; return next_.literal_buffer->pos() - 1;
}
Vector<const char> next_literal() const {
return Vector<const char>(next_literal_string(),
next_literal_length());
} }
// Scans the input as a regular expression pattern, previous // Scans the input as a regular expression pattern, previous
...@@ -339,7 +357,8 @@ class Scanner { ...@@ -339,7 +357,8 @@ class Scanner {
// Buffer to hold literal values (identifiers, strings, numbers) // Buffer to hold literal values (identifiers, strings, numbers)
// using 0-terminated UTF-8 encoding. // using 0-terminated UTF-8 encoding.
UTF8Buffer literals_; UTF8Buffer literal_buffer_1_;
UTF8Buffer literal_buffer_2_;
bool stack_overflow_; bool stack_overflow_;
static StaticResource<Utf8Decoder> utf8_decoder_; static StaticResource<Utf8Decoder> utf8_decoder_;
...@@ -351,7 +370,7 @@ class Scanner { ...@@ -351,7 +370,7 @@ class Scanner {
struct TokenDesc { struct TokenDesc {
Token::Value token; Token::Value token;
Location location; Location location;
int literal_pos, literal_end; UTF8Buffer* literal_buffer;
}; };
TokenDesc current_; // desc for current token (as returned by Next()) TokenDesc current_; // desc for current token (as returned by Next())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment