Repository navigation
Add LEB128 pack and unpack directives R and r (Feature #21785) - #10036
Merged
Merged
Conversation
This was referenced Nov 11, 2026
'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
approved these changes
Nov 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements the
R(ULEB128) andr(SLEB128) pack/unpack directives from Feature #21785, an item on the #9746 checklist. This was split out of #10023 during review.[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 ULEB128ArgumentError: unknown pack directive 'R' in 'R'"\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 withto_int.longfast path, withBigIntegerfor Bignums andLong.MIN_VALUE.radds a sign byte whenever the absolute value fills its last 7-bit group. So-64packs 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 asnil. With*, an incomplete value is skipped.decodeLEB128: rebuilds the value from its 7-bit groups and sign-extends it forr.@CRubynotes:pack_LEB128andunpack_LEB128are tagged with thepack_packandpack_unpack_internalcases they port.decodeLEB128is taggedrb_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 44R/rexamples 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 noR/rtests yet; they arrive with thetest_pack.rbsync.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 theunpack1fix applied.Not in this PR
"\xFF".unpack1("R*")unpacks no value. That goes through theunpack1path fixed forjruby-10.0in Return nil from String#unpack1 when nothing is unpacked #10034. Until that fix is merged forward toruby-4.1, it raises aNullPointerExceptionin compiled code. No spec covers this case.test_pack.rb,test_pack_modify_buffer_r_Ris one of the five excludes discussed in pack/unpack when the string, format or buffer changes during the call #10022.🤖 Generated with Claude Code