authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-08-09 20:37:13 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-08-09 20:37:13 -07:00
logd77ee373c1f1b4156c60dd1fe486d779c28263c9
tree786b705452a4b24d675d4ff79267e57bf3350800
parentb40619a111982e77cc0db8e54979b577ebd737dc

add safeAddWrap


1 files changed, 8 insertions(+), 0 deletions(-)

src/lib.zig+8
......@@ -454,6 +454,14 @@ pub fn safeAdd(a: anytype, b: anytype) @TypeOf(a) {
454454 return a - @as(@TypeOf(a), @intCast(-@as(OneBiggerInt(@TypeOf(b)), b)));
455455}
456456
457/// Allows u32 + i16 to work
458pub fn safeAddWrap(a: anytype, b: anytype) @TypeOf(a) {
459 if (b >= 0) {
460 return a +% @as(@TypeOf(a), @intCast(b));
461 }
462 return a -% @as(@TypeOf(a), @intCast(-@as(OneBiggerInt(@TypeOf(b)), b)));
463}
464
457465pub fn readBytesAlloc(reader: anytype, alloc: std.mem.Allocator, len: usize) ![]u8 {
458466 var list = std.ArrayListUnmanaged(u8){};
459467 try list.ensureTotalCapacityPrecise(alloc, len);