freitagbr / crystal-uvarint Goto Github PK
View Code? Open in Web Editor NEWUnsigned Varint implementation in Crystal
License: MIT License
Unsigned Varint implementation in Crystal
License: MIT License
It would be really great to have a way to build a varint from an array of bytes where we may not know how long the Varint itself is. Imagine you have:
[1000 0001 , 1000 0010 , 0000 0001 , 1000 0001 , 0000 0001, 1000 0010]
It would be nice to have a constructor for Varints like Go's implementation:
value1, numBytesRead = Varint(bytes)
puts value1 # => 16641 (if my math is right)
puts numBytesRead # => 3
# Now we can get the second Varint from the array!
newBytes = getArrayStartingAt(bytes, numBytesRead) # => [1000 0001, 0000 0001, 1000 0010]
value2, numBytesRead = Varint(newBytes)
puts value2 # => 129 (if my math is right)
puts numBytesRead # => 2
lastByteArray = getArrayStartingAt(newBytes, numBytesRead) # => [1000 0010]
value3, numBytesRead = Varint(lastByteArray) # Throws an exception! We can't parse this
As with the final example, if we can't parse a VarInt from the byte array, then we should just throw an exception.
This could either be implemented as a class constructor or a module-level method that returns a Varint.
For many use cases, a UVarInt
will need to be created from an existing hex representation, usually as part of a string or from IO. In these cases, the representation might be included as part of a larger string, like in a multihash, and it may not be known how many bytes represent the varint. A constructor should be created to account for these use cases.
In order to avoid namespace conflicts, the struct UVarInt
definition should be placed inside a module
.
The README needs to be updated with the new constructor method.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.