Skip to content

Allow a negative offset in String#unpack and #unpack1 (Feature #21979) - #10035

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

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

Conversation

@zev

@zev zev commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Implements Feature #21979 for Ruby 4.1: a negative offset: for String#unpack and #unpack1 counts 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.

Call CRuby 4.1 JRuby before JRuby after
"abc".unpack1("C", offset: -2) 98 ArgumentError: offset can't be negative 98
"abc".unpack("C*", offset: -2) [98, 99] ArgumentError: offset can't be negative [98, 99]
"a".unpack1("C", offset: -2) ArgumentError: offset outside of string ArgumentError: offset can't be negative ArgumentError: offset outside of string

Changes

  • unpackInternal replaces the "offset can't be negative" check with CRuby's: offset < 0 ? (offset += len) < 0 : offset > len.
  • Two new excludes in test/mri/excludes/TestPack.rb: test_unpack_offset and test_unpack1_offset.
    • JRuby's copy of test_pack.rb predates Feature #21979, and those tests still expect "offset can't be negative".
    • CRuby master already tests the new behavior. Syncing test_pack.rb, in a separate PR after these pack PRs, will remove the excludes.

Testing

🤖 Generated with Claude Code

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>
@headius
headius merged commit e2ac87b into jruby:ruby-4.1 Nov 11, 2026
7 checks passed
@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