use crate::unicode_id_table::{ID_CONTINUE, ID_START};
use std::cmp::Ordering;
pub(crate) fn is_id_start(cp: u32) -> bool {
cp == '$' as u32 || cp == '_' as u32 || in_table(ID_START, cp)
}
pub(crate) fn is_id_continue(cp: u32) -> bool {
cp == '$' as u32 || in_table(ID_CONTINUE, cp)
}
fn in_table(table: &[(u32, u32)], cp: u32) -> bool {
table
.binary_search_by(|&(lo, hi)| {
if hi < cp {
Ordering::Less
} else if lo > cp {
Ordering::Greater
} else {
Ordering::Equal
}
})
.is_ok()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn both_tables_are_sorted_disjoint_and_maximally_merged() {
for (name, table) in [("ID_START", ID_START), ("ID_CONTINUE", ID_CONTINUE)] {
assert!(!table.is_empty(), "{name} is empty");
for &(lo, hi) in table {
assert!(lo <= hi, "{name} has a reversed range {lo:#x}..{hi:#x}");
assert!(
hi <= 0x10ffff,
"{name} range {hi:#x} exceeds the last code point"
);
}
for pair in table.windows(2) {
let [(_, prev_hi), (next_lo, _)] = pair else {
continue;
};
assert!(
prev_hi < next_lo,
"{name}: {prev_hi:#x} and {next_lo:#x} overlap or are unmerged"
);
assert!(
*prev_hi + 1 < *next_lo,
"{name}: {prev_hi:#x} and {next_lo:#x} are adjacent and should be one range"
);
}
for &(lo, hi) in table {
assert!(hi < 0xd800 || lo > 0xdfff, "{name} covers a surrogate");
}
}
}
#[test]
fn the_tables_match_the_unicode_version_they_claim() {
assert_eq!(ID_START.len(), 684);
assert_eq!(ID_CONTINUE.len(), 799);
let count = |table: &[(u32, u32)]| table.iter().map(|&(lo, hi)| hi - lo + 1).sum::<u32>();
assert_eq!(count(ID_START), 145_916);
assert_eq!(count(ID_CONTINUE), 149_240);
}
#[test]
fn every_id_start_code_point_also_continues_an_identifier() {
for &(lo, hi) in ID_START {
let covered = ID_CONTINUE.iter().any(|&(clo, chi)| clo <= lo && hi <= chi);
assert!(
covered,
"ID_Start range {lo:#x}..{hi:#x} escapes ID_Continue"
);
}
}
#[test]
fn the_range_search_includes_both_endpoints_and_excludes_their_neighbours() {
assert!(!in_table(ID_START, 0x40)); assert!(in_table(ID_START, 0x41)); assert!(in_table(ID_START, 0x4d)); assert!(in_table(ID_START, 0x5a)); assert!(!in_table(ID_START, 0x5b));
assert!(!in_table(ID_CONTINUE, 0x2f));
assert!(in_table(ID_CONTINUE, 0x30));
assert!(in_table(ID_CONTINUE, 0x39));
assert!(!in_table(ID_CONTINUE, 0x3a));
assert!(in_table(ID_START, 0x31350));
assert!(in_table(ID_START, 0x33479));
assert!(!in_table(ID_START, 0x3347a));
assert!(in_table(ID_CONTINUE, 0xe0100));
assert!(in_table(ID_CONTINUE, 0xe01ef));
assert!(!in_table(ID_CONTINUE, 0xe01f0));
assert!(!in_table(ID_CONTINUE, 0x10ffff));
assert!(!in_table(ID_START, 0));
}
#[test]
fn dollar_and_underscore_are_ecmascripts_own_additions() {
assert!(!in_table(ID_START, '$' as u32));
assert!(!in_table(ID_CONTINUE, '$' as u32));
assert!(is_id_start('$' as u32));
assert!(is_id_continue('$' as u32));
assert!(!in_table(ID_START, '_' as u32));
assert!(in_table(ID_CONTINUE, '_' as u32));
assert!(is_id_start('_' as u32));
assert!(is_id_continue('_' as u32));
}
#[test]
fn the_predicates_answer_the_unicode_standard_across_scripts() {
for (cp, what) in [
(0x0041, "LATIN CAPITAL A"),
(0x00aa, "FEMININE ORDINAL INDICATOR"),
(0x00e9, "LATIN SMALL E WITH ACUTE"),
(0x03a9, "GREEK CAPITAL OMEGA"),
(0x05d0, "HEBREW ALEF"),
(0x3042, "HIRAGANA A"),
(0x4e00, "CJK ONE"),
(0x1d49c, "MATHEMATICAL SCRIPT CAPITAL A"),
(0x2118, "SCRIPT CAPITAL P"),
(0x212e, "ESTIMATED SYMBOL"),
] {
assert!(
is_id_start(cp),
"{what} (U+{cp:04X}) must start an identifier"
);
assert!(is_id_continue(cp), "{what} must also continue one");
}
for (cp, what) in [
(0x0030, "DIGIT ZERO"),
(0x0660, "ARABIC-INDIC DIGIT ZERO"),
(0x0301, "COMBINING ACUTE ACCENT"),
(0x00b7, "MIDDLE DOT — Other_ID_Continue"),
(0x200c, "ZERO WIDTH NON-JOINER"),
(0x200d, "ZERO WIDTH JOINER"),
(0xe0100, "VARIATION SELECTOR-17"),
] {
assert!(
!is_id_start(cp),
"{what} (U+{cp:04X}) must not start an identifier"
);
assert!(is_id_continue(cp), "{what} must continue one");
}
for (cp, what) in [
(0x0020, "SPACE"),
(0x002d, "HYPHEN-MINUS"),
(0x0021, "EXCLAMATION MARK"),
(0x200b, "ZERO WIDTH SPACE"),
(0x2029, "PARAGRAPH SEPARATOR"),
(0x1f680, "ROCKET"),
(0xd800, "a lone high surrogate"),
(0xdfff, "a lone low surrogate"),
(0x110000, "one past the last code point"),
(u32::MAX, "not a code point at all"),
] {
assert!(
!is_id_start(cp),
"{what} (U+{cp:04X}) must not start an identifier"
);
assert!(!is_id_continue(cp), "{what} must not continue one");
}
}
}