refactor: optimize decodeUIntLength to iterate instead of recursing
This commit is contained in:
parent
c6b7110db0
commit
cc32dd2075
1 changed files with 6 additions and 15 deletions
21
src/cbor.zig
21
src/cbor.zig
|
|
@ -339,21 +339,12 @@ pub fn decodeType(iter: *[]const u8) error{TooShort}!CborType {
|
||||||
return .{ .type = type_, .minor = bits.minor, .major = bits.major };
|
return .{ .type = type_, .minor = bits.minor, .major = bits.major };
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decodeUIntLengthRecurse(iter: *[]const u8, length: usize, acc: u64) error{TooShort}!u64 {
|
fn decodeUIntLength(iter: *[]const u8, length: usize) error{TooShort}!u64 {
|
||||||
if (iter.len < 1)
|
if (iter.len < length) return error.TooShort;
|
||||||
return error.TooShort;
|
var acc: u64 = 0;
|
||||||
const v: u8 = iter.*[0];
|
for (iter.*[0..length]) |byte| acc = (acc << 8) | byte;
|
||||||
iter.* = iter.*[1..];
|
iter.* = iter.*[length..];
|
||||||
var i = acc | v;
|
return acc;
|
||||||
if (length == 1)
|
|
||||||
return i;
|
|
||||||
i <<= 8;
|
|
||||||
// return @call(.always_tail, decodeUIntLengthRecurse, .{ iter, length - 1, i }); FIXME: @call(.always_tail) seems broken as of 0.11.0-dev.2964+e9cbdb2cf
|
|
||||||
return decodeUIntLengthRecurse(iter, length - 1, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn decodeUIntLength(iter: *[]const u8, length: usize) !u64 {
|
|
||||||
return decodeUIntLengthRecurse(iter, length, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decodePInt(iter: *[]const u8, minor: u5) error{ TooShort, InvalidPIntType }!u64 {
|
fn decodePInt(iter: *[]const u8, minor: u5) error{ TooShort, InvalidPIntType }!u64 {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue