Skip to content

Commit

Permalink
EscapePolicy::Basics shouldn't escape \x08..=\xFF
Browse files Browse the repository at this point in the history
fix #64
  • Loading branch information
zonyitoo committed Feb 17, 2020
1 parent 04294a9 commit 24b20e5
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 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,17 @@ bar = f
let v = conf.get_from(Some(section), key).unwrap();
assert_eq!(v, new_value);
}

#[test]
fn fix_issue64() {
let input = r"some-key=åäö
";

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 24b20e5

Please sign in to comment.