Module: HexFormat

Defined in:
lib/innodb/util/hex_format.rb

Constant Summary collapse

LINE_SIZE =
16
GROUP_SIZE =
8
GROUP_FORMAT_LENGTH =
((LINE_SIZE.to_f / GROUP_SIZE).ceil * (GROUP_SIZE * 3))

Class Method Summary collapse

Class Method Details

.format_group(data) ⇒ Object



8
9
10
# File 'lib/innodb/util/hex_format.rb', line 8

def self.format_group(data)
  data.map { |n| "%02x" % n.ord }.join(" ")
end

.format_groups(data, size) ⇒ Object



12
13
14
# File 'lib/innodb/util/hex_format.rb', line 12

def self.format_groups(data, size)
  data.each_slice(size).map { |g| format_group(g) }.join("  ")
end

.format_hex(data) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/innodb/util/hex_format.rb', line 20

def self.format_hex(data)
  data.chars.each_slice(LINE_SIZE).with_index do |bytes, i|
    yield format("%08i  %-#{GROUP_FORMAT_LENGTH}s  |%-#{LINE_SIZE}s|",
                 (i * LINE_SIZE), format_groups(bytes, GROUP_SIZE), format_printable(bytes))
  end

  nil
end

.format_printable(data) ⇒ Object



16
17
18
# File 'lib/innodb/util/hex_format.rb', line 16

def self.format_printable(data)
  data.join.gsub(/[^[:print:]]/, ".")
end

.puts(data, io: $stdout) ⇒ Object



29
30
31
# File 'lib/innodb/util/hex_format.rb', line 29

def self.puts(data, io: $stdout)
  format_hex(data) { |line| io.puts(line) }
end