Documentation
¶
Index ¶
- func ParseByte1(data []byte, bitStartIndex uint16) (byte, error)
- func ParseByte2(data []byte, bitStartIndex uint16) (byte, error)
- func ParseByte4(data []byte, bitStartIndex uint16) (byte, error)
- func ParseByte6(data []byte, bitStartIndex uint16) (byte, error)
- func ParseByte8(data []byte, bitStartIndex uint16) (byte, error)
- func ParseUInt12(data []byte, bitStartIndex uint16) (uint16, error)
- func ParseUInt16(data []byte, bitStartIndex uint16) (uint16, error)
- type BitStream
- func (bs *BitStream) Base64Encode() []byte
- func (bs *BitStream) GetPosition() uint16
- func (bs *BitStream) Len() uint16
- func (bs *BitStream) ReadByte1() (byte, error)
- func (bs *BitStream) ReadByte2() (byte, error)
- func (bs *BitStream) ReadByte4() (byte, error)
- func (bs *BitStream) ReadByte6() (byte, error)
- func (bs *BitStream) ReadByte8() (byte, error)
- func (bs *BitStream) ReadFibonacciInt() (uint16, error)
- func (bs *BitStream) ReadFibonacciRange() (*IntRange, error)
- func (bs *BitStream) ReadIntRange() (*IntRange, error)
- func (bs *BitStream) ReadTwoBitField(numFields int) ([]byte, error)
- func (bs *BitStream) ReadUInt12() (uint16, error)
- func (bs *BitStream) ReadUInt16() (uint16, error)
- func (bs *BitStream) Reset()
- func (bs *BitStream) SetPosition(pos uint16)
- func (bs *BitStream) WriteByte1(b byte)
- func (bs *BitStream) WriteByte2(b byte)
- func (bs *BitStream) WriteByte4(b byte)
- func (bs *BitStream) WriteByte6(b byte)
- func (bs *BitStream) WriteByte8(b byte)
- func (bs *BitStream) WriteFibonacciInt(num uint16) error
- func (bs *BitStream) WriteIntRange(intRange *IntRange) error
- func (bs *BitStream) WriteTwoBitField(bList []byte)
- func (bs *BitStream) WriteUInt12(b uint16)
- func (bs *BitStream) WriteUInt16(b uint16)
- type IRange
- type IntRange
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseByte1 ¶
ParseByte1 parses 1 bit of data from the data array, starting at the given index
func ParseByte2 ¶ added in v0.2.0
ParseByte2 parses 2 bits of data from the data array, starting at the given index
func ParseByte4 ¶
ParseByte4 parses 4 bits of data from the data array, starting at the given index
func ParseByte6 ¶
ParseByte6 parses 6 bits of data from the data array, starting at the given index
func ParseByte8 ¶
ParseByte8 parses 8 bits of data from the data array, starting at the given index
func ParseUInt12 ¶
ParseUInt12 parses 12 bits of data from the data array, starting at the given index
Types ¶
type BitStream ¶
type BitStream struct {
// contains filtered or unexported fields
}
func NewBitStream ¶
NewBitStream creates a new bitstream object
func NewBitStreamForWrite ¶ added in v0.2.0
func NewBitStreamForWrite() *BitStream
func NewBitStreamFromBase64 ¶
NewBitStreamFromBase64 creates a new bit stream object from a base64-url encoded string
func (*BitStream) Base64Encode ¶ added in v0.2.0
Base64Encode applies base64 encoding on the data in buffer.
func (*BitStream) GetPosition ¶
GetPosition reads out the position of the bit pointer in the bit stream
func (*BitStream) ReadByte2 ¶ added in v0.2.0
ReadByte2 reads 2 bits fron the bitstream, advancing the pointer
func (*BitStream) ReadFibonacciInt ¶
ReadFibonacciInt parses 1 fibonacci encoded int from the data array, starting at the given index Note that 0 cannot be fibonacci encoded, and thus will only be seen if there is an error.
func (*BitStream) ReadFibonacciRange ¶
ReadFibonacciRange
func (*BitStream) ReadIntRange ¶
ReadIntRange parses a Range(Int) and returns an IntRange struct
func (*BitStream) ReadTwoBitField ¶ added in v0.2.0
func (*BitStream) ReadUInt12 ¶
ReadUInt12 reads 12 bits from the bitstream, advancing the pointer
func (*BitStream) ReadUInt16 ¶
ReadUInt16 reads 16 bits from the bitstream, advancing the pointer
func (*BitStream) Reset ¶ added in v0.2.0
func (bs *BitStream) Reset()
Reset clears all the data a BitStream holds.
func (*BitStream) SetPosition ¶
SetPosition sets the position of the bit pointer in the bit stream
func (*BitStream) WriteByte1 ¶ added in v0.2.0
WriteByte1 writes the rightmost bit in b into the BitStream.
func (*BitStream) WriteByte2 ¶ added in v0.2.0
WriteByte2 writes the rightmost 2 bits in b into the BitStream.
func (*BitStream) WriteByte4 ¶ added in v0.2.0
WriteByte4 writes the rightmost 4 bits in b into the BitStream.
func (*BitStream) WriteByte6 ¶ added in v0.2.0
WriteByte6 writes the rightmost 6 bits in b into the BitStream.
func (*BitStream) WriteByte8 ¶ added in v0.2.0
WriteByte8 writes a full byte b into the BitStream.
func (*BitStream) WriteFibonacciInt ¶ added in v0.2.0
WriteFibonacciInt writes int based on Fibonacci Coding https://en.wikipedia.org/wiki/Fibonacci_coding.
By definition, Fibonacci numbers are numbers that are the sum of the two Fibonacci numbers that come before. The numbers are [excluding 0, 1,] 1 (0+1), 2 (1+1), 3 (1+2), 5 (2+3), 8 (3+5), etc. All of the integers that are larger and equal to 0 are able to split into a combination of at least one Fibonacci numbers. For instance, 6 refers to 1+5, while 7 refers to 2+5, where 1, 2, 5 are Fibonacci numbers.
To encode an integer using Fibonacci Coding, just follow these steps: 1.Find the largest Fibonacci number equal to or less than N; subtract this number from N, keeping track of the remainder. 2.If the number subtracted was the i-th Fibonacci number F(i), put a 1 in place i−2 in the code word (counting the left-most digit as place 0). 3.Repeat the previous steps, substituting the remainder for N, until a remainder of 0 is reached. 4.Place an additional 1 after the rightmost digit in the code word.
So a Fibonacci encoded bit sequence is all about marking the corresponding bit as 1 while leaving the others as 0. Here are some examples:
FibonacciInt(1) = 11 1-> (1*1)->1 FibonacciInt(2) = 011 2-> (1*1+2*1)->01 FibonacciInt(3) = 0011 3-> (1*0+2*0+3*1)->001 FibonacciInt(4) = 1011 4-> (1*1+2*0+3*1)->101 FibonacciInt(12) = 101011 12->(1*1+2*0+3*1+5*0+8*1)->10101
According to the algorithm, no sequential ones can be found in the bit sequence, and the final bit is always a single 1, which makes it easy to extract several continuous Fibonacci encoded integers out from a bit stream if we place an additional 1 in the end, since 11 could be an end-of-sequence indicator. The advantage of this is, that a sequence of numbers can be encoded into a sequence of bits without the need to know the length of the bit sequences in advance.
func (*BitStream) WriteIntRange ¶ added in v0.2.0
WriteIntRange writes an IntRange object into the BitStream.
The basic idea is 12 bits for size, 1 bit indicating if a range refers to a single number or not, followed by one or two Fibonacci encoded numbers, and then followed by other ranges fell behind.
For example, an IntRange consisting of [1, 1], [3, 4] would be encoded into 000000000010 0 11 1 0011 11. There are two ranges, so the size is encoded into 000000000010. The first range [1, 1] refers to a single integer 1, so the indicator is 0, followed by Fibonacci encoded 1, while the next range [3, 4] refers to a range containing not only one integer, so the indicator is 1, followed by Fibonacci encoded 3 and 1 (4-3).
func (*BitStream) WriteTwoBitField ¶ added in v0.2.0
WriteTwoBitField encapsulates WriteByte2 to get convenience to some extent, when encoding GPP strings.
func (*BitStream) WriteUInt12 ¶ added in v0.2.0
WriteUInt12 writes the rightmost 12 bits in b into the BitStream. For instance, the input b is 0xff01, the effective part refers to the rightmost 12 bits, which should be 0xf01.
func (*BitStream) WriteUInt16 ¶ added in v0.2.0
WriteUInt16 writes a full uint16 (two bytes) into the BitStream.