SetBit sets or clears the bit at offset in the string value stored at key.
(ctx *Context, txn *db.Transaction)
| 476 | |
| 477 | // SetBit sets or clears the bit at offset in the string value stored at key. |
| 478 | func SetBit(ctx *Context, txn *db.Transaction) (OnCommit, error) { |
| 479 | key := []byte(ctx.Args[0]) |
| 480 | offset, err := strconv.Atoi(ctx.Args[1]) |
| 481 | if err != nil { |
| 482 | return nil, ErrBitOffset |
| 483 | } |
| 484 | if offset < 0 { |
| 485 | return nil, ErrBitOffset |
| 486 | } |
| 487 | |
| 488 | on, err := strconv.Atoi(ctx.Args[2]) |
| 489 | if err != nil { |
| 490 | return nil, ErrBitInteger |
| 491 | } |
| 492 | |
| 493 | // Bits can only be set or cleared... |
| 494 | if (on & ^1) != 0 { |
| 495 | return nil, ErrBitInteger |
| 496 | } |
| 497 | |
| 498 | str, err := txn.String(key) |
| 499 | if err != nil { |
| 500 | if err == db.ErrTypeMismatch { |
| 501 | return nil, ErrTypeMismatch |
| 502 | } |
| 503 | return nil, errors.New("ERR " + err.Error()) |
| 504 | } |
| 505 | val, err := str.SetBit(offset, on) |
| 506 | if err != nil { |
| 507 | return nil, errors.New("ERR " + err.Error()) |
| 508 | } |
| 509 | if val != 0 { |
| 510 | return Integer(ctx.Out, 1), nil |
| 511 | } |
| 512 | return Integer(ctx.Out, 0), nil |
| 513 | } |
| 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) { |