Sets iterate in hash order, so canonicalize a set/frozenset line by sorting its elements. Assumes scalar elements with no nested ", ". Non-sets pass through. */
(line: &str)
| 25 | /* Sets iterate in hash order, so canonicalize a set/frozenset line by sorting |
| 26 | its elements. Assumes scalar elements with no nested ", ". Non-sets pass through. */ |
| 27 | fn normalize_set(line: &str) -> String { |
| 28 | let (prefix, inner, suffix) = if let Some(i) = |
| 29 | line.strip_prefix("frozenset({").and_then(|r| r.strip_suffix("})")) { |
| 30 | ("frozenset({", i, "})") |
| 31 | } else if line.starts_with('{') && line.ends_with('}') && line.len() > 2 && !line.contains(": ") { |
| 32 | ("{", &line[1..line.len() - 1], "}") |
| 33 | } else { |
| 34 | return line.to_string(); |
| 35 | }; |
| 36 | let mut elems: Vec<&str> = inner.split(", ").collect(); |
| 37 | elems.sort_unstable(); |
| 38 | format!("{}{}{}", prefix, elems.join(", "), suffix) |
| 39 | } |
| 40 | |
| 41 | // Apply set normalization line-by-line so both sides compare order-independent. |
| 42 | fn normalize(lines: &[String]) -> Vec<String> { |