Skip to content

Add the 'w' (BER-compressed integer) pack/unpack directive - #448

Merged
tannevaled merged 1 commit into
mainfrom
feat/cluster20
Aug 25, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/cluster20

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Array#pack("w") and String#unpack("w") raised unknown directive 'w'. They
now implement BER compression:

  • pack("w") writes an integer as base-128 groups, most significant first, with
    the high bit set on every byte but the last (zero is a single NUL byte). The
    value may be any size (a Bignum encodes to as many bytes as needed); a Float is
    truncated toward zero and an object is coerced with #to_int. A negative value
    raises ArgumentError, and the result is BINARY.
  • unpack("w") reads base-128 groups until a byte with the high bit clear, one
    integer per count (all of them for *), returning Integer or Bignum values.

The arbitrary-precision arithmetic runs on math/big, so a round trip is exact
for values far beyond a machine word.

Verified against ruby 4.0.6. Full package coverage stays at 100.0%.
Zero-regression rubyspec sweep; core/array/pack/w_spec 6 → 18 and
core/string/unpack/w_spec 4 → 9.

🤖 Generated with Claude Code

Array#pack("w") and String#unpack("w") raised "unknown directive 'w'". They now
implement BER compression:

  - pack("w") writes an integer as base-128 groups, most significant first, with
    the high bit set on every byte but the last (zero is a single NUL byte). The
    value may be any size (a Bignum encodes to as many bytes as needed); a Float
    is truncated toward zero and an object is coerced with #to_int. A negative
    value raises ArgumentError, and the result is BINARY.
  - unpack("w") reads base-128 groups until a byte with the high bit clear, one
    integer per count (all of them for '*'), returning Integer or Bignum values.

The arbitrary-precision arithmetic runs on math/big, so a round trip is exact
for values far beyond a machine word.

Verified against ruby 4.0.6. Full package coverage stays at 100.0%. Zero-
regression rubyspec sweep; core/array/pack/w_spec 6 -> 18 and
core/string/unpack/w_spec 4 -> 9.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit ed4d9c9 into main Aug 25, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/cluster20 branch August 25, 2026 10:12
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.

1 participant