diff --git a/crates/color/src/lib.rs b/crates/color/src/lib.rs index 9e3ddd0b..2d60e3ac 100644 --- a/crates/color/src/lib.rs +++ b/crates/color/src/lib.rs @@ -21,7 +21,8 @@ impl Rgb { format!("#{:02x}{:02x}{:02x}", self.0, self.1, self.2) } pub fn parse_hex(s: &str) -> Option { - let s = s.trim().trim_start_matches('#'); + let s = s.trim(); + let s = s.strip_prefix('#').unwrap_or(s); if s.len() != 6 || !s.chars().all(|c| c.is_ascii_hexdigit()) { return None; } @@ -233,6 +234,15 @@ mod tests { assert_eq!(Rgb::parse_hex("#012345"), Some(Rgb(1, 35, 69))); } + #[test] + fn parse_hex_rejects_repeated_hash() { + assert_eq!(Rgb::parse_hex("##ff0000"), None); + assert_eq!(Rgb::parse_hex("###ff0000"), None); + assert_eq!(Color::parse("##ff0000"), None); + assert_eq!(Rgb::parse_hex("#ff0000"), Some(Rgb(255, 0, 0))); + assert_eq!(Rgb::parse_hex("ff0000"), Some(Rgb(255, 0, 0))); + } + #[test] fn palette_known_entries() { assert_eq!(aci_rgb(1), Rgb(255, 0, 0));