Swift implementation of the Google Protocol Buffers VarInt specification
It is based on the Go implementation of the Google Protocol Buffers varint specification.
Include the following dependency in your Package.swift file
let package = Package( ... dependencies: [ ... .package(url: "https://github.com/swift-libp2p/swift-varint.git", .upToNextMajor(from: "0.0.1")) ], ... )
import VarInt /// Create some arbitrary data let bytes = [UInt8](https://raw.github.com/swift-libp2p/swift-varint/main/"Hello World".data(using: .utf8)!) /// Prefix the bytes with their length so we can recover the data later let uVarIntLengthPrefixedBytes = putUVarInt(UInt64(bytes.count)) + bytes /// ... send the data across a network or something ... /// Read the length prefixed data to determine the length of the payload let lengthPrefix = uVarInt(uVarIntLengthPrefixedBytes) print(lengthPrefix.value) // 11 -> `Hello World` == 11 bytes print(lengthPrefix.bytesRead) // 1 -> The value `11` fits into 1 byte /// So dropping the first byte will result in our original data again... let recBytes = [UInt8](https://raw.github.com/swift-libp2p/swift-varint/main/uVarIntLengthPrefixedBytes.dropFirst(lengthPrefix.bytesRead)) /// The original bytes and the recovered bytes are equal print(bytes == recBytes) // True
/// Signed VarInts public func putVarInt(_ value: Int64) -> [UInt8] public func varInt(_ buffer: [UInt8]) -> DecodedVarInt // (value: Int64, bytesRead: Int) /// Unsigned VarInts public func putUVarInt(_ value: UInt64) -> [UInt8] public func uVarInt(_ buffer: [UInt8]) -> DecodedUVarInt // (value: UInt64, bytesRead: Int)
Contributions are welcomed! This code is very much a proof of concept. I can guarantee you there's a better / safer way to accomplish the same results. Any suggestions, improvements, or even just critques, are welcome!
Let's make this code better together! 🤝
MIT © 2022 Breth Inc.