Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove UTF8 BOM if present #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ error_chain! {
}
}

fn read_file_to_string<P: AsRef<Path>>(p: P) -> Result<String> {
#[doc(hidden)]
pub fn read_file_to_string<P: AsRef<Path>>(p: P) -> Result<String> {
let p = p.as_ref();
let mut f = File::open(p).chain_err(|| ErrorKind::IOError)?;
let mut reader: Vec<u8> = Vec::new();
Expand All @@ -50,7 +51,20 @@ fn read_file_to_string<P: AsRef<Path>>(p: P) -> Result<String> {
let coder = encoding::label::encoding_from_whatwg_label(whtwg_label);
let file_content = match coder {
Some(c) => match c.decode(&reader, encoding::DecoderTrap::Ignore) {
Ok(x) => x,
Ok(x) => {
// handle UTF8 BOM manually
if x.len() > 0 && vec!["utf-8", "utf8"].contains(&&*whtwg_label.to_lowercase()) {
let mut chars = x.chars();
let first = chars.next().unwrap();
if first == '\u{feff}' {
chars.as_str().to_string()
} else {
x
}
} else {
x
}
},
Err(e) => bail!(ErrorKind::DecodingError(e.into_owned())),
},
None => bail!(ErrorKind::EncodingDetectionError),
Expand Down
7 changes: 7 additions & 0 deletions tests/txt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate ultrastar_txt;
use std::collections::HashMap;
use ultrastar_txt::*;
use url::Url;
use std::path::Path;

// usage:
// assert_error_kind!(some_err, ErrorKind::MyErrorType)
Expand Down Expand Up @@ -356,6 +357,12 @@ fn remote_url_audio() {
Source::Remote(Url::parse("https://www.example.com/Testfile.mp3").unwrap()));
}

fn encoding_utf8_bom() {
let txt = read_file_to_string(Path::new("tests/txts/encoding_utf8_bom.txt")).unwrap();
let header = get_simple_txt_header();
assert_eq!(header, parse_txt_header_str(&txt).unwrap());
}

fn get_simple_txt_str() -> &'static str {
include_str!("txts/simple_txt_with_all_features.txt")
}
Expand Down
26 changes: 26 additions & 0 deletions tests/txts/encoding_utf8_bom.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#TITLE:Testsong
#ARTIST:Testartist
#MP3:Testfile.mp3
#GAP:666
#BPM:123
#GENRE:Music
#EDITION:Testmusic
#COVER:Cover.jpg
#BACKGROUND:BG.jpg
#VIDEO:DLzxrzFCyOs.mp4
#VIDEOGAP:777
#RELATIVE:NO
#LANGUAGE:en
#YEAR:1337
: 0 4 59 Test
: 4 4 59 I
: 8 4 59 'm
* 12 4 59 test
: 16 4 59 ing.
- 20
: 24 4 59 Test
: 28 4 59 I
: 32 4 59 'm
F 36 4 59 test
F 40 4 59 ing.
E