4 ms·
Validating should be pretty trivial. Explicitly handling all cases would look something like this: #[inline] const fn is_valid_scalar(c: u32) -> bool {
by ChrisSD 5y ago
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.
- majewsky 5y agoIf you absolutely want to golf it into one line: #[inline] const fn is_valid_scalar(c: u32) -> bool { matches!(c, 0..=0xD7FF) || matches!(c, 0xDE00..=0x10FFFF) } But I prefer your version for its clarity. It should compile into the same thing regardless.