Skip to content

Commit

Permalink
base/json: Get rid of static initializer in JSONReader.
Browse files Browse the repository at this point in the history
Convert kInvalidToken to a static function in Token class called CreateInvalidToken().

This should get rid of the problem with the static initialization.

BUG=94925
TEST=base_unittests --gtest_filter=JSON*

R=tony@chromium.org

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@99200 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
tfarina@chromium.org committed Sep 1, 2011
1 parent aa1834c commit 249153f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
44 changes: 21 additions & 23 deletions base/json/json_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,10 @@ const wchar_t kFalseString[] = L"false";

const int kStackLimit = 100;

} // namespace

namespace base {

static const JSONReader::Token kInvalidToken(JSONReader::Token::INVALID_TOKEN,
0, 0);
namespace {

// A helper method for ParseNumberToken. It reads an int from the end of
// token. The method returns false if there is no valid integer at the end of
// the token.
bool ReadInt(JSONReader::Token& token, bool can_have_leading_zeros) {
bool ReadInt(base::JSONReader::Token& token, bool can_have_leading_zeros) {
wchar_t first = token.NextChar();
int len = 0;

Expand All @@ -56,7 +48,7 @@ bool ReadInt(JSONReader::Token& token, bool can_have_leading_zeros) {
// A helper method for ParseStringToken. It reads |digits| hex digits from the
// token. If the sequence if digits is not valid (contains other characters),
// the method returns false.
bool ReadHexDigits(JSONReader::Token& token, int digits) {
bool ReadHexDigits(base::JSONReader::Token& token, int digits) {
for (int i = 1; i <= digits; ++i) {
wchar_t c = *(token.begin + token.length + i);
if ('\0' == c)
Expand All @@ -73,6 +65,8 @@ bool ReadHexDigits(JSONReader::Token& token, int digits) {

} // namespace

namespace base {

const char* JSONReader::kBadRootElementType =
"Root value must be an array or object.";
const char* JSONReader::kInvalidEscape =
Expand All @@ -91,17 +85,21 @@ const char* JSONReader::kUnquotedDictionaryKey =
"Dictionary keys must be quoted.";

JSONReader::JSONReader()
: start_pos_(NULL), json_pos_(NULL), stack_depth_(0),
: start_pos_(NULL),
json_pos_(NULL),
stack_depth_(0),
allow_trailing_comma_(false),
error_code_(JSON_NO_ERROR), error_line_(0), error_col_(0) {}
error_code_(JSON_NO_ERROR),
error_line_(0),
error_col_(0) {}

/* static */
// static
Value* JSONReader::Read(const std::string& json,
bool allow_trailing_comma) {
return ReadAndReturnError(json, allow_trailing_comma, NULL, NULL);
}

/* static */
// static
Value* JSONReader::ReadAndReturnError(const std::string& json,
bool allow_trailing_comma,
int* error_code_out,
Expand All @@ -119,7 +117,7 @@ Value* JSONReader::ReadAndReturnError(const std::string& json,
return NULL;
}

/* static */
// static
std::string JSONReader::ErrorCodeToString(JsonParseError error_code) {
switch (error_code) {
case JSON_NO_ERROR:
Expand Down Expand Up @@ -194,7 +192,7 @@ Value* JSONReader::JsonToValue(const std::string& json, bool check_root,
return NULL;
}

/* static */
// static
std::string JSONReader::FormatErrorMessage(int line, int column,
const std::string& description) {
if (line || column) {
Expand Down Expand Up @@ -369,14 +367,14 @@ JSONReader::Token JSONReader::ParseNumberToken() {
}

if (!ReadInt(token, false))
return kInvalidToken;
return Token::CreateInvalidToken();

// Optional fraction part
c = token.NextChar();
if ('.' == c) {
++token.length;
if (!ReadInt(token, true))
return kInvalidToken;
return Token::CreateInvalidToken();
c = token.NextChar();
}

Expand All @@ -389,7 +387,7 @@ JSONReader::Token JSONReader::ParseNumberToken() {
c = token.NextChar();
}
if (!ReadInt(token, true))
return kInvalidToken;
return Token::CreateInvalidToken();
}

return token;
Expand Down Expand Up @@ -422,13 +420,13 @@ JSONReader::Token JSONReader::ParseStringToken() {
case 'x':
if (!ReadHexDigits(token, 2)) {
SetErrorCode(JSON_INVALID_ESCAPE, json_pos_ + token.length);
return kInvalidToken;
return Token::CreateInvalidToken();
}
break;
case 'u':
if (!ReadHexDigits(token, 4)) {
SetErrorCode(JSON_INVALID_ESCAPE, json_pos_ + token.length);
return kInvalidToken;
return Token::CreateInvalidToken();
}
break;
case '\\':
Expand All @@ -443,7 +441,7 @@ JSONReader::Token JSONReader::ParseStringToken() {
break;
default:
SetErrorCode(JSON_INVALID_ESCAPE, json_pos_ + token.length);
return kInvalidToken;
return Token::CreateInvalidToken();
}
} else if ('"' == c) {
++token.length;
Expand All @@ -452,7 +450,7 @@ JSONReader::Token JSONReader::ParseStringToken() {
++token.length;
c = token.NextChar();
}
return kInvalidToken;
return Token::CreateInvalidToken();
}

Value* JSONReader::DecodeString(const Token& token) {
Expand Down
7 changes: 6 additions & 1 deletion base/json/json_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,19 @@ class BASE_EXPORT JSONReader {
END_OF_INPUT,
INVALID_TOKEN,
};

Token(Type t, const wchar_t* b, int len)
: type(t), begin(b), length(len) {}
: type(t), begin(b), length(len) {}

// Get the character that's one past the end of this token.
wchar_t NextChar() {
return *(begin + length);
}

static Token CreateInvalidToken() {
return Token(INVALID_TOKEN, 0, 0);
}

Type type;

// A pointer into JSONReader::json_pos_ that's the beginning of this token.
Expand Down

0 comments on commit 249153f

Please sign in to comment.