GetBit gets the bit at offset in the string value stored at key.
(ctx *Context, txn *db.Transaction)
| 514 | |
| 515 | // GetBit gets the bit at offset in the string value stored at key. |
| 516 | func GetBit(ctx *Context, txn *db.Transaction) (OnCommit, error) { |
| 517 | key := []byte(ctx.Args[0]) |
| 518 | offset, err := strconv.Atoi(ctx.Args[1]) |
| 519 | if err != nil || offset < 0 { |
| 520 | return nil, ErrBitOffset |
| 521 | } |
| 522 | |
| 523 | str, err := txn.String(key) |
| 524 | if err != nil { |
| 525 | if err == db.ErrTypeMismatch { |
| 526 | return nil, ErrTypeMismatch |
| 527 | } |
| 528 | return nil, errors.New("ERR " + err.Error()) |
| 529 | } |
| 530 | |
| 531 | if !str.Exist() { |
| 532 | return Integer(ctx.Out, 0), nil |
| 533 | } |
| 534 | |
| 535 | val, err := str.GetBit(offset) |
| 536 | if err != nil { |
| 537 | return nil, errors.New("ERR " + err.Error()) |
| 538 | } |
| 539 | |
| 540 | if val != 0 { |
| 541 | return Integer(ctx.Out, 1), nil |
| 542 | } |
| 543 | return Integer(ctx.Out, 0), nil |
| 544 | } |
| 545 | |
| 546 | // BitCount counts the number of set bits (population counting) in a string. |
| 547 | func BitCount(ctx *Context, txn *db.Transaction) (OnCommit, error) { |