Skip to content

Commit

Permalink
add Check
Browse files Browse the repository at this point in the history
Check is a function that reports whether a peekable reader looks like it
starts with an ID3 tag.
  • Loading branch information
dominikh committed Mar 18, 2015
1 parent 7b56aa0 commit 204402e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
13 changes: 13 additions & 0 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ import (
"io/ioutil"
)

type Peeker interface {
Peek(n int) ([]byte, error)
}

// Check reports whether r looks like it starts with an ID3 tag.
func Check(r Peeker) (bool, error) {
b, err := r.Peek(3)
if err != nil {
return false, err
}
return bytes.Equal(b, Magic[:]), nil
}

type Decoder struct {
r io.Reader
h TagHeader
Expand Down
23 changes: 23 additions & 0 deletions id3_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package id3

import (
"bufio"
"bytes"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -128,6 +130,27 @@ func TestUserFrameNameParsing(t *testing.T) {
}
}

func TestCheck(t *testing.T) {
tests := []struct {
in string
ok bool
}{
{"ID3stuff", true},
{"NotID3stuff", false},
}

for _, test := range tests {
r := bufio.NewReader(strings.NewReader(test.in))
ok, err := Check(r)
if err != nil {
t.Errorf("Unexpected error %q", err)
}
if ok != test.ok {
t.Errorf("expected ok = %T for %q, got %T", test.ok, test.in, ok)
}
}
}

func BenchmarkISO88591ToUTF8(b *testing.B) {
b.SetBytes(int64(len(ISOTestString)))
for i := 0; i < b.N; i++ {
Expand Down

0 comments on commit 204402e

Please sign in to comment.