Skip to content

Commit

Permalink
[#64] EscapePolicy::Basics shouldn't escape \x08..=\xFF
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Feb 17, 2020
1 parent 04294a9 commit 964e4b7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-ini"
version = "0.15.1"
version = "0.15.2"
authors = ["Y. T. Chung <zonyitoo@gmail.com>"]
description = "An Ini configuration file parsing library in Rust"
repository = "https://github.com/zonyitoo/rust-ini"
Expand Down
19 changes: 18 additions & 1 deletion src/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ impl EscapePolicy {
/// per this policy or false if not.
pub fn should_escape(self, c: char) -> bool {
match c {
'\\' | '\x00'..='\x1f' | '\x7f'..='\u{00ff}' => self.escape_basics(),
// A single backslash, must be escaped
// ASCII control characters, U+0000 NUL..= U+001F UNIT SEPARATOR, or U+007F DELETE. The same as char::is_ascii_control()
'\\' | '\x00'..='\x1f' | '\x7f' => self.escape_basics(),
';' | '#' | '=' | ':' => self.escape_reserved(),
'\u{0080}'..='\u{FFFF}' => self.escape_unicode(),
_ => false,
Expand All @@ -110,6 +112,8 @@ impl EscapePolicy {
fn escape_str(s: &str, policy: EscapePolicy) -> String {
let mut escaped: String = String::with_capacity(s.len());
for c in s.chars() {
println!("SHOULD? {} {} {}", c, policy.should_escape(c), c.escape_unicode());

// if we know this is not something to escape as per policy, we just
// write it and continue.
if !policy.should_escape(c) {
Expand Down Expand Up @@ -213,6 +217,7 @@ impl LineSeparator {
}

/// Writing configuration
#[derive(Debug, Clone)]
pub struct WriteOption {
/// Policies about how to escape characters
pub escape_policy: EscapePolicy,
Expand Down Expand Up @@ -1732,4 +1737,16 @@ bar = f
let v = conf.get_from(Some(section), key).unwrap();
assert_eq!(v, new_value);
}

#[test]
fn fix_issue64() {
let input = format!("some-key=åäö{}", super::DEFAULT_LINE_SEPARATOR);

let conf = Ini::load_from_str(&input).unwrap();

let mut output = Vec::new();
conf.write_to_policy(&mut output, EscapePolicy::Basics).unwrap();

assert_eq!(input, String::from_utf8(output).unwrap());
}
}

0 comments on commit 964e4b7

Please sign in to comment.