|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module SuperDiff |
| 4 | + module BinaryString |
| 5 | + module OperationTreeBuilders |
| 6 | + class BinaryString < Basic::OperationTreeBuilders::MultilineString |
| 7 | + BYTES_PER_LINE = 16 |
| 8 | + private_constant :BYTES_PER_LINE |
| 9 | + |
| 10 | + def self.applies_to?(expected, actual) |
| 11 | + SuperDiff::BinaryString.applies_to?(expected, actual) |
| 12 | + end |
| 13 | + |
| 14 | + def initialize(*args) |
| 15 | + args.first[:expected] = binary_to_hex(args.first[:expected]) |
| 16 | + args.first[:actual] = binary_to_hex(args.first[:actual]) |
| 17 | + |
| 18 | + super |
| 19 | + end |
| 20 | + |
| 21 | + protected |
| 22 | + |
| 23 | + def build_operation_tree |
| 24 | + OperationTrees::BinaryString.new([]) |
| 25 | + end |
| 26 | + |
| 27 | + # Prevent creation of BinaryOperation objects which the flattener |
| 28 | + # cannot handle |
| 29 | + def should_compare?(_operation, _next_operation) |
| 30 | + false |
| 31 | + end |
| 32 | + |
| 33 | + private |
| 34 | + |
| 35 | + def split_into_lines(string) |
| 36 | + super.map { |line| line.delete_suffix("\n") }.reject(&:empty?) |
| 37 | + end |
| 38 | + |
| 39 | + def binary_to_hex(data) |
| 40 | + data |
| 41 | + .each_byte |
| 42 | + .each_slice(BYTES_PER_LINE) |
| 43 | + .with_index |
| 44 | + .map { |bytes, index| format_hex_line(index * BYTES_PER_LINE, bytes) } |
| 45 | + .join("\n") |
| 46 | + end |
| 47 | + |
| 48 | + def format_hex_line(offset, bytes) |
| 49 | + hex_pairs = bytes |
| 50 | + .map { |b| format('%02x', b) } |
| 51 | + .each_slice(2) |
| 52 | + .map(&:join) |
| 53 | + .join(' ') |
| 54 | + |
| 55 | + ascii = bytes.map { |b| printable_char(b) }.join |
| 56 | + |
| 57 | + format('%<offset>08x: %<hex>-39s %<ascii>s', offset:, hex: hex_pairs, ascii:) |
| 58 | + end |
| 59 | + |
| 60 | + def printable_char(byte) |
| 61 | + byte >= 32 && byte < 127 ? byte.chr : '.' |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | +end |
0 commit comments