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

Whitespace inside quoted values should not be trimmed #140

Open
zaneduffield opened this issue Nov 8, 2024 · 4 comments
Open

Whitespace inside quoted values should not be trimmed #140

zaneduffield opened this issue Nov 8, 2024 · 4 comments

Comments

@zaneduffield
Copy link

I noticed that quoting a value isn't enough to preserve leading or trailing whitespace.

This code unconditionally trims the value returned by the parse_value function

mval.trim_in_place();

One of the reasons quoting is commonly supported in ini files is to allow explicit whitespace.

I imagine the solution is to move the trim to inside of the parse_value and only call it when the value is unquoted.

@zonyitoo
Copy link
Owner

The white spaces are preserved between quotes (" and ').

rust-ini/src/lib.rs

Lines 1548 to 1569 in b2a09b9

let mut val = match self.ch {
None => Ok(String::new()),
Some('"') if self.opt.enabled_quote => {
self.bump();
self.parse_str_until(&[Some('"')], false).and_then(|s| {
self.bump(); // Eats the last "
// Parse until EOL
self.parse_str_until_eol(cfg!(feature = "inline-comment"))
.map(|x| s + &x)
})
}
Some('\'') if self.opt.enabled_quote => {
self.bump();
self.parse_str_until(&[Some('\'')], false).and_then(|s| {
self.bump(); // Eats the last '
// Parse until EOL
self.parse_str_until_eol(cfg!(feature = "inline-comment"))
.map(|x| s + &x)
})
}
_ => self.parse_str_until_eol(cfg!(feature = "inline-comment")),
}?;

@zaneduffield
Copy link
Author

Outside of that function, the value is trimmed

mval.trim_in_place();

When I notice this issue I was only using escaped whitespace (\r\n, for example). That may be relevant.

@zonyitoo
Copy link
Owner

Ah, right. I couldn't remember why we should trim the value after it was parsed.

@zaneduffield
Copy link
Author

But you think that there might be a good reason to do so?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants