use std::cell::RefCell;
use crate::utils::fx::HashMap;
#[derive(Default)]
struct Canonical {
offsets: HashMap<String, u32>,
text: String,
}
thread_local! {
static CANONICAL: RefCell<Canonical> = RefCell::new(Canonical::default());
}
pub fn register_canonical(str: &str) -> u32 {
debug_assert!(str.is_ascii() && str.len() < 256);
CANONICAL.with(|c| {
let mut c = c.borrow_mut();
let start = match c.offsets.get(str) {
Some(&start) => start,
None => {
let start = c.text.len() as u32;
c.offsets.insert(str.to_owned(), start);
c.text.push_str(str);
start
}
};
(start << 8) | str.len() as u32
})
}
#[inline]
pub fn canonical_start(range: u32) -> u32 {
range >> 8
}
#[inline]
pub fn canonical_end(range: u32) -> u32 {
(range >> 8) + (range & 0xff)
}
pub fn with_canonical_text<R>(f: impl FnOnce(&str) -> R) -> R {
CANONICAL.with(|c| f(&c.borrow().text))
}
pub fn canonical_slice(start: u32, end: u32) -> String {
with_canonical_text(|t| t[start as usize..end as usize].to_owned())
}
pub fn canonical_code_at(offset: u32) -> u32 {
with_canonical_text(|t| t.as_bytes().get(offset as usize).map_or(u32::MAX, |&b| b as u32))
}