zhexdump 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c0405a9a00093fb08a5ff3294e1636d3350e49c83f478c2d4e1fa1575b72bb83
4
- data.tar.gz: 0cf51636f3344e94c0e9a5647fbe1cb9f54b1d134e0d22cd7c595d2f41a03552
3
+ metadata.gz: 2a3f2ca2cbd4b74eb7d4f2e369155faed551cd1aae88c68b69b83db782b3bbad
4
+ data.tar.gz: e1755f4ee72a7144923ccb822d53214455ca0971e82a7cc94c104cf529865675
5
5
  SHA512:
6
- metadata.gz: e117316024d5ecc48f7eefa79cff23603ef728959791c522a93aa50dafc50f05f202aeb17ee5994d2ebe9bd22c1d881840cfad3c6a392076c7aabd6da4f06b4d
7
- data.tar.gz: c3d12f02963fa811e42d44f93a328eebb2a81964350b2f3edebd7666f4ef2122c1fd01488e05be2d690784dae86fbdb0c2d109f76d9d9d929360a4ab3d911171
6
+ metadata.gz: 130772c539567807650e00be726963b6af7c7f9773b537e01410f7229d7baf1f8187c4edb05e8536a15073a3d37193720b312e4f6047d00b1ac47369c95356a9
7
+ data.tar.gz: c3b20059c90fe00c3e45b4eaff82f196dfd3b68515047bc0d7f1d3360678e7891684cdc7d3ffa32e149b316fdf132c686ff4f2da5eb994002545d5ea363e5403
@@ -1,3 +1,3 @@
1
1
  module ZHexdump
2
- VERSION = "0.1.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/zhexdump.rb CHANGED
@@ -19,7 +19,15 @@ module ZHexdump
19
19
  r
20
20
  end
21
21
 
22
+ @defaults = {}
23
+
22
24
  class << self
25
+ attr_accessor :defaults
26
+
27
+ def _get_param(h, key, default)
28
+ h.fetch(key, defaults.fetch(key, default))
29
+ end
30
+
23
31
  def dump data, h = {}
24
32
  offset = h.fetch(:offset, 0)
25
33
  dedup = h.fetch(:dedup, true)
@@ -30,13 +38,20 @@ module ZHexdump
30
38
  offset = 0
31
39
  end
32
40
 
33
- add = h[:add] || 0
34
- size = h[:size] || (data.size-offset)
35
- tail = h[:tail] || "\n"
36
- width = h[:width] || 0x10 # row width, in bytes
37
- output = h[:output] || $>
38
- indent = h[:indent] || 0
39
- offset_format = h[:offset_format] || "%08x: "
41
+ add = _get_param(h, :add, 0)
42
+ size = _get_param(h, :size, data.size-offset)
43
+ tail = _get_param(h, :tail, "\n")
44
+ width = _get_param(h, :width, 16)
45
+ output = _get_param(h, :output, $stdout)
46
+ indent = _get_param(h, :indent, 0)
47
+ offset_format = _get_param(h, :offset_format, "%08x: ")
48
+ group_size = _get_param(h, :group_size, 8)
49
+ group_sep = _get_param(h, :group_separator, ' ')
50
+ show_ascii = _get_param(h, :show_ascii, true)
51
+
52
+ byte_format = _get_param(h, :byte_format, "%02x")
53
+ no_byte_str = _get_param(h, :no_byte_str, ' ')
54
+ byte_sep = _get_param(h, :byte_separator, ' ')
40
55
 
41
56
  indent = ' ' * indent
42
57
  size = data.size-offset if size+offset > data.size
@@ -45,12 +60,13 @@ module ZHexdump
45
60
  while true
46
61
  ascii = ''; hex = ''
47
62
  width.times do |i|
48
- hex << ' ' if i%8==0 && i>0
63
+ hex << group_sep if group_size > 0 && i > 0 && i % group_size == 0
49
64
  if c = ((size > 0) && data[offset+i])
50
- hex << "%02x " % c.ord
51
- ascii << ((32..126).include?(c.ord) ? c : '.')
65
+ ord = c.ord
66
+ hex << (byte_format % ord) << byte_sep
67
+ ascii << (ord == 0 ? ' ' : ((32..126).include?(ord) ? c : '.'))
52
68
  else
53
- hex << ' '
69
+ hex << no_byte_str << byte_sep
54
70
  ascii << ' '
55
71
  end
56
72
  size-=1
@@ -67,7 +83,11 @@ module ZHexdump
67
83
  else
68
84
  row = indent + (show_offset ? (offset_format % (offset + add)) : '') + hex
69
85
  yield(row, offset+add, ascii) if block_given?
70
- row << ' |' + ascii + "|"
86
+ if show_ascii
87
+ row << ' |' + ascii + "|"
88
+ else
89
+ row.rstrip! # remove trailing spaces
90
+ end
71
91
  output << "\n" if offset > start
72
92
  output << row
73
93
  prevdup = false
data/spec/hexdump_spec.rb CHANGED
@@ -59,10 +59,10 @@ describe ZHexdump do
59
59
  s = ''
60
60
  ZHexdump.dump data, :output => s, :indent => 2
61
61
  a = s.split("\n")
62
- a[0].should == " 00000000: 66 6f 6f 62 61 72 00 00 00 00 00 00 00 00 00 00 |foobar..........|"
63
- a[1].should == " 00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|"
62
+ a[0].should == " 00000000: 66 6f 6f 62 61 72 00 00 00 00 00 00 00 00 00 00 |foobar |"
63
+ a[1].should == " 00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | |"
64
64
  a[2].should == " *"
65
- a[3].should == " 00000060: 00 00 00 00 00 00 00 00 00 00 |.......... |"
65
+ a[3].should == " 00000060: 00 00 00 00 00 00 00 00 00 00 | |"
66
66
  end
67
67
 
68
68
  it "adds :add to offset shown" do
@@ -170,4 +170,34 @@ describe ZHexdump do
170
170
  data = "foo"
171
171
  data.to_hexdump.should == "00000000: 66 6f 6f |foo |\n"
172
172
  end
173
+
174
+ it "respects :group size" do
175
+ data = "foobarbaz"
176
+ data.to_hexdump(group_size: 3).should == "00000000: 66 6f 6f 62 61 72 62 61 7a |foobarbaz |\n"
177
+ end
178
+
179
+ it "respects :group_separator" do
180
+ data = "foobarbaz"
181
+ data.to_hexdump(group_size: 3, group_separator: "| ").should == "00000000: 66 6f 6f | 62 61 72 | 62 61 7a | | | |foobarbaz |\n"
182
+ end
183
+
184
+ it "respects :show_ascii" do
185
+ data = "foobarbaz" * 2
186
+ data.to_hexdump(show_ascii: false).should == "00000000: 66 6f 6f 62 61 72 62 61 7a 66 6f 6f 62 61 72 62\n00000010: 61 7a\n"
187
+ end
188
+
189
+ it "respects :byte_format" do
190
+ data = "foobarbaz"
191
+ data.to_hexdump(byte_format: "%03o").should == "00000000: 146 157 157 142 141 162 142 141 172 |foobarbaz |\n"
192
+ end
193
+
194
+ it "respects :byte_separator" do
195
+ data = "foobarbaz"
196
+ data.to_hexdump(byte_separator: ":").should == "00000000: 66:6f:6f:62:61:72:62:61: 7a: : : : : : : : |foobarbaz |\n"
197
+ end
198
+
199
+ it "respects :no_byte_str" do
200
+ data = "foobarbaz"
201
+ data.to_hexdump(no_byte_str: "??").should == "00000000: 66 6f 6f 62 61 72 62 61 7a ?? ?? ?? ?? ?? ?? ?? |foobarbaz |\n"
202
+ end
173
203
  end
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,6 @@ require File.join(File.dirname(__FILE__), '../lib/zhexdump.rb')
2
2
 
3
3
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
4
4
  RSpec.configure do |config|
5
- config.treat_symbols_as_metadata_keys_with_true_values = true
6
5
  config.run_all_when_everything_filtered = true
7
6
  #config.filter_run :focus
8
7
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zhexdump
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey "Zed" Zaikin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-02 00:00:00.000000000 Z
11
+ date: 2025-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  requirements: []
64
- rubygems_version: 3.5.6
64
+ rubygems_version: 3.5.22
65
65
  signing_key:
66
66
  specification_version: 4
67
67
  summary: A highly flexible hexdump implementation.