zhexdump 0.2.0 → 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: 87be9581b8556e6969275cb73e07ff353805db106f1f741bb522a941db276919
4
- data.tar.gz: 0f75b82fe5bb212b0f4ad29d9aea29f89b62f21de3fabc7496c5eb75a387b1dc
3
+ metadata.gz: 2a3f2ca2cbd4b74eb7d4f2e369155faed551cd1aae88c68b69b83db782b3bbad
4
+ data.tar.gz: e1755f4ee72a7144923ccb822d53214455ca0971e82a7cc94c104cf529865675
5
5
  SHA512:
6
- metadata.gz: 1fc327641290592b335173fc7385ef73344f0f5f45bad467f25e7edc5a20a130a0f5a4daf54888932c8084fe500823446ed69676c286fa1d48fc199310a92c5c
7
- data.tar.gz: 0231db65834152f9a9afce551a689a4a786c6a77863acf981929054db6254e8e96f74df59e4aa5e3a8c8c18f5c289107f819abb0b84405e10cf096140fb9eba3
6
+ metadata.gz: 130772c539567807650e00be726963b6af7c7f9773b537e01410f7229d7baf1f8187c4edb05e8536a15073a3d37193720b312e4f6047d00b1ac47369c95356a9
7
+ data.tar.gz: c3b20059c90fe00c3e45b4eaff82f196dfd3b68515047bc0d7f1d3360678e7891684cdc7d3ffa32e149b316fdf132c686ff4f2da5eb994002545d5ea363e5403
@@ -1,3 +1,3 @@
1
1
  module ZHexdump
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/zhexdump.rb CHANGED
@@ -25,7 +25,7 @@ module ZHexdump
25
25
  attr_accessor :defaults
26
26
 
27
27
  def _get_param(h, key, default)
28
- h[key] || defaults[key] || default
28
+ h.fetch(key, defaults.fetch(key, default))
29
29
  end
30
30
 
31
31
  def dump data, h = {}
@@ -45,6 +45,13 @@ module ZHexdump
45
45
  output = _get_param(h, :output, $stdout)
46
46
  indent = _get_param(h, :indent, 0)
47
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, ' ')
48
55
 
49
56
  indent = ' ' * indent
50
57
  size = data.size-offset if size+offset > data.size
@@ -53,13 +60,13 @@ module ZHexdump
53
60
  while true
54
61
  ascii = ''; hex = ''
55
62
  width.times do |i|
56
- hex << ' ' if i%8==0 && i>0
63
+ hex << group_sep if group_size > 0 && i > 0 && i % group_size == 0
57
64
  if c = ((size > 0) && data[offset+i])
58
65
  ord = c.ord
59
- hex << "%02x " % ord
66
+ hex << (byte_format % ord) << byte_sep
60
67
  ascii << (ord == 0 ? ' ' : ((32..126).include?(ord) ? c : '.'))
61
68
  else
62
- hex << ' '
69
+ hex << no_byte_str << byte_sep
63
70
  ascii << ' '
64
71
  end
65
72
  size-=1
@@ -76,7 +83,11 @@ module ZHexdump
76
83
  else
77
84
  row = indent + (show_offset ? (offset_format % (offset + add)) : '') + hex
78
85
  yield(row, offset+add, ascii) if block_given?
79
- row << ' |' + ascii + "|"
86
+ if show_ascii
87
+ row << ' |' + ascii + "|"
88
+ else
89
+ row.rstrip! # remove trailing spaces
90
+ end
80
91
  output << "\n" if offset > start
81
92
  output << row
82
93
  prevdup = false
data/spec/hexdump_spec.rb CHANGED
@@ -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
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.2.0
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-07-23 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.16
64
+ rubygems_version: 3.5.22
65
65
  signing_key:
66
66
  specification_version: 4
67
67
  summary: A highly flexible hexdump implementation.