Skip to content

Add the unpack directive ^ for the current offset (Feature #21796) - #10037

Merged
headius merged 2 commits into
jruby:ruby-4.1from
zev:unpack-caret-offset
Nov 11, 2026
Merged

headius merged 2 commits into
jruby:ruby-4.1from
zev:unpack-caret-offset

Conversation

@zev

@zev zev commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Implements the ^ unpack directive from Feature #21796, an item on the #9746 checklist. It unpacks the current byte offset. This was split out of #10023 during review.

Call CRuby 4.1 JRuby before JRuby after
"\x05hello".unpack("Ca5^") [5, "hello", 6] ArgumentError: unknown unpack directive '^' in 'Ca5^' [5, "hello", 6]
"abcdef".unpack("C^", offset: 2) [99, 3] ArgumentError: unknown unpack directive '^' in 'C^' [99, 3]
"abc".unpack("C^3") [97, 1] ArgumentError: unknown unpack directive '^' in 'C^3' [97, 1]

Changes

  • unpack_offset returns the position counted from the start of the string, even when offset: is given.
  • Like MRI, it ignores a count, so ^3 still unpacks one value.
  • Its @CRuby annotation names the pack_unpack_internal case it ports.

Testing

  • spec/ruby/core/string/unpack/carret_spec.rb: 12 examples, 0 failures, 0 errors, interpreted and with forced JIT. Before, all 9 of its ^ 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 54.
  • Comparison with CRuby 4.1.0-dev: 46 cases mixing ^ with A, Z, X, R, U, m and H, at positive and negative offsets. All are identical when run with all of the split pack PRs applied, because some cases need R and negative offsets.
  • JRuby's test_pack.rb has no ^ tests yet; they arrive with the sync. The new ^ assertion in test_pack_unpack_atmark won't run, since that test is already excluded for another reason. Run by hand it passes: "\x01\x00\x00\x02".unpack("x^@3^x^") gives [1, 3, 4].

🤖 Generated with Claude Code

^ unpacks the current byte offset, counted from the start of the string
even when an offset: argument is given. Like CRuby it ignores a count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@headius
headius merged commit 4205baa into jruby:ruby-4.1 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