| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | |
| 5 | pub fn parse_int(comptime T: type, s: ?string, b: u8, d: T) T { |
| 6 | if (s == null) return d; |
| 7 | return extras.parseDigits(T, s.?, b) catch d; |
| 8 | } |
| 9 | |
| 10 | test { |
| 11 | try std.testing.expect(parse_int(u32, "25", 10, 13) == 25); |
| 12 | } |
| 13 | test { |
| 14 | try std.testing.expect(parse_int(u32, "Q", 10, 13) == 13); |
| 15 | } |
| 16 | test { |
| 17 | try std.testing.expect(parse_int(u32, "", 10, 13) == 13); |
| 18 | } |
| 19 | test { |
| 20 | try std.testing.expect(parse_int(u32, null, 10, 13) == 13); |
| 21 | } |