aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-03-05 10:33:02 -0500
committerGitHub <noreply@github.com>2020-03-05 10:33:02 -0500
commitd7dc7d7a500541cf2ac0d3cfc28511b2fe7e88d4 (patch)
tree6cc1c5a5571a6a593e86220c8dd877bd5318b308 /lib/std
parent378bf1c3b71fdcc823fb9a5382400ec0a659fc00 (diff)
parente9c3b65bf4c7dc6c5fc07b9bcec195a5fe709db8 (diff)
downloadzig-d7dc7d7a500541cf2ac0d3cfc28511b2fe7e88d4.tar.gz
zig-d7dc7d7a500541cf2ac0d3cfc28511b2fe7e88d4.zip
Merge pull request #4633 from daurnimator/4632-i1
Fix formatting of i1 values
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/fmt.zig39
-rw-r--r--lib/std/math.zig49
2 files changed, 47 insertions, 41 deletions
diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig
index 3729b7dc87..240443bbde 100644
--- a/lib/std/fmt.zig
+++ b/lib/std/fmt.zig
@@ -943,19 +943,17 @@ fn formatIntSigned(
.precision = options.precision,
.fill = options.fill,
};
-
- const uint = std.meta.IntType(false, @TypeOf(value).bit_count);
+ const bit_count = @typeInfo(@TypeOf(value)).Int.bits;
+ const Uint = std.meta.IntType(false, bit_count);
if (value < 0) {
- const minus_sign: u8 = '-';
- try output(context, @as(*const [1]u8, &minus_sign)[0..]);
- const new_value = @intCast(uint, -(value + 1)) + 1;
+ try output(context, "-");
+ const new_value = math.absCast(value);
return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output);
} else if (options.width == null or options.width.? == 0) {
- return formatIntUnsigned(@intCast(uint, value), base, uppercase, options, context, Errors, output);
+ return formatIntUnsigned(@intCast(Uint, value), base, uppercase, options, context, Errors, output);
} else {
- const plus_sign: u8 = '+';
- try output(context, @as(*const [1]u8, &plus_sign)[0..]);
- const new_value = @intCast(uint, value);
+ try output(context, "+");
+ const new_value = @intCast(Uint, value);
return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output);
}
}
@@ -1165,19 +1163,22 @@ pub fn allocPrint0(allocator: *mem.Allocator, comptime fmt: []const u8, args: va
test "bufPrintInt" {
var buffer: [100]u8 = undefined;
const buf = buffer[0..];
- std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 2, false, FormatOptions{}), "-101111000110000101001110"));
- std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 10, false, FormatOptions{}), "-12345678"));
- std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 16, false, FormatOptions{}), "-bc614e"));
- std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 16, true, FormatOptions{}), "-BC614E"));
- std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 12345678), 10, true, FormatOptions{}), "12345678"));
+ std.testing.expectEqualSlices(u8, "-1", bufPrintIntToSlice(buf, @as(i1, -1), 10, false, FormatOptions{}));
+
+ std.testing.expectEqualSlices(u8, "-101111000110000101001110", bufPrintIntToSlice(buf, @as(i32, -12345678), 2, false, FormatOptions{}));
+ std.testing.expectEqualSlices(u8, "-12345678", bufPrintIntToSlice(buf, @as(i32, -12345678), 10, false, FormatOptions{}));
+ std.testing.expectEqualSlices(u8, "-bc614e", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, false, FormatOptions{}));
+ std.testing.expectEqualSlices(u8, "-BC614E", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, true, FormatOptions{}));
+
+ std.testing.expectEqualSlices(u8, "12345678", bufPrintIntToSlice(buf, @as(u32, 12345678), 10, true, FormatOptions{}));
- std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 666), 10, false, FormatOptions{ .width = 6 }), " 666"));
- std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 6 }), " 1234"));
- std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 1 }), "1234"));
+ std.testing.expectEqualSlices(u8, " 666", bufPrintIntToSlice(buf, @as(u32, 666), 10, false, FormatOptions{ .width = 6 }));
+ std.testing.expectEqualSlices(u8, " 1234", bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 6 }));
+ std.testing.expectEqualSlices(u8, "1234", bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 1 }));
- std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, 42), 10, false, FormatOptions{ .width = 3 }), "+42"));
- std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -42), 10, false, FormatOptions{ .width = 3 }), "-42"));
+ std.testing.expectEqualSlices(u8, "+42", bufPrintIntToSlice(buf, @as(i32, 42), 10, false, FormatOptions{ .width = 3 }));
+ std.testing.expectEqualSlices(u8, "-42", bufPrintIntToSlice(buf, @as(i32, -42), 10, false, FormatOptions{ .width = 3 }));
}
fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, options: FormatOptions) []u8 {
diff --git a/lib/std/math.zig b/lib/std/math.zig
index 9a1143a17d..ed13e3cd27 100644
--- a/lib/std/math.zig
+++ b/lib/std/math.zig
@@ -670,33 +670,38 @@ fn testRem() void {
/// Returns the absolute value of the integer parameter.
/// Result is an unsigned integer.
-pub fn absCast(x: var) t: {
- if (@TypeOf(x) == comptime_int) {
- break :t comptime_int;
- } else {
- break :t std.meta.IntType(false, @TypeOf(x).bit_count);
+pub fn absCast(x: var) switch(@typeInfo(@TypeOf(x))) {
+ .ComptimeInt => comptime_int,
+ .Int => |intInfo| std.meta.IntType(false, intInfo.bits),
+ else => @compileError("absCast only accepts integers"),
}
-} {
- if (@TypeOf(x) == comptime_int) {
- return if (x < 0) -x else x;
+{
+ switch(@typeInfo(@TypeOf(x))) {
+ .ComptimeInt => {
+ if (x < 0) {
+ return -x;
+ } else {
+ return x;
+ }
+ },
+ .Int => |intInfo| {
+ const Uint = std.meta.IntType(false, intInfo.bits);
+ if (x < 0) {
+ return ~@bitCast(Uint, x +% -1);
+ } else {
+ return @intCast(Uint, x);
+ }
+ },
+ else => unreachable,
}
- const uint = std.meta.IntType(false, @TypeOf(x).bit_count);
- if (x >= 0) return @intCast(uint, x);
-
- return @intCast(uint, -(x + 1)) + 1;
}
test "math.absCast" {
- testing.expect(absCast(@as(i32, -999)) == 999);
- testing.expect(@TypeOf(absCast(@as(i32, -999))) == u32);
-
- testing.expect(absCast(@as(i32, 999)) == 999);
- testing.expect(@TypeOf(absCast(@as(i32, 999))) == u32);
-
- testing.expect(absCast(@as(i32, minInt(i32))) == -minInt(i32));
- testing.expect(@TypeOf(absCast(@as(i32, minInt(i32)))) == u32);
-
- testing.expect(absCast(-999) == 999);
+ testing.expectEqual(@as(u1, 1), absCast(@as(i1, -1)));
+ testing.expectEqual(@as(u32, 999), absCast(@as(i32, -999)));
+ testing.expectEqual(@as(u32, 999), absCast(@as(i32, 999)));
+ testing.expectEqual(@as(u32, -minInt(i32)), absCast(@as(i32, minInt(i32))));
+ testing.expectEqual(999, absCast(-999));
}
/// Returns the negation of the integer parameter.