Skip to content

Commit

Permalink
Add support for UTF-8 BOM encoding (#28)
Browse files Browse the repository at this point in the history
The UTF-8 Byte Order Mark `\ufeff` will be ignored when parsing.

See #27
  • Loading branch information
sonro authored Oct 17, 2022
1 parent 59976fd commit a6b08ba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
14 changes: 13 additions & 1 deletion dotenv/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ impl<R: Read> Iter<R> {
}

/// Loads all variables found in the `reader` into the environment.
pub fn load(self) -> Result<()> {
pub fn load(mut self) -> Result<()> {
self.remove_bom()?;

for item in self {
let (key, value) = item?;
if env::var(&key).is_err() {
Expand All @@ -32,6 +34,16 @@ impl<R: Read> Iter<R> {

Ok(())
}

fn remove_bom(&mut self) -> Result<()> {
let buffer = self.lines.buf.fill_buf().map_err(Error::Io)?;
// https://www.compart.com/en/unicode/U+FEFF
if buffer.starts_with(&[0xEF, 0xBB, 0xBF]) {
// remove the BOM from the bufreader
self.lines.buf.consume(3);
}
Ok(())
}
}

struct QuotedLines<B> {
Expand Down
22 changes: 22 additions & 0 deletions dotenv/tests/test-ignore-bom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
mod common;

use crate::common::*;
use dotenvy::*;
use std::{env, error::Error, result::Result};

#[test]
fn test_ignore_bom() -> Result<(), Box<dyn Error>> {
let bom = "\u{feff}";
let dir = tempdir_with_dotenv(&format!("{}TESTKEY=test_val", bom))?;

let mut path = env::current_dir()?;
path.push(".env");

from_path(&path)?;

assert_eq!(env::var("TESTKEY")?, "test_val");

env::set_current_dir(dir.path().parent().unwrap())?;
dir.close()?;
Ok(())
}

0 comments on commit a6b08ba

Please sign in to comment.