|
|
@@ -4,14 +4,25 @@ import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"github.com/boombuler/barcode"
|
|
|
+ "strings"
|
|
|
)
|
|
|
|
|
|
-var alphaNumericTable map[byte]int = map[byte]int{
|
|
|
- '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
|
|
|
- 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 18, 'J': 19,
|
|
|
- 'K': 20, 'L': 21, 'M': 22, 'N': 23, 'O': 24, 'P': 25, 'Q': 26, 'R': 27, 'S': 28, 'T': 29,
|
|
|
- 'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z': 35, ' ': 36, '$': 37, '%': 38, '*': 39,
|
|
|
- '+': 40, '-': 41, '.': 42, '/': 43, ':': 44,
|
|
|
+const charSet string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"
|
|
|
+
|
|
|
+func stringToAlphaIdx(content string) <-chan int {
|
|
|
+ result := make(chan int)
|
|
|
+ go func() {
|
|
|
+ for _, r := range content {
|
|
|
+ idx := strings.IndexRune(charSet, r)
|
|
|
+ result <- idx
|
|
|
+ if idx < 0 {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ close(result)
|
|
|
+ }()
|
|
|
+
|
|
|
+ return result
|
|
|
}
|
|
|
|
|
|
func encodeAlphaNumeric(content string, ecl ErrorCorrectionLevel) (*barcode.BitList, *versionInfo, error) {
|
|
|
@@ -30,20 +41,22 @@ func encodeAlphaNumeric(content string, ecl ErrorCorrectionLevel) (*barcode.BitL
|
|
|
res.AddBits(int(alphaNumericMode), 4)
|
|
|
res.AddBits(len(content), vi.charCountBits(alphaNumericMode))
|
|
|
|
|
|
+ encoder := stringToAlphaIdx(content)
|
|
|
+
|
|
|
for idx := 0; idx < len(content)/2; idx++ {
|
|
|
- c1, ok1 := alphaNumericTable[content[idx*2]]
|
|
|
- c2, ok2 := alphaNumericTable[content[(idx*2)+1]]
|
|
|
- if !ok1 || !ok2 {
|
|
|
+ c1 := <-encoder
|
|
|
+ c2 := <-encoder
|
|
|
+ if c1 < 0 || c2 < 0 {
|
|
|
return nil, nil, fmt.Errorf("\"%s\" can not be encoded as %s", content, AlphaNumeric)
|
|
|
}
|
|
|
res.AddBits(c1*45+c2, 11)
|
|
|
}
|
|
|
if contentLenIsOdd {
|
|
|
- c1, ok := alphaNumericTable[content[len(content)-1]]
|
|
|
- if !ok {
|
|
|
+ c := <-encoder
|
|
|
+ if c < 0 {
|
|
|
return nil, nil, fmt.Errorf("\"%s\" can not be encoded as %s", content, AlphaNumeric)
|
|
|
}
|
|
|
- res.AddBits(c1, 6)
|
|
|
+ res.AddBits(c, 6)
|
|
|
}
|
|
|
|
|
|
addPaddingAndTerminator(res, vi)
|