authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-08-09 20:37:05 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-08-09 20:37:05 -07:00
logb40619a111982e77cc0db8e54979b577ebd737dc
tree6680b763aabfb7f53946edc1c4ad4b1748fe6bd7
parentf7dff19f70cb267337139a74978a255b8bdee694

safeAdd: handle when b is minInt(B)


1 files changed, 7 insertions(+), 1 deletions(-)

src/lib.zig+7-1
......@@ -451,7 +451,7 @@ pub fn safeAdd(a: anytype, b: anytype) @TypeOf(a) {
451451 if (b >= 0) {
452452 return a + @as(@TypeOf(a), @intCast(b));
453453 }
454 return a - @as(@TypeOf(a), @intCast(-b));
454 return a - @as(@TypeOf(a), @intCast(-@as(OneBiggerInt(@TypeOf(b)), b)));
455455}
456456
457457pub fn readBytesAlloc(reader: anytype, alloc: std.mem.Allocator, len: usize) ![]u8 {
......@@ -651,3 +651,9 @@ pub fn joinPartial(comptime P: type, a: P, b: P) P {
651651 }
652652 return temp;
653653}
654
655pub fn OneBiggerInt(comptime T: type) type {
656 var info = @typeInfo(T);
657 info.Int.bits += 1;
658 return @Type(info);
659}