- 
                Notifications
    You must be signed in to change notification settings 
- Fork 12
Open
Description
It would be great if there was a way to parse LEB128 integers. These are very common in formats like Parquet (which I'm building a parser for), and Protobuf.
extension UInt64 {
    
    public init(parsing input: inout ParserSpan) throws {
        
        var value: UInt64 = 0
        var shift: UInt64 = 0
        
        while true {
            guard shift <= 63 else { // 63 is the max shift for a UInt64
                // The varint is too long to fit in a UInt64.
                throw BinaryParsing.error("Varint too long to fit in UInt64")
            }
            
            let byte = try UInt8(parsingLittleEndian: &input, byteCount: 1)
            
            value |= UInt64(byte & 0x7f) << shift
            
            if (byte & 0x80) == 0 {
                self = value
                return
            }
            
            shift += 7
        }
    }
}
stephentyrone and natecook1000
Metadata
Metadata
Assignees
Labels
No labels