diff --git a/ean/encoder.go b/ean/encoder.go index 6fc32c0..9874663 100644 --- a/ean/encoder.go +++ b/ean/encoder.go @@ -78,25 +78,11 @@ var encoderTable = map[rune]encodedNumber{ }, } -func runeToInt(r rune) int { - if r >= '0' && r <= '9' { - return int(r - '0') - } - return -1 -} - -func intToRune(i int) rune { - if i >= 0 && i <= 9 { - return rune(i + '0') - } - return 'F' -} - func calcCheckNum(code string) rune { x3 := len(code) == 7 sum := 0 for _, r := range code { - curNum := runeToInt(r) + curNum := utils.RuneToInt(r) if curNum < 0 || curNum > 9 { return 'B' } @@ -107,7 +93,7 @@ func calcCheckNum(code string) rune { sum += curNum } - return intToRune((10 - (sum % 10)) % 10) + return utils.IntToRune((10 - (sum % 10)) % 10) } func encodeEAN8(code string) *utils.BitList { diff --git a/ean/encoder_test.go b/ean/encoder_test.go index 43bcdd3..6f6da3f 100644 --- a/ean/encoder_test.go +++ b/ean/encoder_test.go @@ -44,24 +44,3 @@ func Test_EncodeEAN(t *testing.T) { t.Error("\"invalid error\" should not be encodable") } } - -func Test_RuneToIntIntToRune(t *testing.T) { - if intToRune(0) != '0' { - t.Errorf("failed intToRune(0) returned %d", string(intToRune(0))) - } - if intToRune(9) != '9' { - t.Errorf("failed intToRune(9) returned %d", intToRune(9)) - } - if intToRune(10) != 'F' { - t.Errorf("failed intToRune(10) returned %d", intToRune(10)) - } - if runeToInt('0') != 0 { - t.Error("failed runeToInt('0') returned %d", runeToInt(0)) - } - if runeToInt('9') != 9 { - t.Error("failed runeToInt('9') returned %d", runeToInt(9)) - } - if runeToInt('F') != -1 { - t.Error("failed runeToInt('F') returned %d", runeToInt('F')) - } -} diff --git a/utils/runeint.go b/utils/runeint.go new file mode 100644 index 0000000..d2e5e61 --- /dev/null +++ b/utils/runeint.go @@ -0,0 +1,19 @@ +package utils + +// RuneToInt converts a rune between '0' and '9' to an integer between 0 and 9 +// If the rune is outside of this range -1 is returned. +func RuneToInt(r rune) int { + if r >= '0' && r <= '9' { + return int(r - '0') + } + return -1 +} + +// IntToRune converts a digit 0 - 9 to the rune '0' - '9'. If the given int is outside +// of this range 'F' is returned! +func IntToRune(i int) rune { + if i >= 0 && i <= 9 { + return rune(i + '0') + } + return 'F' +} diff --git a/utils/runeint_test.go b/utils/runeint_test.go new file mode 100644 index 0000000..f1fdbc5 --- /dev/null +++ b/utils/runeint_test.go @@ -0,0 +1,24 @@ +package utils + +import "testing" + +func Test_RuneToIntIntToRune(t *testing.T) { + if IntToRune(0) != '0' { + t.Errorf("failed IntToRune(0) returned %d", string(IntToRune(0))) + } + if IntToRune(9) != '9' { + t.Errorf("failed IntToRune(9) returned %d", IntToRune(9)) + } + if IntToRune(10) != 'F' { + t.Errorf("failed IntToRune(10) returned %d", IntToRune(10)) + } + if RuneToInt('0') != 0 { + t.Error("failed RuneToInt('0') returned %d", RuneToInt(0)) + } + if RuneToInt('9') != 9 { + t.Error("failed RuneToInt('9') returned %d", RuneToInt(9)) + } + if RuneToInt('F') != -1 { + t.Error("failed RuneToInt('F') returned %d", RuneToInt('F')) + } +}