Function parseInt [src]

Parses the string buf as signed or unsigned representation in the specified base of an integral value of type T. When base is zero the string prefix is examined to detect the true base: A prefix of "0b" implies base=2, A prefix of "0o" implies base=8, A prefix of "0x" implies base=16, Otherwise base=10 is assumed. Ignores '_' character in buf. See also parseUnsigned.

Prototype

pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T

Parameters

T: typebuf: []const u8base: u8

Possible Errors

InvalidCharacter

The input was empty or contained an invalid character

Overflow

The result cannot fit in the type specified

Example

test parseInt { try std.testing.expectEqual(-10, try parseInt(i32, "-10", 10)); try std.testing.expectEqual(10, try parseInt(i32, "+10", 10)); try std.testing.expectEqual(10, try parseInt(u32, "+10", 10)); try std.testing.expectError(error.Overflow, parseInt(u32, "-10", 10)); try std.testing.expectError(error.InvalidCharacter, parseInt(u32, " 10", 10)); try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "10 ", 10)); try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "_10_", 10)); try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x_10_", 10)); try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x10_", 10)); try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x_10", 10)); try std.testing.expectEqual(255, try parseInt(u8, "255", 10)); try std.testing.expectError(error.Overflow, parseInt(u8, "256", 10)); // +0 and -0 should work for unsigned try std.testing.expectEqual(0, try parseInt(u8, "-0", 10)); try std.testing.expectEqual(0, try parseInt(u8, "+0", 10)); // ensure minInt is parsed correctly try std.testing.expectEqual(math.minInt(i1), try parseInt(i1, "-1", 10)); try std.testing.expectEqual(math.minInt(i8), try parseInt(i8, "-128", 10)); try std.testing.expectEqual(math.minInt(i43), try parseInt(i43, "-4398046511104", 10)); // empty string or bare +- is invalid try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "", 10)); try std.testing.expectError(error.InvalidCharacter, parseInt(i32, "", 10)); try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "+", 10)); try std.testing.expectError(error.InvalidCharacter, parseInt(i32, "+", 10)); try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "-", 10)); try std.testing.expectError(error.InvalidCharacter, parseInt(i32, "-", 10)); // autodectect the base try std.testing.expectEqual(111, try parseInt(i32, "111", 0)); try std.testing.expectEqual(111, try parseInt(i32, "1_1_1", 0)); try std.testing.expectEqual(111, try parseInt(i32, "1_1_1", 0)); try std.testing.expectEqual(7, try parseInt(i32, "+0b111", 0)); try std.testing.expectEqual(7, try parseInt(i32, "+0B111", 0)); try std.testing.expectEqual(7, try parseInt(i32, "+0b1_11", 0)); try std.testing.expectEqual(73, try parseInt(i32, "+0o111", 0)); try std.testing.expectEqual(73, try parseInt(i32, "+0O111", 0)); try std.testing.expectEqual(73, try parseInt(i32, "+0o11_1", 0)); try std.testing.expectEqual(273, try parseInt(i32, "+0x111", 0)); try std.testing.expectEqual(-7, try parseInt(i32, "-0b111", 0)); try std.testing.expectEqual(-7, try parseInt(i32, "-0b11_1", 0)); try std.testing.expectEqual(-73, try parseInt(i32, "-0o111", 0)); try std.testing.expectEqual(-273, try parseInt(i32, "-0x111", 0)); try std.testing.expectEqual(-273, try parseInt(i32, "-0X111", 0)); try std.testing.expectEqual(-273, try parseInt(i32, "-0x1_11", 0)); // bare binary/octal/decimal prefix is invalid try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0b", 0)); try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0o", 0)); try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x", 0)); // edge cases which previously errored due to base overflowing T try std.testing.expectEqual(@as(i2, -2), try std.fmt.parseInt(i2, "-10", 2)); try std.testing.expectEqual(@as(i4, -8), try std.fmt.parseInt(i4, "-10", 8)); try std.testing.expectEqual(@as(i5, -16), try std.fmt.parseInt(i5, "-10", 16)); }

Source

pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T { return parseIntWithGenericCharacter(T, u8, buf, base); }