@@ -1514,17 +1514,27 @@ pub fn Formatter(comptime formatFn: anytype) type {
1514
1514
///
1515
1515
/// Ignores '_' character in `buf`.
1516
1516
/// See also `parseUnsigned`.
1517
+ ///
1518
+ /// Asserts 'T' is an integer type
1517
1519
pub fn parseInt (comptime T : type , buf : []const u8 , base : u8 ) ParseIntError ! T {
1520
+ if (@typeInfo (T ) != .int ) {
1521
+ @compileError ("Cannot parse an integer into a non-integer type" );
1522
+ }
1518
1523
return parseIntWithGenericCharacter (T , u8 , buf , base );
1519
1524
}
1520
1525
1521
1526
/// Like `parseInt`, but with a generic `Character` type.
1527
+ ///
1528
+ /// Asserts that 'Result' is an integer type
1522
1529
pub fn parseIntWithGenericCharacter (
1523
1530
comptime Result : type ,
1524
1531
comptime Character : type ,
1525
1532
buf : []const Character ,
1526
1533
base : u8 ,
1527
1534
) ParseIntError ! Result {
1535
+ if (@typeInfo (Result ) != .int ) {
1536
+ @compileError ("Cannot parse an integer into a non-integer type" );
1537
+ }
1528
1538
if (buf .len == 0 ) return error .InvalidCharacter ;
1529
1539
if (buf [0 ] == '+' ) return parseIntWithSign (Result , Character , buf [1.. ], base , .pos );
1530
1540
if (buf [0 ] == '-' ) return parseIntWithSign (Result , Character , buf [1.. ], base , .neg );
@@ -1591,13 +1601,30 @@ test parseInt {
1591
1601
try std .testing .expectEqual (@as (i5 , -16 ), try std .fmt .parseInt (i5 , "-10" , 16 ));
1592
1602
}
1593
1603
1604
+ /// Parses an integer with a specified `sign`, accepting a generic `Character`
1605
+ /// type for width and a buffer of `Character`s, in the specified `base` of an
1606
+ /// integral type of value `Result`
1607
+ ///
1608
+ /// When `base` is zero the string prefix is examined to detect the true base:
1609
+ /// * A prefix of "0b" implies base=2,
1610
+ /// * A prefix of "0o" implies base=8,
1611
+ /// * A prefix of "0x" implies base=16,
1612
+ /// * Otherwise base=10 is assumed.
1613
+ ///
1614
+ /// Ignores '_' character in `buf`.
1615
+ ///
1616
+ /// Asserts that `Result` is an integer type
1594
1617
fn parseIntWithSign (
1595
1618
comptime Result : type ,
1596
1619
comptime Character : type ,
1597
1620
buf : []const Character ,
1598
1621
base : u8 ,
1599
1622
comptime sign : enum { pos , neg },
1600
1623
) ParseIntError ! Result {
1624
+ if (@typeInfo (Result ) != .int ) {
1625
+ @compileError ("Cannot parse an integer into a non-integer type" );
1626
+ }
1627
+
1601
1628
if (buf .len == 0 ) return error .InvalidCharacter ;
1602
1629
1603
1630
var buf_base = base ;
@@ -1670,7 +1697,12 @@ fn parseIntWithSign(
1670
1697
///
1671
1698
/// Ignores '_' character in `buf`.
1672
1699
/// See also `parseInt`.
1700
+ ///
1701
+ /// Asserts 'T' is an integer type
1673
1702
pub fn parseUnsigned (comptime T : type , buf : []const u8 , base : u8 ) ParseIntError ! T {
1703
+ if (@typeInfo (T ) != .int ) {
1704
+ @compileError ("Cannot parse an integer into a non-integer type" );
1705
+ }
1674
1706
return parseIntWithSign (T , u8 , buf , base , .pos );
1675
1707
}
1676
1708
0 commit comments