Skip to content

Commit 05103e8

Browse files
unicodedata: Simplify generate_name_lookups (#8817)
* Reuse helpers where appropriate * Implement a helper for data that doesn't follow the pattern of the rest of the Unicode files * Use BTreeMap, which is deduped by key and sorted instead of sorting a Vec
1 parent 0ebc657 commit 05103e8

1 file changed

Lines changed: 61 additions & 37 deletions

File tree

‎crates/unicode/build.rs‎

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ impl<R: BufRead> UnicodeLineReader<R> {
5353
}
5454
})
5555
}
56+
57+
fn raw_iter(&mut self) -> UnicodeRawReader<'_, R> {
58+
UnicodeRawReader(self)
59+
}
5660
}
5761

5862
impl UnicodeLineReader<BufReader<File>> {
@@ -138,6 +142,16 @@ impl<R: BufRead> Iterator for UnicodeLineReader<R> {
138142
}
139143
}
140144

145+
struct UnicodeRawReader<'u, R: BufRead>(&'u mut UnicodeLineReader<R>);
146+
147+
impl<R: BufRead> Iterator for UnicodeRawReader<'_, R> {
148+
type Item = Box<str>;
149+
150+
fn next(&mut self) -> Option<Self::Item> {
151+
self.0.next_line_raw()
152+
}
153+
}
154+
141155
struct UnicodeLine {
142156
start: u32,
143157
end: u32,
@@ -231,6 +245,21 @@ fn write_slice_unordered(
231245
write_slice_post(writer);
232246
}
233247

248+
/// Helper to write raw values for data sources that don't follow the common format.
249+
///
250+
/// See: [`write_slice_display`]
251+
fn write_slice_raw<T, W: Write>(
252+
writer: &mut W,
253+
static_name: &str,
254+
array_type: &str,
255+
values: T,
256+
mut f: impl FnMut(&mut W, T),
257+
) {
258+
write_slice_pre(writer, static_name, array_type);
259+
f(writer, values);
260+
write_slice_post(writer);
261+
}
262+
234263
fn write_slice_pre(writer: &mut impl Write, static_name: &str, array_type: &str) {
235264
write!(writer, "static {static_name}: &[{array_type}] = &[").unwrap();
236265
}
@@ -295,7 +324,6 @@ fn full_data_parsers_latest() {
295324

296325
generate_decomp(decomp_lines);
297326
generate_algo_names(algo_names);
298-
generate_name_lookups();
299327
}
300328

301329
fn generate_decomp(decomp_lines: Vec<(u32, String)>) {
@@ -420,7 +448,7 @@ fn generate_algo_names(names: Vec<(u32, Box<str>)>) {
420448
/// accepts only a single character, so named sequences stay out of
421449
/// `lookup_character`.
422450
fn generate_name_lookups() {
423-
let mut aliases: Vec<(String, u32)> = Vec::new();
451+
let mut aliases = BTreeMap::new();
424452
for line in UnicodeLineReader::from_file_name("NameAliases.txt", true) {
425453
let name = line
426454
.field(
@@ -431,28 +459,12 @@ fn generate_name_lookups() {
431459
)),
432460
)
433461
.to_ascii_uppercase();
434-
aliases.push((name, line.start));
462+
aliases.insert(name, line.start);
435463
}
436-
aliases.sort_unstable_by(|a, b| a.0.cmp(&b.0));
437-
aliases.dedup_by(|a, b| a.0 == b.0);
438464

439-
let mut sequences: Vec<(String, String)> = Vec::new();
440-
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
441-
.join("unicode")
442-
.join("latest")
443-
.join("NamedSequences.txt");
444-
let file = File::open(&path).unwrap_or_else(|e| {
445-
panic!(
446-
"{e}: vendored Unicode data file should exist: {}",
447-
path.display()
448-
)
449-
});
450-
for raw in BufReader::new(file).lines() {
451-
let raw = raw.unwrap();
452-
let line = raw.trim();
453-
if line.is_empty() || line.starts_with('#') {
454-
continue;
455-
}
465+
let mut sequences = BTreeMap::new();
466+
let mut sequences_reader = UnicodeLineReader::from_file_name("NamedSequences.txt", true);
467+
for line in sequences_reader.raw_iter() {
456468
let (name, codes) = line
457469
.split_once(';')
458470
.unwrap_or_else(|| panic!("NamedSequences.txt line should be `NAME; HEX HEX`: {line}"));
@@ -466,25 +478,36 @@ fn generate_name_lookups() {
466478
escaped.push_str(&format!("{cp:X}"));
467479
escaped.push('}');
468480
}
469-
sequences.push((name, escaped));
481+
sequences.insert(name, escaped);
470482
}
471-
sequences.sort_unstable_by(|a, b| a.0.cmp(&b.0));
472-
sequences.dedup_by(|a, b| a.0 == b.0);
473483

474484
let mut writer = open_writer("name_lookups.rs");
475-
writeln!(writer, "static NAME_ALIASES: &[(&str, char)] = &[").unwrap();
476-
for (name, code) in &aliases {
477-
let ch = char::from_u32(*code)
478-
.unwrap_or_else(|| panic!("NameAliases.txt code point should be a scalar: {code:04X}"));
479-
writeln!(writer, " (\"{name}\", '\\u{{{:X}}}'),", ch as u32).unwrap();
480-
}
481-
writeln!(writer, "];").unwrap();
485+
write_slice_raw(
486+
&mut writer,
487+
"NAME_ALIASES",
488+
"(&str, char)",
489+
aliases,
490+
|writer, aliases| {
491+
for (name, code) in &aliases {
492+
let ch = char::from_u32(*code).unwrap_or_else(|| {
493+
panic!("NameAliases.txt code point should be a scalar: {code:04X}")
494+
});
495+
writeln!(writer, "(\"{name}\", '\\u{{{:X}}}'),", ch as u32).unwrap();
496+
}
497+
},
498+
);
482499

483-
writeln!(writer, "static NAMED_SEQUENCES: &[(&str, &str)] = &[").unwrap();
484-
for (name, escaped) in &sequences {
485-
writeln!(writer, " (\"{name}\", \"{escaped}\"),").unwrap();
486-
}
487-
writeln!(writer, "];").unwrap();
500+
write_slice_raw(
501+
&mut writer,
502+
"NAMED_SEQUENCES",
503+
"(&str, &str)",
504+
sequences,
505+
|writer, sequences| {
506+
for (name, escaped) in &sequences {
507+
writeln!(writer, "(\"{name}\", \"{escaped}\"),").unwrap();
508+
}
509+
},
510+
);
488511
}
489512

490513
/// Drive parsers that require the full 3.2.0 data.
@@ -864,6 +887,7 @@ fn drive_parsers() {
864887
bidi_class_3_2,
865888
binary_props_3_2,
866889
combining_class_3_2,
890+
generate_name_lookups,
867891
];
868892

869893
let mut handles = Vec::with_capacity(parsers.len() + 2);

0 commit comments

Comments
 (0)