use super::*;
use crate::text::grapheme_clusters::TextProcessing;
pub fn encode_header(value: &str) -> String {
let mut ret = String::with_capacity(value.len());
let mut is_current_window_ascii = true;
let mut current_window_start = 0;
{
let graphemes = value.graphemes_indices();
for (idx, g) in graphemes {
match (g.is_ascii(), is_current_window_ascii) {
(true, true) => {
ret.push_str(g);
}
(true, false) => {
if !g.is_empty() && !g.trim_matches(&[' ', '\t'] as &[_]).is_empty() {
ret.push_str(&format!(
"=?UTF-8?B?{}?=",
BASE64_MIME
.encode(&value.as_bytes()[current_window_start..idx])
.trim()
));
is_current_window_ascii = true;
current_window_start = idx;
ret.push_str(g);
}
}
(false, true) => {
current_window_start = idx;
is_current_window_ascii = false;
}
(false, false) if (((4 * (idx - current_window_start) / 3) + 3) & !3) > 33 => {
ret.push_str(&format!(
"=?UTF-8?B?{}?=",
BASE64_MIME
.encode(&value.as_bytes()[current_window_start..idx])
.trim()
));
if idx != value.len() - 1 {
ret.push(' ');
}
current_window_start = idx;
}
(false, false) => {}
}
}
}
if !is_current_window_ascii {
ret.push_str(&format!(
"=?UTF-8?B?{}?=",
BASE64_MIME
.encode(&value.as_bytes()[current_window_start..])
.trim()
));
}
ret
}