Skip to content

Commit

Permalink
Clean code in redis_reply (#1085)
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice authored Nov 12, 2022
1 parent f80d428 commit dc28d12
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/server/redis_reply.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ std::string Integer(int64_t data) { return ":" + std::to_string(data) + CRLF; }

std::string BulkString(const std::string &data) { return "$" + std::to_string(data.length()) + CRLF + data + CRLF; }

std::string NilString() { return "$-1\r\n"; }
std::string NilString() { return "$-1" CRLF; }

std::string MultiLen(int64_t len) { return "*" + std::to_string(len) + "\r\n"; }
std::string MultiLen(int64_t len) { return "*" + std::to_string(len) + CRLF; }

std::string MultiBulkString(const std::vector<std::string> &values, bool output_nil_for_empty_string) {
std::string result = "*" + std::to_string(values.size()) + CRLF;
for (size_t i = 0; i < values.size(); i++) {
if (values[i].empty() && output_nil_for_empty_string) {
for (const auto &value : values) {
if (value.empty() && output_nil_for_empty_string) {
result += NilString();
} else {
result += BulkString(values[i]);
result += BulkString(value);
}
}
return result;
Expand All @@ -71,14 +71,6 @@ std::string Array(const std::vector<std::string> &list) {
return result;
}

std::string Command2RESP(const std::vector<std::string> &cmd_args) {
std::string output;
output.append("*" + std::to_string(cmd_args.size()) + CRLF);
for (const auto &arg : cmd_args) {
output.append("$" + std::to_string(arg.size()) + CRLF);
output.append(arg + CRLF);
}
return output;
}
std::string Command2RESP(const std::vector<std::string> &cmd_args) { return MultiBulkString(cmd_args, false); }

} // namespace Redis

0 comments on commit dc28d12

Please sign in to comment.