diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2016-05-13 11:11:55 -0700 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2016-05-13 11:11:55 -0700 |
| commit | 6a7f3c8df72349ffbe207ff7f8d98b6993b0e8c4 (patch) | |
| tree | 0de0b746504d0934122885a654ceefbff7874ced /std/io.zig | |
| parent | b68aee4f34dffe60c099cba0a4ca56580e7a9cf5 (diff) | |
| download | zig-6a7f3c8df72349ffbe207ff7f8d98b6993b0e8c4.tar.gz zig-6a7f3c8df72349ffbe207ff7f8d98b6993b0e8c4.zip | |
std: make parsing an unsigned number generic
Diffstat (limited to 'std/io.zig')
| -rw-r--r-- | std/io.zig | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/std/io.zig b/std/io.zig index 98eecef654..be4320ad31 100644 --- a/std/io.zig +++ b/std/io.zig @@ -211,8 +211,8 @@ pub struct InStream { pub error InvalidChar; pub error Overflow; -pub fn parse_u64(buf: []u8, radix: u8) -> %u64 { - var x : u64 = 0; +pub fn parse_unsigned(T: type)(buf: []u8, radix: u8) -> %T { + var x: T = 0; for (buf) |c| { const digit = char_to_digit(c); @@ -222,12 +222,12 @@ pub fn parse_u64(buf: []u8, radix: u8) -> %u64 { } // x *= radix - if (@mul_with_overflow(u64, x, radix, &x)) { + if (@mul_with_overflow(T, x, radix, &x)) { return error.Overflow; } // x += digit - if (@add_with_overflow(u64, x, digit, &x)) { + if (@add_with_overflow(T, x, digit, &x)) { return error.Overflow; } } @@ -404,7 +404,7 @@ pub fn buf_print_f64(out_buf: []u8, x: f64, decimals: isize) -> isize { #attribute("test") fn parse_u64_digit_too_big() { - parse_u64("123a", 10) %% |err| { + parse_unsigned(u64)("123a", 10) %% |err| { if (err == error.InvalidChar) return; unreachable{}; }; |
