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

Skip to content

Commit 2c1875c

Browse files
committed
Add BITCOUNT.
Included docs.
1 parent 2a3a7ea commit 2c1875c

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

README.markdown

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,16 @@ Bitwise operation on multiple keys.
15921592
##### *Return value*
15931593
*LONG*: The size of the string stored in the destination key.
15941594

1595+
## bitcount
1596+
##### *Description*
1597+
Count bits in a string.
1598+
1599+
##### *Parameters*
1600+
*key*
1601+
1602+
##### *Return value*
1603+
*LONG*: The number of bits set to 1 in the value behind the input key.
1604+
15951605
## flushDB
15961606

15971607
##### *Description*

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ PHP_METHOD(Redis, bgrewriteaof);
127127
PHP_METHOD(Redis, slaveof);
128128
PHP_METHOD(Redis, object);
129129
PHP_METHOD(Redis, bitop);
130+
PHP_METHOD(Redis, bitcount);
130131

131132
PHP_METHOD(Redis, mset);
132133
PHP_METHOD(Redis, msetnx);

redis.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ static zend_function_entry redis_functions[] = {
155155
PHP_ME(Redis, slaveof, NULL, ZEND_ACC_PUBLIC)
156156
PHP_ME(Redis, object, NULL, ZEND_ACC_PUBLIC)
157157
PHP_ME(Redis, bitop, NULL, ZEND_ACC_PUBLIC)
158+
PHP_ME(Redis, bitcount, NULL, ZEND_ACC_PUBLIC)
158159

159160
/* 1.1 */
160161
PHP_ME(Redis, mset, NULL, ZEND_ACC_PUBLIC)
@@ -676,6 +677,40 @@ PHP_METHOD(Redis, bitop)
676677
}
677678
/* }}} */
678679

680+
/* {{{ proto boolean Redis::bitcount(string key, [int start], [int end])
681+
*/
682+
PHP_METHOD(Redis, bitcount)
683+
{
684+
zval *object;
685+
RedisSock *redis_sock;
686+
char *key = NULL, *cmd;
687+
int key_len, cmd_len, key_free;
688+
long start = 0, end = -1;
689+
690+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|ll",
691+
&object, redis_ce,
692+
&key, &key_len, &start, &end) == FAILURE) {
693+
RETURN_FALSE;
694+
}
695+
696+
if (redis_sock_get(object, &redis_sock TSRMLS_CC, 0) < 0) {
697+
RETURN_FALSE;
698+
}
699+
700+
/* BITCOUNT key start end */
701+
key_free = redis_key_prefix(redis_sock, &key, &key_len TSRMLS_CC);
702+
cmd_len = redis_cmd_format_static(&cmd, "BITCOUNT", "sdd", key, key_len, (int)start, (int)end);
703+
if(key_free) efree(key);
704+
705+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
706+
IF_ATOMIC() {
707+
redis_long_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
708+
}
709+
REDIS_PROCESS_RESPONSE(redis_long_response);
710+
711+
}
712+
/* }}} */
713+
679714
/* {{{ proto boolean Redis::close()
680715
*/
681716
PHP_METHOD(Redis, close)

0 commit comments

Comments
 (0)