MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / read_varint

Function read_varint

nodedb-codec/src/delta.rs:60–83  ·  view source on GitHub ↗

Read a varint (LEB128-encoded u64) from a byte slice. Returns `(value, bytes_consumed)`.

(data: &[u8])

Source from the content-addressed store, hash-verified

58///
59/// Returns `(value, bytes_consumed)`.
60fn read_varint(data: &[u8]) -> Result<(u64, usize), CodecError> {
61 let mut value: u64 = 0;
62 let mut shift: u32 = 0;
63
64 for (i, &byte) in data.iter().enumerate() {
65 if shift >= 70 {
66 return Err(CodecError::Corrupt {
67 detail: "varint too long (>10 bytes)".into(),
68 });
69 }
70
71 value |= ((byte & 0x7F) as u64) << shift;
72 shift += 7;
73
74 if byte & 0x80 == 0 {
75 return Ok((value, i + 1));
76 }
77 }
78
79 Err(CodecError::Truncated {
80 expected: data.len() + 1,
81 actual: data.len(),
82 })
83}
84
85// ---------------------------------------------------------------------------
86// Public encode / decode API

Callers 2

decodeFunction · 0.70
varint_roundtripFunction · 0.70

Calls 2

iterMethod · 0.45
lenMethod · 0.45

Tested by 1

varint_roundtripFunction · 0.56