parent
afc462d35d
commit
37a840dc2c
@ -68,7 +68,7 @@ When you need to pass binary safe strings in a command, the `%b` specifier can b
|
|||||||
used. Together with a pointer to the string, it requires a `size_t` length argument
|
used. Together with a pointer to the string, it requires a `size_t` length argument
|
||||||
of the string:
|
of the string:
|
||||||
|
|
||||||
reply = redisCommand(context, "SET foo %b", value, valuelen);
|
reply = redisCommand(context, "SET foo %b", value, (size_t) valuelen);
|
||||||
|
|
||||||
Internally, Hiredis splits the command in different arguments and will
|
Internally, Hiredis splits the command in different arguments and will
|
||||||
convert it to the protocol used to communicate with Redis.
|
convert it to the protocol used to communicate with Redis.
|
||||||
|
@ -32,7 +32,7 @@ int main(void) {
|
|||||||
freeReplyObject(reply);
|
freeReplyObject(reply);
|
||||||
|
|
||||||
/* Set a key using binary safe API */
|
/* Set a key using binary safe API */
|
||||||
reply = redisCommand(c,"SET %b %b", "bar", 3, "hello", 5);
|
reply = redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5);
|
||||||
printf("SET (binary API): %s\n", reply->str);
|
printf("SET (binary API): %s\n", reply->str);
|
||||||
freeReplyObject(reply);
|
freeReplyObject(reply);
|
||||||
|
|
||||||
|
@ -917,7 +917,7 @@ err:
|
|||||||
* %b represents a binary safe string
|
* %b represents a binary safe string
|
||||||
*
|
*
|
||||||
* When using %b you need to provide both the pointer to the string
|
* When using %b you need to provide both the pointer to the string
|
||||||
* and the length in bytes. Examples:
|
* and the length in bytes as a size_t. Examples:
|
||||||
*
|
*
|
||||||
* len = redisFormatCommand(target, "GET %s", mykey);
|
* len = redisFormatCommand(target, "GET %s", mykey);
|
||||||
* len = redisFormatCommand(target, "SET %s %b", mykey, myval, myvallen);
|
* len = redisFormatCommand(target, "SET %s %b", mykey, myval, myvallen);
|
||||||
|
8
test.c
8
test.c
@ -130,13 +130,13 @@ static void test_format_commands(void) {
|
|||||||
free(cmd);
|
free(cmd);
|
||||||
|
|
||||||
test("Format command with %%b string interpolation: ");
|
test("Format command with %%b string interpolation: ");
|
||||||
len = redisFormatCommand(&cmd,"SET %b %b","foo",3,"b\0r",3);
|
len = redisFormatCommand(&cmd,"SET %b %b","foo",(size_t)3,"b\0r",(size_t)3);
|
||||||
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nb\0r\r\n",len) == 0 &&
|
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nb\0r\r\n",len) == 0 &&
|
||||||
len == 4+4+(3+2)+4+(3+2)+4+(3+2));
|
len == 4+4+(3+2)+4+(3+2)+4+(3+2));
|
||||||
free(cmd);
|
free(cmd);
|
||||||
|
|
||||||
test("Format command with %%b and an empty string: ");
|
test("Format command with %%b and an empty string: ");
|
||||||
len = redisFormatCommand(&cmd,"SET %b %b","foo",3,"",0);
|
len = redisFormatCommand(&cmd,"SET %b %b","foo",(size_t)3,"",(size_t)0);
|
||||||
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$0\r\n\r\n",len) == 0 &&
|
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$0\r\n\r\n",len) == 0 &&
|
||||||
len == 4+4+(3+2)+4+(3+2)+4+(0+2));
|
len == 4+4+(3+2)+4+(3+2)+4+(0+2));
|
||||||
free(cmd);
|
free(cmd);
|
||||||
@ -182,7 +182,7 @@ static void test_format_commands(void) {
|
|||||||
FLOAT_WIDTH_TEST(double);
|
FLOAT_WIDTH_TEST(double);
|
||||||
|
|
||||||
test("Format command with invalid printf format: ");
|
test("Format command with invalid printf format: ");
|
||||||
len = redisFormatCommand(&cmd,"key:%08p %b",(void*)1234,"foo",3);
|
len = redisFormatCommand(&cmd,"key:%08p %b",(void*)1234,"foo",(size_t)3);
|
||||||
test_cond(len == -1);
|
test_cond(len == -1);
|
||||||
|
|
||||||
const char *argv[3];
|
const char *argv[3];
|
||||||
@ -335,7 +335,7 @@ static void test_blocking_connection(struct config config) {
|
|||||||
freeReplyObject(reply);
|
freeReplyObject(reply);
|
||||||
|
|
||||||
test("%%b String interpolation works: ");
|
test("%%b String interpolation works: ");
|
||||||
reply = redisCommand(c,"SET %b %b","foo",3,"hello\x00world",11);
|
reply = redisCommand(c,"SET %b %b","foo",(size_t)3,"hello\x00world",(size_t)11);
|
||||||
freeReplyObject(reply);
|
freeReplyObject(reply);
|
||||||
reply = redisCommand(c,"GET foo");
|
reply = redisCommand(c,"GET foo");
|
||||||
test_cond(reply->type == REDIS_REPLY_STRING &&
|
test_cond(reply->type == REDIS_REPLY_STRING &&
|
||||||
|
Loading…
Reference in New Issue
Block a user