Skip to content

Commit cc6fb57

Browse files
authored
Fix string casing against the pinned Unicode database (#9824)
Generate compact full-case tables from ICU rather than using the Rust compiler Unicode version. Preserve full mappings, contextual sigma and lone surrogates without repeated runtime ICU mapping overhead. Assisted-by: dot (OpenAI Codex; model version unavailable) Co-authored-by: youknowdot <[email protected]>
1 parent e68658a commit cc6fb57

4 files changed

Lines changed: 298 additions & 30 deletions

File tree

‎crates/unicode/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ unicode_names2 = { workspace = true }
2323
writeable = { workspace = true }
2424

2525
[build-dependencies]
26+
icu_casemap = { workspace = true }
27+
icu_locale = { workspace = true }
2628
icu_properties = { workspace = true }
2729
unicode_names2 = { workspace = true }
2830
zlib-rs = { workspace = true }

‎crates/unicode/build.rs‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,85 @@ fn open_reader(file_name: &str, modern: bool) -> BufReader<File> {
312312
}))
313313
}
314314

315+
/// Generate compact full case mappings from the same ICU data as our predicates.
316+
/// Keep this independent of the Rust toolchain's Unicode version. The default
317+
/// lowercase mapping of sigma is contextualized at runtime, like lower_ucs4.
318+
fn generate_case_mappings() {
319+
use icu_casemap::CaseMapper;
320+
use icu_locale::LanguageIdentifier;
321+
use icu_properties::props::{BinaryProperty, CaseIgnorable, Cased, Lowercase, Uppercase};
322+
323+
fn write_pages(writer: &mut impl Write, name: &str, values: &[u16]) {
324+
let mut pages = Vec::new();
325+
let mut known = BTreeMap::new();
326+
let mut index = Vec::new();
327+
for page in values.as_chunks::<256>().0 {
328+
let next = u16::try_from(known.len()).expect("too many case mapping pages");
329+
let id = *known.entry(page.to_vec()).or_insert_with(|| {
330+
pages.extend_from_slice(page);
331+
next
332+
});
333+
index.push(id);
334+
}
335+
writeln!(writer, "static {name}_INDEX: &[u16] = &{index:?};").unwrap();
336+
writeln!(writer, "static {name}_PAGES: &[u16] = &{pages:?};").unwrap();
337+
}
338+
339+
let mut writer = open_writer("case_mappings.rs");
340+
let mapper = CaseMapper::new();
341+
for (name, lower) in [("LOWER", true), ("UPPER", false)] {
342+
let mut indices = vec![0; 0x110000];
343+
let mut mappings = vec![[char::default(); 3]];
344+
for (value, index) in indices.iter_mut().enumerate() {
345+
let Some(ch) = char::from_u32(value as u32) else {
346+
continue;
347+
};
348+
if ch.is_ascii() {
349+
continue;
350+
}
351+
let mut buf = [0; 4];
352+
let source: &str = ch.encode_utf8(&mut buf);
353+
let mapped = if lower {
354+
mapper.lowercase_to_string(source, &LanguageIdentifier::UNKNOWN)
355+
} else {
356+
mapper.uppercase_to_string(source, &LanguageIdentifier::UNKNOWN)
357+
};
358+
if mapped == source {
359+
continue;
360+
}
361+
assert!(!mapped.is_empty(), "empty full case mapping for {ch:?}");
362+
let mut result = [char::default(); 3];
363+
for (i, ch) in mapped.chars().enumerate() {
364+
assert!(
365+
i < result.len(),
366+
"full case mapping exceeds three characters"
367+
);
368+
assert_ne!(ch, '\0', "NUL in full case mapping");
369+
result[i] = ch;
370+
}
371+
*index = u16::try_from(mappings.len()).expect("too many case mappings");
372+
mappings.push(result);
373+
}
374+
write_pages(&mut writer, name, &indices);
375+
writeln!(
376+
writer,
377+
"static {name}_MAPPINGS: &[[char; 3]] = &{mappings:?};"
378+
)
379+
.unwrap();
380+
}
381+
let flags: Vec<_> = (0..0x110000)
382+
.map(|value| {
383+
char::from_u32(value).map_or(0, |ch| {
384+
u16::from(Cased::for_char(ch))
385+
| (u16::from(CaseIgnorable::for_char(ch)) << 1)
386+
| (u16::from(Lowercase::for_char(ch)) << 2)
387+
| (u16::from(Uppercase::for_char(ch)) << 3)
388+
})
389+
})
390+
.collect();
391+
write_pages(&mut writer, "CASE_FLAGS", &flags);
392+
}
393+
315394
/// Drive parsers that require the latest Unicode data.
316395
///
317396
/// The full data is huge so it's ideal to parse it only once.
@@ -1035,6 +1114,7 @@ fn drive_parsers() {
10351114
combining_class_3_2,
10361115
generate_name_lookups,
10371116
generate_blocks,
1117+
generate_case_mappings,
10381118
];
10391119

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

0 commit comments

Comments
 (0)