hexdump 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: faba56a33b697742b30ee19df9e85ae6295f38ada5d3785c4facae5d5b7d9a84
4
- data.tar.gz: c4347bfbd5347c53824b04dd617a07ae66dbe89e1b5494cbcf6ea05f16eaeb02
3
+ metadata.gz: fc17566453285686c384ffb48945dfa526b3d4ea24a3edec8b193540ec40fead
4
+ data.tar.gz: cfd9d433a9d11d85b43cd98a46e4b8c1aa67dcc572bd0ebfd7771469bbc315ed
5
5
  SHA512:
6
- metadata.gz: c2904b21732bbdfc99205113cfaaf35f508e0ccc76db1beed57d8204c86f7472f0b9e15305f0f4a74f6db13fc41d4aa7a7f15743db06bad1dfbc064798ae0813
7
- data.tar.gz: 7b4f9802586c41e0f6e994c84951432854f06e5a48ed611e80e1d4f03ec38037707a919c18e8412fb3876789494ee8551b98b703b697ac435ee70ea080dc013d
6
+ metadata.gz: bcc397a847ff0d4b9cfef884d6c8c97ed344cbded5242cfeccd2e6e61dc755a4dfb3ac20ddfc65d66f1428dce3ba36ab46858c9c2fdb2e43905b73c4f39e0c10
7
+ data.tar.gz: 3917cbf496621c7ddd8e9aec840be04b1836145f082fa0eefe016438cba570cda39013becd7a07fefdb8e4a0c1e9a13b17690f238da468ed528792c043e1c1f4
@@ -1,10 +1,6 @@
1
1
  name: CI
2
2
 
3
- on:
4
- push:
5
- branches: [ master ]
6
- pull_request:
7
- branches: ['**']
3
+ on: [ push, pull_request ]
8
4
 
9
5
  jobs:
10
6
  tests:
@@ -17,7 +13,9 @@ jobs:
17
13
  - 2.5
18
14
  - 2.6
19
15
  - 2.7
16
+ - 3.0
20
17
  - jruby
18
+ - truffleruby
21
19
  name: Ruby ${{ matrix.ruby }}
22
20
  steps:
23
21
  - uses: actions/checkout@v2
data/ChangeLog.md CHANGED
@@ -1,3 +1,14 @@
1
+ ### 0.3.0 / 2021-04-10
2
+
3
+ * Require Ruby >= 2.0.0.
4
+ * Added support for Ruby 3.0.
5
+ * {Hexdump.dump}, {Hexdump#dump}, and {Hexdump::Dumper#initialize} now accept
6
+ keyword arguments.
7
+ * {Hexdump::Dumper#each} now returns the total number of bytes read.
8
+ * {Hexdump::Dumper#dump} now prints the final number of bytes read on the last
9
+ line.
10
+ * Micro-optimizations to improve performance on JRuby and TruffleRuby.
11
+
1
12
  ### 0.2.4 / 2020-09-26
2
13
 
3
14
  * Default the `:output` option of {Hexdump.dump} to `$stdout` instead of
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011-2020 Hal Brodigan
1
+ Copyright (c) 2011-2021 Hal Brodigan
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
- # hexdump
1
+ # hexdump.rb
2
2
 
3
- * [Source](https://github.com/postmodern/hexdump)
4
- * [Issues](https://github.com/postmodern/hexdump/issues)
3
+ [![CI](https://github.com/postmodern/hexdump.rb/actions/workflows/ruby.yml/badge.svg)](https://github.com/postmodern/hexdump.rb/actions/workflows/ruby.yml)
4
+
5
+ * [Source](https://github.com/postmodern/hexdump.rb)
6
+ * [Issues](https://github.com/postmodern/hexdump.rb/issues)
5
7
  * [Documentation](http://rubydoc.info/gems/hexdump/frames)
6
8
  * [Email](mailto:postmodern.mod3 at gmail.com)
7
9
 
@@ -15,8 +17,9 @@ Simple and Fast hexdumping for Ruby.
15
17
  * Can send the hexdump output to any Object supporting the `<<` method.
16
18
  * Can yield each line of hexdump, instead of printing the output.
17
19
  * Supports printing ASCII, hexadecimal, decimal, octal and binary bytes.
18
- * Supports hexdumping 1, 2, 4, 8 byte words.
19
- * Supports hexdumping Little and Big Endian words.
20
+ * Supports hexdumping bytes (8bit), words (16bit), double-words (32bit), and
21
+ quad-words (64bit).
22
+ * Supports Little Endian and Big Endian modes.
20
23
  * Makes {String}, {StringIO}, {IO}, {File} objects hexdumpable.
21
24
  * Fast-ish.
22
25
 
@@ -28,9 +31,11 @@ Simple and Fast hexdumping for Ruby.
28
31
 
29
32
  Hexdump.dump(data)
30
33
  # 00000000 68 65 6c 6c 6f 00 |hello.|
34
+ # 00000006
31
35
 
32
36
  data.hexdump
33
37
  # 00000000 68 65 6c 6c 6f 00 |hello.|
38
+ # 00000006
34
39
 
35
40
  File.open('dump.txt','w') do |file|
36
41
  data.hexdump(:output => file)
@@ -42,31 +47,39 @@ Simple and Fast hexdumping for Ruby.
42
47
  hex # => ["68", "65", "6c", "6c", "6f", "00"]
43
48
  printable # => ["h", "e", "l", "l", "o", "."]
44
49
  end
50
+ # => 6
45
51
 
46
52
  # configure the width of the hexdump
47
- Hexdump.dump('A' * 30, :width => 10)
53
+ Hexdump.dump('A' * 30, width: 10)
48
54
  # 00000000 41 41 41 41 41 41 41 41 41 41 |AAAAAAAAAA|
49
55
  # 0000000a 41 41 41 41 41 41 41 41 41 41 |AAAAAAAAAA|
50
56
  # 00000014 41 41 41 41 41 41 41 41 41 41 |AAAAAAAAAA|
57
+ # 0000001e
51
58
 
52
- Hexdump.dump(data, :ascii => true)
59
+ Hexdump.dump(data, ascii: true)
53
60
  # 00000000 h e l l o 00 |hello.|
61
+ # 00000006
54
62
 
55
- Hexdump.dump(data, :base => 16)
63
+ Hexdump.dump(data, base: 16)
56
64
  # 00000000 68 65 6c 6c 6f 00 |hello.|
65
+ # 00000006
57
66
 
58
- Hexdump.dump(data, :base => :decimal)
67
+ Hexdump.dump(data, base: :decimal)
59
68
  # 00000000 104 101 108 108 111 0 |hello.|
69
+ # 00000006
60
70
 
61
- Hexdump.dump(data, :base => :octal)
71
+ Hexdump.dump(data, base: :octal)
62
72
  # 00000000 0150 0145 0154 0154 0157 0000 |hello.|
73
+ # 00000006
63
74
 
64
- Hexdump.dump(data, :base => :binary)
75
+ Hexdump.dump(data, base: :binary)
65
76
  # 00000000 01101000 01100101 01101100 01101100 01101111 00000000 |hello.|
77
+ # 00000006
66
78
 
67
- ("ABC" * 10).hexdump(:word_size => 2)
79
+ ("ABC" * 10).hexdump(word_size: 2)
68
80
  # 00000000 4241 4143 4342 4241 4143 4342 4241 4143 |䉁䅃䍂䉁䅃䍂䉁䅃|
69
81
  # 00000010 4342 4241 4143 4342 4241 4143 4342 |䍂䉁䅃䍂䉁䅃䍂|
82
+ # 0000001e
70
83
 
71
84
  ## Install
72
85
 
@@ -74,74 +87,82 @@ Simple and Fast hexdumping for Ruby.
74
87
 
75
88
  ## Benchmarks
76
89
 
77
- Benchmarks show {Hexdump.dump} processing 2.4M of data.
78
-
79
- ### Ruby 1.9.2-p180
80
-
81
- user system total real
82
- hexdump (block) 3.010000 0.010000 3.020000 ( 3.529396)
83
- hexdump 5.430000 0.030000 5.460000 ( 6.216174)
84
- hexdump width=256 (block) 3.010000 0.020000 3.030000 ( 3.308961)
85
- hexdump width=256 4.700000 0.040000 4.740000 ( 5.520189)
86
- hexdump ascii=true (block) 3.050000 0.010000 3.060000 ( 3.501436)
87
- hexdump ascii=true 5.450000 0.040000 5.490000 ( 6.352144)
88
- hexdump word_size=2 (block) 7.420000 0.050000 7.470000 ( 9.174734)
89
- hexdump word_size=2 9.500000 0.070000 9.570000 ( 11.228204)
90
- hexdump word_size=4 (block) 4.110000 0.030000 4.140000 ( 4.849785)
91
- hexdump word_size=4 5.380000 0.060000 5.440000 ( 6.209022)
92
- hexdump word_size=8 (block) 3.350000 0.070000 3.420000 ( 4.147304)
93
- hexdump word_size=8 4.430000 0.040000 4.470000 ( 5.930758)
94
-
95
- ### Ruby 1.8.7-p334
96
-
97
- user system total real
98
- hexdump (block) 8.470000 0.020000 8.490000 ( 9.585524)
99
- hexdump 11.080000 0.050000 11.130000 ( 12.542401)
100
- hexdump width=256 (block) 8.360000 0.030000 8.390000 ( 9.431877)
101
- hexdump width=256 10.310000 0.050000 10.360000 ( 12.278973)
102
- hexdump ascii=true (block) 8.550000 0.030000 8.580000 ( 10.502437)
103
- hexdump ascii=true 11.140000 0.040000 11.180000 ( 12.752712)
104
- hexdump word_size=2 (block) 12.680000 0.060000 12.740000 ( 14.657269)
105
- hexdump word_size=2 13.560000 0.080000 13.640000 ( 16.368675)
106
- hexdump word_size=4 (block) 8.500000 0.040000 8.540000 ( 9.687623)
107
- hexdump word_size=4 9.340000 0.040000 9.380000 ( 10.657158)
108
- hexdump word_size=8 (block) 7.520000 0.040000 7.560000 ( 8.565246)
109
- hexdump word_size=8 8.240000 0.040000 8.280000 ( 9.475693)
110
-
111
- ### JRuby 1.6.0
112
-
113
- user system total real
114
- hexdump (block) 6.742000 0.000000 6.742000 ( 6.495000)
115
- hexdump 7.498000 0.000000 7.498000 ( 7.498000)
116
- hexdump width=256 (block) 4.601000 0.000000 4.601000 ( 4.601000)
117
- hexdump width=256 5.569000 0.000000 5.569000 ( 5.569000)
118
- hexdump ascii=true (block) 5.198000 0.000000 5.198000 ( 5.198000)
119
- hexdump ascii=true 5.799000 0.000000 5.799000 ( 5.798000)
120
- hexdump word_size=2 (block) 8.440000 0.000000 8.440000 ( 8.440000)
121
- hexdump word_size=2 8.698000 0.000000 8.698000 ( 8.698000)
122
- hexdump word_size=4 (block) 5.603000 0.000000 5.603000 ( 5.602000)
123
- hexdump word_size=4 5.999000 0.000000 5.999000 ( 5.999000)
124
- hexdump word_size=8 (block) 7.975000 0.000000 7.975000 ( 7.975000)
125
- hexdump word_size=8 5.255000 0.000000 5.255000 ( 5.255000)
126
-
127
- ### Rubinius 1.2.4
128
-
129
- user system total real
130
- hexdump (block) 5.064230 0.029996 5.094226 ( 6.236865)
131
- hexdump 7.401875 0.039993 7.441868 ( 10.154394)
132
- hexdump width=256 (block) 4.149369 0.054992 4.204361 ( 6.518306)
133
- hexdump width=256 4.960246 0.089986 5.050232 ( 8.647516)
134
- hexdump ascii=true (block) 4.458322 0.026996 4.485318 ( 5.570982)
135
- hexdump ascii=true 6.961941 0.056992 7.018933 ( 9.895088)
136
- hexdump word_size=2 (block) 8.856653 0.078988 8.935641 ( 11.226360)
137
- hexdump word_size=2 10.489405 0.083988 10.573393 ( 12.980509)
138
- hexdump word_size=4 (block) 4.848263 0.047992 4.896255 ( 6.526478)
139
- hexdump word_size=4 6.649989 0.053992 6.703981 ( 8.245247)
140
- hexdump word_size=8 (block) 5.638143 0.047993 5.686136 ( 12.530454)
141
- hexdump word_size=8 7.598844 0.066990 7.665834 ( 16.881667)
90
+ Benchmarks show {Hexdump.dump} processing 25M of data.
91
+
92
+ ### Ruby 2.7.3
93
+
94
+ ```
95
+ user system total real
96
+ Hexdump.dump (output) 10.283433 0.000748 10.284181 ( 10.328899)
97
+ Hexdump.dump width=256 (output) 8.803228 0.005973 8.809201 ( 8.838375)
98
+ Hexdump.dump ascii=true (output) 10.740975 0.001903 10.742878 ( 10.779777)
99
+ Hexdump.dump word_size=2 (output) 15.163195 0.000989 15.164184 ( 15.220481)
100
+ Hexdump.dump word_size=4 (output) 14.279406 0.003840 14.283246 ( 14.345357)
101
+ Hexdump.dump word_size=8 (output) 7.715803 0.002879 7.718682 ( 7.746389)
102
+ Hexdump.dump (block) 5.543268 0.000980 5.544248 ( 5.561494)
103
+ Hexdump.dump width=256 (block) 5.438946 0.000000 5.438946 ( 5.455742)
104
+ Hexdump.dump ascii=true (block) 6.082787 0.000924 6.083711 ( 6.106234)
105
+ Hexdump.dump word_size=2 (block) 11.439610 0.000983 11.440593 ( 11.483788)
106
+ Hexdump.dump word_size=4 (block) 11.111633 0.000954 11.112587 ( 11.158416)
107
+ Hexdump.dump word_size=8 (block) 5.397569 0.002896 5.400465 ( 5.426971)
108
+ ```
109
+
110
+ ### Ruby 3.0.1
111
+
112
+ ```
113
+ user system total real
114
+ Hexdump.dump (output) 12.064022 0.001165 12.065187 ( 12.118272)
115
+ Hexdump.dump width=256 (output) 10.228743 0.009920 10.238663 ( 10.279783)
116
+ Hexdump.dump ascii=true (output) 12.532913 0.000000 12.532913 ( 12.582665)
117
+ Hexdump.dump word_size=2 (output) 17.685782 0.000000 17.685782 ( 17.770686)
118
+ Hexdump.dump word_size=4 (output) 15.835564 0.000000 15.835564 ( 15.917552)
119
+ Hexdump.dump word_size=8 (output) 8.436831 0.000000 8.436831 ( 8.473445)
120
+ Hexdump.dump (block) 6.482589 0.000000 6.482589 ( 6.504816)
121
+ Hexdump.dump width=256 (block) 6.360828 0.000000 6.360828 ( 6.383705)
122
+ Hexdump.dump ascii=true (block) 6.911868 0.000000 6.911868 ( 6.936795)
123
+ Hexdump.dump word_size=2 (block) 13.120488 0.000000 13.120488 ( 13.179957)
124
+ Hexdump.dump word_size=4 (block) 12.349516 0.000000 12.349516 ( 12.412972)
125
+ Hexdump.dump word_size=8 (block) 5.814830 0.000000 5.814830 ( 5.837822)
126
+ ```
127
+
128
+ ### JRuby 9.2.16.0
129
+
130
+ ```
131
+ user system total real
132
+ Hexdump.dump (output) 13.090000 0.240000 13.330000 ( 11.226466)
133
+ Hexdump.dump width=256 (output) 9.350000 0.030000 9.380000 ( 9.165070)
134
+ Hexdump.dump ascii=true (output) 10.910000 0.050000 10.960000 ( 10.665791)
135
+ Hexdump.dump word_size=2 (output) 13.760000 0.150000 13.910000 ( 12.268307)
136
+ Hexdump.dump word_size=4 (output) 11.940000 0.090000 12.030000 ( 11.107564)
137
+ Hexdump.dump word_size=8 (output) 8.170000 0.040000 8.210000 ( 7.419708)
138
+ Hexdump.dump (block) 7.840000 0.020000 7.860000 ( 7.777749)
139
+ Hexdump.dump width=256 (block) 7.540000 0.000000 7.540000 ( 7.466315)
140
+ Hexdump.dump ascii=true (block) 7.680000 0.010000 7.690000 ( 7.622393)
141
+ Hexdump.dump word_size=2 (block) 9.830000 0.020000 9.850000 ( 9.693596)
142
+ Hexdump.dump word_size=4 (block) 9.010000 0.020000 9.030000 ( 8.998687)
143
+ Hexdump.dump word_size=8 (block) 5.740000 0.030000 5.770000 ( 5.709127)
144
+ ```
145
+
146
+ ### TruffleRuby 21.0.0
147
+
148
+ ```
149
+ user system total real
150
+ Hexdump.dump (output) 25.818995 0.855689 26.674684 ( 22.376015)
151
+ Hexdump.dump width=256 (output) 20.489077 0.125966 20.615043 ( 18.301748)
152
+ Hexdump.dump ascii=true (output) 25.214678 0.098018 25.312696 ( 21.714985)
153
+ Hexdump.dump word_size=2 (output) 28.380387 0.192277 28.572664 ( 23.736887)
154
+ Hexdump.dump word_size=4 (output) 31.348977 0.134854 31.483831 ( 27.710968)
155
+ Hexdump.dump word_size=8 (output) 18.850093 0.100256 18.950349 ( 13.921720)
156
+ Hexdump.dump (block) 7.792878 0.050542 7.843420 ( 6.003789)
157
+ Hexdump.dump width=256 (block) 6.526531 0.015898 6.542429 ( 5.777169)
158
+ Hexdump.dump ascii=true (block) 7.425399 0.030799 7.456198 ( 5.705369)
159
+ Hexdump.dump word_size=2 (block) 12.629775 0.028653 12.658428 ( 11.115049)
160
+ Hexdump.dump word_size=4 (block) 20.372094 0.010807 20.382901 ( 19.758073)
161
+ Hexdump.dump word_size=8 (block) 8.828653 0.010889 8.839542 ( 8.017241)
162
+ ```
142
163
 
143
164
  ## Copyright
144
165
 
145
- Copyright (c) 2011-2020 Hal Brodigan
166
+ Copyright (c) 2011-2021 Hal Brodigan
146
167
 
147
168
  See {file:LICENSE.txt} for details.
data/benchmark.rb ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.expand_path('../lib',__FILE__))
4
+
5
+ require 'hexdump'
6
+ require 'benchmark'
7
+
8
+ DATA = ((0..255).map { |b| b.chr }.join) * (1024 * 100)
9
+ OUTPUT = Class.new { def <<(data); end }.new
10
+
11
+ Benchmark.bm(33) do |b|
12
+ b.report('Hexdump.dump (output)') do
13
+ Hexdump.dump(DATA, :output => OUTPUT)
14
+ end
15
+
16
+ b.report('Hexdump.dump width=256 (output)') do
17
+ Hexdump.dump(DATA, width: 256, output: OUTPUT)
18
+ end
19
+
20
+ b.report('Hexdump.dump ascii=true (output)') do
21
+ Hexdump.dump(DATA, ascii: true, output: OUTPUT)
22
+ end
23
+
24
+ [2, 4, 8].each do |word_size|
25
+ b.report("Hexdump.dump word_size=#{word_size} (output)") do
26
+ Hexdump.dump(DATA, word_size: word_size, output: OUTPUT)
27
+ end
28
+ end
29
+
30
+ b.report('Hexdump.dump (block)') do
31
+ Hexdump.dump(DATA) { |index,hex,print| }
32
+ end
33
+
34
+ b.report('Hexdump.dump width=256 (block)') do
35
+ Hexdump.dump(DATA, width: 256) { |index,hex,print| }
36
+ end
37
+
38
+ b.report('Hexdump.dump ascii=true (block)') do
39
+ Hexdump.dump(DATA, ascii: true) { |index,hex,print| }
40
+ end
41
+
42
+ [2, 4, 8].each do |word_size|
43
+ b.report("Hexdump.dump word_size=#{word_size} (block)") do
44
+ Hexdump.dump(DATA, word_size: word_size) { |index,hex,print| }
45
+ end
46
+ end
47
+ end
data/gemspec.yml CHANGED
@@ -4,10 +4,16 @@ description: Simple and Fast hexdumping for Ruby.
4
4
  license: MIT
5
5
  authors: Postmodern
6
6
  email: postmodern.mod3@gmail.com
7
- homepage: https://github.com/postmodern/hexdump#readme
7
+ homepage: https://github.com/postmodern/hexdump.rb#readme
8
8
  has_yard: true
9
9
 
10
- required_ruby_version: ">= 1.8.7"
10
+ metadata:
11
+ documentation_uri: https://rubydoc.info/gems/hexdump
12
+ source_code_uri: https://github.com/postmodern/hexdump.rb
13
+ bug_tracker_uri: https://github.com/postmodern/hexdump.rb/issues
14
+ changelog_uri: https://github.com/postmodern/hexdump.rb/blob/master/ChangeLog.md
15
+
16
+ required_ruby_version: ">= 2.0.0"
11
17
 
12
18
  development_dependencies:
13
19
  bundler: ~> 2.0
data/hexdump.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |gem|
20
20
  gem.authors = Array(gemspec['authors'])
21
21
  gem.email = gemspec['email']
22
22
  gem.homepage = gemspec['homepage']
23
+ gem.metadata = gemspec['metadata'] if gemspec['metadata']
23
24
 
24
25
  glob = lambda { |patterns| gem.files & Dir[*patterns] }
25
26
 
data/lib/hexdump.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  require 'hexdump/hexdump'
2
- require 'hexdump/extensions'
2
+ require 'hexdump/core_ext'
3
3
  require 'hexdump/version'
@@ -0,0 +1,4 @@
1
+ require 'hexdump/core_ext/string'
2
+ require 'hexdump/core_ext/string_io'
3
+ require 'hexdump/core_ext/io'
4
+ require 'hexdump/core_ext/file'
File without changes
File without changes
File without changes
File without changes
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Hexdump
2
4
  #
3
5
  # Handles the parsing of data and formatting of the hexdump.
@@ -8,28 +10,28 @@ module Hexdump
8
10
 
9
11
  # Widths for formatted numbers
10
12
  WIDTHS = {
11
- :hexadecimal => proc { |word_size| word_size * 2 },
12
- :decimal => {
13
+ hexadecimal: ->(word_size) { word_size * 2 },
14
+ decimal: {
13
15
  1 => 3,
14
16
  2 => 5,
15
17
  4 => 10,
16
18
  8 => 20
17
19
  },
18
- :octal => {
20
+ octal: {
19
21
  1 => 3,
20
22
  2 => 6,
21
23
  4 => 11,
22
24
  8 => 22
23
25
  },
24
- :binary => proc { |word_size| word_size * 8 }
26
+ binary: ->(word_size) { word_size * 8 }
25
27
  }
26
28
 
27
29
  # Format Strings for the various bases
28
30
  FORMATS = {
29
- :hexadecimal => proc { |width| "%.#{width}x" },
30
- :decimal => proc { |width| "%#{width}.d" },
31
- :octal => proc { |width| "%.#{width}o" },
32
- :binary => proc { |width| "%.#{width}b" }
31
+ hexadecimal: ->(width) { "%.#{width}x" },
32
+ decimal: ->(width) { "%#{width}.d" },
33
+ octal: ->(width) { "%.#{width}o" },
34
+ binary: ->(width) { "%.#{width}b" }
33
35
  }
34
36
 
35
37
  # Character to represent unprintable characters
@@ -154,25 +156,22 @@ module Hexdump
154
156
  #
155
157
  # Creates a new Hexdump dumper.
156
158
  #
157
- # @param [Hash] options
158
- # Additional options.
159
- #
160
- # @option options [Integer] :width (16)
159
+ # @param [Integer] width (16)
161
160
  # The number of bytes to dump for each line.
162
161
  #
163
- # @option options [Integer] :endian (:little)
162
+ # @param [Integer] endian (:little)
164
163
  # The endianness that the bytes are organized in. Supported endianness
165
164
  # include `:little` and `:big`.
166
165
  #
167
- # @option options [Integer] :word_size (1)
166
+ # @param [Integer] word_size (1)
168
167
  # The number of bytes within a word.
169
168
  #
170
- # @option options [Symbol, Integer] :base (:hexadecimal)
169
+ # @param [Symbol, Integer] base (:hexadecimal)
171
170
  # The base to print bytes in. Supported bases include, `:hexadecimal`,
172
171
  # `:hex`, `16, `:decimal`, `:dec`, `10, `:octal`, `:oct`, `8`,
173
172
  # `:binary`, `:bin` and `2`.
174
173
  #
175
- # @option options [Boolean] :ascii (false)
174
+ # @param [Boolean] ascii (false)
176
175
  # Print ascii characters when possible.
177
176
  #
178
177
  # @raise [ArgumentError]
@@ -180,36 +179,33 @@ module Hexdump
180
179
  #
181
180
  # @since 0.2.0
182
181
  #
183
- def initialize(options={})
184
- @base = case options[:base]
185
- when :hexadecimal, :hex, 16
186
- :hexadecimal
187
- when :decimal, :dec, 10
188
- :decimal
189
- when :octal, :oct, 8
190
- :octal
191
- when :binary, :bin, 2
192
- :binary
193
- when nil
194
- :hexadecimal
182
+ def initialize(width: 16,
183
+ endian: :little,
184
+ word_size: 1,
185
+ base: :hexadecimal,
186
+ ascii: false)
187
+
188
+ @base = case base
189
+ when :hexadecimal, :hex, 16 then :hexadecimal
190
+ when :decimal, :dec, 10 then :decimal
191
+ when :octal, :oct, 8 then :octal
192
+ when :binary, :bin, 2 then :binary
193
+ when nil then :hexadecimal
195
194
  else
196
- raise(ArgumentError,"unknown base #{options[:base].inspect}")
195
+ raise(ArgumentError,"unknown base #{base.inspect}")
197
196
  end
198
197
 
199
- @word_size = options.fetch(:word_size,1)
200
- @endian = case options[:endian]
201
- when 'little', :little
202
- :little
203
- when 'big', :big
204
- :big
205
- when nil
206
- :little
198
+ @word_size = word_size
199
+ @endian = case endian
200
+ when 'little', :little then :little
201
+ when 'big', :big then :big
202
+ when nil then :little
207
203
  else
208
- raise(ArgumentError,"unknown endian: #{options[:endian].inspect}")
204
+ raise(ArgumentError,"unknown endian: #{endian.inspect}")
209
205
  end
210
206
 
211
- @width = (options.fetch(:width,16) / @word_size)
212
- @ascii = options.fetch(:ascii,false)
207
+ @width = (width / @word_size)
208
+ @ascii = ascii
213
209
 
214
210
  @format_width = (WIDTHS[@base][@word_size] || 1)
215
211
  @format = FORMATS[@base][@format_width]
@@ -252,20 +248,16 @@ module Hexdump
252
248
  word = 0
253
249
  count = 0
254
250
 
255
- init_shift = if @endian == :big
256
- ((@word_size - 1) * 8)
257
- else
258
- 0
251
+ init_shift = if @endian == :big then ((@word_size - 1) * 8)
252
+ else 0
259
253
  end
260
254
  shift = init_shift
261
255
 
262
256
  data.each_byte do |b|
263
257
  word |= (b << shift)
264
258
 
265
- if @endian == :big
266
- shift -= 8
267
- else
268
- shift += 8
259
+ if @endian == :big then shift -= 8
260
+ else shift += 8
269
261
  end
270
262
 
271
263
  count += 1
@@ -305,7 +297,8 @@ module Hexdump
305
297
  # @yieldparam [Array<String>] printable
306
298
  # The printable representation of the segment.
307
299
  #
308
- # @return [Enumerator]
300
+ # @return [Integer, Enumerator]
301
+ # If a block is given, then the final number of bytes read is returned.
309
302
  # If no block is given, an Enumerator will be returned.
310
303
  #
311
304
  # @since 0.2.0
@@ -316,21 +309,18 @@ module Hexdump
316
309
  index = 0
317
310
  count = 0
318
311
 
319
- numeric = []
320
- printable = []
312
+ numeric = Array.new(@width)
313
+ printable = Array.new(@width)
321
314
 
322
315
  each_word(data) do |word|
323
- numeric << format_numeric(word)
324
- printable << format_printable(word)
316
+ numeric[count] = format_numeric(word)
317
+ printable[count] = format_printable(word)
325
318
 
326
319
  count += 1
327
320
 
328
321
  if count >= @width
329
322
  yield index, numeric, printable
330
323
 
331
- numeric.clear
332
- printable.clear
333
-
334
324
  index += (@width * @word_size)
335
325
  count = 0
336
326
  end
@@ -338,8 +328,12 @@ module Hexdump
338
328
 
339
329
  if count > 0
340
330
  # yield the remaining data
341
- yield index, numeric, printable
331
+ yield index, numeric[0,count], printable[0,count]
332
+
333
+ index += (count * @word_size)
342
334
  end
335
+
336
+ return index
343
337
  end
344
338
 
345
339
  #
@@ -364,39 +358,17 @@ module Hexdump
364
358
  end
365
359
 
366
360
  bytes_segment_width = ((@width * @format_width) + @width)
367
- line_format = "%.8x %-#{bytes_segment_width}s |%s|\n"
368
-
369
- index = 0
370
- count = 0
371
-
372
- numeric = ''
373
- printable = ''
374
-
375
- each_word(data) do |word|
376
- numeric << format_numeric(word) << ' '
377
- printable << format_printable(word)
361
+ index_format = "%.8x"
362
+ line_format = "#{index_format} %-#{bytes_segment_width}s |%s|#{$/}"
378
363
 
379
- count += 1
380
-
381
- if count >= @width
382
- output << sprintf(line_format,index,numeric,printable)
383
-
384
- numeric = ''
385
- printable = ''
386
-
387
- index += (@width * @word_size)
388
- count = 0
389
- end
364
+ length = each(data) do |index,numeric,printable|
365
+ output << sprintf(line_format,index,numeric.join(' '),printable.join)
390
366
  end
391
367
 
392
- if count > 0
393
- # output the remaining line
394
- output << sprintf(line_format,index,numeric,printable)
395
- end
368
+ output << sprintf("#{index_format}#{$/}",length)
369
+ return nil
396
370
  end
397
371
 
398
- protected
399
-
400
372
  #
401
373
  # Converts the word into a numeric String.
402
374
  #
@@ -434,12 +406,10 @@ module Hexdump
434
406
  def format_printable(word)
435
407
  if @word_size == 1
436
408
  PRINTABLE[word]
437
- elsif (RUBY_VERSION > '1.9.' && (word >= -2 && word <= 0x7fffffff))
438
- begin
439
- word.chr(Encoding::UTF_8)
440
- rescue RangeError
441
- UNPRINTABLE
442
- end
409
+ elsif word >= 0 && word <= 0x7fffffff
410
+ # XXX: https://github.com/jruby/jruby/issues/6652
411
+ char = word.chr(Encoding::UTF_8) rescue nil
412
+ char || UNPRINTABLE
443
413
  else
444
414
  UNPRINTABLE
445
415
  end