Class: Baikal::Hexdump::Field::Data

Inherits:
Baikal::Hexdump::Field show all
Defined in:
lib/baikal/hexdump.rb

Overview

The Data field outputs data on a hexdump row, formatted using a supplied Proc instance. grouping_rules are specified as pairs of group size and separator. All group sizes are processed in parallel. In group boundaries where multiple grouping rules would match, only the leftmost one is used.

Constant Summary collapse

LOWERCASE_HEX =

Formats the byte as a zero-padded two-digit lowercase hexadecimal number.

:stopdoc: Unfortunately, RDoc gets confused by thunk constants.

proc do |value|
  if value then
    raise 'Type mismatch' unless value.is_a? Integer
    sprintf("%02x", value)
  else
    "  "
  end
end
UPPERCASE_HEX =

Formats the byte as a zero-padded two-digit uppercase hexadecimal number.

proc do |value|
  if value then
    raise 'Type mismatch' unless value.is_a? Integer
    sprintf("%02X", value)
  else
    "  "
  end
end
OCTAL =

Formats the byte as a zero-padded three-digit octal number.

proc do |value|
  if value then
    raise 'Type mismatch' unless value.is_a? Integer
    sprintf("%03o", value)
  else
    "   "
  end
end
DECIMAL =

Formats the byte as a space-padded three-digit decimal number.

proc do |value|
  if value then
    raise 'Type mismatch' unless value.is_a? Integer
    sprintf("%3i", value)
  else
    "   "
  end
end
ASCII =

Formats the byte as an ASCII character. Nonprintable characters are replaced by a period.

proc do |value|
  if value then
    raise 'Type mismatch' unless value.is_a? Integer
    value >= 0x20 && value <= 0x7E ? value.chr : "."
  else
    " "
  end
end
LATIN1 =

Decodes the byte as a Latin-1 character and formats it in UTF-8. Nonprintable characters are replaced by a period, as in ASCII.

proc do |value|
  if value then
    raise 'Type mismatch' unless value.is_a? Integer
    if (0x20 .. 0x7E).include? value or (0xA0 .. 0xFF).include? value then
      [value].pack('U')
    else
      "."
    end
  else
    " "
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(formatter, *grouping_rules) ⇒ Data



104
105
106
107
108
109
# File 'lib/baikal/hexdump.rb', line 104

def initialize formatter, *grouping_rules
  super()
  @formatter = formatter
  @grouping_rules = grouping_rules
  return
end

Instance Method Details

#format(row) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/baikal/hexdump.rb', line 111

def format row
  output = ""
  (0 ... row.expected_size).each do |column|
    if column != 0 then
      rule = @grouping_rules.detect{|divisor, separator| column % divisor == 0}
      output << rule[1] if rule
    end
    output << @formatter.call(row.data[column])
  end
  return output
end