aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2016-05-13 11:11:55 -0700
committerAndrew Kelley <superjoe30@gmail.com>2016-05-13 11:11:55 -0700
commit6a7f3c8df72349ffbe207ff7f8d98b6993b0e8c4 (patch)
tree0de0b746504d0934122885a654ceefbff7874ced /std
parentb68aee4f34dffe60c099cba0a4ca56580e7a9cf5 (diff)
downloadzig-6a7f3c8df72349ffbe207ff7f8d98b6993b0e8c4.tar.gz
zig-6a7f3c8df72349ffbe207ff7f8d98b6993b0e8c4.zip
std: make parsing an unsigned number generic
Diffstat (limited to 'std')
-rw-r--r--std/io.zig10
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{};
};