diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2016-01-08 17:52:45 -0700 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2016-01-08 17:52:45 -0700 |
| commit | d14a31100f3f4e7b8d43c8ad794a82da36532aa7 (patch) | |
| tree | 9cb70f9785427c24499dff91bc3383837c7de78d /std | |
| parent | 2a8d6af7ba9dcea5f13e306f2d032f3f344950af (diff) | |
| download | zig-d14a31100f3f4e7b8d43c8ad794a82da36532aa7.tar.gz zig-d14a31100f3f4e7b8d43c8ad794a82da36532aa7.zip | |
implement unknown size array indexing and slicing
Diffstat (limited to 'std')
| -rw-r--r-- | std/builtin.zig | 1 | ||||
| -rw-r--r-- | std/rand.zig | 6 | ||||
| -rw-r--r-- | std/std.zig | 18 |
3 files changed, 10 insertions, 15 deletions
diff --git a/std/builtin.zig b/std/builtin.zig index dbb69644d5..572adf00cc 100644 --- a/std/builtin.zig +++ b/std/builtin.zig @@ -10,6 +10,7 @@ export fn memset(dest: &u8, c: u8, n: usize) -> &u8 { return dest; } +// TODO annotate parameters with noalias export fn memcpy(dest: &u8, src: &const u8, n: usize) -> &u8 { var index : #typeof(n) = 0; while (index != n) { diff --git a/std/rand.zig b/std/rand.zig index 0362e2d8bc..46b3674929 100644 --- a/std/rand.zig +++ b/std/rand.zig @@ -45,8 +45,7 @@ pub struct Rand { var rand_val_array : [#sizeof(u32)]u8; *(rand_val_array.ptr as &u32) = r.get_u32(); while (bytes_left > 0) { - // TODO array index operator so we can remove the .ptr - buf.ptr[buf.len - bytes_left] = rand_val_array[#sizeof(u32) - bytes_left]; + buf[buf.len - bytes_left] = rand_val_array[#sizeof(u32) - bytes_left]; bytes_left -= 1; } } @@ -88,8 +87,7 @@ pub struct Rand { fn get_bytes_aligned(r: &Rand, buf: []u8) -> usize { var bytes_left = buf.len; while (bytes_left >= 4) { - // TODO: array access so we can remove .ptr - *(&buf.ptr[buf.len - bytes_left] as &u32) = r.get_u32(); + *(&buf[buf.len - bytes_left] as &u32) = r.get_u32(); bytes_left -= #sizeof(u32); } return bytes_left; diff --git a/std/std.zig b/std/std.zig index da412e3f90..82471748b1 100644 --- a/std/std.zig +++ b/std/std.zig @@ -26,7 +26,7 @@ pub fn fprint_str(fd: isize, str: []const u8) -> isize { pub fn print_u64(x: u64) -> isize { // TODO use max_u64_base10_digits instead of hardcoding 20 var buf: [20]u8; - const len = buf_print_u64(buf.ptr, x); + const len = buf_print_u64(buf, x); return write(stdout_fileno, buf.ptr, len); } @@ -35,7 +35,7 @@ pub fn print_u64(x: u64) -> isize { pub fn print_i64(x: i64) -> isize { // TODO use max_u64_base10_digits instead of hardcoding 20 var buf: [20]u8; - const len = buf_print_i64(buf.ptr, x); + const len = buf_print_i64(buf, x); return write(stdout_fileno, buf.ptr, len); } @@ -56,8 +56,7 @@ pub fn parse_u64(buf: []u8, radix: u8, result: &u64) -> bool { var i : #typeof(buf.len) = 0; while (i < buf.len) { - // TODO array indexing operator - const c = buf.ptr[i]; + const c = buf[i]; const digit = char_to_digit(c); if (digit > radix) { @@ -100,20 +99,16 @@ fn char_to_digit(c: u8) -> u8 { const max_u64_base10_digits: usize = 20; -// TODO use an array for out_buf instead of pointer. this should give bounds checking in -// debug mode and length can get optimized out in release mode. requires array slicing syntax -// for the buf_print_u64 call. -fn buf_print_i64(out_buf: &u8, x: i64) -> usize { +fn buf_print_i64(out_buf: []u8, x: i64) -> usize { if (x < 0) { out_buf[0] = '-'; - return 1 + buf_print_u64(&out_buf[1], ((-(x + 1)) as u64) + 1); + return 1 + buf_print_u64(out_buf[1...], ((-(x + 1)) as u64) + 1); } else { return buf_print_u64(out_buf, x as u64); } } -// TODO use an array for out_buf instead of pointer. -fn buf_print_u64(out_buf: &u8, x: u64) -> usize { +fn buf_print_u64(out_buf: []u8, x: u64) -> usize { var buf: [max_u64_base10_digits]u8; var a = x; var index = buf.len; @@ -130,6 +125,7 @@ fn buf_print_u64(out_buf: &u8, x: u64) -> usize { const len = buf.len - index; // TODO memcpy intrinsic + // @memcpy(out_buf, buf, len); var i: usize = 0; while (i < len) { out_buf[i] = buf[index + i]; |
