Repository navigation
Add the unpack directive ^ for the current offset (Feature #21796) - #10037
Merged
Merged
Conversation
^ 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
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
^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."\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_offsetreturns the position counted from the start of the string, even whenoffset:is given.^3still unpacks one value.@CRubyannotation names thepack_unpack_internalcase 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.^withA,Z,X,R,U,mandH, at positive and negative offsets. All are identical when run with all of the split pack PRs applied, because some cases needRand negative offsets.test_pack.rbhas no^tests yet; they arrive with the sync. The new^assertion intest_pack_unpack_atmarkwon'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