Skip to content

Commit

Permalink
Refactor hex output of BLOB data
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremycole committed May 19, 2023
1 parent ffd8033 commit 0a878dd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/innodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def self.debug=(value)
require "digest/crc32c"
require "innodb/util/buffer_cursor"
require "innodb/util/read_bits_at_offset"
require "innodb/util/hex_format"

require "innodb/version"
require "innodb/stats"
Expand Down
14 changes: 1 addition & 13 deletions lib/innodb/page/blob.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@ def blob_data
end
end

def dump_hex(string)
slice_size = 16
string.chars.each_slice(slice_size).each_with_index do |slice_bytes, slice_count|
puts "%08i %-23s %-23s |%-16s|" % [
(slice_count * slice_size),
slice_bytes[0..8].map { |n| "%02x" % n.ord }.join(" "),
slice_bytes[8..16].map { |n| "%02x" % n.ord }.join(" "),
slice_bytes.join,
]
end
end

def each_region(&block)
return enum_for(:each_region) unless block_given?

Expand Down Expand Up @@ -75,7 +63,7 @@ def dump
puts

puts "blob data:"
dump_hex(blob_data)
HexFormat.puts(blob_data)
puts

puts
Expand Down
32 changes: 32 additions & 0 deletions lib/innodb/util/hex_format.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

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

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

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

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

def self.format_hex(data)
data.chars.each_slice(LINE_SIZE).each_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

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

0 comments on commit 0a878dd

Please sign in to comment.