use unicode_segmentation::UnicodeSegmentation;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CharsmapError {
Truncated,
InvalidUtf8,
}
impl std::fmt::Display for CharsmapError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Truncated => write!(f, "precompiled_charsmap blob truncated"),
Self::InvalidUtf8 => write!(f, "precompiled_charsmap replacements not UTF-8"),
}
}
}
impl std::error::Error for CharsmapError {}
#[inline]
fn has_leaf(unit: usize) -> bool {
(unit >> 8) & 1 == 1
}
#[inline]
fn value(unit: usize) -> usize {
unit & ((1usize << 31) - 1)
}
#[inline]
fn label(unit: usize) -> usize {
unit & ((1usize << 31) | 0xFF)
}
#[inline]
fn offset(unit: usize) -> usize {
(unit >> 10) << ((unit & (1usize << 9)) >> 6)
}
pub(crate) struct Charsmap {
trie: Vec<u32>,
replacements: String,
ascii: [Option<Box<str>>; 128],
bmp: Vec<(u32, u32)>,
crlf: Option<Box<str>>,
pub(crate) fast_scan: bool,
}
impl Charsmap {
pub(crate) fn parse(blob: &[u8]) -> Result<Self, CharsmapError> {
if blob.len() < 4 {
return Err(CharsmapError::Truncated);
}
let trie_bytes = u32::from_le_bytes(blob[0..4].try_into().unwrap()) as usize;
let trie_end = 4usize.checked_add(trie_bytes).ok_or(CharsmapError::Truncated)?;
if trie_end > blob.len() || trie_bytes % 4 != 0 {
return Err(CharsmapError::Truncated);
}
let trie: Vec<u32> = blob[4..trie_end]
.chunks_exact(4)
.map(|c| u32::from_le_bytes(c.try_into().unwrap()))
.collect();
let replacements = std::str::from_utf8(&blob[trie_end..])
.map_err(|_| CharsmapError::InvalidUtf8)?
.to_string();
let mut map = Self {
trie,
replacements,
ascii: std::array::from_fn(|_| None),
bmp: vec![(u32::MAX, 0); 0x10000],
crlf: None,
fast_scan: true,
};
let mut fast_scan = true;
let mut buf = [0u8; 4];
for cp in 0..0x10000u32 {
let Some(c) = char::from_u32(cp) else {
continue;
};
let s = c.encode_utf8(&mut buf);
let recorded = {
let Some(norm) = map.transform(s) else {
continue;
};
if norm == s {
continue;
}
let start = norm.as_ptr() as usize - map.replacements.as_ptr() as usize;
(start, norm.len())
};
if cp < 128 && (0x20..0x7F).contains(&(cp as u8)) {
fast_scan = false;
}
map.bmp[cp as usize] = (recorded.0 as u32, (recorded.0 + recorded.1) as u32);
if cp < 128 {
let norm = &map.replacements[recorded.0..recorded.0 + recorded.1];
map.ascii[cp as usize] = Some(norm.to_string().into());
}
}
if let Some(norm) = map.transform("\r\n")
&& norm != "\r\n"
{
map.crlf = Some(norm.into());
}
map.fast_scan = fast_scan;
Ok(map)
}
pub(crate) fn parts(&self) -> (&[u32], &str, &[(u32, u32)]) {
(&self.trie, &self.replacements, &self.bmp)
}
pub(crate) fn from_parts(
trie: Vec<u32>,
replacements: String,
bmp: Vec<(u32, u32)>,
) -> Result<Self, CharsmapError> {
if bmp.len() != 0x10000 {
return Err(CharsmapError::Truncated);
}
let mut map = Self {
trie,
replacements,
ascii: std::array::from_fn(|_| None),
bmp,
crlf: None,
fast_scan: true,
};
let mut fast_scan = true;
for cp in 0..128u32 {
let (start, end) = map.bmp[cp as usize];
if start == u32::MAX {
continue;
}
let start = start as usize;
let end = end as usize;
if start > end || end > map.replacements.len() {
return Err(CharsmapError::Truncated);
}
let norm = map
.replacements
.get(start..end)
.ok_or(CharsmapError::InvalidUtf8)?;
if (0x20..0x7F).contains(&(cp as u8)) {
fast_scan = false;
}
map.ascii[cp as usize] = Some(norm.to_string().into());
}
for (start, end) in &map.bmp[128..] {
if *start == u32::MAX {
continue;
}
if *start > *end || *end as usize > map.replacements.len() {
return Err(CharsmapError::Truncated);
}
}
if let Some(norm) = map.transform("\r\n")
&& norm != "\r\n"
{
map.crlf = Some(norm.to_string().into());
}
map.fast_scan = fast_scan;
Ok(map)
}
pub(crate) fn split_blob(blob: &[u8]) -> Result<(Vec<u32>, String), CharsmapError> {
if blob.len() < 4 {
return Err(CharsmapError::Truncated);
}
let trie_bytes = u32::from_le_bytes(blob[0..4].try_into().unwrap()) as usize;
let trie_end = 4usize.checked_add(trie_bytes).ok_or(CharsmapError::Truncated)?;
if trie_end > blob.len() || trie_bytes % 4 != 0 {
return Err(CharsmapError::Truncated);
}
let trie: Vec<u32> = blob[4..trie_end]
.chunks_exact(4)
.map(|c| u32::from_le_bytes(c.try_into().unwrap()))
.collect();
let replacements = std::str::from_utf8(&blob[trie_end..])
.map_err(|_| CharsmapError::InvalidUtf8)?
.to_string();
Ok((trie, replacements))
}
#[inline]
fn first_prefix_match(&self, key: &[u8]) -> Option<usize> {
let mut node_pos = 0usize;
let mut unit = *self.trie.first()? as usize;
node_pos ^= offset(unit);
for &c in key {
if c == 0 {
break;
}
node_pos ^= c as usize;
unit = *self.trie.get(node_pos)? as usize;
if label(unit) != c as usize {
return None;
}
node_pos ^= offset(unit);
if has_leaf(unit) {
return Some(value(*self.trie.get(node_pos)? as usize));
}
}
None
}
#[inline]
pub(crate) fn rewrite_char(&self, c: char) -> Option<&str> {
let cp = c as u32;
if cp >= 0x10000 {
let mut buf = [0u8; 4];
let s = c.encode_utf8(&mut buf);
return self.transform(s).filter(|norm| *norm != s);
}
let (start, end) = self.bmp[cp as usize];
if start == u32::MAX {
None
} else {
Some(&self.replacements[start as usize..end as usize])
}
}
#[inline]
pub(crate) fn transform<'a>(&'a self, chunk: &str) -> Option<&'a str> {
let start = self.first_prefix_match(chunk.as_bytes())?;
let bytes = self.replacements.as_bytes();
let end = memchr::memchr(0, bytes.get(start..)?).map_or(bytes.len(), |p| start + p);
self.replacements.get(start..end)
}
#[inline]
pub(crate) fn ascii(&self, b: u8) -> Option<&str> {
self.ascii.get(b as usize)?.as_deref()
}
#[inline]
pub(crate) fn crlf(&self) -> Option<&str> {
self.crlf.as_deref()
}
pub(crate) fn normalize_graphemes(&self, region: &str, out: &mut String) {
for grapheme in region.graphemes(true) {
if grapheme.len() < 6
&& let Some(norm) = self.transform(grapheme)
{
out.push_str(norm);
continue;
}
for (ci, c) in grapheme.char_indices() {
let part = &grapheme[ci..ci + c.len_utf8()];
match self.transform(part) {
Some(norm) => out.push_str(norm),
None => out.push(c),
}
}
}
}
}