trick_bag 0.63.1 → 0.64.1

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
  SHA1:
3
- metadata.gz: d2ef229476828aed3a6f31de68df08fd63c4ab68
4
- data.tar.gz: 657324791c88fb759a394f5f3df239e7dd1b7aad
3
+ metadata.gz: 93b00bbcaf07fa98422e6fae2c1000f39666cbdb
4
+ data.tar.gz: 4eda3ed496a91247fbecd43402adfc91143ad064
5
5
  SHA512:
6
- metadata.gz: 1be63b3498bc587b88af96dac47a96179c11299a4075efa84634e478e7edb73e9301e21b08869273f43b60785507abe140b7c8c9821f60f1b5efbefb1a4dc759
7
- data.tar.gz: 4143932e28bb58aafc887a3faba3322b662562649dc37c6f9663e884c5cfdaa5e52abf079bc76d437367ad4c794fa52a79079f6ee13348812abf9d304ac57993
6
+ metadata.gz: e9e26ea432ced1e895d6546ce1530aaf474a4117e445907392de1234c70004100975e99d2e8e17f9f0af8e5713615834789023c7b127f122373012fa74eea1c8
7
+ data.tar.gz: 2502fb4ec60ff4c3e073aed59ed3ca5b016e48da70aac8cb1bbc9114fe7c318ceb3fdd5b1ce99f33cccc1195dcd3aa1e134afb4331bed3d9949b3522c02d2827
data/RELEASE_NOTES.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## v0.64.1
2
+
3
+ * Minor documentation fix.
4
+
5
+
6
+ ## v0.64.0
7
+
8
+ * Add BinaryToHexAndAscii formatter.
9
+
10
+
1
11
  ## v0.63.1
2
12
 
3
13
  * Put SshOutputReader in TrickBag::Networking module.
@@ -28,7 +28,7 @@ module CollectionAccess
28
28
  begin
29
29
  Integer(object)
30
30
  rescue
31
- raise "Key is not a number string: #{object}"
31
+ raise "Key cannot be converted to an Integer: #{object}"
32
32
  end
33
33
  end
34
34
 
@@ -0,0 +1,73 @@
1
+ module TrickBag
2
+
3
+ # Takes a string as input and formats it to a string containing offset,
4
+ # hex values, and ASCII values, with unprintable chars printed as '.'.
5
+ # Call as TrickBag::BinaryToHexAndAscii.format(byte_array_or_string)
6
+ #
7
+ # Sample Output:
8
+
9
+ # require 'trick_bag'
10
+ # array = Array.new(32) { |i| i * 7 }
11
+ # puts TrickBag::BinaryToHexAndAscii.format(array)
12
+
13
+ # 0x 0 00 07 0E 15 | 1C 23 2A 31 | 38 3F 46 4D | 54 5B 62 69 .....#*18?FMT[bi
14
+ # 0x 10 70 77 7E 85 | 8C 93 9A A1 | A8 AF B6 BD | C4 CB D2 D9 pw~.............
15
+
16
+ module BinaryToHexAndAscii
17
+
18
+ module_function
19
+
20
+ def format(byte_array_or_string)
21
+ byte_array = if byte_array_or_string.is_a?(String)
22
+ byte_array_or_string.bytes
23
+ else
24
+ byte_array_or_string
25
+ end
26
+
27
+ result = ''
28
+ offset = 0
29
+ byte_array.each_slice(16) do |bytes|
30
+ result << format_line(offset, bytes) << "\n"
31
+ offset += 16
32
+ end
33
+ result
34
+ end
35
+
36
+
37
+ def one_to_four_bytes_as_hex(bytes)
38
+ bytes.map { |b| "%02X" % b }.join(' ')
39
+ end
40
+
41
+
42
+ def offset_string(offset)
43
+ "0x%4x" % [offset] # Offset, e.g. "0x 0" or "0x 10"
44
+ end
45
+
46
+
47
+ def join_hex_sections(sections)
48
+ sections.join(' | ')
49
+ end
50
+
51
+
52
+ def ascii_char(byte)
53
+ (32..126).include?(byte) ? byte.chr : '.'
54
+ end
55
+
56
+
57
+ def ascii_string(bytes)
58
+ bytes.map { |b| ascii_char(b) }.join
59
+ end
60
+
61
+
62
+ def format_line(offset, bytes)
63
+ sections = bytes.each_slice(4).to_a.map { |slice| one_to_four_bytes_as_hex(slice) }
64
+
65
+ offset_string(offset) \
66
+ << ' ' \
67
+ << join_hex_sections(sections).ljust(53) \
68
+ << ' ' \
69
+ << ascii_string(bytes)
70
+ end
71
+ end
72
+
73
+ end
@@ -1,3 +1,3 @@
1
1
  module TrickBag
2
- VERSION = '0.63.1'
2
+ VERSION = '0.64.1'
3
3
  end
@@ -0,0 +1,73 @@
1
+ require_relative '../../spec_helper'
2
+ require_relative '../../../lib/trick_bag/formatters/binary_to_hex_and_ascii'
3
+
4
+
5
+ module TrickBag
6
+ module BinaryToHexAndAscii
7
+
8
+ describe BinaryToHexAndAscii do
9
+
10
+ include BinaryToHexAndAscii
11
+
12
+
13
+ context '#one_to_four_bytes_as_hex' do
14
+
15
+ specify '1 byte works' do
16
+ expect(one_to_four_bytes_as_hex([1])).to eq('01')
17
+ end
18
+
19
+ specify '4 bytes works' do
20
+ expect(one_to_four_bytes_as_hex([0, 16, 64, 255])).to eq('00 10 40 FF')
21
+ end
22
+
23
+ end
24
+
25
+
26
+ context '#offset_string' do
27
+ specify '0x8000 works' do
28
+ expect(offset_string(0x8000)).to match(/8000/)
29
+ end
30
+
31
+ end
32
+
33
+
34
+ context '#join_hex_sections' do
35
+ specify 'is correctly formatted' do
36
+ expect(join_hex_sections(%w(a b c))).to eq('a | b | c')
37
+ end
38
+ end
39
+
40
+
41
+ context '#ascii_char' do
42
+
43
+ test = ->(value, expected) do
44
+ specify "Ascii char of #{value} should be #{expected}" do
45
+ expect(ascii_char(value)).to eq(expected)
46
+ end
47
+ end
48
+ test.('a'.ord, 'a')
49
+ test.('*'.ord, '*')
50
+ test.('_'.ord, '_')
51
+ test.(27, '.')
52
+ test.(127, '.')
53
+ test.(255, '.')
54
+ end
55
+
56
+
57
+ context '#format' do
58
+
59
+ specify '1 line works' do
60
+ bytes = Array.new(15) { |i| i * 8 }
61
+ expect(format(bytes)).to eq("0x 0 00 08 10 18 | 20 28 30 38 | 40 48 50 58 | 60 68 70 .... (08@HPX`hp\n")
62
+ end
63
+
64
+ specify '2 lines works' do
65
+ bytes = Array.new(32) { |i| i * 7 }
66
+ expect(format(bytes)).to eq( \
67
+ "0x 0 00 07 0E 15 | 1C 23 2A 31 | 38 3F 46 4D | 54 5B 62 69 .....#*18?FMT[bi
68
+ 0x 10 70 77 7E 85 | 8C 93 9A A1 | A8 AF B6 BD | C4 CB D2 D9 pw~.............\n")
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trick_bag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.63.1
4
+ version: 0.64.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Bennett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-11 00:00:00.000000000 Z
11
+ date: 2015-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: os
@@ -146,6 +146,7 @@ files:
146
146
  - lib/trick_bag/enumerables/endless_last_enumerable.rb
147
147
  - lib/trick_bag/enumerables/file_line_reader.rb
148
148
  - lib/trick_bag/enumerables/filtered_enumerable.rb
149
+ - lib/trick_bag/formatters/binary_to_hex_and_ascii.rb
149
150
  - lib/trick_bag/formatters/erb_renderer.rb
150
151
  - lib/trick_bag/formatters/formatters.rb
151
152
  - lib/trick_bag/functional/functional.rb
@@ -178,6 +179,7 @@ files:
178
179
  - spec/trick_bag/enumerables/endless_last_enumerable_spec.rb
179
180
  - spec/trick_bag/enumerables/file_line_reader_spec.rb
180
181
  - spec/trick_bag/enumerables/filtered_enumerable_spec.rb
182
+ - spec/trick_bag/formatters/binary_to_hex_and_ascii_spec.rb
181
183
  - spec/trick_bag/formatters/erb_renderer_spec.rb
182
184
  - spec/trick_bag/formatters/formatters_spec.rb
183
185
  - spec/trick_bag/functional/functional_spec.rb
@@ -219,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
221
  version: '0'
220
222
  requirements: []
221
223
  rubyforge_project:
222
- rubygems_version: 2.4.6
224
+ rubygems_version: 2.4.7
223
225
  signing_key:
224
226
  specification_version: 4
225
227
  summary: Miscellaneous general useful tools.
@@ -233,6 +235,7 @@ test_files:
233
235
  - spec/trick_bag/enumerables/endless_last_enumerable_spec.rb
234
236
  - spec/trick_bag/enumerables/file_line_reader_spec.rb
235
237
  - spec/trick_bag/enumerables/filtered_enumerable_spec.rb
238
+ - spec/trick_bag/formatters/binary_to_hex_and_ascii_spec.rb
236
239
  - spec/trick_bag/formatters/erb_renderer_spec.rb
237
240
  - spec/trick_bag/formatters/formatters_spec.rb
238
241
  - spec/trick_bag/functional/functional_spec.rb