Unverified Commit 6d9e7f17 authored by James Lamb's avatar James Lamb Committed by GitHub
Browse files

fixed cpplint errors about string& members (#2929)



* [ci] fixed cpplint errors about string& members

* add constructor

* [ci] fixed cpplint errors about string& members

* add constructor

* refix
Co-authored-by: default avatarguolinke <guolin.ke@outlook.com>
parent d84e9a2e
...@@ -165,7 +165,7 @@ class Value : public JsonValue { ...@@ -165,7 +165,7 @@ class Value : public JsonValue {
return m_value == static_cast<const Value<tag, T> *>(other)->m_value; return m_value == static_cast<const Value<tag, T> *>(other)->m_value;
} }
bool less(const JsonValue * other) const override { bool less(const JsonValue * other) const override {
return m_value < static_cast<const Value<tag, T> *>(other)->m_value; return m_value < (static_cast<const Value<tag, T> *>(other)->m_value);
} }
const T m_value; const T m_value;
...@@ -351,7 +351,8 @@ namespace { ...@@ -351,7 +351,8 @@ namespace {
struct JsonParser final { struct JsonParser final {
/* State /* State
*/ */
const string &str; const char* str;
const size_t str_len;
size_t i; size_t i;
string &err; string &err;
bool failed; bool failed;
...@@ -390,23 +391,23 @@ struct JsonParser final { ...@@ -390,23 +391,23 @@ struct JsonParser final {
bool comment_found = false; bool comment_found = false;
if (str[i] == '/') { if (str[i] == '/') {
i++; i++;
if (i == str.size()) if (i == str_len)
return fail("Unexpected end of input after start of comment", false); return fail("Unexpected end of input after start of comment", false);
if (str[i] == '/') { // inline comment if (str[i] == '/') { // inline comment
i++; i++;
// advance until next line, or end of input // advance until next line, or end of input
while (i < str.size() && str[i] != '\n') { while (i < str_len && str[i] != '\n') {
i++; i++;
} }
comment_found = true; comment_found = true;
} else if (str[i] == '*') { // multiline comment } else if (str[i] == '*') { // multiline comment
i++; i++;
if (i > str.size()-2) if (i > str_len - 2)
return fail("Unexpected end of input inside multi-line comment", false); return fail("Unexpected end of input inside multi-line comment", false);
// advance until closing tokens // advance until closing tokens
while (!(str[i] == '*' && str[i+1] == '/')) { while (!(str[i] == '*' && str[i+1] == '/')) {
i++; i++;
if (i > str.size()-2) if (i > str_len - 2)
return fail("Unexpected end of input inside multi-line comment", false); return fail("Unexpected end of input inside multi-line comment", false);
} }
i += 2; i += 2;
...@@ -442,7 +443,7 @@ struct JsonParser final { ...@@ -442,7 +443,7 @@ struct JsonParser final {
char get_next_token() { char get_next_token() {
consume_garbage(); consume_garbage();
if (failed) return char{0}; if (failed) return char{0};
if (i == str.size()) if (i == str_len)
return fail("Unexpected end of input", char{0}); return fail("Unexpected end of input", char{0});
return str[i++]; return str[i++];
...@@ -481,7 +482,7 @@ struct JsonParser final { ...@@ -481,7 +482,7 @@ struct JsonParser final {
string out; string out;
long last_escaped_codepoint = -1; long last_escaped_codepoint = -1;
while (true) { while (true) {
if (i == str.size()) if (i == str_len)
return fail("Unexpected end of input in string", ""); return fail("Unexpected end of input in string", "");
char ch = str[i++]; char ch = str[i++];
...@@ -503,14 +504,14 @@ struct JsonParser final { ...@@ -503,14 +504,14 @@ struct JsonParser final {
} }
// Handle escapes // Handle escapes
if (i == str.size()) if (i == str_len)
return fail("Unexpected end of input in string", ""); return fail("Unexpected end of input in string", "");
ch = str[i++]; ch = str[i++];
if (ch == 'u') { if (ch == 'u') {
// Extract 4-byte escape sequence // Extract 4-byte escape sequence
string esc = str.substr(i, 4); string esc = string(str + i, 4);
// Explicitly check length of the substring. The following loop // Explicitly check length of the substring. The following loop
// relies on std::string returning the terminating NUL when // relies on std::string returning the terminating NUL when
// accessing str[length]. Checking here reduces brittleness. // accessing str[length]. Checking here reduces brittleness.
...@@ -591,7 +592,7 @@ struct JsonParser final { ...@@ -591,7 +592,7 @@ struct JsonParser final {
if (str[i] != '.' && str[i] != 'e' && str[i] != 'E' if (str[i] != '.' && str[i] != 'e' && str[i] != 'E'
&& (i - start_pos) <= static_cast<size_t>(std::numeric_limits<int>::digits10)) { && (i - start_pos) <= static_cast<size_t>(std::numeric_limits<int>::digits10)) {
return std::atoi(str.c_str() + start_pos); return std::atoi(str + start_pos);
} }
// Decimal part // Decimal part
...@@ -618,7 +619,7 @@ struct JsonParser final { ...@@ -618,7 +619,7 @@ struct JsonParser final {
i++; i++;
} }
return std::strtod(str.c_str() + start_pos, nullptr); return std::strtod(str + start_pos, nullptr);
} }
/* expect(str, res) /* expect(str, res)
...@@ -629,11 +630,12 @@ struct JsonParser final { ...@@ -629,11 +630,12 @@ struct JsonParser final {
Json expect(const string &expected, Json res) { Json expect(const string &expected, Json res) {
CHECK_NE(i, 0) CHECK_NE(i, 0)
i--; i--;
if (str.compare(i, expected.length(), expected) == 0) { auto substr = string(str + i, expected.length());
if (substr == expected) {
i += expected.length(); i += expected.length();
return res; return res;
} else { } else {
return fail("Parse error: expected " + expected + ", got " + str.substr(i, expected.length())); return fail("Parse error: expected " + expected + ", got " + substr);
} }
} }
...@@ -730,7 +732,7 @@ struct JsonParser final { ...@@ -730,7 +732,7 @@ struct JsonParser final {
} // namespace } // namespace
Json Json::parse(const string &in, string &err, JsonParse strategy) { Json Json::parse(const string &in, string &err, JsonParse strategy) {
JsonParser parser { in, 0, err, false, strategy }; JsonParser parser{in.c_str(), in.size(), 0, err, false, strategy};
Json result = parser.parse_json(0); Json result = parser.parse_json(0);
// Check for any trailing garbage // Check for any trailing garbage
...@@ -748,7 +750,7 @@ vector<Json> Json::parse_multi(const string &in, ...@@ -748,7 +750,7 @@ vector<Json> Json::parse_multi(const string &in,
std::string::size_type &parser_stop_pos, std::string::size_type &parser_stop_pos,
string &err, string &err,
JsonParse strategy) { JsonParse strategy) {
JsonParser parser { in, 0, err, false, strategy }; JsonParser parser{in.c_str(), in.size(), 0, err, false, strategy};
parser_stop_pos = 0; parser_stop_pos = 0;
vector<Json> json_vec; vector<Json> json_vec;
while (parser.i != in.size() && !parser.failed) { while (parser.i != in.size() && !parser.failed) {
......
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