Sha256: 5d94083e0457363382047312bde3b96e36ae57b1cc11dc6503f507ccef9abcc0

Contents?: true

Size: 1.17 KB

Versions: 150

Compression:

Stored size: 1.17 KB

Contents

# encoding: binary

# This was written by Arai-san and published at
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/31987


module HexDump
  # str must be in BINARY encoding in 1.9
  def encode(str)
    offset = 0
    result = []
    while raw = str.slice(offset, 16) and raw.length > 0
      # data field
      data = ''
      for v in raw.unpack('N* a*')
	if v.kind_of? Integer
	  data << sprintf("%08x ", v)
	else
	  v.each_byte {|c| data << sprintf("%02x", c) }
	end
      end
      # text field
      text = raw.tr("\000-\037\177-\377", ".")
      result << sprintf("%08x  %-36s  %s", offset, data, text)
      offset += 16
      # omit duplicate line
      if /^(#{regex_quote_n(raw)})+/n =~ str[offset .. -1]
	result << sprintf("%08x  ...", offset)
	offset += $&.length
	# should print at the end
	if offset == str.length
	  result << sprintf("%08x  %-36s  %s", offset-16, data, text)
	end
      end
    end
    result
  end
  module_function :encode

  if RUBY_VERSION >= "1.9"
    # raw must be in BINARY encoding in 1.9
    def self.regex_quote_n(raw)
      Regexp.quote(raw)
    end
  else
    def self.regex_quote_n(raw)
      Regexp.quote(raw, 'n')
    end
  end
end

Version data entries

150 entries across 142 versions & 13 rubygems

Version Path
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/httpclient-2.8.3/lib/hexdump.rb
httpclient-fixcerts-2.8.5 lib/hexdump.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/httpclient-2.8.3/lib/hexdump.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/httpclient-2.8.3/lib/hexdump.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/httpclient-2.8.3/lib/hexdump.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/lib/hexdump.rb
video_chat_get-0.1.9 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/lib/hexdump.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/lib/hexdump.rb
video_chat_get-0.1.6 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/lib/hexdump.rb
vagrant-unbundled-2.2.10.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/lib/hexdump.rb
video_chat_get-0.1.5 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/lib/hexdump.rb
video_chat_get-0.1.4 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/lib/hexdump.rb
video_chat_get-0.1.3 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/lib/hexdump.rb
video_chat_get-0.1.2 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/lib/hexdump.rb
video_chat_get-0.1.1 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/lib/hexdump.rb
video_chat_get-0.1.0 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/lib/hexdump.rb
vagrant-unbundled-2.2.9.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/lib/hexdump.rb
vagrant-unbundled-2.2.8.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/lib/hexdump.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/httpclient-2.8.3/lib/hexdump.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.6.0/gems/httpclient-2.8.3/lib/hexdump.rb