Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions code39/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package code39

import (
"errors"
"strconv"
"strings"

"github.com/boombuler/barcode"
Expand Down Expand Up @@ -75,12 +74,12 @@ var extendedTable = map[rune]string{
127: `%T`,
}

func getChecksum(content string) string {
func getChecksum(content string) rune {
sum := 0
for _, r := range content {
info, ok := encodeTable[r]
if !ok || info.value < 0 {
return "#"
return 0
}

sum += info.value
Expand All @@ -89,10 +88,10 @@ func getChecksum(content string) string {
sum = sum % 43
for r, v := range encodeTable {
if v.value == sum {
return string(r)
return r
}
}
return "#"
return 0
}

func prepare(content string) (string, error) {
Expand Down Expand Up @@ -124,9 +123,10 @@ func EncodeWithColor(content string, includeChecksum bool, fullASCIIMode bool, c
return nil, errors.New("invalid data! try full ascii mode")
}

checkChar := getChecksum(content)
data := "*" + content
if includeChecksum {
data += getChecksum(content)
data += string(checkChar)
}
data += "*"

Expand All @@ -144,11 +144,8 @@ func EncodeWithColor(content string, includeChecksum bool, fullASCIIMode bool, c
result.AddBit(info.data...)
}

checkSum, err := strconv.ParseInt(getChecksum(content), 10, 64)
if err != nil {
checkSum = 0
}
return utils.New1DCodeIntCheckSumWithColor(barcode.TypeCode39, content, result, int(checkSum), color), nil
checkSum := encodeTable[checkChar].value
return utils.New1DCodeIntCheckSumWithColor(barcode.TypeCode39, content, result, checkSum, color), nil
}

// Encode returns a code39 barcode for the given content
Expand Down
18 changes: 18 additions & 0 deletions code39/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ func doTest(t *testing.T, addCS, fullASCII bool, data, testResult string) {
}
}

func Test_ChecksumValue(t *testing.T) {
letter, err := Encode("A", true, false)
if err != nil {
t.Fatal(err)
}
if letter.CheckSum() != 10 {
t.Errorf("Encode(A) checksum = %d, want 10", letter.CheckSum())
}

digit, err := Encode("1", false, false)
if err != nil {
t.Fatal(err)
}
if digit.CheckSum() != 1 {
t.Errorf("Encode(1) checksum = %d, want 1", digit.CheckSum())
}
}

func Test_Encode(t *testing.T) {
doTest(t, false, false, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
"1001011011010110101001011010110100101101101101001010101011001011011010110010101"+
Expand Down
Loading