Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(564)

Unified Diff: lib/json/json.dart

Issue 10254029: Fix errors in the JSON parser. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Further optimize length. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/json/json.dart
diff --git a/lib/json/json.dart b/lib/json/json.dart
index a3542c5cf0aca1258bb40b883277f838b3d237c1..0c52517c3a08e6c1f56e9877742300d97f9759ff 100644
--- a/lib/json/json.dart
+++ b/lib/json/json.dart
@@ -96,7 +96,9 @@ class _JsonParser {
return new _JsonParser._internal(json)._parseToplevel();
}
- _JsonParser._internal(String json) : this.json = '${json} ' {
+ _JsonParser._internal(String json)
Anton Muhin 2012/04/28 09:09:24 nit: ._internal(this.json) ?
Anders Johnsen 2012/04/28 09:28:26 Not possible when extracting length in initializer
+ : json = json,
+ length = json.length {
if (tokens !== null) return;
// Use a list as jump-table, faster then switch and if.
@@ -221,7 +223,7 @@ class _JsonParser {
}
if (c == BACKSLASH) {
position++;
- if (position == json.length) {
+ if (position == length) {
_error('\\ at the end of input');
}
@@ -251,7 +253,7 @@ class _JsonParser {
c = TAB;
break;
case CHAR_U:
- if (position + 5 > json.length) {
+ if (position + 5 > length) {
_error('Invalid unicode esacape sequence');
}
final codeString = json.substring(position + 1, position + 5);
@@ -291,15 +293,15 @@ class _JsonParser {
if (_isChar(DOT)) {
position++;
if (_isDigit()) {
+ position++;
isInt = false;
while (_isDigit()) position++;
} else {
- position--; // No digit, backtrack.
+ _error("Expected digit following comma");
Anton Muhin 2012/04/28 09:09:24 nit: single quotes
Anton Muhin 2012/04/28 09:09:24 is it a correct thing, may we double check with JS
Anders Johnsen 2012/04/28 09:28:26 Done.
Anders Johnsen 2012/04/28 09:28:26 There is not token in JSON that can follow a numbe
}
}
if (_isChar(CHAR_E) || _isChar(CHAR_CAPITAL_E)) {
Anton Muhin 2012/04/28 09:09:24 ditto here: fetch char once and then do both check
Anders Johnsen 2012/04/28 09:28:26 Done.
- int backtrackTo = position;
position++;
if (_isChar(MINUS) || _isChar(PLUS)) position++;
Anton Muhin 2012/04/28 09:09:24 ditto
Anders Johnsen 2012/04/28 09:28:26 Done.
if (_isDigit()) {
@@ -307,7 +309,7 @@ class _JsonParser {
isInt = false;
while (_isDigit()) position++;
} else {
- position = backtrackTo; // No digit, backtrack.
+ _error("Expected digit following 'e' or 'E'");
Anton Muhin 2012/04/28 09:09:24 ditto nit for ''
Anders Johnsen 2012/04/28 09:28:26 Done.
}
}
@@ -319,17 +321,21 @@ class _JsonParser {
}
}
- bool _isChar(int char) => _char() == char;
+ bool _isChar(int char) {
+ if (position >= length) return false;
+ return json.charCodeAt(position) == char;
+ }
bool _isDigit() {
- int char = _char();
+ if (position >= length) return false;
+ int char = json.charCodeAt(position);
return char >= CHAR_0 && char <= CHAR_9;
}
bool _isToken(int tokenKind) => _token() == tokenKind;
int _char() {
- if (position >= json.length) {
+ if (position >= length) {
_error("Unexpected end of JSON stream");
}
return json.charCodeAt(position);
@@ -337,14 +343,14 @@ class _JsonParser {
int _token() {
while (true) {
- if (position >= json.length) return null;
+ if (position >= length) return null;
int char = json.charCodeAt(position);
int token = tokens[char];
if (token === WHITESPACE) {
position++;
continue;
}
- if (token === null) _error("Invalid JSON token");
+ if (token === null) return 0;
return token;
}
}
@@ -354,6 +360,7 @@ class _JsonParser {
}
final String json;
+ final int length;
int position = 0;
static List<int> tokens;
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698