Просмотр исходного кода

fixes shouldUseCTable

depending on the position of FNC1 the results should be different...
Florian Sundermann 9 лет назад
Родитель
Сommit
3e02de6fc1
2 измененных файлов с 22 добавлено и 0 удалено
  1. 4 0
      code128/encode.go
  2. 18 0
      code128/encode_test.go

+ 4 - 0
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' {

+ 18 - 0
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")
+	}
+}