[pve-devel] [PATCH installer 2/5] common: fqdn: implement case-insensitive comparison as per RFC 952

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Feb 23 17:10:37 CET 2024


Am 15/02/2024 um 13:39 schrieb Christoph Heiss:
> +impl PartialEq for Fqdn {
> +    fn eq(&self, other: &Self) -> bool {
> +        // Case-insensitive comparison, as per RFC 952 "ASSUMPTIONS",
> +        // RFC 1035 sec. 2.3.3. "Character Case" and RFC 4343 as a whole
> +        let a = self
> +            .parts
> +            .iter()
> +            .map(|s| s.to_lowercase())
> +            .collect::<Vec<String>>();
> +
> +        let b = other
> +            .parts
> +            .iter()
> +            .map(|s| s.to_lowercase())
> +            .collect::<Vec<String>>();
> +
> +        a == b

could be probably more efficiently done by doing the comparison part wise, not
on the whole thing, and with an early abort for mismatching part count, e.g.,
something like:

if self.parts.len() != other.parts.len() {             
    return false;                                      
}                                                      
                                                       
self.parts                                             
    .iter()                                            
    .zip(other.parts.iter())                           
    .all(|(a, b)| a.to_lowercase() == b.to_lowercase())

> @@ -309,4 +329,10 @@ mod tests {
>              "foo.example.com"
>          );
>      }
> +
> +    #[test]
> +    fn fqdn_compare() {
> +        assert_eq!(Fqdn::from("example.com"), Fqdn::from("ExAmPle.Com"));
> +        assert_eq!(Fqdn::from("ExAmPle.Com"), Fqdn::from("example.com"));

I always like throwing in some assert_ne ones for sanity checking (e.g., such
optimization tries like above), as otherwise one won't notice if this is broken,
like by just always returning false..





More information about the pve-devel mailing list