diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2016-01-08 03:59:37 -0700 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2016-01-08 03:59:37 -0700 |
| commit | 0c84ecd19d0dbe6a98dd5e3f440cc9d7a78ea7d9 (patch) | |
| tree | c05148fa68edb77dbdedb501d590e469e41f8567 /std | |
| parent | e1f498212c74c48d740b959484123478dec748ff (diff) | |
| download | zig-0c84ecd19d0dbe6a98dd5e3f440cc9d7a78ea7d9.tar.gz zig-0c84ecd19d0dbe6a98dd5e3f440cc9d7a78ea7d9.zip | |
codegen: fix else if expression and maybe unwrap expr
Diffstat (limited to 'std')
| -rw-r--r-- | std/std.zig | 69 |
1 files changed, 52 insertions, 17 deletions
diff --git a/std/std.zig b/std/std.zig index 411f59b423..da412e3f90 100644 --- a/std/std.zig +++ b/std/std.zig @@ -39,28 +39,63 @@ pub fn print_i64(x: i64) -> isize { return write(stdout_fileno, buf.ptr, len); } -/* // TODO error handling -pub fn readline(buf: []u8) -> ?[]u8 { - var index : usize = 0; - while (index < buf.len) { - // TODO unknown size array indexing operator - const err = read(stdin_fileno, &buf.ptr[index], 1); - if (err != 0) { - return null; +pub fn readline(buf: []u8, out_len: &usize) -> bool { + // TODO unknown size array indexing operator + const amt_read = read(stdin_fileno, buf.ptr, buf.len); + if (amt_read < 0) { + return true; + } + *out_len = amt_read as usize; + return false; +} + +// TODO return ?u64 when we support returning struct byval +pub fn parse_u64(buf: []u8, radix: u8, result: &u64) -> bool { + var x : u64 = 0; + + var i : #typeof(buf.len) = 0; + while (i < buf.len) { + // TODO array indexing operator + const c = buf.ptr[i]; + const digit = char_to_digit(c); + + if (digit > radix) { + return true; + } + + x *= radix; + x += digit; + + /* TODO intrinsics mul and add with overflow + // x *= radix + if (@mul_with_overflow_u64(x, radix, &x)) { + return true; } - // TODO unknown size array indexing operator - if (buf.ptr[index] == '\n') { - return buf[0...index + 1]; + + // x += digit + if (@add_with_overflow_u64(x, digit, &x)) { + return true; } - index += 1; + */ + + i += 1; } - return null; + + *result = x; + return false; } -*/ -fn digit_to_char(digit: u64) -> u8 { - '0' + (digit as u8) +fn char_to_digit(c: u8) -> u8 { + if ('0' <= c && c <= '9') { + c - '0' + } else if ('A' <= c && c <= 'Z') { + c - 'A' + 10 + } else if ('a' <= c && c <= 'z') { + c - 'a' + 10 + } else { + #max_value(u8) + } } const max_u64_base10_digits: usize = 20; @@ -86,7 +121,7 @@ fn buf_print_u64(out_buf: &u8, x: u64) -> usize { while (true) { const digit = a % 10; index -= 1; - buf[index] = digit_to_char(digit); + buf[index] = '0' + (digit as u8); a /= 10; if (a == 0) break; |
