use crate::core::util::error::lucene_error::LuceneError;
use crate::core::util::error::lucene_error::Result;
pub struct UnicodeUtil;
impl UnicodeUtil {
pub const UNI_SUR_HIGH_START: i32 = 0xD800;
pub const UNI_SUR_HIGH_END: i32 = 0xDBFF;
pub const UNI_SUR_LOW_START: i32 = 0xDC00;
pub const UNI_SUR_LOW_END: i32 = 0xDFFF;
pub const MAX_UTF8_BYTES_PER_CHAR: i32 = 3;
pub(crate) fn code_point_at<'a>(
utf8: &[u8],
pos: usize,
reuse: &'a mut UTF8CodePoint,
) -> Result<&'a mut UTF8CodePoint> {
if pos >= utf8.len() {
return Err(LuceneError::illegal_argument(format!(
"Position {} out of bounds for utf8 array of length {}",
pos,
utf8.len()
)));
}
let lead_byte = utf8[pos] as usize;
let num_bytes = UTF8_CODE_LENGTH[lead_byte];
if num_bytes == i32::MIN {
return Err(LuceneError::illegal_argument(format!(
"Invalid UTF8 header byte: 0x{lead_byte:X}"
)));
}
reuse.num_bytes = num_bytes as usize;
let mut v: i32;
match num_bytes {
1 => {
reuse.code_point = lead_byte as i32;
return Ok(reuse);
},
2 => v = (lead_byte & 0b0001_1111) as i32,
3 => v = (lead_byte & 0b0000_1111) as i32,
4 => v = (lead_byte & 0b0000_0111) as i32,
_ => {
return Err(LuceneError::illegal_argument(format!(
"Invalid UTF8 header byte: 0x{lead_byte:X}"
)));
},
}
let limit = pos + reuse.num_bytes;
let mut i = pos + 1;
while i < limit {
if i >= utf8.len() {
return Err(LuceneError::illegal_argument("UTF-8 sequence truncated"));
}
v = (v << 6) | ((utf8[i] & 0b0011_1111) as i32);
i += 1;
}
reuse.code_point = v;
Ok(reuse)
}
pub fn new_string(code_points: &[i32], offset: usize, count: usize) -> Result<String> {
if offset + count > code_points.len() {
return Err(LuceneError::illegal_argument(
"offset + count out of bounds",
));
}
let mut result = String::with_capacity(count);
for &cp in &code_points[offset..offset + count] {
if !(0..=0x10FFFF).contains(&cp) {
return Err(LuceneError::illegal_argument(format!(
"Invalid code point: {cp}"
)));
}
if let Some(c) = std::char::from_u32(cp as u32) {
result.push(c);
} else {
return Err(LuceneError::illegal_argument(format!(
"Invalid Unicode scalar value: {cp}"
)));
}
}
Ok(result)
}
pub fn max_utf8_length(code: i32) -> Result<usize> {
match code {
0x0000..=0x007F => Ok(1), 0x0080..=0x07FF => Ok(2), 0x0800..=0xFFFF => Ok(3), 0x10000..=0x10FFFF => Ok(4), _ => Err(LuceneError::illegal_argument("Invalid Unicode code point")),
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct UTF8CodePoint {
pub code_point: i32,
pub num_bytes: usize,
}
pub(crate) static UTF8_CODE_LENGTH: [i32; 248] = {
const V: i32 = i32::MIN;
[
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, V, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
]
};