Skip to content

Commit

Permalink
chore: get rid of ToUpper/ToLower mutations on arguments (#3950)
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Gershman <roman@dragonflydb.io>
  • Loading branch information
romange authored Oct 18, 2024
1 parent 84e22aa commit 14220a6
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 48 deletions.
3 changes: 1 addition & 2 deletions src/server/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,11 @@ OpResult<ScanOpts> ScanOpts::TryFrom(CmdArgList args) {
ScanOpts scan_opts;

for (unsigned i = 0; i < args.size(); i += 2) {
ToUpper(&args[i]);
string_view opt = ArgS(args, i);
if (i + 1 == args.size()) {
return facade::OpStatus::SYNTAX_ERR;
}

string opt = absl::AsciiStrToUpper(ArgS(args, i));
if (opt == "COUNT") {
if (!absl::SimpleAtoi(ArgS(args, i + 1), &scan_opts.limit)) {
return facade::OpStatus::INVALID_INT;
Expand Down
12 changes: 0 additions & 12 deletions src/server/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,6 @@ std::ostream& operator<<(std::ostream& os, const GlobalState& state);

enum class TimeUnit : uint8_t { SEC, MSEC };

inline void ToUpper(const MutableSlice* val) {
for (auto& c : *val) {
c = absl::ascii_toupper(c);
}
}

inline void ToLower(const MutableSlice* val) {
for (auto& c : *val) {
c = absl::ascii_tolower(c);
}
}

bool ParseHumanReadableBytes(std::string_view str, int64_t* num_bytes);
bool ParseDouble(std::string_view src, double* value);

Expand Down
2 changes: 1 addition & 1 deletion src/server/debugcmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ DebugCmd::DebugCmd(ServerFamily* owner, ConnectionContext* cntx) : sf_(*owner),
}

void DebugCmd::Run(CmdArgList args) {
string_view subcmd = ArgS(args, 0);
string subcmd = absl::AsciiStrToUpper(ArgS(args, 0));
if (subcmd == "HELP") {
string_view help_arr[] = {
"DEBUG <subcommand> [<arg> [value] [opt] ...]. Subcommands are:",
Expand Down
3 changes: 1 addition & 2 deletions src/server/main_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1898,8 +1898,7 @@ void Service::Eval(CmdArgList args, ConnectionContext* cntx) {
}

void Service::EvalSha(CmdArgList args, ConnectionContext* cntx) {
ToLower(&args[0]);
string_view sha = ArgS(args, 0);
string sha = absl::AsciiStrToLower(ArgS(args, 0));

BorrowedInterpreter interpreter{cntx};
CallSHA(args, sha, interpreter, cntx);
Expand Down
2 changes: 1 addition & 1 deletion src/server/memory_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ MemoryCmd::MemoryCmd(ServerFamily* owner, ConnectionContext* cntx) : cntx_(cntx)
}

void MemoryCmd::Run(CmdArgList args) {
string_view sub_cmd = ArgS(args, 0);
string sub_cmd = absl::AsciiStrToUpper(ArgS(args, 0));

if (sub_cmd == "HELP") {
string_view help_arr[] = {
Expand Down
2 changes: 1 addition & 1 deletion src/server/script_mgr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ScriptMgr::ScriptKey::ScriptKey(string_view sha) : array{} {
}

void ScriptMgr::Run(CmdArgList args, ConnectionContext* cntx) {
string_view subcmd = ArgS(args, 0);
string subcmd = absl::AsciiStrToUpper(ArgS(args, 0));

if (subcmd == "HELP") {
string_view kHelp[] = {
Expand Down
42 changes: 13 additions & 29 deletions src/server/server_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,7 @@ void ClientSetInfo(CmdArgList args, ConnectionContext* cntx) {
return cntx->SendError("No connection");
}

ToUpper(&args[0]);
string_view type = ArgS(args, 0);
string type = absl::AsciiStrToUpper(ArgS(args, 0));
string_view val = ArgS(args, 1);

if (type == "LIB-NAME") {
Expand Down Expand Up @@ -510,8 +509,7 @@ void ClientKill(CmdArgList args, absl::Span<facade::Listener*> listeners, Connec
};
}
} else if (args.size() == 2) {
ToUpper(&args[0]);
string_view filter_type = ArgS(args, 0);
string filter_type = absl::AsciiStrToUpper(ArgS(args, 0));
string_view filter_value = ArgS(args, 1);
if (filter_type == "ADDR") {
evaluator = [filter_value](facade::Connection* conn) {
Expand Down Expand Up @@ -1842,8 +1840,7 @@ void ServerFamily::Auth(CmdArgList args, ConnectionContext* cntx) {
}

void ServerFamily::Client(CmdArgList args, ConnectionContext* cntx) {
ToUpper(&args[0]);
string_view sub_cmd = ArgS(args, 0);
string sub_cmd = absl::AsciiStrToUpper(ArgS(args, 0));
CmdArgList sub_args = args.subspan(1);

if (sub_cmd == "SETNAME") {
Expand Down Expand Up @@ -1871,8 +1868,7 @@ void ServerFamily::Client(CmdArgList args, ConnectionContext* cntx) {
}

void ServerFamily::Config(CmdArgList args, ConnectionContext* cntx) {
ToUpper(&args[0]);
string_view sub_cmd = ArgS(args, 0);
string sub_cmd = absl::AsciiStrToUpper(ArgS(args, 0));

if (sub_cmd == "HELP") {
string_view help_arr[] = {
Expand All @@ -1896,8 +1892,7 @@ void ServerFamily::Config(CmdArgList args, ConnectionContext* cntx) {
return cntx->SendError(WrongNumArgsError("config|set"));
}

ToLower(&args[1]);
string_view param = ArgS(args, 1);
string param = absl::AsciiStrToLower(ArgS(args, 1));

ConfigRegistry::SetResult result = config_registry.Set(param, ArgS(args, 2));

Expand Down Expand Up @@ -1954,16 +1949,12 @@ void ServerFamily::Config(CmdArgList args, ConnectionContext* cntx) {
}

void ServerFamily::Debug(CmdArgList args, ConnectionContext* cntx) {
ToUpper(&args[0]);

DebugCmd dbg_cmd{this, cntx};

return dbg_cmd.Run(args);
}

void ServerFamily::Memory(CmdArgList args, ConnectionContext* cntx) {
ToUpper(&args[0]);

MemoryCmd mem_cmd{this, cntx};

return mem_cmd.Run(args);
Expand All @@ -1986,8 +1977,7 @@ std::optional<ServerFamily::VersionBasename> ServerFamily::GetVersionAndBasename
bool new_version = absl::GetFlag(FLAGS_df_snapshot_format);

if (args.size() >= 1) {
ToUpper(&args[0]);
string_view sub_cmd = ArgS(args, 0);
string sub_cmd = absl::AsciiStrToUpper(ArgS(args, 0));
if (sub_cmd == "DF") {
new_version = true;
} else if (sub_cmd == "RDB") {
Expand Down Expand Up @@ -2185,11 +2175,10 @@ void ServerFamily::Info(CmdArgList args, ConnectionContext* cntx) {
return cntx->SendError(kSyntaxErr);
}

string_view section;
string section;

if (args.size() == 1) {
ToUpper(&args[0]);
section = ArgS(args, 0);
section = absl::AsciiStrToUpper(ArgS(args, 0));
}

string info;
Expand Down Expand Up @@ -2867,9 +2856,8 @@ void ServerFamily::ReplConf(CmdArgList args, ConnectionContext* cntx) {

for (unsigned i = 0; i < args.size(); i += 2) {
DCHECK_LT(i + 1, args.size());
ToUpper(&args[i]);

std::string_view cmd = ArgS(args, i);
string cmd = absl::AsciiStrToUpper(ArgS(args, i));
std::string_view arg = ArgS(args, i + 1);
if (cmd == "CAPA") {
if (arg == "dragonfly" && args.size() == 2 && i == 0) {
Expand Down Expand Up @@ -2994,8 +2982,6 @@ void ServerFamily::Role(CmdArgList args, ConnectionContext* cntx) {
}

void ServerFamily::Script(CmdArgList args, ConnectionContext* cntx) {
ToUpper(&args.front());

script_mgr_->Run(std::move(args), cntx);
}

Expand All @@ -3010,8 +2996,7 @@ void ServerFamily::LastSave(CmdArgList args, ConnectionContext* cntx) {

void ServerFamily::Latency(CmdArgList args, ConnectionContext* cntx) {
auto* rb = static_cast<RedisReplyBuilder*>(cntx->reply_builder());
ToUpper(&args[0]);
string_view sub_cmd = ArgS(args, 0);
string sub_cmd = absl::AsciiStrToUpper(ArgS(args, 0));

if (sub_cmd == "LATEST") {
return rb->SendEmptyArray();
Expand Down Expand Up @@ -3050,8 +3035,7 @@ void ServerFamily::Dfly(CmdArgList args, ConnectionContext* cntx) {
}

void ServerFamily::SlowLog(CmdArgList args, ConnectionContext* cntx) {
ToUpper(&args[0]);
string_view sub_cmd = ArgS(args, 0);
string sub_cmd = absl::AsciiStrToUpper(ArgS(args, 0));

if (sub_cmd == "HELP") {
string_view help[] = {
Expand Down Expand Up @@ -3095,8 +3079,8 @@ void ServerFamily::SlowLog(CmdArgList args, ConnectionContext* cntx) {
}

void ServerFamily::Module(CmdArgList args, ConnectionContext* cntx) {
ToUpper(&args[0]);
if (ArgS(args, 0) != "LIST")
string sub_cmd = absl::AsciiStrToUpper(ArgS(args, 0));
if (sub_cmd != "LIST")
return cntx->SendError(kSyntaxErr);

auto* rb = static_cast<RedisReplyBuilder*>(cntx->reply_builder());
Expand Down

0 comments on commit 14220a6

Please sign in to comment.