apple / apple/swift-binary-parsing

Feature Request: LEB128 integers

Open
#26 8 comments 2 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
387
Forks
24
Avg merge
1h 3m
Merged PRs (30d)
2

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
}
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the existing UInt64 parsing APIs in the swift-binary-parsing repository and the proposed UInt64 init(parsing:) entry point shown in the issue. Check how parsing errors and integer parsing tests are organized; done means LEB128 values from formats such as Parquet and Protobuf can be parsed, with oversized values rejected as described.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
data
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.