diff options
author | michael-grunder <michael.grunder@gmail.com> | 2022-10-13 21:36:48 +0300 |
---|---|---|
committer | Michael Grunder <michael.grunder@gmail.com> | 2022-10-13 21:53:57 +0300 |
commit | 4d8afd387ed2c8685a92f046ac84467a00199db8 (patch) | |
tree | 15ceb17f5b8455a8c09d92b17172d3c1a084b423 /redis_commands.c | |
parent | 872ae1079f8a3f61153e9aa74ab95ba35b706e5b (diff) |
Refactor BITPOS and implement BIT/BYTE option.
Update BITPOS to use the new argument parsing macros as well as
implement Redis 7.0.0's BIT/BYTE modifier.
Additionally I changed `int $bit` to `bool $bit` as its a more
appropriate datatype.
See #2068
Diffstat (limited to 'redis_commands.c')
-rw-r--r-- | redis_commands.c | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/redis_commands.c b/redis_commands.c index 3a49e48a..e415ae5c 100644 --- a/redis_commands.c +++ b/redis_commands.c @@ -2523,38 +2523,41 @@ int redis_restore_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, return SUCCESS; } -/* BITPOS */ +/* BITPOS key bit [start [end [BYTE | BIT]]] */ int redis_bitpos_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, char **cmd, int *cmd_len, short *slot, void **ctx) { - char *key; - int argc; - zend_long bit, start, end; - size_t key_len; + zend_long start = 0, end = -1; + zend_bool bit = 0, bybit = 0; + smart_string cmdstr = {0}; + zend_string *key = NULL; - argc = ZEND_NUM_ARGS(); - if (zend_parse_parameters(argc, "sl|ll", &key, &key_len, &bit, - &start, &end) == FAILURE) - { - return FAILURE; - } + ZEND_PARSE_PARAMETERS_START(2, 5) + Z_PARAM_STR(key) + Z_PARAM_BOOL(bit) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(start) + Z_PARAM_LONG(end) + Z_PARAM_BOOL(bybit) + ZEND_PARSE_PARAMETERS_END_EX(return FAILURE); - // Prevalidate bit - if (bit != 0 && bit != 1) { - return FAILURE; - } + REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 2 + (ZEND_NUM_ARGS() > 2 ? 2 : 0) + !!bybit, "BITPOS"); - // Construct command based on arg count - if (argc == 2) { - *cmd_len = REDIS_CMD_SPPRINTF(cmd, "BITPOS", "kd", key, key_len, bit); - } else if (argc == 3) { - *cmd_len = REDIS_CMD_SPPRINTF(cmd, "BITPOS", "kdd", key, key_len, bit, - start); - } else { - *cmd_len = REDIS_CMD_SPPRINTF(cmd, "BITPOS", "kddd", key, key_len, bit, - start, end); + redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot); + redis_cmd_append_sstr_long(&cmdstr, bit); + + /* Start and length if we were passed either */ + if (ZEND_NUM_ARGS() > 2) { + redis_cmd_append_sstr_long(&cmdstr, start); + redis_cmd_append_sstr_long(&cmdstr, end); } + /* Finally, BIT or BYTE if we were passed that argument */ + REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, !!bybit, "BIT"); + + *cmd = cmdstr.c; + *cmd_len = cmdstr.len; + return SUCCESS; } |