pub(crate) fn is_control(c: char) -> bool {
matches!(c, '\u{0}'..='\u{1f}' | '\u{7f}'..='\u{9f}')
}
fn is_space(c: char) -> bool {
matches!(c, ' ' | '\u{85}' | '\u{a0}' | '\u{1680}' | '\u{2000}'..='\u{200a}' | '\u{2028}' | '\u{2029}' | '\u{202f}' | '\u{205f}' | '\u{3000}')
}
fn all(s: &str, ok: impl Fn(u8) -> bool) -> bool {
s.bytes().all(ok)
}
pub(crate) fn plan_id(s: &str) -> bool {
let groups: Vec<&str> = s.split('-').collect();
groups.len() == 5 && groups.iter().zip([8, 4, 4, 4, 12]).all(|(g, n)| g.len() == n && all(g, |b| b.is_ascii_hexdigit()))
}
pub(crate) fn surface(s: &str) -> bool {
(1..=32).contains(&s.len()) && s.as_bytes()[0].is_ascii_lowercase() && all(s, |b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
}
pub(crate) fn locale(s: &str) -> bool {
let mut parts = s.split('-');
let lang = parts.next().unwrap_or_default();
let rest: Vec<&str> = parts.collect();
(2..=3).contains(&lang.len())
&& all(lang, |b| b.is_ascii_alphabetic())
&& rest.len() <= 3
&& rest.iter().all(|p| (2..=8).contains(&p.len()) && all(p, |b| b.is_ascii_alphanumeric()))
}
pub(crate) fn lingara_origin(s: &str) -> bool {
let s = s.strip_suffix('/').unwrap_or(s);
let (host, port) = match s.rsplit_once(':') {
Some((h, p)) if !h.ends_with("//") && !h.ends_with(':') && h.contains("//") => (h, Some(p)),
_ => (s, None),
};
let port_ok = port.is_none_or(|p| (1..=5).contains(&p.len()) && all(p, |b| b.is_ascii_digit()));
let https = host.strip_prefix("https://").is_some_and(|h| h.split('.').all(|l| !l.is_empty() && all(l, |b| b.is_ascii_alphanumeric() || b == b'-')));
let local = matches!(host, "http://localhost" | "http://127.0.0.1");
port_ok && (https || local)
}
pub(crate) fn title(s: &str) -> bool {
(1..=80).contains(&s.chars().count()) && !s.chars().any(is_control)
}
pub(crate) fn id(s: &str) -> bool {
(1..=64).contains(&s.len()) && s.as_bytes()[0].is_ascii_alphabetic() && all(s, |b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-')
}
fn url_text(s: &str) -> bool {
s.len() <= 2048 && !s.chars().any(|c| is_control(c) || is_space(c))
}
pub(crate) fn token_endpoint(s: &str) -> bool {
let shape = (s.starts_with('/') && !s.starts_with("//")) || s.starts_with("https://");
shape && url_text(s)
}
pub(crate) fn nonce(s: &str) -> bool {
(8..=128).contains(&s.len()) && all(s, |b| b.is_ascii_alphanumeric() || b"+/=_-".contains(&b))
}
pub(crate) fn element_src(s: &str) -> bool {
s.starts_with("https://") && url_text(s)
}