Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/opal_patches.rb

Overview


Phase 7: Array#pack(‘H*’) / String#unpack1(‘H*’) for hex<->bin. Opal’s pack.rb / unpack.rb don’t register the ‘H’ directive (“hex string, high nibble first”). Crypto code (Digest, OpenSSL, jwt) heavily uses these to convert between binary and hex, so we add a minimal handler that intercepts the “H*” / “H<n>” format and falls back to the original implementation for everything else.


Instance Method Summary collapse

Instance Method Details

#pack(format) ⇒ Object

‘H*` consumes the entire hex string. `H<n>` consumes exactly `n` nibbles (= n/2 bytes, rounded down). Matches CRuby semantics so `[hex].pack(’H4’)‘ yields the first 2 bytes — Copilot caught this divergence in the initial Phase 7 PR.



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/opal_patches.rb', line 514

def pack(format)
  fmt = format.to_s
  return pack_without_homura_hex(format) unless fmt == 'H*' || fmt =~ /\AH(\d+)\z/

  hex = self.first.to_s
  nibble_count = if fmt == 'H*'
                   hex.length
                 else
                   [fmt[1..-1].to_i, hex.length].min
                 end
  nibble_count -= 1 if nibble_count.odd?  # round down to whole bytes
  out = ''
  i = 0
  while i < nibble_count
    out = out + hex[i, 2].to_i(16).chr
    i += 2
  end
  out
end

#pack_without_homura_hexObject



508
# File 'lib/opal_patches.rb', line 508

alias_method :pack_without_homura_hex, :pack