use super::*;
#[must_use]
pub(crate) fn contains_ascii_lowered_needle(haystack: &[u8], needle_lower: &[u8]) -> bool {
let n_len: usize = needle_lower.len();
if n_len == 0 {
return true;
}
if n_len > haystack.len() {
return false;
}
let first: u8 = needle_lower[0];
let tail: &[u8] = &needle_lower[1..];
let limit: usize = haystack.len() - n_len;
let mut pos: usize = 0;
while pos <= limit {
if haystack[pos] == first && haystack[pos + 1..pos + n_len] == *tail {
return true;
}
pos += 1;
}
false
}
#[must_use]
pub(crate) fn contains_ascii_ignore_case(haystack: &str, needle: &str) -> bool {
let h: &[u8] = haystack.as_bytes();
let n: &[u8] = needle.as_bytes();
let n_len: usize = n.len();
if n_len == 0 {
return true;
}
if n_len > h.len() {
return false;
}
let first: u8 = n[0].to_ascii_lowercase();
let tail: &[u8] = &n[1..];
let limit: usize = h.len() - n_len;
let mut pos: usize = 0;
while pos <= limit {
if h[pos].to_ascii_lowercase() == first
&& h[pos + 1..pos + n_len].eq_ignore_ascii_case(tail)
{
return true;
}
pos += 1;
}
false
}
#[must_use]
pub(crate) fn compute_data_table_column_widths(
headers: &[String],
rows: &[Vec<String>],
) -> (Vec<f32>, f32) {
let mut col_count: usize = headers.len();
for row in rows.iter() {
if row.len() > col_count {
col_count = row.len();
}
}
if col_count == 0 {
return (Vec::new(), 0.0);
}
let mut widths: Vec<f32> = Vec::with_capacity(col_count);
let mut total: f32 = 0.0;
for ci in 0..col_count {
let mut max_w: f32 = headers
.get(ci)
.map(|h: &String| estimate_data_cell_width(h.as_str(), 12.0, true))
.unwrap_or(0.0);
for row in rows.iter() {
if let Some(cell) = row.get(ci) {
let cw: f32 = estimate_data_cell_width(cell.as_str(), 13.0, false);
if cw > max_w {
max_w = cw;
}
}
}
let col_w: f32 = (max_w + 38.0).clamp(96.0, 460.0);
widths.push(col_w);
total += col_w;
}
(widths, total)
}
#[must_use]
fn estimate_data_cell_width(text: &str, font_size: f32, is_bold: bool) -> f32 {
let weight_scale: f32 = if is_bold { 1.08 } else { 1.0 };
let mut best_w: f32 = 0.0;
for line in text.split('\n') {
let mut w: f32 = 0.0;
for ch in line.chars() {
let factor: f32 = if !ch.is_ascii() {
1.15
} else if matches!(
ch,
'i' | 'l' | 'I' | '1' | '.' | ',' | ':' | ';' | '|' | '!' | '\'' | ' '
) {
0.38
} else if ch.is_ascii_uppercase() || matches!(ch, 'w' | 'm' | '@' | '%' | '#' | '&') {
0.76
} else {
0.60
};
w += font_size * factor * weight_scale;
}
if w > best_w {
best_w = w;
}
}
best_w
}
#[must_use]
fn parse_sortable_number(raw: &str) -> Option<f64> {
let trimmed: &str = raw.trim();
if trimmed.is_empty() {
return None;
}
let stripped: &str = trimmed
.strip_prefix('$')
.unwrap_or(trimmed)
.strip_suffix('%')
.unwrap_or(trimmed.strip_prefix('$').unwrap_or(trimmed))
.trim();
if stripped.is_empty() {
return None;
}
let parsed: Option<f64> = if stripped.contains(',') {
let cleaned: String = stripped.replace(',', "");
cleaned.parse::<f64>().ok()
} else {
stripped.parse::<f64>().ok()
};
parsed.filter(|v: &f64| v.is_finite())
}
#[must_use]
pub(crate) fn compare_data_table_cells(a: &str, b: &str) -> Ordering {
if let (Some(na), Some(nb)) = (parse_sortable_number(a), parse_sortable_number(b)) {
let num_ord: Ordering = na.total_cmp(&nb);
if num_ord != Ordering::Equal {
return num_ord;
}
}
if a.is_ascii() && b.is_ascii() {
for (ba, bb) in a.bytes().zip(b.bytes()) {
let ord: Ordering = ba.to_ascii_lowercase().cmp(&bb.to_ascii_lowercase());
if ord != Ordering::Equal {
return ord;
}
}
let len_ord: Ordering = a.len().cmp(&b.len());
if len_ord != Ordering::Equal {
return len_ord;
}
return a.cmp(b);
}
let la: String = a.to_lowercase();
let lb: String = b.to_lowercase();
let ord: Ordering = la.cmp(&lb);
if ord != Ordering::Equal {
return ord;
}
a.cmp(b)
}
#[must_use]
pub(crate) fn tone_to_surface_role(tone: EuvTone) -> SurfaceRole {
match tone {
EuvTone::Neutral => SurfaceRole::Secondary,
EuvTone::Primary => SurfaceRole::Primary,
EuvTone::Success => SurfaceRole::Success,
EuvTone::Warning => SurfaceRole::Warning,
EuvTone::Destructive => SurfaceRole::Destructive,
}
}