pub(crate) fn strip_prefix_ignore_ascii_case<'a>(s: &'a str, prefix: &str) -> Option<&'a str> {
let head = s.get(..prefix.len())?;
head.eq_ignore_ascii_case(prefix)
.then(|| &s[prefix.len()..])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_prefix_matches_in_any_ascii_case() {
for prefix in ["max-age=", "MAX-AGE=", "Max-Age=", "mAx-AgE="] {
assert_eq!(
strip_prefix_ignore_ascii_case(&format!("{prefix}300"), "max-age="),
Some("300"),
"{prefix} should match"
);
}
}
#[test]
fn a_different_prefix_does_not_match() {
assert_eq!(strip_prefix_ignore_ascii_case("no-cache", "max-age="), None);
assert_eq!(strip_prefix_ignore_ascii_case("", "Bearer"), None);
}
#[test]
fn the_remainder_is_returned_unexamined() {
assert_eq!(
strip_prefix_ignore_ascii_case("Bearerish", "Bearer"),
Some("ish")
);
assert_eq!(strip_prefix_ignore_ascii_case("Bearer", "Bearer"), Some(""));
}
#[test]
fn a_prefix_ending_inside_a_character_does_not_match() {
assert_eq!(strip_prefix_ignore_ascii_case("é", "ma"), None);
assert_eq!(strip_prefix_ignore_ascii_case("mé", "max"), None);
}
#[test]
fn non_ascii_case_does_not_fold() {
assert_eq!(strip_prefix_ignore_ascii_case("İd=1", "id="), None);
}
}