MCPcopy Create free account
hub / github.com/IceFireDB/IceFireDB / cmdBITCOUNT

Function cmdBITCOUNT

strings.go:636–659  ·  view source on GitHub ↗

cmdBITCOUNT handles the BITCOUNT command, which counts the number of set bits (1s) in a string. The command can optionally take start and end indices to limit the range of bits to count, and an additional argument to specify whether the indices are in bytes or bits.

(m uhaha.Machine, args []string)

Source from the content-addressed store, hash-verified

634// cmdBITCOUNT handles the BITCOUNT command, which counts the number of set bits (1s) in a string.
635// The command can optionally take start and end indices to limit the range of bits to count,
636// and an additional argument to specify whether the indices are in bytes or bits.
637func cmdBITCOUNT(m uhaha.Machine, args []string) (interface{}, error) {
638 // Validate the number of arguments provided by the client.
639 if len(args) < 2 || len(args) > 5 {
640 return nil, uhaha.ErrWrongNumArgs
641 }
642
643 // Convert the key from string to byte slice for database operations.
644 key := []byte(args[1])
645
646 // Parse the optional start and end indices and the optional BYTE/BIT argument.
647 start, end, bitMode, err := parseBitRange(args[2:])
648 if err != nil {
649 return nil, err
650 }
651
652 // Count the number of set bits within the specified range using the ldb.BitCount function.
653 n, err := ldb.BitCount(key, start, end, bitMode)
654 if err != nil {
655 return nil, err
656 }
657
658 // Return the count as a simple integer response.
659 return redcon.SimpleInt(n), nil
660}
661
662// expireSecondsFromMillis converts a millisecond TTL into the whole seconds the

Callers

nothing calls this directly

Calls 1

parseBitRangeFunction · 0.85

Tested by

no test coverage detected