diff --git a/code128/encode.go b/code128/encode.go index cfbe9f3..757347e 100644 --- a/code128/encode.go +++ b/code128/encode.go @@ -30,6 +30,10 @@ func shouldUseCTable(nextRunes []rune, curEncoding byte) bool { } for i := 0; i < requiredDigits; i++ { if i%2 == 0 && nextRunes[i] == FNC1 { + requiredDigits++ + if len(nextRunes) < requiredDigits { + return false + } continue } if nextRunes[i] < '0' || nextRunes[i] > '9' { diff --git a/code128/encode_test.go b/code128/encode_test.go index ed3bf6a..411ea30 100644 --- a/code128/encode_test.go +++ b/code128/encode_test.go @@ -70,3 +70,21 @@ func Test_EncodeCTable(t *testing.T) { "11101001100"+ // CheckSum == 24 "1100011101011") // Stop } + +func Test_shouldUseCTable(t *testing.T) { + if !shouldUseCTable([]rune{FNC1, '1', '2'}, startCSymbol) { + t.Error("[FNC1]12 failed") + } + if shouldUseCTable([]rune{FNC1, '1'}, startCSymbol) { + t.Error("[FNC1]1 failed") + } + if shouldUseCTable([]rune{'0', FNC1, '1'}, startCSymbol) { + t.Error("0[FNC1]1 failed") + } + if !shouldUseCTable([]rune{'0', '1', FNC1, '2', '3'}, startBSymbol) { + t.Error("01[FNC1]23 failed") + } + if shouldUseCTable([]rune{'0', '1', FNC1}, startBSymbol) { + t.Error("01[FNC1] failed") + } +}