diff --git a/lint/httplib.h b/lint/httplib.h index f3e109e..30e19b9 100644 --- a/lint/httplib.h +++ b/lint/httplib.h @@ -301,7 +301,7 @@ inline void read_file(const std::string& path, std::string& out) fs.seekg(0, std::ios_base::end); auto size = fs.tellg(); fs.seekg(0); - out.resize(size); + out.resize(static_cast(size)); fs.read(&out[0], size); } @@ -345,11 +345,11 @@ inline const char* get_header_value(const MultiMap& map, const char* key, const return def; } -inline int get_header_value_int(const MultiMap& map, const char* key, int def) +inline size_t get_header_value_int(const MultiMap& map, const char* key, size_t def) { auto it = map.find(key); if (it != map.end()) { - return std::stoi(it->second); + return static_cast(std::stoul(it->second)); } return def; } @@ -424,7 +424,7 @@ inline std::string encode_url(const std::string& s) { std::string result; - for (auto i = 0; s[i]; i++) { + for (size_t i = 0; s[i]; i++) { switch (s[i]) { case ' ': result += "+"; break; case '\'': result += "%27"; break; @@ -435,7 +435,7 @@ inline std::string encode_url(const std::string& s) if (s[i] < 0) { result += '%'; char hex[4]; - size_t len = snprintf(hex, sizeof(hex), "%02X", (unsigned char)s[i]); + auto len = static_cast(snprintf(hex, sizeof(hex), "%02X", static_cast(s[i]))); assert(len == 2); result.append(hex, len); } else { @@ -463,10 +463,10 @@ inline bool is_hex(char c, int& v) return false; } -inline int from_hex_to_i(const std::string& s, int i, int cnt, int& val) +inline size_t from_hex_to_i(const std::string& s, size_t i, int cnt, int& val) { val = 0; - for (; s[i] && cnt; i++, cnt--) { + for ( ; s[i] && cnt; i++, cnt--) { int v = 0; if (is_hex(s[i], v)) { val = val * 16 + v; @@ -483,26 +483,26 @@ inline size_t to_utf8(int code, char* buff) buff[0] = (code & 0x7F); return 1; } else if (code < 0x0800) { - buff[0] = (0xC0 | ((code >> 6) & 0x1F)); - buff[1] = (0x80 | (code & 0x3F)); + buff[0] = static_cast(0xC0 | ((code >> 6) & 0x1F)); + buff[1] = static_cast(0x80 | (code & 0x3F)); return 2; } else if (code < 0xD800) { - buff[0] = (0xE0 | ((code >> 12) & 0xF)); - buff[1] = (0x80 | ((code >> 6) & 0x3F)); - buff[2] = (0x80 | (code & 0x3F)); + buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); + buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[2] = static_cast(0x80 | (code & 0x3F)); return 3; } else if (code < 0xE000) { // D800 - DFFF is invalid... return 0; } else if (code < 0x10000) { - buff[0] = (0xE0 | ((code >> 12) & 0xF)); - buff[1] = (0x80 | ((code >> 6) & 0x3F)); - buff[2] = (0x80 | (code & 0x3F)); + buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); + buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[2] = static_cast(0x80 | (code & 0x3F)); return 3; } else if (code < 0x110000) { - buff[0] = (0xF0 | ((code >> 18) & 0x7)); - buff[1] = (0x80 | ((code >> 12) & 0x3F)); - buff[2] = (0x80 | ((code >> 6) & 0x3F)); - buff[3] = (0x80 | (code & 0x3F)); + buff[0] = static_cast(0xF0 | ((code >> 18) & 0x7)); + buff[1] = static_cast(0x80 | ((code >> 12) & 0x3F)); + buff[2] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[3] = static_cast(0x80 | (code & 0x3F)); return 4; } @@ -514,7 +514,7 @@ inline std::string decode_url(const std::string& s) { std::string result; - for (int i = 0; s[i]; i++) { + for (size_t i = 0; s[i]; i++) { if (s[i] == '%') { i++; assert(s[i]); @@ -539,7 +539,7 @@ inline std::string decode_url(const std::string& s) // HEX int val = 0; i = from_hex_to_i(s, i, 2, val); - result += val; + result += static_cast(val); } } else if (s[i] == '+') { result += ' '; diff --git a/lint/peglint.cc b/lint/peglint.cc index 450ea99..0a56c87 100644 --- a/lint/peglint.cc +++ b/lint/peglint.cc @@ -123,7 +123,7 @@ int main(int argc, const char** argv) std::cout << "-------\t--------" << std::endl; size_t prev_pos = 0; parser.enable_trace([&](auto name, auto s, auto /*n*/, auto& /*sv*/, auto& c, auto& /*dt*/) { - auto pos = s - c.s; + auto pos = static_cast(s - c.s); auto backtrack = (pos < prev_pos ? "*" : ""); string indent; auto level = c.nest_level; @@ -133,7 +133,7 @@ int main(int argc, const char** argv) std::cout << pos << ":" << c.nest_level << backtrack << "\t" << indent << name << std::endl; - prev_pos = pos; + prev_pos = static_cast(pos); }); } diff --git a/peglib.h b/peglib.h index 0a60a06..607b871 100644 --- a/peglib.h +++ b/peglib.h @@ -253,7 +253,7 @@ struct SemanticValues : protected std::vector } template - auto transform(size_t beg = 0, size_t end = -1) const -> vector { + auto transform(size_t beg = 0, size_t end = static_cast(-1)) const -> vector { return this->transform(beg, end, [](const any& v) { return v.get(); }); } @@ -432,11 +432,11 @@ typedef std::function(-1); } inline bool fail(size_t len) { - return len == -1; + return len == static_cast(-1); } /* @@ -512,21 +512,21 @@ public: } auto col = s - this->s; - auto has_cache = cache_register[def_count * col + def_id]; + auto has_cache = cache_register[def_count * static_cast(col) + def_id]; if (has_cache) { - if (cache_success[def_count * col + def_id]) { + if (cache_success[def_count * static_cast(col) + def_id]) { const auto& key = std::make_pair(s - this->s, def_id); std::tie(len, val) = cache_result[key]; return; } else { - len = -1; + len = static_cast(-1); return; } } else { fn(val); - cache_register[def_count * col + def_id] = true; - cache_success[def_count * col + def_id] = success(len); + cache_register[def_count * static_cast(col) + def_id] = true; + cache_success[def_count * static_cast(col) + def_id] = success(len); if (success(len)) { const auto& key = std::make_pair(s - this->s, def_id); cache_result[key] = std::make_pair(len, val); @@ -611,7 +611,7 @@ public: const auto& rule = *ope; auto len = rule.parse(s + i, n - i, sv, c, dt); if (fail(len)) { - return -1; + return static_cast(-1); } i += len; } @@ -669,7 +669,7 @@ public: } id++; } - return -1; + return static_cast(-1); } void accept(Visitor& v) override; @@ -697,10 +697,10 @@ public: auto len = rule.parse(s + i, n - i, sv, c, dt); if (fail(len)) { if (sv.size() != save_sv_size) { - sv.erase(sv.begin() + save_sv_size); + sv.erase(sv.begin() + static_cast(save_sv_size)); } if (sv.tokens.size() != save_tok_size) { - sv.tokens.erase(sv.tokens.begin() + save_tok_size); + sv.tokens.erase(sv.tokens.begin() + static_cast(save_tok_size)); } c.error_pos = save_error_pos; break; @@ -722,14 +722,14 @@ public: size_t parse(const char* s, size_t n, SemanticValues& sv, Context& c, any& dt) const override { c.trace("OneOrMore", s, n, sv, dt); - auto len = 0; + size_t len = 0; { c.nest_level++; auto se = make_scope_exit([&]() { c.nest_level--; }); const auto& rule = *ope_; len = rule.parse(s, n, sv, c, dt); if (fail(len)) { - return -1; + return static_cast(-1); } } auto save_error_pos = c.error_pos; @@ -740,13 +740,13 @@ public: auto save_sv_size = sv.size(); auto save_tok_size = sv.tokens.size(); const auto& rule = *ope_; - auto len = rule.parse(s + i, n - i, sv, c, dt); + len = rule.parse(s + i, n - i, sv, c, dt); if (fail(len)) { if (sv.size() != save_sv_size) { - sv.erase(sv.begin() + save_sv_size); + sv.erase(sv.begin() + static_cast(save_sv_size)); } if (sv.tokens.size() != save_tok_size) { - sv.tokens.erase(sv.tokens.begin() + save_tok_size); + sv.tokens.erase(sv.tokens.begin() + static_cast(save_tok_size)); } c.error_pos = save_error_pos; break; @@ -779,10 +779,10 @@ public: return len; } else { if (sv.size() != save_sv_size) { - sv.erase(sv.begin() + save_sv_size); + sv.erase(sv.begin() + static_cast(save_sv_size)); } if (sv.tokens.size() != save_tok_size) { - sv.tokens.erase(sv.tokens.begin() + save_tok_size); + sv.tokens.erase(sv.tokens.begin() + static_cast(save_tok_size)); } c.error_pos = save_error_pos; return 0; @@ -812,7 +812,7 @@ public: if (success(len)) { return 0; } else { - return -1; + return static_cast(-1); } } @@ -839,7 +839,7 @@ public: auto len = rule.parse(s, n, chldsv, c, dt); if (success(len)) { c.set_error_pos(s); - return -1; + return static_cast(-1); } else { c.error_pos = save_error_pos; return 0; @@ -873,7 +873,7 @@ public: // TODO: UTF8 support if (n < 1) { c.set_error_pos(s); - return -1; + return static_cast(-1); } auto ch = s[0]; auto i = 0u; @@ -891,7 +891,7 @@ public: } } c.set_error_pos(s); - return -1; + return static_cast(-1); } void accept(Visitor& v) override; @@ -909,7 +909,7 @@ public: // TODO: UTF8 support if (n < 1 || s[0] != ch_) { c.set_error_pos(s); - return -1; + return static_cast(-1); } return 1; } @@ -927,7 +927,7 @@ public: // TODO: UTF8 support if (n < 1) { c.set_error_pos(s); - return -1; + return static_cast(-1); } return 1; } @@ -1369,7 +1369,7 @@ inline size_t LiteralString::parse(const char* s, size_t n, SemanticValues& sv, for (; i < lit_.size(); i++) { if (i >= n || s[i] != lit_[i]) { c.set_error_pos(s); - return -1; + return static_cast(-1); } } @@ -1378,7 +1378,7 @@ inline size_t LiteralString::parse(const char* s, size_t n, SemanticValues& sv, if (c.whitespaceOpe) { auto len = c.whitespaceOpe->parse(s + i, n - i, sv, c, dt); if (fail(len)) { - return -1; + return static_cast(-1); } i += len; } @@ -1398,7 +1398,7 @@ inline size_t TokenBoundary::parse(const char* s, size_t n, SemanticValues& sv, if (c.whitespaceOpe) { auto l = c.whitespaceOpe->parse(s + len, n - len, sv, c, dt); if (fail(l)) { - return -1; + return static_cast(-1); } len += l; } @@ -1450,7 +1450,7 @@ inline size_t Holder::parse(const char* s, size_t n, SemanticValues& sv, Context c.message = e.what(); } } - len = -1; + len = static_cast(-1); } } }); @@ -1582,7 +1582,7 @@ inline std::shared_ptr cap(const std::shared_ptr& ope, MatchAction ma, } inline std::shared_ptr cap(const std::shared_ptr& ope, MatchAction ma) { - return std::make_shared(ope, ma, (size_t)-1, std::string()); + return std::make_shared(ope, ma, static_cast(-1), std::string()); } inline std::shared_ptr tok(const std::shared_ptr& ope) { @@ -1849,7 +1849,7 @@ private: Data& data = *dt.get(); auto ignore = (sv.size() == 4); - auto baseId = ignore ? 1 : 0; + auto baseId = ignore ? 1u : 0u; const auto& name = sv[baseId].get(); auto ope = sv[baseId + 2].get>(); @@ -1935,7 +1935,7 @@ private: switch (sv.choice()) { case 0: { // Reference auto ignore = (sv.size() == 2); - auto baseId = ignore ? 1 : 0; + auto baseId = ignore ? 1u : 0u; const auto& ident = sv[baseId].get(); @@ -2126,7 +2126,7 @@ private: char ret = 0; int val; while (i < n && is_hex(s[i], val)) { - ret = ret * 16 + val; + ret = static_cast(ret * 16 + val); i++; } return std::make_pair(ret, i); @@ -2136,7 +2136,7 @@ private: char ret = 0; int val; while (i < n && is_digit(s[i], val)) { - ret = ret * 8 + val; + ret = static_cast(ret * 8 + val); i++; } return std::make_pair(ret, i); @@ -2190,7 +2190,7 @@ const int AstDefaultTag = -1; #ifndef PEGLIB_NO_CONSTEXPR_SUPPORT inline constexpr unsigned int str2tag(const char* str, int h = 0) { - return !str[h] ? 5381 : (str2tag(str, h + 1) * 33) ^ str[h]; + return !str[h] ? 5381 : (str2tag(str, h + 1) * 33) ^ static_cast(str[h]); } inline constexpr unsigned int operator "" _(const char* s, size_t) { diff --git a/test/test.cc b/test/test.cc index 5c93bbd..1a983fb 100644 --- a/test/test.cc +++ b/test/test.cc @@ -786,7 +786,7 @@ TEST_CASE("User rule test", "[user rule]") return name.size(); } } - return -1; + return static_cast(-1); }) }, {