Commit 1b5d1877ae
Changed files (4)
test/cases/enum_with_members.zig
@@ -1,31 +0,0 @@
-const std = @import("std");
-const assert = std.debug.assert;
-const io = std.io;
-const str = std.str;
-
-enum ET {
- SINT: i32,
- UINT: u32,
-
- pub fn print(a: &ET, buf: []u8) -> %usize {
- return switch (*a) {
- SINT => |x| { io.bufPrintInt(i32, buf, x) },
- UINT => |x| { io.bufPrintInt(u32, buf, x) },
- }
- }
-}
-
-fn enumWithMembers() {
- @setFnTest(this, true);
-
- const a = ET.SINT { -42 };
- const b = ET.UINT { 42 };
- var buf: [20]u8 = undefined;
-
- assert(%%a.print(buf) == 3);
- assert(str.eql(buf[0...3], "-42"));
-
- assert(%%b.print(buf) == 2);
- assert(str.eql(buf[0...2], "42"));
-}
-
test/cases3/enum_with_members.zig
@@ -0,0 +1,83 @@
+const ET = enum {
+ SINT: i32,
+ UINT: u32,
+
+ pub fn print(a: &ET, buf: []u8) -> %usize {
+ return switch (*a) {
+ ET.SINT => |x| { bufPrintInt(i32, buf, x) },
+ ET.UINT => |x| { bufPrintInt(u32, buf, x) },
+ }
+ }
+};
+
+fn enumWithMembers() {
+ @setFnTest(this);
+
+ const a = ET.SINT { -42 };
+ const b = ET.UINT { 42 };
+ var buf: [20]u8 = undefined;
+
+ assert(%%a.print(buf) == 3);
+ assert(memeql(buf[0...3], "-42"));
+
+ assert(%%b.print(buf) == 2);
+ assert(memeql(buf[0...2], "42"));
+}
+
+// TODO all the below should be imported from std
+
+const max_u64_base10_digits = 20;
+pub fn bufPrintInt(inline T: type, out_buf: []u8, x: T) -> usize {
+ if (T.is_signed) bufPrintSigned(T, out_buf, x) else bufPrintUnsigned(T, out_buf, x)
+}
+
+fn bufPrintSigned(inline T: type, out_buf: []u8, x: T) -> usize {
+ const uint = @intType(false, T.bit_count);
+ if (x < 0) {
+ out_buf[0] = '-';
+ return 1 + bufPrintUnsigned(uint, out_buf[1...], uint(-(x + 1)) + 1);
+ } else {
+ return bufPrintUnsigned(uint, out_buf, uint(x));
+ }
+}
+
+fn bufPrintUnsigned(inline T: type, out_buf: []u8, x: T) -> usize {
+ var buf: [max_u64_base10_digits]u8 = undefined;
+ var a = x;
+ var index: usize = buf.len;
+
+ while (true) {
+ const digit = a % 10;
+ index -= 1;
+ buf[index] = '0' + u8(digit);
+ a /= 10;
+ if (a == 0)
+ break;
+ }
+
+ const len = buf.len - index;
+
+ @memcpy(&out_buf[0], &buf[index], len);
+
+ return len;
+}
+
+// TODO const assert = @import("std").debug.assert;
+fn assert(ok: bool) {
+ if (!ok)
+ @unreachable();
+}
+
+// TODO import from std.str
+pub fn memeql(a: []const u8, b: []const u8) -> bool {
+ sliceEql(u8, a, b)
+}
+
+// TODO import from std.str
+pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
+ if (a.len != b.len) return false;
+ for (a) |item, index| {
+ if (b[index] != item) return false;
+ }
+ return true;
+}
test/self_hosted.zig
@@ -2,7 +2,6 @@ const std = @import("std");
const assert = std.debug.assert;
const str = std.str;
const cstr = std.cstr;
-const test_enum_with_members = @import("cases/enum_with_members.zig");
fn explicitCastMaybePointers() {
test/self_hosted3.zig
@@ -6,6 +6,7 @@ const test_cast= @import("cases3/cast.zig");
const test_const_slice_child = @import("cases3/const_slice_child.zig");
const test_defer = @import("cases3/defer.zig");
const test_enum = @import("cases3/enum.zig");
+const test_enum_with_members = @import("cases3/enum_with_members.zig");
const test_error = @import("cases3/error.zig");
const test_eval = @import("cases3/eval.zig");
const test_fn = @import("cases3/fn.zig");