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

Add multi-line support #63

Open
wants to merge 1 commit 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
66 changes: 62 additions & 4 deletions dotenv/src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use std::collections::HashMap;
use std::env;
use std::io::prelude::*;
use std::io::{BufReader, Lines};
use std::io::BufReader;

use crate::errors::*;
use crate::parse;

pub struct Iter<R> {
lines: Lines<BufReader<R>>,
lines: QuotedLines<BufReader<R>>,
substitution_data: HashMap<String, Option<String>>,
}

impl<R: Read> Iter<R> {
pub fn new(reader: R) -> Iter<R> {
Iter {
lines: BufReader::new(reader).lines(),
lines: QuotedLines {
buf: BufReader::new(reader),
},
substitution_data: HashMap::new(),
}
}
Expand All @@ -31,14 +33,70 @@ impl<R: Read> Iter<R> {
}
}

struct QuotedLines<B> {
buf: B,
}

fn is_complete(buf: &String) -> bool {
let mut escape = false;
let mut strong_quote = false;
let mut weak_quote = false;
let mut count = 0_u32;

for c in buf.chars() {
if escape {
escape = false
} else {
match c {
'\\' => escape = true,
'"' if !strong_quote => {
count += 1;
weak_quote = true
}
'\'' if !weak_quote => {
count += 1;
strong_quote = true
}
_ => (),
}
}
}
count % 2 == 0
}

impl<B: BufRead> Iterator for QuotedLines<B> {
type Item = Result<String>;

fn next(&mut self) -> Option<Result<String>> {
let mut buf = String::new();
loop {
match self.buf.read_line(&mut buf) {
Ok(0) => return None,
Ok(_n) => {
if is_complete(&buf) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks to me like it would cause an exponential slowdown if a quote contained a large number of lines - it would have to loop through every character read so far for each new line.

Would it be possible to make this a stateful parser which keeps track of what quotes have been seen so far, and only looks at each character once be possible without making it significantly more complicated?

I mean, the current code won't slow down any current use cases, and env variables are unlikely to be very large. But it seems non-ideal to have an exponential slowdown if it's avoidable?

I'm asking as an observer: I don't have any ownership over dotenv.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have a go at it

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have it working:
#78

if buf.ends_with('\n') {
buf.pop();
if buf.ends_with('\r') {
buf.pop();
}
}
return Some(Ok(buf));
}
}
Err(e) => return Some(Err(Error::Io(e))),
}
}
}
}

impl<R: Read> Iterator for Iter<R> {
type Item = Result<(String, String)>;

fn next(&mut self) -> Option<Self::Item> {
loop {
let line = match self.lines.next() {
Some(Ok(line)) => line,
Some(Err(err)) => return Some(Err(Error::Io(err))),
Some(Err(err)) => return Some(Err(err)),
None => return None,
};

Expand Down
20 changes: 20 additions & 0 deletions dotenv/tests/test-multiline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
mod common;

use dotenv::*;
use std::env;

use crate::common::*;

#[test]
fn test_multiline() {
let value = "-----BEGIN PRIVATE KEY-----\n-----END PRIVATE KEY-----\\n\\\"QUOTED\\\"";
let weak = "-----BEGIN PRIVATE KEY-----\n-----END PRIVATE KEY-----\n\"QUOTED\"";
let dir = tempdir_with_dotenv(&format!("WEAK=\"{}\"\nSTRONG='{}'", value, value)).unwrap();

dotenv().ok();
assert_eq!(var("WEAK").unwrap(), weak);
assert_eq!(var("STRONG").unwrap(), value);

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