Skip to content

Commit

Permalink
Add new function printable_string
Browse files Browse the repository at this point in the history
Signed-off-by: Ze Gan <[email protected]>
  • Loading branch information
Pterosaur committed Jul 11, 2023
1 parent f3c3046 commit ddb4e6d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
11 changes: 11 additions & 0 deletions common/rediscommand.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <vector>
#include <hiredis/hiredis.h>
#include "rediscommand.h"
#include "stringutility.h"

using namespace std;

Expand Down Expand Up @@ -122,6 +123,16 @@ void RedisCommand::formatDEL(const std::string& key)
return format("DEL %s", key.c_str());
}

int RedisCommand::appendTo(RedisContext *ctx) const
{
return redisAppendFormattedCommand(ctx, c_str(), length());
}

std::string RedisCommand::printable_string() const
{
return binary_to_printable(std::string(temp, len));
}

const char *RedisCommand::c_str() const
{
return temp;
Expand Down
8 changes: 7 additions & 1 deletion common/rediscommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ typedef std::tuple<std::string, std::string, std::vector<FieldValueTuple> > KeyO
#define kfvOp std::get<1>
#define kfvFieldsValues std::get<2>

class RedisContext;

class RedisCommand {
public:
RedisCommand();
Expand Down Expand Up @@ -64,11 +66,15 @@ class RedisCommand {
/* Format DEL key command */
void formatDEL(const std::string& key);

int appendTo(RedisContext *ctx) const;

std::string printable_string() const;

private:
const char *c_str() const;

size_t length() const;

private:
char *temp;
int len;
};
Expand Down
2 changes: 1 addition & 1 deletion common/redispipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class RedisPipeline {
case REDIS_REPLY_STATUS:
case REDIS_REPLY_INTEGER:
{
int rc = redisAppendFormattedCommand(m_db->getContext(), command.c_str(), command.length());
int rc = command.appendTo(m_db->getContext());
if (rc != REDIS_OK)
{
// The only reason of error is REDIS_ERR_OOM (Out of memory)
Expand Down
12 changes: 6 additions & 6 deletions common/redisreply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ inline void guard(FUNC func, const char* command)

RedisReply::RedisReply(RedisContext *ctx, const RedisCommand& command)
{
int rc = redisAppendFormattedCommand(ctx->getContext(), command.c_str(), command.length());
int rc = command.appendTo(ctx->getContext());
if (rc != REDIS_OK)
{
// The only reason of error is REDIS_ERR_OOM (Out of memory)
Expand All @@ -91,12 +91,12 @@ RedisReply::RedisReply(RedisContext *ctx, const RedisCommand& command)
{
throw RedisError("Failed to redisGetReply with " + string(command.c_str()), ctx->getContext());
}
guard([&]{checkReply();}, command.c_str());
guard([&]{checkReply();}, command.printable_string().c_str());
}

RedisReply::RedisReply(RedisContext *ctx, const string& command)
{
int rc = redisAppendCommand(ctx->getContext(), command.c_str());
int rc = redisAppendFormattedCommand(ctx->getContext(), command.c_str(), command.length());
if (rc != REDIS_OK)
{
// The only reason of error is REDIS_ERR_OOM (Out of memory)
Expand All @@ -109,19 +109,19 @@ RedisReply::RedisReply(RedisContext *ctx, const string& command)
{
throw RedisError("Failed to redisGetReply with " + command, ctx->getContext());
}
guard([&]{checkReply();}, command.c_str());
guard([&]{checkReply();}, binary_to_printable(command).c_str());
}

RedisReply::RedisReply(RedisContext *ctx, const RedisCommand& command, int expectedType)
: RedisReply(ctx, command)
{
guard([&]{checkReplyType(expectedType);}, command.c_str());
guard([&]{checkReplyType(expectedType);}, command.printable_string().c_str());
}

RedisReply::RedisReply(RedisContext *ctx, const string& command, int expectedType)
: RedisReply(ctx, command)
{
guard([&]{checkReplyType(expectedType);}, command.c_str());
guard([&]{checkReplyType(expectedType);}, command.printable_string().c_str());
}

RedisReply::RedisReply(redisReply *reply) :
Expand Down
54 changes: 54 additions & 0 deletions common/stringutility.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,58 @@ static inline std::string binary_to_hex(const void *buffer, size_t length)
return s;
}

static inline std::string binary_to_printable(const std::string &s)
{
std::string printable;
printable.reserve(s.length() * 4);

for (char c: s)
{
if (std::isprint(c))
{
if (c == '\\')
{
printable.push_back('\\');
}
else
{
printable.push_back(c);
}
}
else if (std::isspace(c))
{
printable.push_back('\\');
if (c == '\n')
{
printable.push_back('n');
}
else if (c == '\r')
{
printable.push_back('r');
}
else if (c == '\t')
{
printable.push_back('t');
}
else if (c == '\v')
{
printable.push_back('v');
}
else if (c == '\f')
{
printable.push_back('f');
}
}
else
{
printable.push_back('\\');
printable.push_back('x');
printable.push_back("0123456789ABCDEF"[c >> 4]);
printable.push_back("0123456789ABCDEF"[c & 0xf]);
}
}

return printable;
}

}

0 comments on commit ddb4e6d

Please sign in to comment.