From 92886077a485efc2d70d1087f78758a4cf1f7db1 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Thu, 29 Nov 2018 12:09:44 -0500 Subject: [PATCH] Adding String func so Encoding is a fmt.Stringer Signed-off-by: Dave Henderson --- README.md | 19 ++----------------- utfbom.go | 18 ++++++++++++++++++ utfbom_test.go | 13 +++++++++++++ 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 2f06eca..8ece280 100644 --- a/README.md +++ b/README.md @@ -37,22 +37,7 @@ func trySkip(byteData []byte) { // skip BOM and detect encoding sr, enc := utfbom.Skip(bytes.NewReader(byteData)) - var encStr string - switch enc { - case utfbom.UTF8: - encStr = "UTF8" - case utfbom.UTF16BigEndian: - encStr = "UTF16 big endian" - case utfbom.UTF16LittleEndian: - encStr = "UTF16 little endian" - case utfbom.UTF32BigEndian: - encStr = "UTF32 big endian" - case utfbom.UTF32LittleEndian: - encStr = "UTF32 little endian" - default: - encStr = "Unknown, no byte-order mark found" - } - fmt.Println("Detected encoding:", encStr) + fmt.Printf("Detected encoding: %s\n", enc) output, err = ioutil.ReadAll(sr) if err != nil { fmt.Println(err) @@ -74,7 +59,7 @@ ReadAll with BOM detection and skipping [104 101 108 108 111] Input: [104 101 108 108 111] ReadAll with BOM skipping [104 101 108 108 111] -Detected encoding: Unknown, no byte-order mark found +Detected encoding: Unknown ReadAll with BOM detection and skipping [104 101 108 108 111] ``` diff --git a/utfbom.go b/utfbom.go index 648184a..77a303e 100644 --- a/utfbom.go +++ b/utfbom.go @@ -32,6 +32,24 @@ const ( UTF32LittleEndian ) +// String returns a user-friendly string representation of the encoding. Satisfies fmt.Stringer interface. +func (e Encoding) String() string { + switch e { + case UTF8: + return "UTF8" + case UTF16BigEndian: + return "UTF16BigEndian" + case UTF16LittleEndian: + return "UTF16LittleEndian" + case UTF32BigEndian: + return "UTF32BigEndian" + case UTF32LittleEndian: + return "UTF32LittleEndian" + default: + return "Unknown" + } +} + const maxConsecutiveEmptyReads = 100 // Skip creates Reader which automatically detects BOM (Unicode Byte Order Mark) and removes it as necessary. diff --git a/utfbom_test.go b/utfbom_test.go index 30794c8..ec6743f 100644 --- a/utfbom_test.go +++ b/utfbom_test.go @@ -231,3 +231,16 @@ func TestReader_ReadEmpty(t *testing.T) { } } } + +func TestEncoding_String(t *testing.T) { + for e := Unknown; e <= UTF32LittleEndian; e++ { + s := e.String() + if s == "" { + t.Errorf("no string for %#v", e) + } + } + s := Encoding(999).String() + if s != "Unknown" { + t.Errorf("wrong string '%s' for invalid encoding", s) + } +}