Validating should be pretty trivial. Explicitly handling all cases would look something like this:
#[inline]
const fn is_valid_scalar(c: u32) -> bool {
match c {
0..=0xD7FF => true,
0xD800..=0xDFFF => false, // surrogate pairs
0xE000..=0x10FFFF => true,
0x110000..=u32::MAX => false, // outside of Unicode range
}
}
But this could be reduced down to a simple if statement that tests for the invalid ranges.