Refactor readTFrame and added readWFrame

This commit is contained in:
David Howden 2015-05-24 12:20:16 +10:00
parent f782cbdf44
commit b55f474bf9
2 changed files with 13 additions and 14 deletions

View File

@ -219,7 +219,7 @@ func readID3v2Frames(r io.Reader, h *ID3v2Header) (map[string]interface{}, error
result[rawName] = t
case name[0] == 'T':
txt, err := readTFrame(b, true) // text is encoded
txt, err := readTFrame(b)
if err != nil {
return nil, err
}
@ -240,7 +240,7 @@ func readID3v2Frames(r io.Reader, h *ID3v2Header) (map[string]interface{}, error
result[rawName] = t
case name[0] == 'W':
txt, err := readTFrame(b, false) // url are not encoded
txt, err := readWFrame(b)
if err != nil {
return nil, err
}

View File

@ -13,25 +13,24 @@ import (
"unicode/utf16"
)
// when the frame is not encoded, add a 0 at the start
func readTFrame(b []byte, encoded bool) (string, error) {
if !encoded {
b = append([]byte{0}, b[0:]...)
func readWFrame(b []byte) (string, error) {
// Frame text is always encoded in ISO-8859-1
b = append([]byte{0}, b...)
return readTFrame(b)
}
func readTFrame(b []byte) (string, error) {
if len(b) == 0 {
return "", nil
}
txt, err := parseText(b)
txt, err := decodeText(b[0], b[1:])
if err != nil {
return "", err
}
return strings.Join(strings.Split(txt, string([]byte{0})), ""), nil
}
func parseText(b []byte) (string, error) {
if len(b) == 0 {
return "", nil
}
return decodeText(b[0], b[1:])
}
func decodeText(enc byte, b []byte) (string, error) {
if len(b) == 0 {
return "", nil