Repository navigation
Add the 'w' (BER-compressed integer) pack/unpack directive - #448
Merged
Merged
Conversation
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>
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.
Array#pack("w")andString#unpack("w")raisedunknown directive 'w'. Theynow implement BER compression:
pack("w")writes an integer as base-128 groups, most significant first, withthe 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 valueraises
ArgumentError, and the result is BINARY.unpack("w")reads base-128 groups until a byte with the high bit clear, oneinteger per count (all of them for
*), returning Integer or Bignum values.The arbitrary-precision arithmetic runs on
math/big, so a round trip is exactfor 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_spec6 → 18 andcore/string/unpack/w_spec4 → 9.🤖 Generated with Claude Code