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.



573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/opal_patches.rb', line 573

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

  hex = self.first.to_s
  nibble_count = if fmt == "H*"
    hex.length
  else
    [fmt[1..-1].to_i, hex.length].min
  end
  # round down to whole bytes
  nibble_count -= 1 if nibble_count.odd?
  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



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

alias_method :pack_without_homura_hex, :pack