Skip to content

Commit

Permalink
Merge pull request #2 from hairyhenderson/encoding-stringer
Browse files Browse the repository at this point in the history
Adding String func so Encoding is a fmt.Stringer
  • Loading branch information
dimchansky authored Dec 5, 2018
2 parents c410c23 + 9288607 commit d2133a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
19 changes: 2 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
```

Expand Down
18 changes: 18 additions & 0 deletions utfbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions utfbom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit d2133a1

Please sign in to comment.