Skip to content

Commit ab15157

Browse files
committed
revert using regex
1 parent 047a0c6 commit ab15157

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

src/tools/tidy/src/style.rs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use crate::walk::{filter_dirs, walk};
2121
use regex::{Regex, RegexSet};
22+
use std::collections::HashMap;
2223
use std::{ffi::OsStr, path::Path};
2324

2425
/// Error code markdown is restricted to 80 columns because they can be
@@ -65,8 +66,39 @@ const ANNOTATIONS_TO_IGNORE: &[&str] = &[
6566
"//@ normalize-stderr-test",
6667
];
6768

69+
fn generate_problems<'a>(
70+
consts: &'a [u32],
71+
letter_digit: &'a HashMap<char, char>,
72+
) -> impl Iterator<Item = u32> + 'a {
73+
consts.into_iter().flat_map(move |const_value| {
74+
let mut problem = format!("{:X}", const_value);
75+
for (key, value) in letter_digit {
76+
problem = problem.replace(&value.to_string(), &key.to_string());
77+
}
78+
let indexes: Vec<usize> = problem
79+
.chars()
80+
.enumerate()
81+
.filter_map(|(index, c)| if letter_digit.contains_key(&c) { Some(index) } else { None })
82+
.collect();
83+
(0..1 << indexes.len()).map(move |i| {
84+
let result = problem
85+
.chars()
86+
.enumerate()
87+
.map(|(index, c)| {
88+
if let Some(pos) = indexes.iter().position(|&x| x == index) {
89+
if (i >> pos) & 1 == 1 { letter_digit[&c] } else { c }
90+
} else {
91+
c
92+
}
93+
})
94+
.collect::<String>();
95+
u32::from_str_radix(&result, 0x10).unwrap()
96+
})
97+
})
98+
}
99+
68100
// Intentionally written in decimal rather than hex
69-
const PROBLEMATIC_CONSTS: &[u32] = &[
101+
const ROOT_PROBLEMATIC_CONSTS: &[u32] = &[
70102
184594741, 2880289470, 2881141438, 2965027518, 2976579765, 3203381950, 3405691582, 3405697037,
71103
3735927486, 3735932941, 4027431614, 4276992702, 195934910, 252707358, 762133, 179681982,
72104
173390526,
@@ -268,16 +300,14 @@ pub fn check(path: &Path, bad: &mut bool) {
268300
// We only check CSS files in rustdoc.
269301
path.extension().map_or(false, |e| e == "css") && !is_in(path, "src", "librustdoc")
270302
}
271-
let problematic_consts_strings: Vec<String> = (PROBLEMATIC_CONSTS.iter().map(u32::to_string))
272-
.chain(PROBLEMATIC_CONSTS.iter().map(|v| {
273-
format!("{:x}", v)
274-
.replace("a", "[aA4]")
275-
.replace("b", "[bB8]")
276-
.replace("c", "[cC]")
277-
.replace("d", "[dD]")
278-
.replace("e", "[eE3]")
279-
.replace("f", "[fF]")
280-
}))
303+
let problematic_consts = generate_problems(
304+
ROOT_PROBLEMATIC_CONSTS,
305+
&[('A', '4'), ('B', '8'), ('E', '3')].iter().cloned().collect(),
306+
)
307+
.collect::<Vec<u32>>();
308+
let problematic_consts_strings: Vec<String> = (problematic_consts.iter().map(u32::to_string))
309+
.chain(problematic_consts.iter().map(|v| format!("{:x}", v)))
310+
.chain(problematic_consts.iter().map(|v| format!("{:X}", v)))
281311
.collect();
282312
let problematic_regex = RegexSet::new(problematic_consts_strings.as_slice()).unwrap();
283313

0 commit comments

Comments
 (0)