Skip to content

Commit a910bfe

Browse files
MaxFreedomPollardcrmne
authored andcommitted
Keep the first line of a .lrc file saved with a byte order mark (#1034)
A local file's .lrc lyrics saved as UTF-8 with a byte order mark lost their first line. String::from_utf8_lossy keeps the mark as U+FEFF, which is not whitespace, so parse_lrc's trim_start left it in front of the first stamp: that line no longer started with "[" and was skipped. A file whose only line was timed showed its stamp as plain words instead. The sidecar's text now drops the mark before it is parsed, as the skin and settings readers already do.
1 parent 466bdf5 commit a910bfe

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

‎src/local_tags.rs‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,16 @@ pub fn picture(path: &Path) -> Option<Vec<u8>> {
9898
/// beside it first, then the lyrics in its tags.
9999
pub fn lyrics(path: &Path) -> Option<crate::lyrics::Lyrics> {
100100
let sidecar = path.with_extension("lrc");
101-
if let Some(found) = std::fs::read(&sidecar)
102-
.ok()
103-
.and_then(|bytes| crate::lyrics::from_text(&String::from_utf8_lossy(&bytes)))
104-
{
105-
return Some(found);
101+
if let Ok(bytes) = std::fs::read(&sidecar) {
102+
// A `.lrc` file saved on Windows often starts with a byte order
103+
// mark, and U+FEFF is not whitespace, so `trim_start` leaves it on
104+
// the first line: that line's stamp stops being a stamp and the
105+
// line is lost.
106+
let text = String::from_utf8_lossy(&bytes);
107+
let text = text.strip_prefix('\u{feff}').unwrap_or(&text);
108+
if let Some(found) = crate::lyrics::from_text(text) {
109+
return Some(found);
110+
}
106111
}
107112
embedded(path)
108113
.lyrics
@@ -261,4 +266,24 @@ pub(crate) mod tests {
261266
assert!(lyrics(&bare).is_none());
262267
let _ = std::fs::remove_dir_all(dir);
263268
}
269+
270+
/// A `.lrc` file saved with a byte order mark keeps its first line,
271+
/// whose stamp the mark used to hide.
272+
#[test]
273+
fn a_lyrics_file_saved_with_a_byte_order_mark_keeps_its_first_line() {
274+
let dir = folder("bom");
275+
let song = dir.join("song.mp3");
276+
std::fs::write(&song, mp3_with(&[])).unwrap();
277+
std::fs::write(
278+
dir.join("song.lrc"),
279+
b"\xef\xbb\xbf[00:01.00]First line\n[00:02.00]Second line\n",
280+
)
281+
.unwrap();
282+
let found = lyrics(&song).unwrap();
283+
assert!(found.synced);
284+
assert_eq!(found.lines.len(), 2);
285+
assert_eq!(found.lines[0].text, "First line");
286+
assert_eq!(found.lines[0].at_ms, Some(1_000));
287+
let _ = std::fs::remove_dir_all(dir);
288+
}
264289
}

0 commit comments

Comments
 (0)