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 +4 -4
- data/bin/pretty2diag.rb +12 -0
- data/cbor-diag.gemspec +1 -1
- data/lib/cbor-diagnostic.rb +22 -14
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d862488b37e86df16ba27e69a7e2bdb28c0fb039
|
4
|
+
data.tar.gz: bf923d105ff2c9770430b0ac401720daff93573b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0ac9fda4585effeae900c06727e737c33d20872f0c7ed1703d428cceb80c36d5289d8d06a6dfda1b6f8d2e905888cb29a7edd54942d1eb556ed48e739e38a39
|
7
|
+
data.tar.gz: 6e76c59d8011d343a9d1d8e90cc14d9f1e4f1b4a886c006ae30bd5eb36eddb7997301a48b060846e4e811c45a5e539040e9117088e32d47c3c7c831839526b31
|
data/bin/pretty2diag.rb
ADDED
@@ -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.
|
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"
|
data/lib/cbor-diagnostic.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
52
|
-
|
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(
|
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.
|
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-
|
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
|