extern crate alloc;
use alloc::string::String;
#[must_use]
pub fn check_postcode_uk(input: &str) -> bool {
if input.is_empty() {
return false;
}
if input.contains('\r') || input.contains('\n') || input.contains('\t') {
return false;
}
let compact: String = input
.trim()
.chars()
.filter(|&c| c != ' ')
.map(|c| c.to_ascii_uppercase())
.collect();
if compact.len() <= 3 || !compact.is_char_boundary(compact.len() - 3) {
return false;
}
let (outward, inward) = compact.split_at(compact.len() - 3);
if outward == "GIR" && inward == "0AA" {
return true;
}
let mut in_ok = false;
for (i, b) in inward.bytes().enumerate() {
match i {
0 => in_ok = b.is_ascii_digit(),
1 | 2 => in_ok &= b.is_ascii_uppercase(),
_ => {
in_ok = false;
break;
}
}
}
if !in_ok {
return false;
}
let mut letters = 0;
for &b in outward.as_bytes() {
if b.is_ascii_uppercase() {
letters += 1;
} else {
break;
}
}
if !matches!(letters, 1 | 2) {
return false;
}
let Some(rest) = outward.as_bytes().get(letters..) else {
return false;
};
let Some((&digit, tail)) = rest.split_first() else {
return false;
};
if !digit.is_ascii_digit() {
return false;
}
tail.len() <= 1 && tail.iter().all(|b| b.is_ascii_alphanumeric())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn accepts_uk_patterns() {
for s in [
"SW1A 1AA", "M1 1AE", "B33 8TH", "DN55 1PT", "GIR 0AA", "sw1a1aa",
] {
assert!(check_postcode_uk(s), "must accept {s}");
}
}
#[test]
fn rejects_uk_patterns() {
for s in [
"", "SW1A1A", "12345", "SW1A 1A", "ZZZ 1AA", "ééé", "K1A 0B1", "123 AB",
] {
assert!(!check_postcode_uk(s), "must reject {s}");
}
}
}