diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-02-12 17:22:35 -0500 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-02-12 17:35:51 -0500 |
| commit | 6dba1f1c8eee5e2f037c7ef216bc64423aef8e00 (patch) | |
| tree | f39a29e98b7e3404b114aaa08cd26d767a1666c1 /std/io.zig | |
| parent | ca180d3f02914d282505752a1d2fe08e175f9d99 (diff) | |
| download | zig-6dba1f1c8eee5e2f037c7ef216bc64423aef8e00.tar.gz zig-6dba1f1c8eee5e2f037c7ef216bc64423aef8e00.zip | |
slice and array re-work plus some misc. changes
* `@truncate` builtin allows casting to the same size integer.
It also performs two's complement casting between signed and
unsigned integers.
* The idiomatic way to convert between bytes and numbers is now
`mem.readInt` and `mem.writeInt` instead of an unsafe cast.
It works at compile time, is safer, and looks cleaner.
* Implicitly casting an array to a slice is allowed only if the
slice is const.
* Constant pointer values know if their memory is from a compile-
time constant value or a compile-time variable.
* Cast from [N]u8 to []T no longer allowed, but [N]u8 to []const T
still allowed.
* Fix inability to pass a mutable pointer to comptime variable at
compile-time to a function and have the function modify the
memory pointed to by the pointer.
* Add the `comptime T: type` parameter back to mem.eql. Prevents
accidentally creating instantiations for arrays.
Diffstat (limited to 'std/io.zig')
| -rw-r--r-- | std/io.zig | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/std/io.zig b/std/io.zig index 05bbba1bea..13b7c36abc 100644 --- a/std/io.zig +++ b/std/io.zig @@ -6,7 +6,6 @@ const system = switch(@compileVar("os")) { const errno = @import("errno.zig"); const math = @import("math.zig"); -const endian = @import("endian.zig"); const debug = @import("debug.zig"); const assert = debug.assert; const os = @import("os.zig"); @@ -365,7 +364,7 @@ pub const InStream = struct { pub fn readByte(is: &InStream) -> %u8 { var result: [1]u8 = undefined; - %return is.readNoEof(result); + %return is.readNoEof(result[0...]); return result[0]; } @@ -378,10 +377,9 @@ pub const InStream = struct { } pub fn readInt(is: &InStream, is_be: bool, comptime T: type) -> %T { - var result: T = undefined; - const result_slice = ([]u8)((&result)[0...1]); - %return is.readNoEof(result_slice); - return endian.swapIf(!is_be, T, result); + var bytes: [@sizeOf(T)]u8 = undefined; + %return is.readNoEof(bytes[0...]); + return mem.readInt(bytes, T, is_be); } pub fn readVarInt(is: &InStream, is_be: bool, comptime T: type, size: usize) -> %T { @@ -390,7 +388,7 @@ pub const InStream = struct { var input_buf: [8]u8 = undefined; const input_slice = input_buf[0...size]; %return is.readNoEof(input_slice); - return mem.sliceAsInt(input_slice, is_be, T); + return mem.readInt(input_slice, T, is_be); } pub fn seekForward(is: &InStream, amount: usize) -> %void { @@ -589,18 +587,19 @@ fn testParseUnsignedComptime() { fn testBufPrintInt() { @setFnTest(this); - var buf: [max_int_digits]u8 = undefined; - assert(mem.eql(bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110")); - assert(mem.eql(bufPrintIntToSlice(buf, i32(-12345678), 10, false, 0), "-12345678")); - assert(mem.eql(bufPrintIntToSlice(buf, i32(-12345678), 16, false, 0), "-bc614e")); - assert(mem.eql(bufPrintIntToSlice(buf, i32(-12345678), 16, true, 0), "-BC614E")); + var buffer: [max_int_digits]u8 = undefined; + const buf = buffer[0...]; + assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110")); + assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, 0), "-12345678")); + assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, false, 0), "-bc614e")); + assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, true, 0), "-BC614E")); - assert(mem.eql(bufPrintIntToSlice(buf, u32(12345678), 10, true, 0), "12345678")); + assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(12345678), 10, true, 0), "12345678")); - assert(mem.eql(bufPrintIntToSlice(buf, u32(666), 10, false, 6), "000666")); - assert(mem.eql(bufPrintIntToSlice(buf, u32(0x1234), 16, false, 6), "001234")); - assert(mem.eql(bufPrintIntToSlice(buf, u32(0x1234), 16, false, 1), "1234")); + assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(666), 10, false, 6), "000666")); + assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 6), "001234")); + assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 1), "1234")); - assert(mem.eql(bufPrintIntToSlice(buf, i32(42), 10, false, 3), "+42")); - assert(mem.eql(bufPrintIntToSlice(buf, i32(-42), 10, false, 3), "-42")); + assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(42), 10, false, 3), "+42")); + assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-42), 10, false, 3), "-42")); } |
