Actions
Feature #21785
openAdd signed and unsigned LEB128 support to pack / unpack
Feature #21785:
Add signed and unsigned LEB128 support to pack / unpack
Status:
Open
Assignee:
-
Target version:
-
Description
Hi,
I'd like to add signed and unsigned LEB128 support to the pack and unpack methods. LEB128 is a variable length encoding scheme for integers. You can read the wikipedia entry about it here: https://en.wikipedia.org/wiki/LEB128
LEB128 is used in DWARF, WebAssembly, MQTT, and Protobuf. I'm sure there are other formats, but these are the ones I'm familiar with.
I sent a pull request here: https://github.com/ruby/ruby/pull/15589
I'm proposing K for the unsigned version and k for the signed version. I just picked k because it was available, I'm open to other format strings.
Thanks for consideration!
Updated by tenderlovemaking (Aaron Patterson) 38 minutes ago
Sorry, I probably should have put an example in the original post. Here is a sample of the usage:
irb(main):003> [0xFFF].pack("K")
=> "\xFF\x1F"
irb(main):004> [0xFFF].pack("K").unpack1("K")
=> 4095
irb(main):005> [-123].pack("k")
=> "\x85\x7F"
irb(main):006> [-123].pack("k").unpack1("k")
=> -123
Actions