Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a3d2f13

Browse files
Add 'BIT'/'BYTE' modifier to BITCOUNT + tests (#2149)
BITCOUNT can take a third optional argument ('BIT', or 'BYTE'). Additionally add a specific test for BITCOUNT that tests all of the legal ways it can be called in PhpRedis.
1 parent b3ce048 commit a3d2f13

5 files changed

Lines changed: 39 additions & 7 deletions

File tree

redis.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function bgSave(): bool;
4242
public function bgrewriteaof(): bool;
4343

4444
/** @return int|Redis */
45-
public function bitcount(string $key, int $start = 0, int $end = -1);
45+
public function bitcount(string $key, int $start = 0, int $end = -1, bool $bybit = false);
4646

4747
/**
4848
* @return int|Redis

redis_arginfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 8d3ef188b058066309394ffaaf00489572d7b629 */
2+
* Stub hash: b9da355c27e6fb1b776164d40a521703e31713b5 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null")
@@ -53,6 +53,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_bitcount, 0, 0, 1)
5353
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
5454
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, start, IS_LONG, 0, "0")
5555
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, end, IS_LONG, 0, "-1")
56+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, bybit, _IS_BOOL, 0, "false")
5657
ZEND_END_ARG_INFO()
5758

5859
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_bitop, 0, 3, IS_LONG, 0)

redis_commands.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,15 +2267,21 @@ int redis_bitcount_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
22672267
char *key;
22682268
size_t key_len;
22692269
zend_long start = 0, end = -1;
2270+
zend_bool isbit = 0;
22702271

2271-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ll", &key, &key_len,
2272-
&start, &end) == FAILURE)
2272+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|llb", &key, &key_len,
2273+
&start, &end, &isbit) == FAILURE)
22732274
{
22742275
return FAILURE;
22752276
}
22762277

2277-
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "BITCOUNT", "kdd", key, key_len,
2278-
(int)start, (int)end);
2278+
if (isbit) {
2279+
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "BITCOUNT", "kdds", key, key_len,
2280+
(int)start, (int)end, "BIT", 3);
2281+
} else {
2282+
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "BITCOUNT", "kdd", key, key_len,
2283+
(int)start, (int)end);
2284+
}
22792285

22802286
return SUCCESS;
22812287
}

redis_legacy_arginfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 8d3ef188b058066309394ffaaf00489572d7b629 */
2+
* Stub hash: b9da355c27e6fb1b776164d40a521703e31713b5 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_INFO(0, options)
@@ -48,6 +48,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_bitcount, 0, 0, 1)
4848
ZEND_ARG_INFO(0, key)
4949
ZEND_ARG_INFO(0, start)
5050
ZEND_ARG_INFO(0, end)
51+
ZEND_ARG_INFO(0, bybit)
5152
ZEND_END_ARG_INFO()
5253

5354
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_bitop, 0, 0, 3)

tests/RedisTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,30 @@ public function testPubSub() {
206206
$this->assertFalse($this->redis->pubsub("numsub", "not-an-array"));
207207
}
208208

209+
/* These test cases were generated randomly. We're just trying to test
210+
that PhpRedis handles all combination of arguments correctly. */
211+
public function testBitcount() {
212+
/* key */
213+
$this->redis->set('bitcountkey', hex2bin('bd906b854ca76cae'));
214+
$this->assertEquals(33, $this->redis->bitcount('bitcountkey'));
215+
216+
/* key, start */
217+
$this->redis->set('bitcountkey', hex2bin('400aac171382a29bebaab554f178'));
218+
$this->assertEquals(4, $this->redis->bitcount('bitcountkey', 13));
219+
220+
/* key, start, end */
221+
$this->redis->set('bitcountkey', hex2bin('b1f32405'));
222+
$this->assertEquals(2, $this->redis->bitcount('bitcountkey', 3, 3));
223+
224+
/* key, start, end BYTE */
225+
$this->redis->set('bitcountkey', hex2bin('10eb8939e68bfdb640260f0629f3'));
226+
$this->assertEquals(1, $this->redis->bitcount('bitcountkey', 8, 8, false));
227+
228+
/* key, start, end, BIT */
229+
$this->redis->set('bitcountkey', hex2bin('cd0e4c80f9e4590d888a10'));
230+
$this->assertEquals(5, $this->redis->bitcount('bitcountkey', 0, 9, true));
231+
}
232+
209233
public function testBitsets() {
210234

211235
$this->redis->del('key');

0 commit comments

Comments
 (0)