Skip to content

Add LEB128 pack and unpack directives R and r (Feature #21785) - #10036

Merged
headius merged 2 commits into
jruby:ruby-4.1from
zev:pack-leb128
Nov 11, 2026
Merged

headius merged 2 commits into
jruby:ruby-4.1from
zev:pack-leb128

Conversation

@zev

@zev zev commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Implements the R (ULEB128) and r (SLEB128) pack/unpack directives from Feature #21785, an item on the #9746 checklist. This was split out of #10023 during review.

Call CRuby 4.1 JRuby before JRuby after
[300].pack("R") "\xAC\x02" ArgumentError: unknown pack directive 'R' in 'R' "\xAC\x02"
[-64].pack("r") "\xC0\x7F" ArgumentError: unknown pack directive 'r' in 'r' "\xC0\x7F"
[-1].pack("R") ArgumentError: can't encode negative numbers in ULEB128 ArgumentError: unknown pack directive 'R' in 'R' same as CRuby
"\xAC\x02".unpack("R") [300] ArgumentError: unknown unpack directive 'R' in 'R' [300]
"\xFF".unpack("R2") [nil, nil] ArgumentError: unknown unpack directive 'R' in 'R2' [nil, nil]
"\xFF".unpack("R*") [] ArgumentError: unknown unpack directive 'R' in 'R*' []

Changes

All in core/src/main/java/org/jruby/util/Pack.java:

  • pack_LEB128: converts each value with to_int.
    • It uses a long fast path, with BigInteger for Bignums and Long.MIN_VALUE.
    • The byte count follows MRI rather than the shortest encoding: r adds a sign byte whenever the absolute value fills its last 7-bit group. So -64 packs as "\xC0\x7F", not "\x40". Both decode to -64, but the bytes match CRuby.
  • unpack_LEB128: with a count, a value cut off by the end of the string, and every value after it, unpack as nil. With *, an incomplete value is skipped.
  • decodeLEB128: rebuilds the value from its 7-bit groups and sign-extends it for r.
  • @CRuby notes:
    • pack_LEB128 and unpack_LEB128 are tagged with the pack_pack and pack_unpack_internal cases they port.
    • decodeLEB128 is tagged rb_integer_unpack, noting it covers only the LEB128 form.

Testing

  • spec/ruby/core/array/pack/r_spec.rb, spec/ruby/core/string/unpack/r_spec.rb: 59 examples, 0 failures, 0 errors, interpreted and with forced JIT. Before, all 44 R/r examples errored.
  • spec/ruby/core/array/pack, spec/ruby/core/string/unpack, unpack_spec.rb, unpack1_spec.rb: 0 failures. The errors drop from 63 to 19; the 19 left are the other split PRs' features.
  • test/mri/ruby/test_pack.rb: 41 tests, 0 failures. JRuby's copy has no R/r tests yet; they arrive with the test_pack.rb sync.
  • Byte-for-byte comparison with CRuby 4.1.0-dev (6203c21e58): 7,286 cases, all identical. They cover pack output, unpack, unpack1, block form, and error class and message, for 2^n ± 1 up to 2^300, 3,000 random values, and cut-off inputs with counts and *. I ran it with all of the split pack PRs and the unpack1 fix applied.

Not in this PR

🤖 Generated with Claude Code

'R' packs and unpacks unsigned LEB128 and 'r' signed LEB128. Like
CRuby, 'r' adds a sign byte whenever the absolute value fills its last
7-bit group, so -64 packs as "\xC0\x7F" rather than the shortest "\x40".
With a count, a value cut off by the end of the string and every value
after it unpack as nil; with '*' an incomplete value is skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@headius
headius merged commit c6a585e into jruby:ruby-4.1 Nov 11, 2026
@headius headius added this to the JRuby 10.2.0.0 milestone Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants