...
1 package aztec
2
3 import (
4 "fmt"
5
6 "github.com/boombuler/barcode/utils"
7 )
8
9 type token interface {
10 fmt.Stringer
11 prev() token
12 appendTo(bits *utils.BitList, text []byte)
13 }
14
15 type simpleToken struct {
16 token
17 value int
18 bitCount byte
19 }
20
21 type binaryShiftToken struct {
22 token
23 bShiftStart int
24 bShiftByteCnt int
25 }
26
27 func newSimpleToken(prev token, value int, bitCount byte) token {
28 return &simpleToken{prev, value, bitCount}
29 }
30 func newShiftToken(prev token, bShiftStart int, bShiftCnt int) token {
31 return &binaryShiftToken{prev, bShiftStart, bShiftCnt}
32 }
33
34 func (st *simpleToken) prev() token {
35 return st.token
36 }
37 func (st *simpleToken) appendTo(bits *utils.BitList, text []byte) {
38 bits.AddBits(st.value, st.bitCount)
39 }
40 func (st *simpleToken) String() string {
41 value := st.value & ((1 << st.bitCount) - 1)
42 value |= 1 << st.bitCount
43 return "<" + fmt.Sprintf("%b", value)[1:] + ">"
44 }
45
46 func (bst *binaryShiftToken) prev() token {
47 return bst.token
48 }
49 func (bst *binaryShiftToken) appendTo(bits *utils.BitList, text []byte) {
50 for i := 0; i < bst.bShiftByteCnt; i++ {
51 if i == 0 || (i == 31 && bst.bShiftByteCnt <= 62) {
52
53
54 bits.AddBits(31, 5)
55 if bst.bShiftByteCnt > 62 {
56 bits.AddBits(bst.bShiftByteCnt-31, 16)
57 } else if i == 0 {
58
59 if bst.bShiftByteCnt < 31 {
60 bits.AddBits(bst.bShiftByteCnt, 5)
61 } else {
62 bits.AddBits(31, 5)
63 }
64 } else {
65
66 bits.AddBits(bst.bShiftByteCnt-31, 5)
67 }
68 }
69 bits.AddByte(text[bst.bShiftStart+i])
70 }
71 }
72
73 func (bst *binaryShiftToken) String() string {
74 return fmt.Sprintf("<%d::%d>", bst.bShiftStart, (bst.bShiftStart + bst.bShiftByteCnt - 1))
75 }
76
View as plain text