From a118725c8722d44d354fedf20e56ea93ed907fc3 Mon Sep 17 00:00:00 2001 From: andrepd Date: Mon, 17 Nov 2025 12:56:07 +0000 Subject: [PATCH 1/2] Restrict is_digit to radices between 2 and 36 (align with std) This is the behaviour for `char::is_digit`. --- src/ascii_char.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/ascii_char.rs b/src/ascii_char.rs index 39f2ceb..2cf5da4 100644 --- a/src/ascii_char.rs +++ b/src/ascii_char.rs @@ -468,15 +468,15 @@ impl AsciiChar { /// /// # Panics /// - /// Radixes greater than 36 are not supported and will result in a panic. + /// Panics if given a radix smaller than 2 or larger than 36. #[must_use] pub fn is_digit(self, radix: u32) -> bool { match (self as u8, radix) { - (b'0'..=b'9', 0..=36) => u32::from(self as u8 - b'0') < radix, + (b'0'..=b'9', 2..=36) => u32::from(self as u8 - b'0') < radix, (b'a'..=b'z', 11..=36) => u32::from(self as u8 - b'a') < radix - 10, (b'A'..=b'Z', 11..=36) => u32::from(self as u8 - b'A') < radix - 10, - (_, 0..=36) => false, - (_, _) => panic!("radixes greater than 36 are not supported"), + (_, 2..=36) => false, + (_, _) => panic!("radix must be in the range 2 to 36 inclusive"), } } @@ -1050,15 +1050,22 @@ mod tests { #[test] fn is_digit_strange_radixes() { - assert_eq!(AsciiChar::_0.is_digit(0), '0'.is_digit(0)); - assert_eq!(AsciiChar::_0.is_digit(1), '0'.is_digit(1)); + assert_eq!(AsciiChar::_0.is_digit(2), '0'.is_digit(2)); + assert_eq!(AsciiChar::_1.is_digit(2), '1'.is_digit(2)); + assert_eq!(AsciiChar::_2.is_digit(2), '2'.is_digit(2)); assert_eq!(AsciiChar::_5.is_digit(5), '5'.is_digit(5)); assert_eq!(AsciiChar::z.is_digit(35), 'z'.is_digit(35)); } #[test] #[should_panic] - fn is_digit_bad_radix() { + fn is_digit_bad_radix_small() { + let _ = AsciiChar::_0.is_digit(1); + } + + #[test] + #[should_panic] + fn is_digit_bad_radix_big() { let _ = AsciiChar::_7.is_digit(37); } From 7fa5f75cfe075c109b3d6750d013ddb3af0436fa Mon Sep 17 00:00:00 2001 From: andrepd Date: Mon, 17 Nov 2025 12:56:26 +0000 Subject: [PATCH 2/2] Silence miscellaneous warnings --- src/ascii_char.rs | 6 +++--- src/ascii_str.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ascii_char.rs b/src/ascii_char.rs index 2cf5da4..d2803f8 100644 --- a/src/ascii_char.rs +++ b/src/ascii_char.rs @@ -704,7 +704,7 @@ impl AsciiChar { // the unprintable characters from 0x0 to 0x1f, ordered correctly. // As `b` is guaranteed to be within 0x0 to 0x1f, the conversion represents a // valid character. - b @ 0x0..=0x1f => unsafe { mem::transmute('␀' as u32 + b as u32) }, + b @ 0x0..=0x1f => unsafe { char::from_u32_unchecked('␀' as u32 + b as u32) }, // 0x7f (delete) has it's own character at codepoint 0x2420, not 0x247f, so it is special // cased to return it's character @@ -794,8 +794,8 @@ macro_rules! impl_into_partial_eq_ord { ($wider:ty, $to_wider:expr) => { impl From for $wider { #[inline] - fn from(a: AsciiChar) -> $wider { - $to_wider(a) + fn from(chr: AsciiChar) -> $wider { + $to_wider(chr) } } impl PartialEq<$wider> for AsciiChar { diff --git a/src/ascii_str.rs b/src/ascii_str.rs index bfdf54e..3568e85 100644 --- a/src/ascii_str.rs +++ b/src/ascii_str.rs @@ -226,7 +226,7 @@ impl AsciiStr { /// Returns an iterator over the characters of the `AsciiStr`. #[inline] #[must_use] - pub fn chars(&self) -> Chars { + pub fn chars(&self) -> Chars<'_> { Chars(self.slice.iter()) } @@ -234,7 +234,7 @@ impl AsciiStr { /// value of each `AsciiChar`. #[inline] #[must_use] - pub fn chars_mut(&mut self) -> CharsMut { + pub fn chars_mut(&mut self) -> CharsMut<'_> { CharsMut(self.slice.iter_mut()) }