use std::{borrow::Cow, ffi::OsStr, path::Path};
pub fn precompose(s: Cow<'_, str>) -> Cow<'_, str> {
use unicode_normalization::{char, is_nfc};
if is_nfc(s.as_ref()) {
return s;
}
fn push(out: &mut Vec<char>, starter: &mut Option<usize>, max_class: &mut u8, ch: char) -> bool {
let class = char::canonical_combining_class(ch);
if let Some(starter) = *starter
&& (*max_class == 0 || *max_class < class)
&& let Some(composed) = char::compose(out[starter], ch)
{
out[starter] = composed;
return true;
}
if class == 0 {
*starter = Some(out.len());
*max_class = 0;
} else {
*max_class = (*max_class).max(class);
}
out.push(ch);
false
}
let mut out = Vec::with_capacity(s.chars().count());
let mut starter = None;
let mut max_class = 0;
let mut changed = false;
for ch in s.chars() {
let mut first = true;
char::decompose_canonical(ch, |decomposed| {
changed |= !first || decomposed != ch;
first = false;
changed |= push(&mut out, &mut starter, &mut max_class, decomposed);
});
}
if changed {
Cow::Owned(out.into_iter().collect())
} else {
s
}
}
pub fn decompose(s: Cow<'_, str>) -> Cow<'_, str> {
use unicode_normalization::{UnicodeNormalization, is_nfd};
if is_nfd(s.as_ref()) {
s
} else {
Cow::Owned(s.as_ref().nfd().collect())
}
}
pub fn precompose_path(path: Cow<'_, Path>) -> Cow<'_, Path> {
match path.to_str() {
None => path,
Some(maybe_decomposed) => match precompose(maybe_decomposed.into()) {
Cow::Borrowed(_) => path,
Cow::Owned(precomposed) => Cow::Owned(precomposed.into()),
},
}
}
pub fn precompose_os_string(name: Cow<'_, OsStr>) -> Cow<'_, OsStr> {
match name.to_str() {
None => name,
Some(maybe_decomposed) => match precompose(maybe_decomposed.into()) {
Cow::Borrowed(_) => name,
Cow::Owned(precomposed) => Cow::Owned(precomposed.into()),
},
}
}
#[cfg(feature = "bstr")]
pub fn precompose_bstr(s: Cow<'_, bstr::BStr>) -> Cow<'_, bstr::BStr> {
use bstr::ByteSlice;
match s.to_str().ok() {
None => s,
Some(maybe_decomposed) => match precompose(maybe_decomposed.into()) {
Cow::Borrowed(_) => s,
Cow::Owned(precomposed) => Cow::Owned(precomposed.into()),
},
}
}