@@ -23,6 +23,16 @@ macro_rules! unlikely {
2323 } ;
2424}
2525
26+ // Use this when the generated code should differ between signed and unsigned types.
27+ macro_rules! sign_dependent_expr {
28+ ( signed ? if signed { $signed_case: expr } if unsigned { $unsigned_case: expr } ) => {
29+ $signed_case
30+ } ;
31+ ( unsigned ? if signed { $signed_case: expr } if unsigned { $unsigned_case: expr } ) => {
32+ $unsigned_case
33+ } ;
34+ }
35+
2636// All these modules are technically private and only exposed for coretests:
2737#[ cfg( not( no_fp_fmt_parse) ) ]
2838pub mod bignum;
@@ -1410,15 +1420,25 @@ const fn from_str_radix_panic(radix: u32) {
14101420}
14111421
14121422macro_rules! from_str_radix {
1413- ( $( $int_ty: ty) +) => { $(
1423+ ( $signedness : ident $ ( $int_ty: ty) +) => { $(
14141424 impl $int_ty {
14151425 /// Converts a string slice in a given base to an integer.
14161426 ///
1417- /// The string is expected to be an optional `+` sign
1418- /// followed by digits.
1419- /// Leading and trailing whitespace represent an error.
1420- /// Digits are a subset of these characters, depending on `radix`:
1427+ /// The string is expected to be an optional
1428+ #[ doc = sign_dependent_expr!{
1429+ $signedness ?
1430+ if signed {
1431+ " `+` or `-` "
1432+ }
1433+ if unsigned {
1434+ " `+` "
1435+ }
1436+ } ]
1437+ /// sign followed by only digits. Leading and trailing non-digit characters (including
1438+ /// whitespace) represent an error. Underscores (which are accepted in rust literals)
1439+ /// also represent an error.
14211440 ///
1441+ /// Digits are a subset of these characters, depending on `radix`:
14221442 /// * `0-9`
14231443 /// * `a-z`
14241444 /// * `A-Z`
@@ -1430,10 +1450,13 @@ macro_rules! from_str_radix {
14301450 /// # Examples
14311451 ///
14321452 /// Basic usage:
1433- ///
14341453 /// ```
14351454 #[ doc = concat!( "assert_eq!(" , stringify!( $int_ty) , "::from_str_radix(\" A\" , 16), Ok(10));" ) ]
14361455 /// ```
1456+ /// Trailing space returns error:
1457+ /// ```
1458+ #[ doc = concat!( "assert!(" , stringify!( $int_ty) , "::from_str_radix(\" 1 \" , 10).is_err());" ) ]
1459+ /// ```
14371460 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
14381461 #[ rustc_const_stable( feature = "const_int_from_str" , since = "1.82.0" ) ]
14391462 pub const fn from_str_radix( src: & str , radix: u32 ) -> Result <$int_ty, ParseIntError > {
@@ -1535,20 +1558,31 @@ macro_rules! from_str_radix {
15351558 ) +}
15361559}
15371560
1538- from_str_radix ! { i8 u8 i16 u16 i32 u32 i64 u64 i128 u128 }
1561+ from_str_radix ! { unsigned u8 u16 u32 u64 u128 }
1562+ from_str_radix ! { signed i8 i16 i32 i64 i128 }
15391563
15401564// Re-use the relevant implementation of from_str_radix for isize and usize to avoid outputting two
15411565// identical functions.
15421566macro_rules! from_str_radix_size_impl {
1543- ( $( $t: ident $size: ty) ,* ) => { $(
1567+ ( $( $signedness : ident $ t: ident $size: ty) ,* ) => { $(
15441568 impl $size {
15451569 /// Converts a string slice in a given base to an integer.
15461570 ///
1547- /// The string is expected to be an optional `+` sign
1548- /// followed by digits.
1549- /// Leading and trailing whitespace represent an error.
1550- /// Digits are a subset of these characters, depending on `radix`:
1571+ /// The string is expected to be an optional
1572+ #[ doc = sign_dependent_expr!{
1573+ $signedness ?
1574+ if signed {
1575+ " `+` or `-` "
1576+ }
1577+ if unsigned {
1578+ " `+` "
1579+ }
1580+ } ]
1581+ /// sign followed by only digits. Leading and trailing non-digit characters (including
1582+ /// whitespace) represent an error. Underscores (which are accepted in rust literals)
1583+ /// also represent an error.
15511584 ///
1585+ /// Digits are a subset of these characters, depending on `radix`:
15521586 /// * `0-9`
15531587 /// * `a-z`
15541588 /// * `A-Z`
@@ -1560,10 +1594,13 @@ macro_rules! from_str_radix_size_impl {
15601594 /// # Examples
15611595 ///
15621596 /// Basic usage:
1563- ///
15641597 /// ```
15651598 #[ doc = concat!( "assert_eq!(" , stringify!( $size) , "::from_str_radix(\" A\" , 16), Ok(10));" ) ]
15661599 /// ```
1600+ /// Trailing space returns error:
1601+ /// ```
1602+ #[ doc = concat!( "assert!(" , stringify!( $size) , "::from_str_radix(\" 1 \" , 10).is_err());" ) ]
1603+ /// ```
15671604 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
15681605 #[ rustc_const_stable( feature = "const_int_from_str" , since = "1.82.0" ) ]
15691606 pub const fn from_str_radix( src: & str , radix: u32 ) -> Result <$size, ParseIntError > {
@@ -1576,8 +1613,8 @@ macro_rules! from_str_radix_size_impl {
15761613}
15771614
15781615#[ cfg( target_pointer_width = "16" ) ]
1579- from_str_radix_size_impl ! { i16 isize , u16 usize }
1616+ from_str_radix_size_impl ! { signed i16 isize , unsigned u16 usize }
15801617#[ cfg( target_pointer_width = "32" ) ]
1581- from_str_radix_size_impl ! { i32 isize , u32 usize }
1618+ from_str_radix_size_impl ! { signed i32 isize , unsigned u32 usize }
15821619#[ cfg( target_pointer_width = "64" ) ]
1583- from_str_radix_size_impl ! { i64 isize , u64 usize }
1620+ from_str_radix_size_impl ! { signed i64 isize , unsigned u64 usize }
0 commit comments