diff --git a/src/commands/redis_cmd.cc b/src/commands/redis_cmd.cc index 87ffae04991..d5063de4027 100644 --- a/src/commands/redis_cmd.cc +++ b/src/commands/redis_cmd.cc @@ -4310,8 +4310,7 @@ class CommandPerfLog : public Commander { if (args[2] == "*") { cnt_ = 0; } else { - Status s = Util::DecimalStringToNum(args[2], &cnt_); - return s; + cnt_ = GET_OR_RET(ParseInt(args[2], 10)); } } @@ -4348,8 +4347,7 @@ class CommandSlowlog : public Commander { if (args[2] == "*") { cnt_ = 0; } else { - Status s = Util::DecimalStringToNum(args[2], &cnt_); - return s; + cnt_ = GET_OR_RET(ParseInt(args[2], 10)); } } @@ -5086,15 +5084,12 @@ class CommandCluster : public Commander { if (subcommand_ == "import") { if (args.size() != 4) return {Status::RedisParseErr, errWrongNumOfArguments}; - auto s = Util::DecimalStringToNum(args[2], &slot_); - if (!s.IsOK()) return s; + slot_ = GET_OR_RET(ParseInt(args[2], 10)); - int64_t state = 0; - s = Util::DecimalStringToNum(args[3], &state, static_cast(kImportStart), - static_cast(kImportNone)); - if (!s.IsOK()) return {Status::NotOK, "Invalid import state"}; + auto state = ParseInt(args[3], {kImportStart, kImportNone}, 10); + if (!state) return {Status::NotOK, "Invalid import state"}; - state_ = static_cast(state); + state_ = static_cast(*state); return Status::OK(); } @@ -5181,8 +5176,7 @@ class CommandClusterX : public Commander { if (subcommand_ == "migrate") { if (args.size() != 4) return {Status::RedisParseErr, errWrongNumOfArguments}; - auto s = Util::DecimalStringToNum(args[2], &slot_); - if (!s.IsOK()) return s; + slot_ = GET_OR_RET(ParseInt(args[2], 10)); dst_node_id_ = args[3]; return Status::OK(); diff --git a/src/common/string_util.cc b/src/common/string_util.cc index 03cfe67ecf7..b01588eb049 100644 --- a/src/common/string_util.cc +++ b/src/common/string_util.cc @@ -26,24 +26,6 @@ namespace Util { -Status DecimalStringToNum(const std::string &str, int64_t *n, int64_t min, int64_t max) { - auto parse_result = ParseInt(str, NumericRange{min, max}, 10); - if (!parse_result) { - return parse_result.ToStatus(); - } - *n = *parse_result; - return Status::OK(); -} - -Status OctalStringToNum(const std::string &str, int64_t *n, int64_t min, int64_t max) { - auto parse_result = ParseInt(str, NumericRange{min, max}, 8); - if (!parse_result) { - return parse_result.ToStatus(); - } - *n = *parse_result; - return Status::OK(); -} - const std::string Float2String(double d) { if (std::isinf(d)) { return d > 0 ? "inf" : "-inf"; diff --git a/src/common/string_util.h b/src/common/string_util.h index e67893c7308..f8680f45d40 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -24,8 +24,6 @@ namespace Util { -Status DecimalStringToNum(const std::string &str, int64_t *n, int64_t min = INT64_MIN, int64_t max = INT64_MAX); -Status OctalStringToNum(const std::string &str, int64_t *n, int64_t min = INT64_MIN, int64_t max = INT64_MAX); const std::string Float2String(double d); std::string ToLower(std::string in); bool EqualICase(std::string_view lhs, std::string_view rhs); diff --git a/src/config/config.cc b/src/config/config.cc index 5fecfe7ecd7..01f7754d06f 100644 --- a/src/config/config.cc +++ b/src/config/config.cc @@ -260,16 +260,13 @@ void Config::initFieldValidator() { } std::vector args = Util::Split(v, "-"); if (args.size() != 2) { - return Status(Status::NotOK, "invalid range format, the range should be between 0 and 24"); + return {Status::NotOK, "invalid range format, the range should be between 0 and 24"}; } - int64_t start = 0, stop = 0; - Status s = Util::DecimalStringToNum(args[0], &start, 0, 24); - if (!s.IsOK()) return s; - s = Util::DecimalStringToNum(args[1], &stop, 0, 24); - if (!s.IsOK()) return s; - if (start > stop) return Status(Status::NotOK, "invalid range format, start should be smaller than stop"); - compaction_checker_range.Start = static_cast(start); - compaction_checker_range.Stop = static_cast(stop); + auto start = GET_OR_RET(ParseInt(args[0], {0, 24}, 10)), + stop = GET_OR_RET(ParseInt(args[1], {0, 24}, 10)); + if (start > stop) return {Status::NotOK, "invalid range format, start should be smaller than stop"}; + compaction_checker_range.Start = start; + compaction_checker_range.Stop = stop; return Status::OK(); }}, {"rename-command", diff --git a/src/config/config_type.h b/src/config/config_type.h index 65e774d9eb1..566f699454c 100644 --- a/src/config/config_type.h +++ b/src/config/config_type.h @@ -22,6 +22,8 @@ #include +#include +#include #include #include #include @@ -147,10 +149,9 @@ class OctalField : public ConfigField { return Status::OK(); } Status Set(const std::string &v) override { - int64_t n; - auto s = Util::OctalStringToNum(v, &n, min_, max_); + auto s = ParseInt(v, {min_, max_}, 8); if (!s.IsOK()) return s; - *receiver_ = static_cast(n); + *receiver_ = *s; return Status::OK(); } diff --git a/src/storage/scripting.cc b/src/storage/scripting.cc index 7d165715336..1c3b3a71c9c 100644 --- a/src/storage/scripting.cc +++ b/src/storage/scripting.cc @@ -58,6 +58,7 @@ #include "commands/redis_cmd.h" #include "fmt/format.h" +#include "parse_util.h" #include "rand.h" #include "server/redis_connection.h" #include "server/server.h" @@ -250,14 +251,11 @@ Status evalGenericCommand(Redis::Connection *conn, const std::vectorOwner()->Lua(); } - auto s = Util::DecimalStringToNum(args[2], &numkeys); - if (!s.IsOK()) { - return s; - } + numkeys = GET_OR_RET(ParseInt(args[2], 10)); if (numkeys > int64_t(args.size() - 3)) { - return Status(Status::NotOK, "Number of keys can't be greater than number of args"); + return {Status::NotOK, "Number of keys can't be greater than number of args"}; } else if (numkeys < -1) { - return Status(Status::NotOK, "Number of keys can't be negative"); + return {Status::NotOK, "Number of keys can't be negative"}; } /* We obtain the script SHA1, then check if this function is already @@ -286,13 +284,13 @@ Status evalGenericCommand(Redis::Connection *conn, const std::vectorScriptGet(funcname + 2, &body); if (!s.IsOK()) { lua_pop(lua, 1); /* remove the error handler from the stack. */ - return Status(Status::NotOK, "NOSCRIPT No matching script. Please use EVAL"); + return {Status::NotOK, "NOSCRIPT No matching script. Please use EVAL"}; } } else { body = args[1]; } std::string sha; - s = createFunction(srv, body, &sha, lua); + auto s = createFunction(srv, body, &sha, lua); if (!s.IsOK()) { lua_pop(lua, 1); /* remove the error handler from the stack. */ return s; @@ -619,18 +617,15 @@ const char *redisProtocolToLuaType(lua_State *lua, const char *reply) { const char *redisProtocolToLuaType_Int(lua_State *lua, const char *reply) { const char *p = strchr(reply + 1, '\r'); - int64_t value = 0; - - Util::DecimalStringToNum(std::string(reply + 1, p - reply - 1), &value); + auto value = ParseInt(std::string(reply + 1, p - reply - 1), 10).ValueOr(0); lua_pushnumber(lua, static_cast(value)); return p + 2; } const char *redisProtocolToLuaType_Bulk(lua_State *lua, const char *reply) { const char *p = strchr(reply + 1, '\r'); - int64_t bulklen = 0; + auto bulklen = ParseInt(std::string(reply + 1, p - reply - 1), 10).ValueOr(0); - Util::DecimalStringToNum(std::string(reply + 1, p - reply - 1), &bulklen); if (bulklen == -1) { lua_pushboolean(lua, 0); return p + 2; @@ -662,10 +657,9 @@ const char *redisProtocolToLuaType_Error(lua_State *lua, const char *reply) { const char *redisProtocolToLuaType_Aggregate(lua_State *lua, const char *reply, int atype) { const char *p = strchr(reply + 1, '\r'); - int64_t mbulklen = 0; + int64_t mbulklen = ParseInt(std::string(reply + 1, p - reply - 1), 10).ValueOr(0); int j = 0; - Util::DecimalStringToNum(std::string(reply + 1, p - reply - 1), &mbulklen); p += 2; if (mbulklen == -1) { lua_pushboolean(lua, 0); @@ -835,9 +829,9 @@ void sortArray(lua_State *lua) { void setGlobalArray(lua_State *lua, const std::string &var, const std::vector &elems) { lua_newtable(lua); - for (int i = 0; i < elems.size(); i++) { + for (size_t i = 0; i < elems.size(); i++) { lua_pushlstring(lua, elems[i].c_str(), elems[i].size()); - lua_rawseti(lua, -2, i + 1); + lua_rawseti(lua, -2, static_cast(i) + 1); } lua_setglobal(lua, var.c_str()); }