Skip to content

Commit

Permalink
fixed cpplint errors about string& members (microsoft#2929)
Browse files Browse the repository at this point in the history
* [ci] fixed cpplint errors about string& members

* add constructor

* [ci] fixed cpplint errors about string& members

* add constructor

* refix

Co-authored-by: guolinke <guolin.ke@outlook.com>
  • Loading branch information
jameslamb and guolinke authored Apr 5, 2020
1 parent d84e9a2 commit 6d9e7f1
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/io/json11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class Value : public JsonValue {
return m_value == static_cast<const Value<tag, T> *>(other)->m_value;
}
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;
Expand Down Expand Up @@ -351,7 +351,8 @@ namespace {
struct JsonParser final {
/* State
*/
const string &str;
const char* str;
const size_t str_len;
size_t i;
string &err;
bool failed;
Expand Down Expand Up @@ -390,23 +391,23 @@ struct JsonParser final {
bool comment_found = false;
if (str[i] == '/') {
i++;
if (i == str.size())
if (i == str_len)
return fail("Unexpected end of input after start of comment", false);
if (str[i] == '/') { // inline comment
i++;
// advance until next line, or end of input
while (i < str.size() && str[i] != '\n') {
while (i < str_len && str[i] != '\n') {
i++;
}
comment_found = true;
} else if (str[i] == '*') { // multiline comment
i++;
if (i > str.size()-2)
if (i > str_len - 2)
return fail("Unexpected end of input inside multi-line comment", false);
// advance until closing tokens
while (!(str[i] == '*' && str[i+1] == '/')) {
i++;
if (i > str.size()-2)
if (i > str_len - 2)
return fail("Unexpected end of input inside multi-line comment", false);
}
i += 2;
Expand Down Expand Up @@ -442,7 +443,7 @@ struct JsonParser final {
char get_next_token() {
consume_garbage();
if (failed) return char{0};
if (i == str.size())
if (i == str_len)
return fail("Unexpected end of input", char{0});

return str[i++];
Expand Down Expand Up @@ -481,7 +482,7 @@ struct JsonParser final {
string out;
long last_escaped_codepoint = -1;
while (true) {
if (i == str.size())
if (i == str_len)
return fail("Unexpected end of input in string", "");

char ch = str[i++];
Expand All @@ -503,14 +504,14 @@ struct JsonParser final {
}

// Handle escapes
if (i == str.size())
if (i == str_len)
return fail("Unexpected end of input in string", "");

ch = str[i++];

if (ch == 'u') {
// 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
// relies on std::string returning the terminating NUL when
// accessing str[length]. Checking here reduces brittleness.
Expand Down Expand Up @@ -591,7 +592,7 @@ struct JsonParser final {

if (str[i] != '.' && str[i] != 'e' && str[i] != 'E'
&& (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
Expand All @@ -618,7 +619,7 @@ struct JsonParser final {
i++;
}

return std::strtod(str.c_str() + start_pos, nullptr);
return std::strtod(str + start_pos, nullptr);
}

/* expect(str, res)
Expand All @@ -629,11 +630,12 @@ struct JsonParser final {
Json expect(const string &expected, Json res) {
CHECK_NE(i, 0)
i--;
if (str.compare(i, expected.length(), expected) == 0) {
auto substr = string(str + i, expected.length());
if (substr == expected) {
i += expected.length();
return res;
} else {
return fail("Parse error: expected " + expected + ", got " + str.substr(i, expected.length()));
return fail("Parse error: expected " + expected + ", got " + substr);
}
}

Expand Down Expand Up @@ -730,7 +732,7 @@ struct JsonParser final {
} // namespace

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);

// Check for any trailing garbage
Expand All @@ -748,7 +750,7 @@ vector<Json> Json::parse_multi(const string &in,
std::string::size_type &parser_stop_pos,
string &err,
JsonParse strategy) {
JsonParser parser { in, 0, err, false, strategy };
JsonParser parser{in.c_str(), in.size(), 0, err, false, strategy};
parser_stop_pos = 0;
vector<Json> json_vec;
while (parser.i != in.size() && !parser.failed) {
Expand Down

0 comments on commit 6d9e7f1

Please sign in to comment.