type.go: Bugfix for newlines interpreted as binary. Add tests.

Fixes #55
pull/69/head
Dustin Oprea 2021-04-28 00:19:46 -04:00
parent a1cb4443b2
commit dca55bf8ca
2 changed files with 28 additions and 4 deletions

View File

@ -156,11 +156,11 @@ type SignedRational struct {
}
func isPrintableText(s string) bool {
// TODO(dustin): Add text
for _, c := range s {
if unicode.IsPrint(rune(c)) == false {
// unicode.IsPrint() returns false for newline characters.
if c == 0x0d || c == 0x0a {
continue
} else if unicode.IsPrint(rune(c)) == false {
return false
}
}

View File

@ -411,3 +411,27 @@ func TestTranslateStringToType__InvalidType(t *testing.T) {
// log.Panicf("from-string encoding for type not supported; this shouldn't happen: [%s]", tagType.String())
// return nil, nil
// }
func TestIsPrintableText_letters(t *testing.T) {
if isPrintableText("abc") != true {
t.Fatalf("Printable text interpreted as nonprintable.")
}
}
func TestIsPrintableText_space(t *testing.T) {
if isPrintableText(" ") != true {
t.Fatalf("Printable text interpreted as nonprintable.")
}
}
func TestIsPrintableText_newlines(t *testing.T) {
if isPrintableText("\r\n") != true {
t.Fatalf("Printable text interpreted as nonprintable.")
}
}
func TestIsPrintableText_punctuationAndSymbols(t *testing.T) {
if isPrintableText(",:-/$©") != true {
t.Fatalf("Printable text interpreted as nonprintable.")
}
}