Skip to content

Commit 588bd66

Browse files
committed
Skip the UTF-8 BOM bytes when reading the file contents in FileSource.
1 parent 0680a28 commit 588bd66

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/file/source/file.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,17 @@ where
115115
.unwrap_or_else(|| filename.clone());
116116

117117
// Read contents from file
118-
let text = fs::read_to_string(filename)?;
118+
let buf = fs::read(filename)?;
119+
120+
// If it exists, skip the UTF-8 BOM byte sequence: EF BB BF
121+
let buf = if buf.len() >= 3 && &buf[0..3] == b"\xef\xbb\xbf" {
122+
&buf[3..]
123+
} else {
124+
&buf
125+
};
126+
127+
let c = String::from_utf8_lossy(buf);
128+
let text = c.into_owned();
119129

120130
Ok(FileSourceResult {
121131
uri: Some(uri.to_string_lossy().into_owned()),

0 commit comments

Comments
 (0)