Skip to content

Commit e98ba5f

Browse files
committed
add mem.readVarInt, fix InStream.readVarInt, fix stack traces
fixes a regression from b883bc8
1 parent 6395cf8 commit e98ba5f

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

std/debug/index.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ const Constant = struct {
12001200
fn asUnsignedLe(self: *const Constant) !u64 {
12011201
if (self.payload.len > @sizeOf(u64)) return error.InvalidDebugInfo;
12021202
if (self.signed) return error.InvalidDebugInfo;
1203-
return mem.readIntSliceLittle(u64, self.payload);
1203+
return mem.readVarInt(u64, self.payload, builtin.Endian.Little);
12041204
}
12051205
};
12061206

std/io.zig

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,12 @@ pub fn InStream(comptime ReadError: type) type {
183183
return mem.readIntSlice(T, bytes, endian);
184184
}
185185

186-
pub fn readVarInt(self: *Self, comptime T: type, endian: builtin.Endian, size: usize) !T {
187-
assert(size <= @sizeOf(T));
188-
var bytes: [@sizeOf(T)]u8 = undefined;
189-
try self.readNoEof(bytes[0..size]);
190-
return mem.readIntSlice(T, bytes, endian);
186+
pub fn readVarInt(self: *Self, comptime ReturnType: type, endian: builtin.Endian, size: usize) !ReturnType {
187+
assert(size <= @sizeOf(ReturnType));
188+
var bytes_buf: [@sizeOf(ReturnType)]u8 = undefined;
189+
const bytes = bytes_buf[0..size];
190+
try self.readNoEof(bytes);
191+
return mem.readVarInt(ReturnType, bytes, endian);
191192
}
192193

193194
pub fn skipBytes(self: *Self, num_bytes: usize) !void {

std/mem.zig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,27 @@ test "mem.indexOf" {
407407
assert(lastIndexOfScalar(u8, "boo", 'o').? == 2);
408408
}
409409

410+
/// Reads an integer from memory with size equal to bytes.len.
411+
/// T specifies the return type, which must be large enough to store
412+
/// the result.
413+
pub fn readVarInt(comptime ReturnType: type, bytes: []const u8, endian: builtin.Endian) ReturnType {
414+
var result: ReturnType = 0;
415+
switch (endian) {
416+
builtin.Endian.Big => {
417+
for (bytes) |b| {
418+
result = (result << 8) | b;
419+
}
420+
},
421+
builtin.Endian.Little => {
422+
const ShiftType = math.Log2Int(ReturnType);
423+
for (bytes) |b, index| {
424+
result = result | (ReturnType(b) << @intCast(ShiftType, index * 8));
425+
}
426+
},
427+
}
428+
return result;
429+
}
430+
410431
/// Reads an integer from memory with bit count specified by T.
411432
/// The bit count of T must be evenly divisible by 8.
412433
/// This function cannot fail and cannot cause undefined behavior.

0 commit comments

Comments
 (0)