Skip to content

Commit 0d9f56c

Browse files
committed
Add BITPOS command
1 parent 79b5b69 commit 0d9f56c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/Redis.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,38 @@ public function setRange( $key, $offset, $value ) {}
17951795
*/
17961796
public function strlen( $key ) {}
17971797

1798+
/**
1799+
* Return the position of the first bit set to 1 or 0 in a string. The position is returned, thinking of the
1800+
* string as an array of bits from left to right, where the first byte's most significant bit is at position 0,
1801+
* the second byte's most significant bit is at position 8, and so forth.
1802+
* @param string $key
1803+
* @param int $bit
1804+
* @param int $start
1805+
* @param int $end
1806+
* @return int The command returns the position of the first bit set to 1 or 0 according to the request.
1807+
* If we look for set bits (the bit argument is 1) and the string is empty or composed of just
1808+
* zero bytes, -1 is returned. If we look for clear bits (the bit argument is 0) and the string
1809+
* only contains bit set to 1, the function returns the first bit not part of the string on the
1810+
* right. So if the string is three bytes set to the value 0xff the command BITPOS key 0 will
1811+
* return 24, since up to bit 23 all the bits are 1. Basically, the function considers the right
1812+
* of the string as padded with zeros if you look for clear bits and specify no range or the
1813+
* start argument only. However, this behavior changes if you are looking for clear bits and
1814+
* specify a range with both start and end. If no clear bit is found in the specified range, the
1815+
* function returns -1 as the user specified a clear range and there are no 0 bits in that range.
1816+
* @link http://redis.io/commands/bitpos
1817+
* @example
1818+
* <pre>
1819+
* $redis->set('key', '\xff\xff');
1820+
* $redis->bitpos('key', 1); // int(0)
1821+
* $redis->bitpos('key', 1, 1); // int(8)
1822+
* $redis->bitpos('key', 1, 3); // int(-1)
1823+
* $redis->bitpos('key', 0); // int(16)
1824+
* $redis->bitpos('key', 0, 1); // int(16)
1825+
* $redis->bitpos('key', 0, 1, 5); // int(-1)
1826+
* </pre>
1827+
*/
1828+
public function bitpos( $key, $bit, $start = 0, $end = null) {}
1829+
17981830
/**
17991831
* Return a single bit out of a larger string
18001832
*

0 commit comments

Comments
 (0)