pub fn mask(text: &str, format_patterns: &str) -> String {
if text.is_empty() {
return String::new();
}
let text_bytes = text.as_bytes();
let text_len = text_bytes.len();
let mut best_format = "";
let mut best_x_count = 0;
for format in format_patterns.split('|') {
let format = format.trim();
let x_count = format.bytes().filter(|&b| b == b'X').count();
if best_format.is_empty() || (text_len > best_x_count && x_count > best_x_count) {
best_format = format;
best_x_count = x_count;
}
}
if text_len > best_x_count {
eprintln!("Warning: The provided text has more characters ({}) than the format can handle ({})",
text_len, best_x_count);
}
let mut result = String::with_capacity(best_format.len());
let mut text_index = 0;
for &b in best_format.as_bytes() {
if b == b'X' {
if text_index < text_len {
result.push(text_bytes[text_index] as char);
text_index += 1;
} else {
break;
}
} else {
result.push(b as char);
}
}
result
}