Coder Social home page Coder Social logo

crystal-uvarint's Issues

Add way to construct incrementally, from IO

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.

Feature request: some API for parsing from a string/buffer

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.

Update README.md

The README needs to be updated with the new constructor method.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.