Repository navigation
Conversation
…unt (Feature #22118) These read, change and count single bits of a string's bytes. Bit 0 is the least significant bit of the first byte; lsb_first: false numbers the bits in each byte from the most significant bit instead. bit_set, bit_clear, bit_flip and bit_count also take a region, as an offset and a length or as a Range (Feature #22279). Writes raise IndexError when the region runs past the end; bit_count clips it. The frozen check comes before the bounds check, as in CRuby (Bug #22316). The logic lives in the new org.jruby.util.StringBits. Bit positions from 2**63 to 2**64-1 are kept as Long.MAX_VALUE: they are past the end of every JRuby string, so the results are the same as CRuby's. Updates the bit_set, bit_clear, bit_flip and bit_count specs from CRuby's spec/ruby, which add the region and frozen cases. Co-Authored-By: Claude Opus 5.5 <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.
Adds the String bit methods from Ruby 4.1:
bit_get,bit_set?,bit_set,bit_clear,bit_flipandbit_count(Feature #22118). It includes the region arguments (Feature #22279) and the frozen check before the bounds check (Bug #22316). These are on the #9746 checklist. Thebitwise_*methods from the same feature come in a separate PR, based on this one.CRuby changes: ruby/ruby@2461caebba, ruby/ruby@03a426ec8d, ruby/ruby@d8d75cee41. Current code: string.c#L7424-L8209.
"\xAA".b.bit_get(1)1NoMethodError: undefined method 'bit_get' ...1"\xAA".b.bit_get(0, lsb_first: false)1NoMethodError1"\xAA".b.bit_set?(8)nilNoMethodErrornil(+"\x00\x00").b.bit_set(4, 8)"\xF0\x0F"NoMethodError"\xF0\x0F"(+"\xFF").b.bit_clear(0..3)"\xF0"NoMethodError"\xF0"(+"\x00").b.bit_flip(0, 9)IndexError: bit range out of rangeNoMethodError"\x00".b.freeze.bit_set(9, 0)FrozenError: can't modify frozen String: "\x00"NoMethodError"\xFF\x0F".b.bit_count(4, 100)8NoMethodError8"\x00".b.bit_get(-1)IndexError: bit index out of rangeNoMethodError"\x00".b.bit_count(0)ArgumentError: no bit length givenNoMethodErrorChanges
org.jruby.util.StringBits. It holds the logic, split like CRuby's helpers: offset and length conversion, Range conversion, the byte mask for a region, the region write, and the bit count. Each method has a@CRubyannotation, with anotewhere it ports only part of a function.2**63. CRuby keeps positions asuint64_t. Positions from2**63to2**64-1are stored asLong.MAX_VALUE. They are past the end of every JRuby string, so every comparison gives the same result. Above2**64-1, it raisesArgumentError, as CRuby does.RubyString. It adds thin@JRubyMethodoverloads withkeywords = true, so a positional Hash is not read aslsb_first:(s.bit_get(0, {lsb_first: false})raisesArgumentError, as in CRuby).frozen?after converting the arguments and before the bounds check. The string size is read after the frozen check, because a chilled-string warning handler can change the string. Writes clear the code range.VarHandleandLong.bitCount.Specs
I copied CRuby's newer
bit_set_spec.rb,bit_clear_spec.rb,bit_flip_spec.rbandbit_count_spec.rbfromspec/ruby/core/string(CRubyd8d75cee41). They add the region and frozen cases.bit_get_spec.rbandbit_set_p_spec.rbalready matched CRuby.Testing
bit_*spec files: 47 examples, 0 failures, 0 errors on JRuby (interpreter and forced JIT) and CRuby 4.1.0-dev. CRuby 4.0.7 runs 0 examples, because of the version guard. On the base branch, all 29 old examples errored.spec/ruby/core/string(mspec ci): no new failures. The 29bit_*errors are gone. The remaining errors are thebitwise_*specs, which the next PR fixes.test/mri/ruby/test_string.rb: 0 failures, as before. JRuby's copy has no bit tests yet.test_string.rb(master, copied outside the tree): alltest_bit_*tests pass, except the 9*_chilled_string_warningtests. Those fail because of an existing JRuby issue (see below). The same tests pass by hand without--debug.lsb_first:, frozen strings, encodings, shared strings, andto_intthat changes the receiver) gives the same output as CRuby 4.1.0-dev, except for the messages listed below.Not in this PR
s.bit_get(0, 1, lsb_first: true)says(given 3, expected 1..2), while CRuby says(given 2, expected 1).s.bit_getwith no arguments saysexpected 1..2, while CRuby saysexpected 1. Other methods that take keywords, likeString#unpack1, already behave the same way.unknown keyword: :foo). CRuby lists both (unknown keywords: :foo, :bar). The message comes from the sharedArgsUtil.extractKeywordArg.--debug, JRuby's chilled-string warning (RubyStringDebugChilledString) is sent withoutcategory: :deprecated, so aWarning.warnhandler that checks the category doesn't see it. JRuby'sassert_separatelyadds--debug, so CRuby's*_chilled_string_warningtests (including the existingsetbyteand<<ones) fail on JRuby.String#setbyteraises a JavaArrayIndexOutOfBoundsException, because it checks the index before the frozen check. CRuby fixed the same case in0b60b0e4c5.🤖 Generated with Claude Code