Repository navigation
Allow a negative offset in String#unpack and #unpack1 (Feature #21979) - #10035
Merged
Merged
Conversation
A negative offset counts back from the end of the string, and one past the start raises "offset outside of string", as in CRuby 4.1. Excludes test_unpack_offset and test_unpack1_offset. JRuby's copy of test_pack.rb predates this change and still expects "offset can't be negative"; CRuby master already tests the new behavior, so a sync of test_pack.rb will remove these excludes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 11, 2026
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 Feature #21979 for Ruby 4.1: a negative
offset:forString#unpackand#unpack1counts back from the end of the string. This was split out of #10023 during review. This 4.1 change isn't on the #9746 checklist."abc".unpack1("C", offset: -2)98ArgumentError: offset can't be negative98"abc".unpack("C*", offset: -2)[98, 99]ArgumentError: offset can't be negative[98, 99]"a".unpack1("C", offset: -2)ArgumentError: offset outside of stringArgumentError: offset can't be negativeArgumentError: offset outside of stringChanges
unpackInternalreplaces the "offset can't be negative" check with CRuby's:offset < 0 ? (offset += len) < 0 : offset > len.test/mri/excludes/TestPack.rb:test_unpack_offsetandtest_unpack1_offset.test_pack.rbpredates Feature #21979, and those tests still expect "offset can't be negative".test_pack.rb, in a separate PR after these pack PRs, will remove the excludes.Testing
spec/ruby/core/string/unpack_spec.rb,unpack1_spec.rb: 17 examples, 0 failures, 0 errors, interpreted and with forced JIT. Before, the 4 negative-offset 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 59; the 59 left areR/r,^andx!/@!, which Add LEB128 pack and unpack directives R and r (Feature #21785) #10036, Add the unpack directive ^ for the current offset (Feature #21796) #10037 and Add the alignment directives x! and @! to pack and unpack (Feature #22185) #10023 cover.test/mri/ruby/test_pack.rb: 39 tests, 0 failures, with the 2 new excludes.🤖 Generated with Claude Code