Skip to content

Commit

Permalink
fixup: don't trim trailing commas
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Nov 27, 2023
1 parent 8e6ccb9 commit 8ffba1a
Showing 1 changed file with 3 additions and 29 deletions.
32 changes: 3 additions & 29 deletions mullvad-daemon/src/settings/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,32 +211,6 @@ fn trim_json(json_patch: &str) -> String {
patch = format!("{{\n{patch}\n}}");
}

// Removing trailing commas
let mut trailing_commas_positions = vec![];
let mut quoting = false;
let mut escaping = false;
for (index, c) in patch.char_indices() {
if !escaping && c == '\\' {
escaping = true;
} else if escaping {
escaping = false;
} else if c == '"' {
quoting = !quoting;
} else if c == ',' && !quoting {
let remaining_str: &str = &patch[index..];
if let Some(c) = remaining_str.chars().skip(1).find(|c| !c.is_whitespace()) {
const STOP_PARENS: &str = "]}";
if STOP_PARENS.contains(c) {
trailing_commas_positions.push(index);
}
}
}
}

for index in trailing_commas_positions.into_iter().rev() {
patch.remove(index);
}

patch
}

Expand Down Expand Up @@ -399,10 +373,10 @@ fn test_trimmed_json() {
"a": [
{
"b": 1,
"c": 2,
"c": 2
},
{},
],
{}
]
"#;
let expected = r#"
{
Expand Down

0 comments on commit 8ffba1a

Please sign in to comment.