refactor(lyrics): apply simplify-review cleanups

- stripBOM: bytes.TrimPrefix instead of []byte<->string round-trip (no alloc)
- ParseLyrics: pre-size the candidates slice
- move isTTMLDocument to lyrics_ttml.go beside its only caller (the dispatch
  layer should hold no per-format knowledge)
This commit is contained in:
Deluan 2026-06-19 16:35:57 -04:00
commit 3c74ed6ade
2 changed files with 16 additions and 16 deletions

View file

@ -1,7 +1,7 @@
package model
import (
"encoding/xml"
"bytes"
"fmt"
"slices"
"strings"
@ -38,7 +38,7 @@ func ParseLyrics(suffix, lang string, contents []byte) (LyricList, error) {
// Sniffing tries every format in order; a known suffix selects just its own.
// Unmatched suffixes leave no candidates, so parseFirstMatch falls to plain.
var candidates []lyricParser
candidates := make([]lyricParser, 0, len(lyricFormats))
for _, f := range lyricFormats {
if sniff || slices.Contains(f.suffixes, strings.ToLower(suffix)) {
candidates = append(candidates, f.parse)
@ -72,18 +72,5 @@ func plainLRC(lang string, contents []byte) (LyricList, error) {
}
func stripBOM(contents []byte) []byte {
return []byte(strings.TrimPrefix(string(contents), "\ufeff"))
}
func isTTMLDocument(text string) bool {
decoder := xml.NewDecoder(strings.NewReader(strings.TrimSpace(text)))
for {
token, err := decoder.Token()
if err != nil {
return false
}
if start, ok := token.(xml.StartElement); ok {
return strings.EqualFold(start.Name.Local, "tt")
}
}
return bytes.TrimPrefix(contents, []byte("\ufeff"))
}

View file

@ -106,6 +106,19 @@ func parseTTML(contents []byte) (LyricList, error) {
return parseTTMLWithDefaultLang("xxx", contents)
}
func isTTMLDocument(text string) bool {
decoder := xml.NewDecoder(strings.NewReader(strings.TrimSpace(text)))
for {
token, err := decoder.Token()
if err != nil {
return false
}
if start, ok := token.(xml.StartElement); ok {
return strings.EqualFold(start.Name.Local, "tt")
}
}
}
func parseTTMLWithDefaultLang(defaultLang string, contents []byte) (LyricList, error) {
contents = xmlEncodingRegex.ReplaceAll(contents, []byte(`<?xml$1encoding="UTF-8"$2?>`))