QRgen/private/BitArray

  Source   Edit

Bit array implementation

This module implements a simple BitArray object which consists of a regular seq[uint8] and a pos.

There are 2 main procedures, one to add more bits to the BitArray, add, and another to skip to the next byte, nextByte.

For ease of use there are some templates to help write less code, like [], so instead of writing myBitArray.data[i] you would write myBitArray[i], as if BitArray was a regular seq[uint8].

Types

BitArray = object
  pos*: uint16
  data*: seq[uint8]

A BitArray object used by EncodedQRCode.

pos is used to keep track of where new bits will be placed.

  Source   Edit

Procs

func add(self: var BitArray; val: SomeUnsignedInt; len: uint8)
Add len amount of bits from val starting from the rightmost bit.

Example:

var myBitArray = newBitArray(1)
myBitArray.add 0b110011'u8, 4 # should add 0011, not 110011
assert myBitArray[0] == 0b0011_0000
#                              ^ pos will be here
  Source   Edit
func newBitArray(size: uint16): BitArray {....raises: [], tags: [].}
Creates a new BitArray object whose data will have a cap of size and it's len will also be set to size.   Source   Edit
func nextByte(self: var BitArray): uint8 {....raises: [], tags: [].}

Moves pos to the next byte, unless it is pointing to the start of an empty byte.

Example:

myBitArray.data:
0101_0110, 1010_0000, 0000_0000
                ^ pos points here

after using nextByte():

myBitArray.data:
0101_0110, 1010_0000, 0000_0000
                      ^ pos points here
  Source   Edit

Templates

template `[]=`(self: var BitArray; i: SomeInteger; val: uint8)
Set the value of data in index i to val.   Source   Edit
template `[]`(self: BitArray; i: SomeInteger): uint8
Get the value of data in index i.   Source   Edit
template `[]`[T, S](self: BitArray; i: HSlice[T, S]): seq[uint8]
Get the values of data in the slice i.   Source   Edit
template len(self: BitArray): int
Get the len of self.data. Used nstead of writing data.data.len in other modules.   Source   Edit
template unsafeAdd(self: var BitArray; val: uint8)
Add val to the end of data.
Warning: This is unsafe since, by default, data's len is set to it's max capacity and pos is not updated.
  Source   Edit
template unsafeDelete(self: var BitArray; pos: uint16)
Remove the value from data in position pos.
Warning: This is unsafe since pos is not updated.
  Source   Edit
template unsafeSet(self: var BitArray; pos: uint16 | BackwardsIndex; val: uint8)
Sets the value from data in position pos to val.
Warning: This is unsafe since you may be setting the value of bits not yet reached by pos, which would then be overwritten.
  Source   Edit