summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authortfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-01 17:13:20 +0000
committertfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-01 17:13:20 +0000
commit249153fc4daad5833679a22d26291324384e8175 (patch)
tree606fbbb97a2d997341037fc53780fbaac717eec1 /base
parentaa1834c788f85aef89c3e39cf8258e8d2ad42281 (diff)
downloadchromium_src-249153fc4daad5833679a22d26291324384e8175.zip
chromium_src-249153fc4daad5833679a22d26291324384e8175.tar.gz
chromium_src-249153fc4daad5833679a22d26291324384e8175.tar.bz2
base/json: Get rid of static initializer in JSONReader.
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
Diffstat (limited to 'base')
-rw-r--r--base/json/json_reader.cc44
-rw-r--r--base/json/json_reader.h7
2 files changed, 27 insertions, 24 deletions
diff --git a/base/json/json_reader.cc b/base/json/json_reader.cc
index bc7bcd0..d3d712a 100644
--- a/base/json/json_reader.cc
+++ b/base/json/json_reader.cc
@@ -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;
@@ -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)
@@ -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 =
@@ -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,
@@ -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:
@@ -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) {
@@ -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();
}
@@ -389,7 +387,7 @@ JSONReader::Token JSONReader::ParseNumberToken() {
c = token.NextChar();
}
if (!ReadInt(token, true))
- return kInvalidToken;
+ return Token::CreateInvalidToken();
}
return token;
@@ -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 '\\':
@@ -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;
@@ -452,7 +450,7 @@ JSONReader::Token JSONReader::ParseStringToken() {
++token.length;
c = token.NextChar();
}
- return kInvalidToken;
+ return Token::CreateInvalidToken();
}
Value* JSONReader::DecodeString(const Token& token) {
diff --git a/base/json/json_reader.h b/base/json/json_reader.h
index 39927de..e91da2e 100644
--- a/base/json/json_reader.h
+++ b/base/json/json_reader.h
@@ -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.