#![allow(dead_code)]
use std::sync::OnceLock;
pub const MUNGER_TRAP: u16 = 0xA9E0;
pub const MUNGER_TRAP_OFFSET: u16 = 0x01E0;
pub const XMUNGER_TRAP: u16 = 0xA819;
pub const XMUNGER_TRAP_OFFSET: u16 = 0x0019;
static TRACE_MUNGER: OnceLock<bool> = OnceLock::new();
pub fn trace_munger_enabled() -> bool {
*TRACE_MUNGER.get_or_init(|| std::env::var_os("SYSTEMLESS_TRACE_MUNGER").is_some())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MungerMode {
SearchOnly,
Insert,
Delete,
Replace,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MungerEvaluation {
pub return_offset: i32,
pub new_data: Option<Vec<u8>>,
pub mode: MungerMode,
}
impl MungerEvaluation {
#[inline]
pub fn is_success(&self) -> bool {
self.return_offset >= 0
}
#[inline]
pub fn is_modified(&self) -> bool {
self.new_data.is_some()
}
}
pub fn evaluate_munger(
data: &[u8],
offset: i32,
ptr1_is_null: bool,
needle: &[u8],
len1: i32,
ptr2_is_null: bool,
replacement: &[u8],
len2: i32,
) -> MungerEvaluation {
if offset < 0 {
return MungerEvaluation {
return_offset: -1,
new_data: None,
mode: if ptr2_is_null {
MungerMode::SearchOnly
} else {
MungerMode::Replace
},
};
}
let offset = offset as usize;
if offset > data.len() {
return MungerEvaluation {
return_offset: -1,
new_data: None,
mode: if ptr2_is_null {
MungerMode::SearchOnly
} else {
MungerMode::Replace
},
};
}
let mut replace_offset = offset;
let mut replace_len = len1.max(0) as usize;
if !ptr1_is_null && len1 > 0 {
let mut search = offset;
let mut found = None;
while search < data.len() {
let remaining = data.len() - search;
let compare_len = needle.len().min(remaining);
if compare_len > 0 && data[search..search + compare_len] == needle[..compare_len] {
found = Some((search, compare_len == needle.len()));
break;
}
search += 1;
}
let Some((found_offset, full_match)) = found else {
return MungerEvaluation {
return_offset: -1,
new_data: None,
mode: if ptr2_is_null {
MungerMode::SearchOnly
} else {
MungerMode::Replace
},
};
};
replace_offset = found_offset;
if full_match {
replace_len = needle.len();
} else {
return MungerEvaluation {
return_offset: -1,
new_data: None,
mode: if ptr2_is_null {
MungerMode::SearchOnly
} else {
MungerMode::Replace
},
};
}
} else if ptr1_is_null && len1 < 0 {
replace_len = data.len() - offset;
}
replace_len = replace_len.min(data.len().saturating_sub(replace_offset));
if ptr2_is_null && !ptr1_is_null {
return MungerEvaluation {
return_offset: replace_offset as i32,
new_data: None,
mode: MungerMode::SearchOnly,
};
}
let effective_replacement = if ptr2_is_null || len2 <= 0 {
&[][..]
} else {
let rep_len = (len2 as usize).min(replacement.len());
&replacement[..rep_len]
};
let tail_start = replace_offset + replace_len;
let mut new_data = Vec::with_capacity(data.len() - replace_len + effective_replacement.len());
new_data.extend_from_slice(&data[..replace_offset]);
new_data.extend_from_slice(effective_replacement);
new_data.extend_from_slice(&data[tail_start..]);
let return_offset = (replace_offset + effective_replacement.len()) as i32;
let mode = if replace_len == 0 {
MungerMode::Insert
} else if effective_replacement.is_empty() {
MungerMode::Delete
} else {
MungerMode::Replace
};
MungerEvaluation {
return_offset,
new_data: Some(new_data),
mode,
}
}
pub const TRUNC_STRING_SELECTOR: u32 = 0x8208_FFE0;
pub const TRUNC_TEXT_SELECTOR: u32 = 0x820C_FFDE;
pub const TRUNC_END: u16 = 0;
pub const TRUNC_MIDDLE: u16 = 0x4000;
pub const TRUNC_NOT_TRUNCATED: i16 = 0;
pub const TRUNC_TRUNCATED: i16 = 1;
pub const TRUNC_ERR: i16 = -1;
pub const MAC_ROMAN_ELLIPSIS: u8 = 0xC9;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TruncWhere {
End,
Middle,
}
impl TruncWhere {
pub fn from_code(code: u16) -> Option<Self> {
match code {
0 => Some(Self::End),
0x4000 => Some(Self::Middle),
_ => None,
}
}
pub fn to_code(self) -> u16 {
match self {
Self::End => TRUNC_END,
Self::Middle => TRUNC_MIDDLE,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TruncEvaluation {
pub result: i16,
pub new_text: Option<Vec<u8>>,
}
impl TruncEvaluation {
#[inline]
pub fn is_truncated(&self) -> bool {
self.result == TRUNC_TRUNCATED
}
#[inline]
pub fn is_success(&self) -> bool {
self.result >= 0
}
}
#[inline]
pub fn default_char_width_measure(bytes: &[u8]) -> i16 {
(bytes.len() as i16).saturating_mul(6)
}
pub fn evaluate_trunc_text(
text: &[u8],
width: i16,
trunc_where: u16,
ellipsis: u8,
mut measure: impl FnMut(&[u8]) -> i16,
) -> TruncEvaluation {
let Some(where_) = TruncWhere::from_code(trunc_where) else {
return TruncEvaluation {
result: TRUNC_ERR,
new_text: None,
};
};
if width < 0 {
return TruncEvaluation {
result: TRUNC_ERR,
new_text: None,
};
}
if measure(text) <= width {
return TruncEvaluation {
result: TRUNC_NOT_TRUNCATED,
new_text: None,
};
}
if measure(&[ellipsis]) > width {
return TruncEvaluation {
result: TRUNC_ERR,
new_text: None,
};
}
for retained in (0..text.len()).rev() {
let left = match where_ {
TruncWhere::Middle => retained.div_ceil(2),
TruncWhere::End => retained,
};
let right = retained - left;
let mut candidate = Vec::with_capacity(retained + 1);
candidate.extend_from_slice(&text[..left]);
candidate.push(ellipsis);
candidate.extend_from_slice(&text[text.len() - right..]);
if measure(&candidate) <= width {
return TruncEvaluation {
result: TRUNC_TRUNCATED,
new_text: Some(candidate),
};
}
}
TruncEvaluation {
result: TRUNC_ERR,
new_text: None,
}
}
pub fn evaluate_trunc_string(
string_bytes: &[u8],
width: i16,
trunc_where: u16,
ellipsis: u8,
measure: impl FnMut(&[u8]) -> i16,
) -> TruncEvaluation {
let clamped_len = string_bytes.len().min(255);
evaluate_trunc_text(
&string_bytes[..clamped_len],
width,
trunc_where,
ellipsis,
measure,
)
}
pub const STYLED_LINE_BREAK_SELECTOR: u32 = 0x821C_FFFE;
pub const SM_BREAK_WORD: u16 = 0;
pub const SM_BREAK_CHAR: u16 = 1;
pub const SM_BREAK_OVERFLOW: u16 = 2;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
pub enum StyledLineBreakCode {
Word = SM_BREAK_WORD,
Char = SM_BREAK_CHAR,
Overflow = SM_BREAK_OVERFLOW,
}
impl StyledLineBreakCode {
#[inline]
pub const fn to_code(self) -> u16 {
self as u16
}
#[inline]
pub const fn from_code(code: u16) -> Option<Self> {
match code {
SM_BREAK_WORD => Some(Self::Word),
SM_BREAK_CHAR => Some(Self::Char),
SM_BREAK_OVERFLOW => Some(Self::Overflow),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StyledLineBreakEvaluation {
pub code: u16,
pub output_offset: u32,
pub remaining_width: i32,
}
impl StyledLineBreakEvaluation {
#[inline]
pub fn break_code(&self) -> Option<StyledLineBreakCode> {
StyledLineBreakCode::from_code(self.code)
}
#[inline]
pub fn is_word_break(&self) -> bool {
self.code == SM_BREAK_WORD
}
#[inline]
pub fn is_char_break(&self) -> bool {
self.code == SM_BREAK_CHAR
}
#[inline]
pub fn is_overflow(&self) -> bool {
self.code == SM_BREAK_OVERFLOW
}
}
#[inline]
pub fn is_roman_break_space(byte: u8) -> bool {
matches!(byte, b' ' | b'\t' | b'\r' | b'\n')
}
pub fn find_last_space_break(
text: &[u8],
start: usize,
fit_end: usize,
text_end: usize,
) -> Option<usize> {
let mut break_offset = None;
let mut offset = start;
let limit = fit_end.min(text.len());
let end = text_end.min(text.len());
while offset < limit {
if is_roman_break_space(text[offset]) {
let mut after_spaces = offset + 1;
while after_spaces < end && is_roman_break_space(text[after_spaces]) {
after_spaces += 1;
}
break_offset = Some(after_spaces);
offset = after_spaces;
} else {
offset += 1;
}
}
break_offset
}
#[inline]
pub fn fixed_to_pixels(raw: u32) -> i32 {
let signed = raw as i32;
if (raw & 0xFFFF_0000) != 0 {
signed >> 16
} else {
signed
}
}
#[inline]
pub fn pixels_to_width_like(raw: u32, pixels: i32) -> u32 {
let pixels = pixels.clamp(0, 0x7FFF);
if (raw & 0xFFFF_0000) != 0 {
((pixels as i32) << 16) as u32
} else {
pixels as u32
}
}
#[inline]
pub fn default_char_advance_measure(_byte: u8) -> i32 {
6
}
pub fn evaluate_styled_line_break(
text: &[u8],
text_start: u32,
text_end: u32,
available_pixels: i32,
first_style_run_on_line: bool,
mut measure_char: impl FnMut(u8) -> i32,
) -> StyledLineBreakEvaluation {
let len = text.len() as u32;
let start = text_start.min(len) as usize;
let end = text_end.max(text_start).min(len) as usize;
let available = available_pixels.max(0);
let mut run_width = 0i32;
let mut fit_offset = start;
for offset in start..end {
let byte_width = measure_char(text[offset]).max(0);
if run_width.saturating_add(byte_width) > available {
break;
}
run_width = run_width.saturating_add(byte_width);
fit_offset = offset + 1;
}
if fit_offset == end {
let remaining = available.saturating_sub(run_width);
return StyledLineBreakEvaluation {
code: SM_BREAK_OVERFLOW,
output_offset: end as u32,
remaining_width: remaining,
};
}
if let Some(word_offset) = find_last_space_break(text, start, fit_offset, end) {
let mut word_width = 0i32;
for offset in start..word_offset {
word_width = word_width.saturating_add(measure_char(text[offset]).max(0));
}
let remaining = available.saturating_sub(word_width).max(0);
StyledLineBreakEvaluation {
code: SM_BREAK_WORD,
output_offset: word_offset as u32,
remaining_width: remaining,
}
} else if first_style_run_on_line {
let char_offset = if fit_offset > start {
fit_offset
} else {
(start + 1).min(end)
};
let mut char_width = 0i32;
for offset in start..char_offset {
char_width = char_width.saturating_add(measure_char(text[offset]).max(0));
}
let remaining = available.saturating_sub(char_width).max(0);
StyledLineBreakEvaluation {
code: SM_BREAK_CHAR,
output_offset: char_offset as u32,
remaining_width: remaining,
}
} else {
StyledLineBreakEvaluation {
code: SM_BREAK_WORD,
output_offset: start as u32,
remaining_width: available,
}
}
}
pub const REL_STRING_TRAP: u16 = 0xA050;
pub const REL_STRING_TRAP_NUM: u16 = 0x50;
pub const CMP_STRING_TRAP: u16 = 0xA03C;
pub const CMP_STRING_TRAP_NUM: u16 = 0x3C;
pub const REL_STRING_LESS: i16 = -1;
pub const REL_STRING_EQUAL: i16 = 0;
pub const REL_STRING_GREATER: i16 = 1;
pub fn mac_roman_strip_diacriticals(ch: u8) -> u8 {
match ch {
0x80 => b'A', 0x81 => b'A', 0x82 => b'C', 0x83 => b'E', 0x84 => b'N', 0x85 => b'O', 0x86 => b'U', 0xAE => b'A', 0xAF => b'O', 0xCB => b'A', 0xCC => b'A', 0xCD => b'O', 0xCE => b'O', 0x87 => b'a', 0x88 => b'a', 0x89 => b'a', 0x8A => b'a', 0x8B => b'a', 0x8C => b'a', 0x8D => b'c', 0x8E => b'e', 0x8F => b'e', 0x90 => b'e', 0x91 => b'e', 0x92 => b'i', 0x93 => b'i', 0x94 => b'i', 0x95 => b'i', 0x96 => b'n', 0x97 => b'o', 0x98 => b'o', 0x99 => b'o', 0x9A => b'o', 0x9B => b'o', 0x9C => b'u', 0x9D => b'u', 0x9E => b'u', 0x9F => b'u', 0xBB => b'a', 0xBC => b'o', 0xBE => b'a', 0xBF => b'o', 0xCF => b'o', 0xD8 => b'y', _ => ch,
}
}
pub fn mac_roman_to_upper(ch: u8, strip_marks: bool) -> u8 {
if strip_marks {
let stripped = mac_roman_strip_diacriticals(ch);
if stripped.is_ascii_lowercase() {
return stripped - 32;
}
return stripped;
}
if ch.is_ascii_lowercase() {
return ch - 32;
}
match ch {
0x88 => 0xCB, 0x8A => 0x80, 0x8B => 0xCC, 0x8C => 0x81, 0x8D => 0x82, 0x8E => 0x83, 0x96 => 0x84, 0x9A => 0x85, 0x9B => 0xCD, 0x9F => 0x86, 0xBE => 0xAE, 0xBF => 0xAF, 0xCF => 0xCE, _ => ch,
}
}
pub fn mac_roman_to_lower(ch: u8) -> u8 {
if ch.is_ascii_uppercase() {
return ch + 32;
}
match ch {
0x80 => 0x8A, 0x81 => 0x8C, 0x82 => 0x8D, 0x83 => 0x8E, 0x84 => 0x96, 0x85 => 0x9A, 0x86 => 0x9F, 0xAE => 0xBE, 0xAF => 0xBF, 0xCB => 0x88, 0xCC => 0x8B, 0xCD => 0x9B, 0xCE => 0xCF, _ => ch,
}
}
pub fn evaluate_rel_string(
str_a: &[u8],
str_b: &[u8],
case_sensitive: bool,
diac_sensitive: bool,
) -> i16 {
let min_len = std::cmp::min(str_a.len(), str_b.len());
for i in 0..min_len {
let mut a = str_a[i];
let mut b = str_b[i];
if !diac_sensitive {
a = mac_roman_strip_diacriticals(a);
b = mac_roman_strip_diacriticals(b);
}
if !case_sensitive {
a = mac_roman_to_upper(a, false);
b = mac_roman_to_upper(b, false);
}
if a < b {
return REL_STRING_LESS;
} else if a > b {
return REL_STRING_GREATER;
}
}
match str_a.len().cmp(&str_b.len()) {
std::cmp::Ordering::Less => REL_STRING_LESS,
std::cmp::Ordering::Equal => REL_STRING_EQUAL,
std::cmp::Ordering::Greater => REL_STRING_GREATER,
}
}
pub fn evaluate_equal_string(
str_a: &[u8],
str_b: &[u8],
case_sensitive: bool,
diac_sensitive: bool,
) -> bool {
if str_a.len() != str_b.len() {
return false;
}
str_a.iter().zip(str_b.iter()).all(|(&a_byte, &b_byte)| {
let mut a = a_byte;
let mut b = b_byte;
if !diac_sensitive {
a = mac_roman_strip_diacriticals(a);
b = mac_roman_strip_diacriticals(b);
}
if !case_sensitive {
a = mac_roman_to_upper(a, false);
b = mac_roman_to_upper(b, false);
}
a == b
})
}
pub fn evaluate_iu_comp_string(str_a: &[u8], str_b: &[u8]) -> i16 {
let min_len = std::cmp::min(str_a.len(), str_b.len());
for i in 0..min_len {
let a = mac_roman_to_upper(str_a[i], true);
let b = mac_roman_to_upper(str_b[i], true);
if a < b {
return REL_STRING_LESS;
} else if a > b {
return REL_STRING_GREATER;
}
}
match str_a.len().cmp(&str_b.len()) {
std::cmp::Ordering::Less => REL_STRING_LESS,
std::cmp::Ordering::Equal => REL_STRING_EQUAL,
std::cmp::Ordering::Greater => REL_STRING_GREATER,
}
}
pub fn evaluate_iu_equal_string(str_a: &[u8], str_b: &[u8]) -> bool {
if str_a.len() != str_b.len() {
return false;
}
str_a
.iter()
.map(|&byte| mac_roman_to_upper(byte, true))
.eq(str_b.iter().map(|&byte| mac_roman_to_upper(byte, true)))
}
pub fn evaluate_mag_string(a: &[u8], b: &[u8]) -> i16 {
match a.cmp(b) {
std::cmp::Ordering::Less => -1,
std::cmp::Ordering::Equal => 0,
std::cmp::Ordering::Greater => 1,
}
}
pub fn evaluate_mag_id_string(a: &[u8], b: &[u8]) -> u16 {
if a.len() != b.len() {
return 1;
}
if a.iter()
.map(|&c| c.to_ascii_lowercase())
.eq(b.iter().map(|&c| c.to_ascii_lowercase()))
{
0
} else {
1
}
}
pub fn evaluate_script_order(script1: i16, script2: i16) -> i16 {
match script1.cmp(&script2) {
std::cmp::Ordering::Less => -1,
std::cmp::Ordering::Equal => 0,
std::cmp::Ordering::Greater => 1,
}
}
pub fn evaluate_lang_order(lang1: i16, lang2: i16) -> i16 {
match lang1.cmp(&lang2) {
std::cmp::Ordering::Less => -1,
std::cmp::Ordering::Equal => 0,
std::cmp::Ordering::Greater => 1,
}
}
pub fn evaluate_text_order(
a: &[u8],
b: &[u8],
a_script: i16,
b_script: i16,
a_lang: i16,
b_lang: i16,
) -> i16 {
if a_script != b_script {
evaluate_script_order(a_script, b_script)
} else if a_lang != b_lang {
evaluate_lang_order(a_lang, b_lang)
} else {
evaluate_mag_string(a, b)
}
}
#[inline]
pub fn evaluate_string_order(
a: &[u8],
b: &[u8],
a_script: i16,
b_script: i16,
a_lang: i16,
b_lang: i16,
) -> i16 {
evaluate_text_order(a, b, a_script, b_script, a_lang, b_lang)
}
pub fn evaluate_iu_metric() -> bool {
false
}
pub const UPR_STRING_TRAP: u16 = 0xA054;
pub const UPR_STRING_TRAP_NUM: u16 = 0x54;
pub const TEXT_CONVERT_TRAP: u16 = 0xA056;
pub const TEXT_CONVERT_TRAP_NUM: u16 = 0x56;
pub const LOWER_TEXT_TRAP: u16 = 0xA056;
pub const STRIP_TEXT_TRAP: u16 = 0xA256;
pub const UPPER_TEXT_TRAP: u16 = 0xA456;
pub const STRIP_UPPER_TEXT_TRAP: u16 = 0xA656;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextTransformOperation {
Lower,
StripDiacritics,
Upper,
StripUpper,
}
#[inline]
pub fn evaluate_transform_char(ch: u8, op: TextTransformOperation) -> u8 {
match op {
TextTransformOperation::Lower => mac_roman_to_lower(ch),
TextTransformOperation::StripDiacritics => mac_roman_strip_diacriticals(ch),
TextTransformOperation::Upper => mac_roman_to_upper(ch, false),
TextTransformOperation::StripUpper => mac_roman_to_upper(ch, true),
}
}
pub fn evaluate_transform_text(bytes: &mut [u8], op: TextTransformOperation) {
for b in bytes.iter_mut() {
*b = evaluate_transform_char(*b, op);
}
}
pub fn evaluate_upper_string(bytes: &mut [u8], diac_sensitive: bool) {
let strip_diacritics = !diac_sensitive;
for b in bytes.iter_mut() {
*b = mac_roman_to_upper(*b, strip_diacritics);
}
}
pub const PACK6_TRAP: u16 = 0xA9ED;
pub const PACK6_TRAP_OFFSET: u16 = 0x01ED;
pub const PACK6_IU_DATE_STRING_SELECTOR: u16 = 0x0000;
pub const PACK6_IU_TIME_STRING_SELECTOR: u16 = 0x0002;
pub const PACK6_IU_METRIC_SELECTOR: u16 = 0x0004;
pub const PACK6_IU_GET_INTL_SELECTOR: u16 = 0x0006;
pub const PACK6_IU_SET_INTL_SELECTOR: u16 = 0x0008;
pub const PACK6_IU_MAG_STRING_SELECTOR: u16 = 0x000A;
pub const PACK6_IU_MAG_ID_STRING_SELECTOR: u16 = 0x000C;
pub const PACK6_IU_DATE_P_STRING_SELECTOR: u16 = 0x000E;
pub const PACK6_IU_TIME_P_STRING_SELECTOR: u16 = 0x0010;
pub const PACK6_IU_L_DATE_STRING_SELECTOR: u16 = 0x0014;
pub const PACK6_IU_L_TIME_STRING_SELECTOR: u16 = 0x0016;
pub const PACK6_IU_CLEAR_CACHE_SELECTOR: u16 = 0x0018;
pub const PACK6_IU_MAG_P_STRING_SELECTOR: u16 = 0x001A; pub const PACK6_IU_MAG_ID_P_STRING_SELECTOR: u16 = 0x001C; pub const PACK6_IU_SCRIPT_ORDER_SELECTOR: u16 = 0x001E;
pub const PACK6_IU_LANG_ORDER_SELECTOR: u16 = 0x0020;
pub const PACK6_IU_TEXT_ORDER_SELECTOR: u16 = 0x0022;
pub const PACK6_IU_GET_INTL_TABLE_SELECTOR: u16 = 0x0024;
pub const SM_WORD_SELECT_TABLE: i16 = 0;
pub const SM_WORD_WRAP_TABLE: i16 = 1;
pub const SM_NUMBER_PARTS_TABLE: i16 = 2;
pub const SM_UN_TOKEN_TABLE: i16 = 3;
pub const SM_WHITE_SPACE_LIST: i16 = 4;
pub const IU_WORD_SELECT_TABLE: i16 = SM_WORD_SELECT_TABLE;
pub const IU_WORD_WRAP_TABLE: i16 = SM_WORD_WRAP_TABLE;
pub const IU_NUMBER_PARTS_TABLE: i16 = SM_NUMBER_PARTS_TABLE;
pub const IU_UN_TOKEN_TABLE: i16 = SM_UN_TOKEN_TABLE;
pub const IU_WHITE_SPACE_LIST: i16 = SM_WHITE_SPACE_LIST;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IntlResourceTableKind {
WordSelect,
WordWrap,
NumberParts,
UnToken,
WhiteSpace,
}
impl IntlResourceTableKind {
pub fn from_table_code(table_code: i16) -> Option<Self> {
match table_code {
0 => Some(Self::WordSelect),
1 => Some(Self::WordWrap),
2 => Some(Self::NumberParts),
3 => Some(Self::UnToken),
4 => Some(Self::WhiteSpace),
_ => None,
}
}
pub fn target_resource_tag(&self) -> &'static [u8; 4] {
match self {
Self::WordSelect | Self::WordWrap => b"itl2",
Self::NumberParts | Self::UnToken | Self::WhiteSpace => b"itl4",
}
}
}
pub const PACK7_TRAP: u16 = 0xA9EE;
pub const PACK7_TRAP_OFFSET: u16 = 0x01EE;
pub const NUM_TO_STRING_SELECTOR: u16 = 0;
pub const STRING_TO_NUM_SELECTOR: u16 = 1;
#[inline]
pub fn evaluate_num_to_string(number: i32) -> Vec<u8> {
format!("{}", number).into_bytes()
}
pub fn evaluate_string_to_num(bytes: &[u8]) -> i32 {
let mut index = 0usize;
while bytes
.get(index)
.is_some_and(|byte| byte.is_ascii_whitespace())
{
index += 1;
}
let mut sign = 1i64;
if let Some(&byte) = bytes.get(index) {
if byte == b'-' {
sign = -1;
index += 1;
} else if byte == b'+' {
index += 1;
}
}
let mut value = 0i64;
while let Some(&byte) = bytes.get(index) {
if !byte.is_ascii_digit() {
break;
}
value = value.wrapping_mul(10).wrapping_add(i64::from(byte - b'0'));
index += 1;
}
value.wrapping_mul(sign) as i32
}
pub const VISIBLE_LENGTH_SELECTOR: u32 = 0x8408_0028;
pub const REPLACE_TEXT_SELECTOR: u32 = 0x820C_FFDC;
pub const FONT2SCRIPT_SELECTOR: i32 = 6;
pub const CHAR_BYTE_SELECTOR: i32 = 16;
pub const CHAR_TYPE_SELECTOR: i32 = 18;
pub const HILITE_TEXT_SELECTOR: u32 = 0x800E_001C;
pub const HILITE_TEXT_LEGACY_SELECTOR: i32 = 28;
pub const NFIND_WORD_SELECTOR: u32 = 0x8012_FFE2;
pub const PARSE_TABLE_SELECTOR: u32 = 0x8204_0022;
pub const PORTION_TEXT_SELECTOR: u32 = 0x8408_0024;
pub const FIND_SCRIPT_RUN_SELECTOR: u32 = 0x820C_0026;
pub const FIND_WORD_BREAKS_SELECTOR: u32 = 0xC012_001A;
pub const GET_FORMAT_ORDER_SELECTOR: u32 = 0x8012_FFFC;
pub const PIXEL2CHAR_SELECTOR: i32 = 20;
pub const CHAR2PIXEL_SELECTOR: i32 = 22;
pub const TRANSLITERATE_SELECTOR: i32 = 24;
pub const FIND_WORD_SELECTOR: i32 = 26;
pub const DRAW_JUST_SELECTOR: i32 = 30;
pub const MEASURE_JUST_SELECTOR: i32 = 32;
pub const SM_TRANS_ASCII: i16 = 0;
pub const SM_TRANS_NATIVE: i16 = 1;
pub const SM_TRANS_CASE: i16 = 0x00FE;
pub const SM_TRANS_SYSTEM: i16 = 0x00FF;
pub const SM_TRANS_LOWER: i16 = 0x4000;
pub const SM_MASK_ALL: u32 = 0xFFFF_FFFF;
pub const SM_MASK_ASCII: u32 = 0x0000_0001;
pub const FONT_SCRIPT_SELECTOR: i32 = 0;
pub const INTL_SCRIPT_SELECTOR: i32 = 2;
pub const KEY_SCRIPT_SELECTOR: i32 = 4;
pub const SM_CHAR_ASCII: i16 = 0;
pub const SM_CHAR_EURO: i16 = 7;
pub const SM_CHAR_PUNCT: i16 = 2;
pub const SM_CHAR_LEFT: i16 = 0;
pub const SM_CHAR_RIGHT: i16 = 1;
pub const SMC_TYPE_MASK: i16 = 0x000F;
pub const SMC_RESERVED: i16 = 0x00F0;
pub const SMC_CLASS_MASK: i16 = 0x0F00;
pub const SMC_ORIENTATION_MASK: i16 = 0x1000;
pub const SMC_RIGHT_MASK: i16 = 0x2000;
pub const SMC_UPPER_MASK: i16 = 0x4000;
pub const SMC_DOUBLE_MASK: i16 = -0x8000;
pub const SM_CHAR_TYPE_PUNCT: i16 = 0x0000;
pub const SM_CHAR_TYPE_ASCII: i16 = 0x0001;
pub const SM_CHAR_TYPE_EXT_ASCII: i16 = 0x0007;
pub const SM_PUNCT_NORMAL: i16 = 0x0000;
pub const SM_PUNCT_NUMBER: i16 = 0x0100;
pub const SM_PUNCT_SYMBOL: i16 = 0x0200;
pub const SM_PUNCT_BLANK: i16 = 0x0300;
pub const SM_CHAR_HORIZONTAL: i16 = 0x0000;
pub const SM_CHAR_VERTICAL: i16 = 0x1000;
pub const SM_CHAR_LOWER: i16 = 0x0000;
pub const SM_CHAR_UPPER: i16 = 0x4000;
pub const SM_CHAR_1BYTE: i16 = 0x0000;
pub const SM_CHAR_2BYTE: i16 = -0x8000;
pub const GET_ENVIRONS_SELECTOR: i32 = 8;
pub const SET_ENVIRONS_SELECTOR: i32 = 10;
pub const GET_SCRIPT_SELECTOR: i32 = 12;
pub const SET_SCRIPT_SELECTOR: i32 = 14;
pub const SYS_DIRECTION: u32 = 0x0BAC;
pub const SM_VERSION: i16 = 0;
pub const SM_ENABLED: i16 = 2;
pub const SM_BIDIRECT: i16 = 4;
pub const SM_FONT: i16 = 6;
pub const SM_INTL: i16 = 8;
pub const SM_PRINT: i16 = 10;
pub const SM_SYS_SCRIPT: i16 = 12;
pub const SM_LAST_SCRIPT: i16 = 14;
pub const SM_KEY_SCRIPT: i16 = 16;
pub const SM_SYS_FONT: i16 = 18;
pub const SM_APP_FONT: i16 = 20;
pub const SM_KCHR_CACHE: i16 = 38;
pub const SM_KEY_DISABLE_KCHR: i16 = 40;
pub const SM_DUAL_CARET: i16 = 42;
pub const SM_SCRIPT_VERSION: i16 = 0;
pub const SM_SCRIPT_ENABLED: i16 = 2;
pub const SM_SCRIPT_RIGHT: i16 = 4;
pub const SM_SCRIPT_SINGLE: i16 = 6;
pub const SM_SCRIPT_PREF_SYS: i16 = 8;
pub const SM_SCRIPT_DEF_FONT_SIZE: i16 = 12;
pub const SM_SCRIPT_APP_FOND: i16 = 14;
pub const SM_SCRIPT_BUNDLE: i16 = 16;
pub const SM_SCRIPT_SYS_FOND: i16 = 20;
pub const SM_SCRIPT_PUNCT: i16 = 22;
pub const SM_SCRIPT_KEYS: i16 = 28;
pub const SM_SCRIPT_ICON: i16 = 30;
pub const NIL_HANDLE_ERR: i16 = -109;
pub const MEM_WZ_ERR: i16 = -111;
pub const MEM_FULL_ERR: i16 = -108;
pub const SM_SINGLE_BYTE: i16 = 0;
pub const SM_FIRST_BYTE: i16 = 1;
pub const SM_LAST_BYTE: i16 = -1;
pub const SM_MIDDLE_BYTE: i16 = 2;
pub const SM_ROMAN: u8 = 0;
pub const SM_JAPANESE: u8 = 1;
pub const SM_TRAD_CHINESE: u8 = 2;
pub const SM_KOREAN: u8 = 3;
pub const SM_SIMP_CHINESE: u8 = 25;
#[inline]
pub fn font_id_to_script(font_id: i16) -> u8 {
let font = font_id as u16;
if (0x4000..=0xBFFF).contains(&font) {
(((font - 0x4000) / 0x0200) + 1) as u8
} else {
0
}
}
#[inline]
pub fn is_script_enabled(script: u8) -> bool {
script == SM_ROMAN
}
#[inline]
pub fn font_id_to_enabled_script(font_id: i16) -> u8 {
let script = font_id_to_script(font_id);
if is_script_enabled(script) {
script
} else {
SM_ROMAN
}
}
#[inline]
pub fn script_is_multibyte(script: u8) -> bool {
matches!(
script,
SM_JAPANESE | SM_TRAD_CHINESE | SM_KOREAN | SM_SIMP_CHINESE
)
}
#[inline]
pub fn script_is_lead_byte(script: u8, byte: u8) -> bool {
match script {
SM_JAPANESE => (0x81..=0x9F).contains(&byte) || (0xE0..=0xFC).contains(&byte),
SM_TRAD_CHINESE | SM_KOREAN | SM_SIMP_CHINESE => (0xA1..=0xFE).contains(&byte),
_ => false,
}
}
pub fn script_character_len(script: u8, text: &[u8], offset: usize) -> usize {
let Some(&byte) = text.get(offset) else {
return 0;
};
if script_is_lead_byte(script, byte) && offset + 1 < text.len() {
2
} else {
1
}
}
pub fn script_is_complete_text(script: u8, text: &[u8]) -> bool {
let mut offset = 0;
while offset < text.len() {
if script_is_lead_byte(script, text[offset]) {
if offset + 1 == text.len() {
return false;
}
offset += 2;
} else {
offset += 1;
}
}
true
}
pub fn evaluate_char_byte(text: &[u8], offset: usize, script: u8) -> i16 {
if !script_is_multibyte(script) || offset >= text.len() {
return SM_SINGLE_BYTE;
}
let mut pos = 0;
while pos < text.len() {
if script_is_lead_byte(script, text[pos]) && pos + 1 < text.len() {
if pos == offset {
return SM_FIRST_BYTE;
}
if pos + 1 == offset {
return SM_LAST_BYTE;
}
pos += 2;
} else {
if pos == offset {
return SM_SINGLE_BYTE;
}
pos += 1;
}
}
SM_SINGLE_BYTE
}
#[inline]
pub fn evaluate_visible_length(bytes: &[u8]) -> usize {
let mut visible = bytes.len();
while visible > 0 && matches!(bytes[visible - 1], b' ' | b'\t') {
visible -= 1;
}
visible
}
pub fn evaluate_replace_text(
base: &[u8],
substitution: &[u8],
key: &[u8],
script: u8,
) -> (i16, Option<Vec<u8>>) {
if base.is_empty() || key.is_empty() {
return (0, None);
}
if !script_is_complete_text(script, key) {
return (0, None);
}
let mut offset = 0;
let mut substitutions = 0i16;
let mut replaced = Vec::with_capacity(base.len());
while offset < base.len() {
let candidate_end = offset.saturating_add(key.len());
let matches_key = candidate_end <= base.len() && base[offset..candidate_end] == *key;
if matches_key {
replaced.extend_from_slice(substitution);
substitutions = substitutions.saturating_add(1);
offset = candidate_end;
} else {
let char_len = script_character_len(script, base, offset);
let next_offset = offset.saturating_add(char_len).min(base.len());
replaced.extend_from_slice(&base[offset..next_offset]);
offset = next_offset;
}
}
if substitutions == 0 {
(0, None)
} else {
(substitutions, Some(replaced))
}
}
pub fn evaluate_hilite_text(
text_len: usize,
first_offset: usize,
second_offset: usize,
) -> [(i16, i16); 3] {
let max_len = text_len.min(i16::MAX as usize);
let low = first_offset.min(second_offset).min(max_len) as i16;
let high = first_offset.max(second_offset).min(max_len) as i16;
[(low, high), (0, 0), (0, 0)]
}
pub fn evaluate_find_word(text: &[u8], offset: usize) -> [(i16, i16); 3] {
let len = text.len().min(i16::MAX as usize);
if len == 0 {
return [(0, 0), (0, 0), (0, 0)];
}
if offset >= len {
let l = len as i16;
return [(l, l), (0, 0), (0, 0)];
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum CharClass {
Whitespace,
Word,
Punctuation,
}
fn classify(b: u8) -> CharClass {
if matches!(b, b' ' | b'\t' | b'\r' | b'\n') {
CharClass::Whitespace
} else if b.is_ascii_alphanumeric() || b == b'_' || (0x80..=0xFE).contains(&b) {
CharClass::Word
} else {
CharClass::Punctuation
}
}
let target_class = classify(text[offset]);
let mut start = offset;
while start > 0 && classify(text[start - 1]) == target_class {
start -= 1;
}
let mut end = offset + 1;
while end < len && classify(text[end]) == target_class {
end += 1;
}
[(start as i16, end as i16), (0, 0), (0, 0)]
}
pub fn evaluate_parse_table(table: &mut [u8; 256], script: u8) -> bool {
if !script_is_multibyte(script) {
table.fill(SM_SINGLE_BYTE as u8);
false
} else {
for (byte, entry) in table.iter_mut().enumerate() {
if script_is_lead_byte(script, byte as u8) {
*entry = SM_FIRST_BYTE as u8;
} else {
*entry = SM_SINGLE_BYTE as u8;
}
}
true
}
}
pub fn evaluate_portion_text(text: &[u8]) -> i32 {
if text.is_empty() {
return 0;
}
let spaces = text.iter().filter(|&&b| b == b' ').count();
if spaces > 0 {
(spaces as i32) << 16
} else {
0x0001_0000
}
}
#[inline]
pub fn evaluate_find_script_run(text_len: usize, script: u8) -> (u8, usize) {
(script, text_len)
}
#[inline]
pub fn evaluate_find_word_breaks(text: &[u8], offset: usize) -> [(i16, i16); 3] {
evaluate_find_word(text, offset)
}
pub fn evaluate_get_format_order(
first_format: i16,
last_format: i16,
line_right: bool,
) -> Vec<i16> {
if first_format > last_format {
return Vec::new();
}
let count = (last_format - first_format + 1) as usize;
let mut ordering = Vec::with_capacity(count);
if line_right {
for i in 0..count {
ordering.push(last_format - i as i16);
}
} else {
for i in 0..count {
ordering.push(first_format + i as i16);
}
}
ordering
}
pub fn evaluate_char2pixel(text: &[u8], slop: i16, offset: i16, direction: i16) -> i16 {
if text.is_empty() || offset <= 0 {
return 0;
}
let clamped_offset = (offset as usize).min(text.len());
let mut base_px: i32 = text[..clamped_offset]
.iter()
.map(|&b| default_char_advance_measure(b))
.sum();
if slop != 0 {
let space_count = text.iter().filter(|&&b| b == 0x20).count();
let extra_slop: i32 = if space_count > 0 {
let spaces_sub = text[..clamped_offset]
.iter()
.filter(|&&b| b == 0x20)
.count();
(slop as i32 * spaces_sub as i32) / space_count as i32
} else {
(slop as i32 * clamped_offset as i32) / text.len() as i32
};
base_px += extra_slop;
}
if direction == -1 {
let total_base: i32 = text
.iter()
.map(|&b| default_char_advance_measure(b))
.sum::<i32>()
+ slop as i32;
let dist = (total_base - base_px).max(0);
dist.clamp(0, i16::MAX as i32) as i16
} else {
base_px.clamp(0, i16::MAX as i32) as i16
}
}
pub fn evaluate_pixel2char(text: &[u8], slop: i16, pixel_width: i16) -> (i16, bool) {
if text.is_empty() || pixel_width <= 0 {
return (0, false);
}
let total_width = evaluate_char2pixel(text, slop, text.len() as i16, 0) as i32;
if pixel_width as i32 >= total_width {
return (text.len() as i16, false);
}
for i in 0..text.len() {
let left = evaluate_char2pixel(text, slop, i as i16, 0) as i32;
let right = evaluate_char2pixel(text, slop, (i + 1) as i16, 0) as i32;
if (pixel_width as i32) < right {
let mid = (left + right) / 2;
let leading_edge = (pixel_width as i32) <= mid;
return (i as i16, leading_edge);
}
}
(text.len() as i16, false)
}
pub fn evaluate_measure_just(text: &[u8], slop: i16) -> Vec<i16> {
let mut locs = Vec::with_capacity(text.len() + 1);
locs.push(0);
for i in 1..=text.len() {
locs.push(evaluate_char2pixel(text, slop, i as i16, 0));
}
locs
}
pub fn evaluate_transliterate(src: &[u8], target: i16, _src_mask: u32) -> (Vec<u8>, i16) {
let base_target = target & 0x00FF;
let is_case = base_target == SM_TRANS_CASE;
if is_case {
let mut out = src.to_vec();
if (target & SM_TRANS_LOWER) != 0 {
evaluate_transform_text(&mut out, TextTransformOperation::Lower);
} else {
evaluate_transform_text(&mut out, TextTransformOperation::Upper);
}
(out, 0)
} else {
(src.to_vec(), 0)
}
}
pub fn evaluate_char_type(text: &[u8], offset: usize) -> i16 {
let Some(&byte) = text.get(offset) else {
return 0;
};
if byte < 0x80 {
0
} else {
SM_CHAR_EURO << 8
}
}
pub fn evaluate_script_manager_variable(selector: i16, kchr_ptr: u32) -> u32 {
match selector {
SM_VERSION => 0x0710, SM_ENABLED => 1, SM_BIDIRECT => 0, SM_FONT => 0, SM_INTL => 0, SM_PRINT => 0,
SM_SYS_SCRIPT => SM_ROMAN as u32,
SM_LAST_SCRIPT => SM_ROMAN as u32,
SM_KEY_SCRIPT => SM_ROMAN as u32,
SM_SYS_FONT => 0, SM_APP_FONT => 1, SM_KCHR_CACHE => kchr_ptr,
SM_KEY_DISABLE_KCHR => 0,
SM_DUAL_CARET => 0,
_ => 0,
}
}
pub fn evaluate_script_variable(script: i16, selector: i16) -> u32 {
let is_roman = script == SM_ROMAN as i16;
match selector {
SM_SCRIPT_VERSION => {
if is_roman {
0x0710
} else {
0
}
}
SM_SCRIPT_ENABLED => {
if is_roman {
1
} else {
0
}
}
SM_SCRIPT_RIGHT => 0, SM_SCRIPT_SINGLE => {
if is_roman {
1
} else {
0
}
}
SM_SCRIPT_PREF_SYS => 0,
SM_SCRIPT_DEF_FONT_SIZE => {
if is_roman {
12
} else {
0
}
}
SM_SCRIPT_APP_FOND => {
if is_roman {
1 } else {
0
}
}
SM_SCRIPT_BUNDLE => 0,
SM_SCRIPT_SYS_FOND => 0, SM_SCRIPT_PUNCT => 0,
SM_SCRIPT_KEYS => 0,
SM_SCRIPT_ICON => 0,
_ => 0,
}
}
#[inline]
pub fn evaluate_get_sys_direction(raw_direction: u16) -> i16 {
raw_direction as i16
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_munger_replaces_first_occurrence() {
let data = b"Hello, ^0 world";
let needle = b"^0";
let replacement = b"Ace";
let eval = evaluate_munger(
data,
0,
false,
needle,
needle.len() as i32,
false,
replacement,
replacement.len() as i32,
);
assert_eq!(eval.return_offset, 10);
assert_eq!(eval.mode, MungerMode::Replace);
assert!(eval.is_success());
assert!(eval.is_modified());
assert_eq!(
eval.new_data.as_deref(),
Some(b"Hello, Ace world".as_slice())
);
}
#[test]
fn test_munger_search_only_mode() {
let data = b"there's the apple";
let needle = b"the";
let eval = evaluate_munger(
data,
4,
false,
needle,
needle.len() as i32,
true, &[],
0,
);
assert_eq!(eval.return_offset, 8);
assert_eq!(eval.mode, MungerMode::SearchOnly);
assert!(eval.is_success());
assert!(!eval.is_modified());
assert_eq!(eval.new_data, None);
}
#[test]
fn test_munger_len1_zero_inserts_at_offset() {
let data = b"apple";
let replacement = b"X";
let eval = evaluate_munger(
data,
2,
false,
&[],
0, false,
replacement,
replacement.len() as i32,
);
assert_eq!(eval.return_offset, 3);
assert_eq!(eval.mode, MungerMode::Insert);
assert_eq!(eval.new_data.as_deref(), Some(b"apXple".as_slice()));
}
#[test]
fn test_munger_len2_zero_deletes_target() {
let data = b"abc123def";
let needle = b"123";
let eval = evaluate_munger(
data,
0,
false,
needle,
needle.len() as i32,
false, &[],
0, );
assert_eq!(eval.return_offset, 3);
assert_eq!(eval.mode, MungerMode::Delete);
assert_eq!(eval.new_data.as_deref(), Some(b"abcdef".as_slice()));
}
#[test]
fn test_munger_nil_ptr1_negative_len1_replaces_tail() {
let data = b"abcdef";
let replacement = b"XYZ";
let eval = evaluate_munger(
data,
3,
true, &[],
-1, false,
replacement,
replacement.len() as i32,
);
assert_eq!(eval.return_offset, 6);
assert_eq!(eval.mode, MungerMode::Replace);
assert_eq!(eval.new_data.as_deref(), Some(b"abcXYZ".as_slice()));
}
#[test]
fn test_munger_nil_ptr1_positive_len1_overwrites_bytes() {
let data = b"abcdef";
let replacement = b"99";
let eval = evaluate_munger(
data,
2,
true, &[],
2, false,
replacement,
replacement.len() as i32,
);
assert_eq!(eval.return_offset, 4);
assert_eq!(eval.mode, MungerMode::Replace);
assert_eq!(eval.new_data.as_deref(), Some(b"ab99ef".as_slice()));
}
#[test]
fn test_munger_target_not_found_returns_negative() {
let data = b"hello";
let needle = b"zz";
let replacement = b"A";
let eval = evaluate_munger(
data,
0,
false,
needle,
needle.len() as i32,
false,
replacement,
replacement.len() as i32,
);
assert_eq!(eval.return_offset, -1);
assert_eq!(eval.new_data, None);
assert!(!eval.is_success());
}
#[test]
fn test_munger_partial_tail_match_returns_negative() {
let data = b"ab";
let needle = b"abc";
let replacement = b"Z";
let eval = evaluate_munger(
data,
0,
false,
needle,
needle.len() as i32,
false,
replacement,
replacement.len() as i32,
);
assert_eq!(eval.return_offset, -1);
assert_eq!(eval.new_data, None);
}
#[test]
fn test_munger_offset_out_of_bounds() {
let data = b"abc";
let eval_negative = evaluate_munger(data, -1, false, b"a", 1, true, &[], 0);
assert_eq!(eval_negative.return_offset, -1);
assert_eq!(eval_negative.new_data, None);
let eval_past_end = evaluate_munger(data, 4, false, b"a", 1, true, &[], 0);
assert_eq!(eval_past_end.return_offset, -1);
assert_eq!(eval_past_end.new_data, None);
}
#[test]
fn test_trap_constants() {
assert_eq!(MUNGER_TRAP, 0xA9E0);
assert_eq!(XMUNGER_TRAP, 0xA819);
assert_eq!(MUNGER_TRAP_OFFSET, 0x01E0);
assert_eq!(XMUNGER_TRAP_OFFSET, 0x0019);
assert_eq!(0xA800 | MUNGER_TRAP_OFFSET, MUNGER_TRAP);
assert_eq!(0xA800 | XMUNGER_TRAP_OFFSET, XMUNGER_TRAP);
}
#[test]
fn test_trunc_constants() {
assert_eq!(TRUNC_STRING_SELECTOR, 0x8208_FFE0);
assert_eq!(TRUNC_TEXT_SELECTOR, 0x820C_FFDE);
assert_eq!(TRUNC_END, 0);
assert_eq!(TRUNC_MIDDLE, 0x4000);
assert_eq!(TRUNC_NOT_TRUNCATED, 0);
assert_eq!(TRUNC_TRUNCATED, 1);
assert_eq!(TRUNC_ERR, -1);
assert_eq!(MAC_ROMAN_ELLIPSIS, 0xC9);
}
#[test]
fn test_trunc_where_conversions() {
assert_eq!(TruncWhere::from_code(0), Some(TruncWhere::End));
assert_eq!(TruncWhere::from_code(0x4000), Some(TruncWhere::Middle));
assert_eq!(TruncWhere::from_code(1), None);
assert_eq!(TruncWhere::End.to_code(), 0);
assert_eq!(TruncWhere::Middle.to_code(), 0x4000);
}
#[test]
fn test_evaluate_trunc_text_fits_untruncated() {
let text = b"Hello";
let eval = evaluate_trunc_text(
text,
40,
TRUNC_END,
MAC_ROMAN_ELLIPSIS,
default_char_width_measure,
);
assert_eq!(eval.result, TRUNC_NOT_TRUNCATED);
assert_eq!(eval.new_text, None);
assert!(!eval.is_truncated());
assert!(eval.is_success());
}
#[test]
fn test_evaluate_trunc_text_truncates_end() {
let text = b"Hello World";
let eval = evaluate_trunc_text(
text,
36,
TRUNC_END,
MAC_ROMAN_ELLIPSIS,
default_char_width_measure,
);
assert_eq!(eval.result, TRUNC_TRUNCATED);
assert!(eval.is_truncated());
assert!(eval.is_success());
let expected = [b'H', b'e', b'l', b'l', b'o', MAC_ROMAN_ELLIPSIS];
assert_eq!(eval.new_text.as_deref(), Some(&expected[..]));
}
#[test]
fn test_evaluate_trunc_text_truncates_middle() {
let text = b"Hello World";
let eval = evaluate_trunc_text(
text,
36,
TRUNC_MIDDLE,
MAC_ROMAN_ELLIPSIS,
default_char_width_measure,
);
assert_eq!(eval.result, TRUNC_TRUNCATED);
assert!(eval.is_truncated());
let expected = [b'H', b'e', b'l', MAC_ROMAN_ELLIPSIS, b'l', b'd'];
assert_eq!(eval.new_text.as_deref(), Some(&expected[..]));
}
#[test]
fn test_evaluate_trunc_text_too_narrow_returns_err() {
let text = b"Hello";
let eval = evaluate_trunc_text(
text,
4,
TRUNC_END,
MAC_ROMAN_ELLIPSIS,
default_char_width_measure,
);
assert_eq!(eval.result, TRUNC_ERR);
assert_eq!(eval.new_text, None);
assert!(!eval.is_success());
let eval_neg = evaluate_trunc_text(
text,
-1,
TRUNC_END,
MAC_ROMAN_ELLIPSIS,
default_char_width_measure,
);
assert_eq!(eval_neg.result, TRUNC_ERR);
}
#[test]
fn test_evaluate_trunc_text_invalid_where_code() {
let text = b"Hello World";
let eval = evaluate_trunc_text(
text,
36,
0x1234,
MAC_ROMAN_ELLIPSIS,
default_char_width_measure,
);
assert_eq!(eval.result, TRUNC_ERR);
assert_eq!(eval.new_text, None);
}
#[test]
fn test_evaluate_trunc_string_truncates_pascal_content() {
let pstring = b"Application Name";
let eval = evaluate_trunc_string(
pstring,
48,
TRUNC_END,
MAC_ROMAN_ELLIPSIS,
default_char_width_measure,
);
assert_eq!(eval.result, TRUNC_TRUNCATED);
let text = eval.new_text.unwrap();
assert_eq!(text.len(), 8);
assert_eq!(text.last(), Some(&MAC_ROMAN_ELLIPSIS));
}
#[test]
fn test_comparison_constants() {
assert_eq!(REL_STRING_TRAP, 0xA050);
assert_eq!(REL_STRING_TRAP_NUM, 0x50);
assert_eq!(CMP_STRING_TRAP, 0xA03C);
assert_eq!(CMP_STRING_TRAP_NUM, 0x3C);
assert_eq!(REL_STRING_LESS, -1);
assert_eq!(REL_STRING_EQUAL, 0);
assert_eq!(REL_STRING_GREATER, 1);
}
#[test]
fn test_mac_roman_casing_and_diacritics() {
assert_eq!(mac_roman_strip_diacriticals(0x8E), b'e');
assert_eq!(mac_roman_to_upper(b'e', false), b'E');
assert_eq!(mac_roman_to_upper(0x8E, false), 0x83); assert_eq!(mac_roman_to_upper(0x8E, true), b'E');
assert_eq!(mac_roman_to_lower(b'E'), b'e');
assert_eq!(mac_roman_to_lower(0x83), 0x8E); }
#[test]
fn test_evaluate_rel_string_matrix() {
let accented_lower = [0x8E]; let plain_lower = [b'e'];
let plain_upper = [b'E'];
assert_eq!(
evaluate_rel_string(&accented_lower, &plain_upper, false, false),
0
);
assert_eq!(
evaluate_rel_string(&plain_lower, &plain_upper, false, false),
0
);
assert_eq!(
evaluate_rel_string(&accented_lower, &plain_upper, false, true),
1
);
assert_eq!(
evaluate_rel_string(&plain_lower, &plain_upper, false, true),
0
);
assert_eq!(
evaluate_rel_string(&accented_lower, &plain_upper, true, false),
1
);
assert_eq!(
evaluate_rel_string(&plain_lower, &plain_upper, true, false),
1
);
assert_eq!(
evaluate_rel_string(&accented_lower, &plain_upper, true, true),
1
);
assert_eq!(
evaluate_rel_string(&plain_lower, &plain_upper, true, true),
1
);
assert_eq!(evaluate_rel_string(b"foo", b"foobar", false, false), -1);
assert_eq!(evaluate_rel_string(b"foobar", b"foo", false, false), 1);
}
#[test]
fn test_evaluate_equal_string_matrix() {
let accented_lower = [0x8E]; let plain_lower = [b'e'];
let plain_upper = [b'E'];
assert!(evaluate_equal_string(
&accented_lower,
&plain_upper,
false,
false
));
assert!(evaluate_equal_string(
&plain_lower,
&plain_upper,
false,
false
));
assert!(!evaluate_equal_string(
&accented_lower,
&plain_upper,
false,
true
));
assert!(evaluate_equal_string(
&plain_lower,
&plain_upper,
false,
true
));
assert!(!evaluate_equal_string(
&accented_lower,
&plain_upper,
true,
false
));
assert!(!evaluate_equal_string(
&plain_lower,
&plain_upper,
true,
false
));
assert!(!evaluate_equal_string(
&accented_lower,
&plain_upper,
true,
true
));
assert!(!evaluate_equal_string(
&plain_lower,
&plain_upper,
true,
true
));
assert!(!evaluate_equal_string(b"short", b"longer", false, false));
}
#[test]
fn test_evaluate_iu_comp_and_equal() {
assert_eq!(evaluate_iu_comp_string(b"Rose", b"ros\x8E"), 0);
assert!(evaluate_iu_equal_string(b"Rose", b"ros\x8E"));
assert_eq!(evaluate_iu_comp_string(b"Rose", b"Rope"), 1);
assert!(!evaluate_iu_equal_string(b"Rose", b"Rope"));
assert_eq!(evaluate_iu_comp_string(b"Rope", b"Rose"), -1);
}
#[test]
fn test_text_transform_constants() {
assert_eq!(UPR_STRING_TRAP, 0xA054);
assert_eq!(UPR_STRING_TRAP_NUM, 0x54);
assert_eq!(TEXT_CONVERT_TRAP, 0xA056);
assert_eq!(TEXT_CONVERT_TRAP_NUM, 0x56);
assert_eq!(LOWER_TEXT_TRAP, 0xA056);
assert_eq!(STRIP_TEXT_TRAP, 0xA256);
assert_eq!(UPPER_TEXT_TRAP, 0xA456);
assert_eq!(STRIP_UPPER_TEXT_TRAP, 0xA656);
}
#[test]
fn test_evaluate_transform_text_operations() {
let text = vec![b'C', b'a', b'f', 0x8E, b' ', b'1', b'2', b'3'];
let mut lower = text.clone();
evaluate_transform_text(&mut lower, TextTransformOperation::Lower);
assert_eq!(lower, vec![b'c', b'a', b'f', 0x8E, b' ', b'1', b'2', b'3']);
let mut strip = text.clone();
evaluate_transform_text(&mut strip, TextTransformOperation::StripDiacritics);
assert_eq!(strip, vec![b'C', b'a', b'f', b'e', b' ', b'1', b'2', b'3']);
let mut upper = text.clone();
evaluate_transform_text(&mut upper, TextTransformOperation::Upper);
assert_eq!(upper, vec![b'C', b'A', b'F', 0x83, b' ', b'1', b'2', b'3']);
let mut strip_upper = text.clone();
evaluate_transform_text(&mut strip_upper, TextTransformOperation::StripUpper);
assert_eq!(
strip_upper,
vec![b'C', b'A', b'F', b'E', b' ', b'1', b'2', b'3']
);
}
#[test]
fn test_evaluate_upper_string() {
let mut str1 = vec![b'c', b'a', b'f', 0x8E];
evaluate_upper_string(&mut str1, true);
assert_eq!(str1, vec![b'C', b'A', b'F', 0x83]);
let mut str2 = vec![b'c', b'a', b'f', 0x8E];
evaluate_upper_string(&mut str2, false);
assert_eq!(str2, vec![b'C', b'A', b'F', b'E']);
}
#[test]
fn test_binary_decimal_constants() {
assert_eq!(PACK7_TRAP, 0xA9EE);
assert_eq!(PACK7_TRAP_OFFSET, 0x01EE);
assert_eq!(NUM_TO_STRING_SELECTOR, 0);
assert_eq!(STRING_TO_NUM_SELECTOR, 1);
}
#[test]
fn test_evaluate_num_to_string() {
assert_eq!(evaluate_num_to_string(0), b"0");
assert_eq!(evaluate_num_to_string(42), b"42");
assert_eq!(evaluate_num_to_string(-1234), b"-1234");
assert_eq!(evaluate_num_to_string(i32::MAX), b"2147483647");
assert_eq!(evaluate_num_to_string(i32::MIN), b"-2147483648");
}
#[test]
fn test_evaluate_string_to_num() {
assert_eq!(evaluate_string_to_num(b"0"), 0);
assert_eq!(evaluate_string_to_num(b"42"), 42);
assert_eq!(evaluate_string_to_num(b"-1234"), -1234);
assert_eq!(evaluate_string_to_num(b"+567"), 567);
assert_eq!(evaluate_string_to_num(b" 123"), 123);
assert_eq!(evaluate_string_to_num(b" -456"), -456);
assert_eq!(evaluate_string_to_num(b" +789"), 789);
assert_eq!(evaluate_string_to_num(b"123abc"), 123);
assert_eq!(evaluate_string_to_num(b" -99xyz"), -99);
assert_eq!(evaluate_string_to_num(b"not a number"), 0);
assert_eq!(evaluate_string_to_num(b""), 0);
assert_eq!(evaluate_string_to_num(b"2147483647"), i32::MAX);
}
#[test]
fn test_styled_line_break_code_conversions() {
assert_eq!(
StyledLineBreakCode::from_code(0),
Some(StyledLineBreakCode::Word)
);
assert_eq!(
StyledLineBreakCode::from_code(1),
Some(StyledLineBreakCode::Char)
);
assert_eq!(
StyledLineBreakCode::from_code(2),
Some(StyledLineBreakCode::Overflow)
);
assert_eq!(StyledLineBreakCode::from_code(3), None);
assert_eq!(StyledLineBreakCode::Word.to_code(), 0);
assert_eq!(StyledLineBreakCode::Char.to_code(), 1);
assert_eq!(StyledLineBreakCode::Overflow.to_code(), 2);
}
#[test]
fn test_styled_line_break_fixed_pixels_conversions() {
assert_eq!(fixed_to_pixels(100), 100);
assert_eq!(pixels_to_width_like(100, 80), 80);
assert_eq!(fixed_to_pixels(50 << 16), 50);
assert_eq!(pixels_to_width_like(50 << 16, 35), 35 << 16);
}
#[test]
fn test_styled_line_break_overflow_when_entire_run_fits() {
let text = b"HELLO";
let eval = evaluate_styled_line_break(
text,
0,
text.len() as u32,
40,
true,
default_char_advance_measure,
);
assert_eq!(eval.code, SM_BREAK_OVERFLOW);
assert_eq!(eval.output_offset, 5);
assert_eq!(eval.remaining_width, 10);
assert!(eval.is_overflow());
}
#[test]
fn test_styled_line_break_breaks_at_word_boundary() {
let text = b"HELLO WORLD";
let available = 42;
let eval = evaluate_styled_line_break(
text,
0,
text.len() as u32,
available,
true,
default_char_advance_measure,
);
assert_eq!(eval.code, SM_BREAK_WORD);
assert_eq!(eval.output_offset, 6);
assert_eq!(eval.remaining_width, 6); assert!(eval.is_word_break());
}
#[test]
fn test_styled_line_break_swallows_multiple_trailing_spaces() {
let text = b"FOO BAR";
let available = 24;
let eval = evaluate_styled_line_break(
text,
0,
text.len() as u32,
available,
true,
default_char_advance_measure,
);
assert_eq!(eval.code, SM_BREAK_WORD);
assert_eq!(eval.output_offset, 6);
assert!(eval.is_word_break());
}
#[test]
fn test_styled_line_break_char_break_on_first_style_run() {
let text = b"ABCDEFGHIJ";
let available = 18;
let eval = evaluate_styled_line_break(
text,
0,
text.len() as u32,
available,
true,
default_char_advance_measure,
);
assert_eq!(eval.code, SM_BREAK_CHAR);
assert_eq!(eval.output_offset, 3);
assert_eq!(eval.remaining_width, 0);
assert!(eval.is_char_break());
}
#[test]
fn test_styled_line_break_no_char_break_on_subsequent_style_run() {
let text = b"ABCDEFGHIJ";
let available = 18;
let eval = evaluate_styled_line_break(
text,
0,
text.len() as u32,
available,
false,
default_char_advance_measure,
);
assert_eq!(eval.code, SM_BREAK_WORD);
assert_eq!(eval.output_offset, 0);
assert_eq!(eval.remaining_width, available);
assert!(eval.is_word_break());
}
#[test]
fn test_styled_line_break_zero_width_first_style_run_makes_progress() {
let text = b"HELLO";
let eval = evaluate_styled_line_break(
text,
0,
text.len() as u32,
0,
true,
default_char_advance_measure,
);
assert_eq!(eval.code, SM_BREAK_CHAR);
assert_eq!(eval.output_offset, 1);
assert_eq!(eval.remaining_width, 0);
}
#[test]
fn test_visible_length_matrix() {
assert_eq!(evaluate_visible_length(b"hello \t "), 5);
assert_eq!(evaluate_visible_length(b" \t"), 0);
assert_eq!(evaluate_visible_length(b""), 0);
assert_eq!(evaluate_visible_length(b"abc def"), 7);
assert_eq!(evaluate_visible_length(b"hello\tworld\t "), 11);
assert_eq!(evaluate_visible_length(b"no whitespace"), 13);
}
#[test]
fn test_font_id_to_script() {
assert_eq!(font_id_to_script(0), SM_ROMAN);
assert_eq!(font_id_to_script(1), SM_ROMAN);
assert_eq!(font_id_to_script(0x3FFF), SM_ROMAN);
assert_eq!(font_id_to_script(0x4000), SM_JAPANESE);
assert_eq!(font_id_to_script(0x4200), SM_TRAD_CHINESE);
assert_eq!(font_id_to_script(0x4400), SM_KOREAN);
assert_eq!(font_id_to_script(0x7000), 25); assert_eq!(font_id_to_script(-1), SM_ROMAN);
assert_eq!(font_id_to_script(0x7FFF), 32);
}
#[test]
fn test_font_id_to_enabled_script() {
assert!(is_script_enabled(SM_ROMAN));
assert!(!is_script_enabled(SM_JAPANESE));
assert!(!is_script_enabled(SM_TRAD_CHINESE));
assert_eq!(font_id_to_enabled_script(0), SM_ROMAN);
assert_eq!(font_id_to_enabled_script(1), SM_ROMAN);
assert_eq!(font_id_to_enabled_script(3), SM_ROMAN);
assert_eq!(font_id_to_enabled_script(0x3FFF), SM_ROMAN);
assert_eq!(font_id_to_enabled_script(0x4000), SM_ROMAN);
assert_eq!(font_id_to_enabled_script(0x4200), SM_ROMAN);
assert_eq!(font_id_to_enabled_script(0x4400), SM_ROMAN);
}
#[test]
fn test_char_byte_matrix() {
let text_roman = b"Hello, World!";
for i in 0..text_roman.len() {
assert_eq!(evaluate_char_byte(text_roman, i, SM_ROMAN), SM_SINGLE_BYTE);
}
let text_jp = &[0x81, 0x40, b'A', 0x82, 0x60];
assert_eq!(evaluate_char_byte(text_jp, 0, SM_JAPANESE), SM_FIRST_BYTE);
assert_eq!(evaluate_char_byte(text_jp, 1, SM_JAPANESE), SM_LAST_BYTE);
assert_eq!(evaluate_char_byte(text_jp, 2, SM_JAPANESE), SM_SINGLE_BYTE);
assert_eq!(evaluate_char_byte(text_jp, 3, SM_JAPANESE), SM_FIRST_BYTE);
assert_eq!(evaluate_char_byte(text_jp, 4, SM_JAPANESE), SM_LAST_BYTE);
assert_eq!(evaluate_char_byte(text_jp, 5, SM_JAPANESE), SM_SINGLE_BYTE);
}
#[test]
fn test_replace_text_matrix() {
let (count, result) = evaluate_replace_text(b"A^0B^0", b"LONG", b"^0", SM_ROMAN);
assert_eq!(count, 2);
assert_eq!(result.as_deref(), Some(&b"ALONGBLONG"[..]));
let (count, result) = evaluate_replace_text(b"aaaa", b"a", b"aa", SM_ROMAN);
assert_eq!(count, 2);
assert_eq!(result.as_deref(), Some(&b"aa"[..]));
let (count, result) = evaluate_replace_text(b"unchanged", b"rep", b"missing", SM_ROMAN);
assert_eq!(count, 0);
assert_eq!(result, None);
let (count, result) = evaluate_replace_text(b"aaaa", b"", b"aa", SM_ROMAN);
assert_eq!(count, 2);
assert_eq!(result.as_deref(), Some(&b""[..]));
let (count, result) =
evaluate_replace_text(&[0x81, 0x40, 0x40], b"X", &[0x40], SM_JAPANESE);
assert_eq!(count, 1);
assert_eq!(result.as_deref(), Some(&[0x81, 0x40, b'X'][..]));
assert_eq!(evaluate_replace_text(b"", b"X", b"a", SM_ROMAN), (0, None));
assert_eq!(
evaluate_replace_text(b"base", b"X", b"", SM_ROMAN),
(0, None)
);
let (count, result) = evaluate_replace_text(&[0x81, 0x40], b"X", &[0x81], SM_JAPANESE);
assert_eq!(count, 0);
assert_eq!(result, None);
}
#[test]
fn test_hilite_text_matrix() {
assert_eq!(evaluate_hilite_text(10, 2, 7), [(2, 7), (0, 0), (0, 0)]);
assert_eq!(evaluate_hilite_text(10, 7, 2), [(2, 7), (0, 0), (0, 0)]);
assert_eq!(evaluate_hilite_text(10, 4, 4), [(4, 4), (0, 0), (0, 0)]);
assert_eq!(evaluate_hilite_text(5, 2, 100), [(2, 5), (0, 0), (0, 0)]);
assert_eq!(evaluate_hilite_text(5, 100, 200), [(5, 5), (0, 0), (0, 0)]);
assert_eq!(evaluate_hilite_text(0, 0, 5), [(0, 0), (0, 0), (0, 0)]);
}
#[test]
fn test_find_word_matrix() {
let text = b"Hello, world!";
assert_eq!(evaluate_find_word(text, 0), [(0, 5), (0, 0), (0, 0)]);
assert_eq!(evaluate_find_word(text, 3), [(0, 5), (0, 0), (0, 0)]);
assert_eq!(evaluate_find_word(text, 4), [(0, 5), (0, 0), (0, 0)]);
assert_eq!(evaluate_find_word(text, 5), [(5, 6), (0, 0), (0, 0)]);
assert_eq!(evaluate_find_word(text, 6), [(6, 9), (0, 0), (0, 0)]);
assert_eq!(evaluate_find_word(text, 7), [(6, 9), (0, 0), (0, 0)]);
assert_eq!(evaluate_find_word(text, 8), [(6, 9), (0, 0), (0, 0)]);
assert_eq!(evaluate_find_word(text, 9), [(9, 14), (0, 0), (0, 0)]);
assert_eq!(evaluate_find_word(text, 13), [(9, 14), (0, 0), (0, 0)]);
assert_eq!(evaluate_find_word(text, 14), [(14, 15), (0, 0), (0, 0)]);
assert_eq!(evaluate_find_word(text, 15), [(15, 15), (0, 0), (0, 0)]);
assert_eq!(evaluate_find_word(b"", 0), [(0, 0), (0, 0), (0, 0)]);
}
#[test]
fn test_char_type_matrix() {
let text = [b'A', b'!', 0x8E, 0xFF];
assert_eq!(evaluate_char_type(&text, 0), 0);
assert_eq!(evaluate_char_type(&text, 1), 0);
assert_eq!(evaluate_char_type(&text, 2), SM_CHAR_EURO << 8);
assert_eq!(evaluate_char_type(&text, 3), SM_CHAR_EURO << 8);
assert_eq!(evaluate_char_type(&text, 4), 0);
}
#[test]
fn test_script_manager_variable_matrix() {
assert_eq!(evaluate_script_manager_variable(SM_VERSION, 0), 0x0710);
assert_eq!(evaluate_script_manager_variable(SM_ENABLED, 0), 1);
assert_eq!(evaluate_script_manager_variable(SM_BIDIRECT, 0), 0);
assert_eq!(evaluate_script_manager_variable(SM_FONT, 0), 0);
assert_eq!(evaluate_script_manager_variable(SM_INTL, 0), 0);
assert_eq!(evaluate_script_manager_variable(SM_PRINT, 0), 0);
assert_eq!(evaluate_script_manager_variable(SM_SYS_SCRIPT, 0), 0);
assert_eq!(evaluate_script_manager_variable(SM_LAST_SCRIPT, 0), 0);
assert_eq!(evaluate_script_manager_variable(SM_KEY_SCRIPT, 0), 0);
assert_eq!(evaluate_script_manager_variable(SM_SYS_FONT, 0), 0);
assert_eq!(evaluate_script_manager_variable(SM_APP_FONT, 0), 1);
assert_eq!(
evaluate_script_manager_variable(SM_KCHR_CACHE, 0x1234_5678),
0x1234_5678
);
assert_eq!(evaluate_script_manager_variable(SM_KEY_DISABLE_KCHR, 0), 0);
assert_eq!(evaluate_script_manager_variable(SM_DUAL_CARET, 0), 0);
assert_eq!(evaluate_script_manager_variable(999, 0), 0);
}
#[test]
fn test_script_variable_matrix() {
assert_eq!(
evaluate_script_variable(SM_ROMAN as i16, SM_SCRIPT_VERSION),
0x0710
);
assert_eq!(
evaluate_script_variable(SM_ROMAN as i16, SM_SCRIPT_ENABLED),
1
);
assert_eq!(
evaluate_script_variable(SM_ROMAN as i16, SM_SCRIPT_RIGHT),
0
);
assert_eq!(
evaluate_script_variable(SM_ROMAN as i16, SM_SCRIPT_SINGLE),
1
);
assert_eq!(
evaluate_script_variable(SM_ROMAN as i16, SM_SCRIPT_PREF_SYS),
0
);
assert_eq!(
evaluate_script_variable(SM_ROMAN as i16, SM_SCRIPT_DEF_FONT_SIZE),
12
);
assert_eq!(
evaluate_script_variable(SM_ROMAN as i16, SM_SCRIPT_APP_FOND),
1
);
assert_eq!(
evaluate_script_variable(SM_ROMAN as i16, SM_SCRIPT_BUNDLE),
0
);
assert_eq!(
evaluate_script_variable(SM_ROMAN as i16, SM_SCRIPT_SYS_FOND),
0
);
assert_eq!(
evaluate_script_variable(SM_ROMAN as i16, SM_SCRIPT_PUNCT),
0
);
assert_eq!(evaluate_script_variable(SM_ROMAN as i16, SM_SCRIPT_KEYS), 0);
assert_eq!(evaluate_script_variable(SM_ROMAN as i16, SM_SCRIPT_ICON), 0);
assert_eq!(
evaluate_script_variable(SM_JAPANESE as i16, SM_SCRIPT_VERSION),
0
);
assert_eq!(
evaluate_script_variable(SM_JAPANESE as i16, SM_SCRIPT_ENABLED),
0
);
assert_eq!(
evaluate_script_variable(SM_JAPANESE as i16, SM_SCRIPT_SINGLE),
0
);
assert_eq!(
evaluate_script_variable(SM_JAPANESE as i16, SM_SCRIPT_DEF_FONT_SIZE),
0
);
}
#[test]
fn test_get_sys_direction() {
assert_eq!(evaluate_get_sys_direction(0), 0); assert_eq!(evaluate_get_sys_direction(0xFFFF), -1); }
#[test]
fn test_parse_table() {
let mut table = [0xFF; 256];
let is_2byte = evaluate_parse_table(&mut table, SM_ROMAN);
assert!(!is_2byte);
assert!(table.iter().all(|&b| b == SM_SINGLE_BYTE as u8));
let mut sjis_table = [0xFF; 256];
let is_2byte_jp = evaluate_parse_table(&mut sjis_table, SM_JAPANESE);
assert!(is_2byte_jp);
assert_eq!(sjis_table[0x41], SM_SINGLE_BYTE as u8); assert_eq!(sjis_table[0x81], SM_FIRST_BYTE as u8);
assert_eq!(sjis_table[0xE0], SM_FIRST_BYTE as u8);
}
#[test]
fn test_portion_text() {
assert_eq!(evaluate_portion_text(b""), 0);
assert_eq!(evaluate_portion_text(b"Hello"), 0x0001_0000); assert_eq!(evaluate_portion_text(b"Hello World"), 0x0001_0000); assert_eq!(evaluate_portion_text(b"A B C D"), 0x0003_0000); }
#[test]
fn test_find_script_run() {
assert_eq!(evaluate_find_script_run(10, SM_ROMAN), (SM_ROMAN, 10));
assert_eq!(evaluate_find_script_run(0, SM_ROMAN), (SM_ROMAN, 0));
}
#[test]
fn test_find_word_breaks() {
let text = b"The quick brown fox";
let breaks = evaluate_find_word_breaks(text, 5); assert_eq!(breaks[0], (4, 9));
}
#[test]
fn test_get_format_order() {
assert_eq!(evaluate_get_format_order(0, 3, false), vec![0, 1, 2, 3]);
assert_eq!(evaluate_get_format_order(0, 3, true), vec![3, 2, 1, 0]);
assert_eq!(evaluate_get_format_order(2, 4, false), vec![2, 3, 4]);
assert_eq!(evaluate_get_format_order(4, 2, false), Vec::<i16>::new());
}
#[test]
fn test_char2pixel_and_pixel2char() {
let text = b"Hello World";
assert_eq!(evaluate_char2pixel(b"", 0, 0, 0), 0);
assert_eq!(evaluate_char2pixel(text, 0, 0, 0), 0);
assert_eq!(evaluate_char2pixel(text, 0, 2, 0), 12);
assert_eq!(evaluate_char2pixel(text, 0, 5, 0), 30);
assert_eq!(evaluate_char2pixel(text, 0, 11, 0), 66);
assert_eq!(evaluate_char2pixel(text, 0, 11, -1), 0);
assert_eq!(evaluate_char2pixel(text, 0, 5, -1), 36);
assert_eq!(evaluate_pixel2char(b"", 0, 10), (0, false));
assert_eq!(evaluate_pixel2char(text, 0, 0), (0, false));
assert_eq!(evaluate_pixel2char(text, 0, 13), (2, true));
assert_eq!(evaluate_pixel2char(text, 0, 16), (2, false));
assert_eq!(evaluate_pixel2char(text, 0, 100), (11, false));
}
#[test]
fn test_measure_just() {
let text = b"Hi";
let locs = evaluate_measure_just(text, 0);
assert_eq!(locs, vec![0, 6, 12]);
}
#[test]
fn test_transliterate() {
let text = b"Hello World";
let (lower, err) =
evaluate_transliterate(text, SM_TRANS_CASE | SM_TRANS_LOWER, SM_MASK_ALL);
assert_eq!(err, 0);
assert_eq!(lower, b"hello world");
let (upper, err) = evaluate_transliterate(b"hello world", SM_TRANS_CASE, SM_MASK_ALL);
assert_eq!(err, 0);
assert_eq!(upper, b"HELLO WORLD");
let (ascii, err) = evaluate_transliterate(text, SM_TRANS_ASCII, SM_MASK_ALL);
assert_eq!(err, 0);
assert_eq!(ascii, b"Hello World");
}
#[test]
fn test_mag_string_and_mag_id_string() {
assert_eq!(evaluate_mag_string(b"apple", b"banana"), -1);
assert_eq!(evaluate_mag_string(b"apple", b"apple"), 0);
assert_eq!(evaluate_mag_string(b"banana", b"apple"), 1);
assert_eq!(evaluate_mag_id_string(b"Apple", b"apple"), 0);
assert_eq!(evaluate_mag_id_string(b"APPLE", b"apple"), 0);
assert_eq!(evaluate_mag_id_string(b"Apple", b"banana"), 1);
assert_eq!(evaluate_mag_id_string(b"short", b"longer"), 1);
}
#[test]
fn test_script_and_lang_order() {
assert_eq!(evaluate_script_order(0, 1), -1);
assert_eq!(evaluate_script_order(1, 1), 0);
assert_eq!(evaluate_script_order(2, 1), 1);
assert_eq!(evaluate_lang_order(0, 1), -1);
assert_eq!(evaluate_lang_order(1, 1), 0);
assert_eq!(evaluate_lang_order(2, 1), 1);
}
#[test]
fn test_text_order() {
assert_eq!(evaluate_text_order(b"abc", b"xyz", 0, 1, 0, 0), -1);
assert_eq!(evaluate_text_order(b"xyz", b"abc", 2, 1, 0, 0), 1);
assert_eq!(evaluate_text_order(b"abc", b"xyz", 0, 0, 0, 1), -1);
assert_eq!(evaluate_text_order(b"xyz", b"abc", 0, 0, 2, 1), 1);
assert_eq!(evaluate_text_order(b"apple", b"banana", 0, 0, 0, 0), -1);
assert_eq!(evaluate_text_order(b"apple", b"apple", 0, 0, 0, 0), 0);
assert_eq!(evaluate_text_order(b"banana", b"apple", 0, 0, 0, 0), 1);
}
#[test]
fn test_iu_metric() {
assert!(!evaluate_iu_metric());
}
#[test]
fn test_string_order() {
assert_eq!(evaluate_string_order(b"abc", b"xyz", 0, 1, 0, 0), -1);
assert_eq!(evaluate_string_order(b"xyz", b"abc", 2, 1, 0, 0), 1);
assert_eq!(evaluate_string_order(b"abc", b"xyz", 0, 0, 0, 1), -1);
assert_eq!(evaluate_string_order(b"xyz", b"abc", 0, 0, 2, 1), 1);
assert_eq!(evaluate_string_order(b"apple", b"banana", 0, 0, 0, 0), -1);
assert_eq!(evaluate_string_order(b"apple", b"apple", 0, 0, 0, 0), 0);
assert_eq!(evaluate_string_order(b"banana", b"apple", 0, 0, 0, 0), 1);
}
#[test]
fn test_intl_resource_table_kind_and_selectors() {
assert_eq!(SM_WORD_SELECT_TABLE, 0);
assert_eq!(SM_WORD_WRAP_TABLE, 1);
assert_eq!(SM_NUMBER_PARTS_TABLE, 2);
assert_eq!(SM_UN_TOKEN_TABLE, 3);
assert_eq!(SM_WHITE_SPACE_LIST, 4);
assert_eq!(IU_WORD_SELECT_TABLE, 0);
assert_eq!(IU_WORD_WRAP_TABLE, 1);
assert_eq!(IU_NUMBER_PARTS_TABLE, 2);
assert_eq!(IU_UN_TOKEN_TABLE, 3);
assert_eq!(IU_WHITE_SPACE_LIST, 4);
assert_eq!(
IntlResourceTableKind::from_table_code(0),
Some(IntlResourceTableKind::WordSelect)
);
assert_eq!(
IntlResourceTableKind::from_table_code(1),
Some(IntlResourceTableKind::WordWrap)
);
assert_eq!(
IntlResourceTableKind::from_table_code(2),
Some(IntlResourceTableKind::NumberParts)
);
assert_eq!(
IntlResourceTableKind::from_table_code(3),
Some(IntlResourceTableKind::UnToken)
);
assert_eq!(
IntlResourceTableKind::from_table_code(4),
Some(IntlResourceTableKind::WhiteSpace)
);
assert_eq!(IntlResourceTableKind::from_table_code(5), None);
assert_eq!(
IntlResourceTableKind::WordSelect.target_resource_tag(),
b"itl2"
);
assert_eq!(
IntlResourceTableKind::WordWrap.target_resource_tag(),
b"itl2"
);
assert_eq!(
IntlResourceTableKind::NumberParts.target_resource_tag(),
b"itl4"
);
assert_eq!(
IntlResourceTableKind::UnToken.target_resource_tag(),
b"itl4"
);
assert_eq!(
IntlResourceTableKind::WhiteSpace.target_resource_tag(),
b"itl4"
);
}
#[test]
fn test_character_classification_constants_and_masks() {
assert_eq!(SMC_TYPE_MASK, 0x000F);
assert_eq!(SMC_RESERVED, 0x00F0);
assert_eq!(SMC_CLASS_MASK, 0x0F00);
assert_eq!(SMC_ORIENTATION_MASK, 0x1000);
assert_eq!(SMC_RIGHT_MASK, 0x2000);
assert_eq!(SMC_UPPER_MASK, 0x4000);
assert_eq!(SMC_DOUBLE_MASK, -0x8000);
assert_eq!(SM_CHAR_TYPE_PUNCT, 0x0000);
assert_eq!(SM_CHAR_TYPE_ASCII, 0x0001);
assert_eq!(SM_CHAR_TYPE_EXT_ASCII, 0x0007);
assert_eq!(SM_PUNCT_NORMAL, 0x0000);
assert_eq!(SM_PUNCT_NUMBER, 0x0100);
assert_eq!(SM_PUNCT_SYMBOL, 0x0200);
assert_eq!(SM_PUNCT_BLANK, 0x0300);
assert_eq!(SM_CHAR_HORIZONTAL, 0x0000);
assert_eq!(SM_CHAR_VERTICAL, 0x1000);
assert_eq!(SM_CHAR_LOWER, 0x0000);
assert_eq!(SM_CHAR_UPPER, 0x4000);
assert_eq!(SM_CHAR_1BYTE, 0x0000);
assert_eq!(SM_CHAR_2BYTE, -0x8000);
}
#[test]
fn test_pack6_constants() {
assert_eq!(PACK6_TRAP, 0xA9ED);
assert_eq!(PACK6_TRAP_OFFSET, 0x01ED);
assert_eq!(PACK6_IU_METRIC_SELECTOR, 0x0004);
assert_eq!(PACK6_IU_MAG_STRING_SELECTOR, 0x000A);
assert_eq!(PACK6_IU_MAG_ID_STRING_SELECTOR, 0x000C);
assert_eq!(PACK6_IU_CLEAR_CACHE_SELECTOR, 0x0018);
assert_eq!(PACK6_IU_MAG_P_STRING_SELECTOR, 0x001A);
assert_eq!(PACK6_IU_MAG_ID_P_STRING_SELECTOR, 0x001C);
assert_eq!(PACK6_IU_SCRIPT_ORDER_SELECTOR, 0x001E);
assert_eq!(PACK6_IU_LANG_ORDER_SELECTOR, 0x0020);
assert_eq!(PACK6_IU_TEXT_ORDER_SELECTOR, 0x0022);
assert_eq!(PACK6_IU_GET_INTL_TABLE_SELECTOR, 0x0024);
}
}