Skip to content

Commit

Permalink
Made varint size public
Browse files Browse the repository at this point in the history
  • Loading branch information
dadepo committed Nov 17, 2024
1 parent e0902d9 commit cbf56ef
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/lib.zig
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
const std = @import("std");

fn varintSize(value: anytype) u32 {
var count: u32 = 0;
var v = value;

while (v != 0) : (v >>= 7) {
count += 1;
}

return if (count == 0) 1 else count;
}

fn encodedLen(comptime number: anytype) u8 {
switch (@typeInfo(@TypeOf(number))) {
.Int, .ComptimeInt => {},
Expand All @@ -30,6 +19,17 @@ fn encodedHexLen(comptime rawHexString: []const u8) u8 {
return varintSize(number);
}

pub fn varintSize(value: anytype) u32 {
var count: u32 = 0;
var v = value;

while (v != 0) : (v >>= 7) {
count += 1;
}

return if (count == 0) 1 else count;
}

pub fn encode(number: anytype) [encodedLen(number)]u8 {
var out: [encodedLen(number)]u8 = [_]u8{0} ** encodedLen(number);
const n: usize = number;
Expand Down

0 comments on commit cbf56ef

Please sign in to comment.