mirror of
https://github.com/ziglang/zig.git
synced 2026-01-02 03:25:01 +00:00
Merge pull request #4633 from daurnimator/4632-i1
Fix formatting of i1 values
This commit is contained in:
commit
d7dc7d7a50
@ -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.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, "-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.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, "12345678", bufPrintIntToSlice(buf, @as(u32, 12345678), 10, true, FormatOptions{}));
|
||||
|
||||
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.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 {
|
||||
|
||||
@ -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.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user