cbor-diag 0.2.8 → 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
  SHA1:
3
- metadata.gz: af225452a464f0da9d3c7e3773df7960b8cceb61
4
- data.tar.gz: 89b51efa1e56a620a871ebb28e07f307acb7f912
3
+ metadata.gz: d862488b37e86df16ba27e69a7e2bdb28c0fb039
4
+ data.tar.gz: bf923d105ff2c9770430b0ac401720daff93573b
5
5
  SHA512:
6
- metadata.gz: bfee8e6a5331f98b65527ff59b09c2db6c63103928ddcae82c6e3e7a4fb2baa3592ce39abc8b4b384288db30bee81ac4935d822c2ca07839005ad66a0a0a3eb5
7
- data.tar.gz: ae170dd44bf170ba2ee46b1124b1309785d85142a8016579d1823ff704ff4d26db7b1a865b28b6b64bf51e8f26e6b0140bc83cb4e090839af9551088ea1430d8
6
+ metadata.gz: b0ac9fda4585effeae900c06727e737c33d20872f0c7ed1703d428cceb80c36d5289d8d06a6dfda1b6f8d2e905888cb29a7edd54942d1eb556ed48e739e38a39
7
+ data.tar.gz: 6e76c59d8011d343a9d1d8e90cc14d9f1e4f1b4a886c006ae30bd5eb36eddb7997301a48b060846e4e811c45a5e539040e9117088e32d47c3c7c831839526b31
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ require 'cbor-pretty'
3
+ require 'cbor-diagnostic'
4
+
5
+
6
+ def extractbytes(s)
7
+ s.each_line.map {|ln| ln.sub(/#.*/, '')}.join.scan(/[0-9a-fA-F][0-9a-fA-F]/).map {|b| b.to_i(16).chr(Encoding::BINARY)}.join
8
+ end
9
+
10
+ i = extractbytes(ARGF)
11
+ o = CBOR.decode(i)
12
+ puts o.cbor_diagnostic
data/cbor-diag.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "cbor-diag"
3
- s.version = "0.2.8"
3
+ s.version = "0.3.0"
4
4
  s.summary = "CBOR (Concise Binary Object Representation) diagnostic notation"
5
5
  s.description = %q{cbor-diag implements diagnostic notation for CBOR, RFC 7049}
6
6
  s.author = "Carsten Bormann"
@@ -4,19 +4,19 @@ unless defined?(CBOR)
4
4
  end
5
5
 
6
6
  class Object
7
- def cbor_diagnostic
7
+ def cbor_diagnostic(_=nil)
8
8
  inspect
9
9
  end
10
10
  end
11
11
 
12
12
  class NilClass
13
- def cbor_diagnostic
13
+ def cbor_diagnostic(_=nil)
14
14
  "null"
15
15
  end
16
16
  end
17
17
 
18
18
  class Float
19
- def cbor_diagnostic # do a little bit of JSON.stringify gaming (ECMA-262, 9.8.1)
19
+ def cbor_diagnostic(_=nil) # do a little bit of JSON.stringify gaming (ECMA-262, 9.8.1)
20
20
  a = abs
21
21
  if a < 1 && a >= 1e-6
22
22
  inspect.sub(/(\d)[.](\d+)e-(\d+)/) {"0.#{"0" * ($3.to_i - 1)}#{$1}#{$2}"}
@@ -40,35 +40,43 @@ class String
40
40
  def hexbytes(sep = '')
41
41
  bytes.map{|x| "%02X" % x}.join(sep)
42
42
  end
43
- def cbor_diagnostic
43
+ def cbor_diagnostic(options = {})
44
44
  if lengths = cbor_stream?
45
45
  pos = 0
46
- "(_ #{lengths.map{|l| r = self[pos, l].cbor_diagnostic; pos += l; r}.join(", ")})"
46
+ "(_ #{lengths.map{|l| r = self[pos, l].cbor_diagnostic(options); pos += l; r}.join(", ")})"
47
47
  else
48
48
  if encoding == Encoding::BINARY
49
- "h'#{hexbytes}'"
49
+ if options[:bytes_as_text] && (u8 = dup.force_encoding(Encoding::UTF_8)).valid_encoding?
50
+ "'#{u8.cbor_diagnostic(options)[1..-2].gsub("'", "\\\\'")}'" # \' is a backref, so needs \\'
51
+ else
52
+ "h'#{hexbytes}'"
53
+ end
50
54
  else
51
- inspect.encode(Encoding::UTF_16BE).bytes.each_slice(2).map {
52
- |c1, c2| c = (c1 << 8)+c2; c < 128 ? c.chr : '\u%04x' % c }.join
55
+ if options[:utf8]
56
+ inspect
57
+ else
58
+ inspect.encode(Encoding::UTF_16BE).bytes.each_slice(2).map {
59
+ |c1, c2| c = (c1 << 8)+c2; c < 128 ? c.chr : '\u%04x' % c }.join
60
+ end
53
61
  end
54
62
  end
55
63
  end
56
64
  end
57
65
 
58
66
  class Array
59
- def cbor_diagnostic
60
- "[#{"_ " if cbor_stream?}#{map(&:cbor_diagnostic).join(", ")}]"
67
+ def cbor_diagnostic(options = {})
68
+ "[#{"_ " if cbor_stream?}#{map {|x| x.cbor_diagnostic(options)}.join(", ")}]"
61
69
  end
62
70
  end
63
71
 
64
72
  class Hash
65
- def cbor_diagnostic
66
- "{#{"_ " if cbor_stream?}#{map{ |k, v| %{#{k.cbor_diagnostic}: #{v.cbor_diagnostic}}}.join(", ")}}"
73
+ def cbor_diagnostic(options = {})
74
+ "{#{"_ " if cbor_stream?}#{map{ |k, v| %{#{k.cbor_diagnostic(options)}: #{v.cbor_diagnostic(options)}}}.join(", ")}}"
67
75
  end
68
76
  end
69
77
 
70
78
  class CBOR::Tagged
71
- def cbor_diagnostic
72
- "#{tag}(#{data.cbor_diagnostic})"
79
+ def cbor_diagnostic(options = {})
80
+ "#{tag}(#{data.cbor_diagnostic(options)})"
73
81
  end
74
82
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cbor-diag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carsten Bormann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-27 00:00:00.000000000 Z
11
+ date: 2017-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -64,6 +64,7 @@ executables:
64
64
  - json2cbor.rb
65
65
  - json2pretty.rb
66
66
  - pretty2cbor.rb
67
+ - pretty2diag.rb
67
68
  - yaml2cbor.rb
68
69
  extensions: []
69
70
  extra_rdoc_files: []
@@ -77,6 +78,7 @@ files:
77
78
  - bin/json2cbor.rb
78
79
  - bin/json2pretty.rb
79
80
  - bin/pretty2cbor.rb
81
+ - bin/pretty2diag.rb
80
82
  - bin/yaml2cbor.rb
81
83
  - cbor-diag.gemspec
82
84
  - lib/cbor-diag-parser.rb