Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove old string parsing util and replace them by ParseInt #1191

Merged
merged 2 commits into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions src/commands/redis_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t>(args[2], 10));
}
}

Expand Down Expand Up @@ -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<int64_t>(args[2], 10));
}
}

Expand Down Expand Up @@ -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<int64_t>(args[2], 10));

int64_t state = 0;
s = Util::DecimalStringToNum(args[3], &state, static_cast<int64_t>(kImportStart),
static_cast<int64_t>(kImportNone));
if (!s.IsOK()) return {Status::NotOK, "Invalid import state"};
auto state = ParseInt<unsigned>(args[3], {kImportStart, kImportNone}, 10);
if (!state) return {Status::NotOK, "Invalid import state"};

state_ = static_cast<ImportStatus>(state);
state_ = static_cast<ImportStatus>(*state);
return Status::OK();
}

Expand Down Expand Up @@ -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<int64_t>(args[2], 10));

dst_node_id_ = args[3];
return Status::OK();
Expand Down
18 changes: 0 additions & 18 deletions src/common/string_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t>(str, NumericRange<int64_t>{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<int64_t>(str, NumericRange<int64_t>{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";
Expand Down
2 changes: 0 additions & 2 deletions src/common/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 6 additions & 9 deletions src/config/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,13 @@ void Config::initFieldValidator() {
}
std::vector<std::string> 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<int>(start);
compaction_checker_range.Stop = static_cast<int>(stop);
auto start = GET_OR_RET(ParseInt<int>(args[0], {0, 24}, 10)),
stop = GET_OR_RET(ParseInt<int>(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",
Expand Down
7 changes: 4 additions & 3 deletions src/config/config_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <fmt/format.h>

#include <climits>
#include <functional>
#include <limits>
#include <string>
#include <utility>
Expand Down Expand Up @@ -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<int>(v, {min_, max_}, 8);
if (!s.IsOK()) return s;
*receiver_ = static_cast<int>(n);
*receiver_ = *s;
return Status::OK();
}

Expand Down
28 changes: 11 additions & 17 deletions src/storage/scripting.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -250,14 +251,11 @@ Status evalGenericCommand(Redis::Connection *conn, const std::vector<std::string
lua = conn->Owner()->Lua();
}

auto s = Util::DecimalStringToNum(args[2], &numkeys);
if (!s.IsOK()) {
return s;
}
numkeys = GET_OR_RET(ParseInt<int64_t>(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
Expand Down Expand Up @@ -286,13 +284,13 @@ Status evalGenericCommand(Redis::Connection *conn, const std::vector<std::string
auto s = srv->ScriptGet(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;
Expand Down Expand Up @@ -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<int64_t>(std::string(reply + 1, p - reply - 1), 10).ValueOr(0);
lua_pushnumber(lua, static_cast<lua_Number>(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<int64_t>(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;
Expand Down Expand Up @@ -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<int64_t>(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);
Expand Down Expand Up @@ -835,9 +829,9 @@ void sortArray(lua_State *lua) {

void setGlobalArray(lua_State *lua, const std::string &var, const std::vector<std::string> &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<int>(i) + 1);
}
lua_setglobal(lua, var.c_str());
}
Expand Down