fn sliceToInt(comptime T: type, slice: []const u8) !T T: typeslice: []const u8777
778
779
780
781
782
783
784
785fn sliceToInt(comptime T: type, slice: []const u8) !T {
if (isNumberFormattedLikeAnInteger(slice))
return std.fmt.parseInt(T, slice, 10);
// Try to coerce a float to an integer.
const float = try std.fmt.parseFloat(f128, slice);
if (@round(float) != float) return error.InvalidNumber;
if (float > @as(f128, @floatFromInt(std.math.maxInt(T))) or float < @as(f128, @floatFromInt(std.math.minInt(T)))) return error.Overflow;
return @as(T, @intCast(@as(i128, @intFromFloat(float))));
}