@@ -687,6 +687,71 @@ impl u8 {
687687 pub fn escape_ascii ( & self ) -> ascii:: EscapeDefault {
688688 ascii:: escape_default ( * self )
689689 }
690+
691+ /// Converts a value to a digit in the given radix.
692+ ///
693+ /// A 'radix' here is sometimes also called a 'base'. A radix of two
694+ /// indicates a binary number, a radix of ten, decimal, and a radix of
695+ /// sixteen, hexadecimal, to give some common values. Arbitrary
696+ /// radices are supported.
697+ ///
698+ /// 'Digit' is defined to be only the following ASCII characters:
699+ ///
700+ /// * `0-9`
701+ /// * `a-z`
702+ /// * `A-Z`
703+ ///
704+ /// # Errors
705+ ///
706+ /// Returns `None` if the value does not refer to a digit in the given radix.
707+ ///
708+ /// # Panics
709+ ///
710+ /// Panics if given a radix larger than 36.
711+ ///
712+ /// # Examples
713+ ///
714+ /// Basic usage:
715+ ///
716+ /// ```
717+ /// assert_eq!(b'1'.to_digit(10), Some(1));
718+ /// assert_eq!(b'f'.to_digit(16), Some(15));
719+ /// ```
720+ ///
721+ /// Passing a non-digit results in failure:
722+ ///
723+ /// ```
724+ /// assert_eq!(b'f'.to_digit(10), None);
725+ /// assert_eq!(b'z'.to_digit(16), None);
726+ /// ```
727+ ///
728+ /// Passing a large radix, causing a panic:
729+ ///
730+ /// ```should_panic
731+ /// // this panics
732+ /// b'1'.to_digit(37);
733+ /// ```
734+ #[ unstable( feature = "ascii_to_digit" , issue = "none" ) ]
735+ #[ inline]
736+ pub fn to_digit ( & self , radix : u32 ) -> Option < u32 > {
737+ assert ! ( radix <= 36 , "to_digit: radix is too high (maximum 36)" ) ;
738+ // the code is split up here to improve execution speed for cases where
739+ // the `radix` is constant and 10 or smaller
740+ let val = if intrinsics:: likely ( radix <= 10 ) {
741+ // If not a digit, a number greater than radix will be created.
742+ self . wrapping_sub ( b'0' )
743+ } else {
744+ match self {
745+ b'0' ..=b'9' => self - b'0' ,
746+ b'a' ..=b'z' => self - b'a' + 10 ,
747+ b'A' ..=b'Z' => self - b'A' + 10 ,
748+ _ => return None ,
749+ }
750+ } ;
751+ let val: u32 = val. into ( ) ;
752+
753+ if val < radix { Some ( val) } else { None }
754+ }
690755}
691756
692757#[ lang = "u16" ]
0 commit comments