From 5f25aa314484f8bbec1f45f4e1011e164c45aa76 Mon Sep 17 00:00:00 2001 From: Michael Richardson Date: Mon, 27 Jun 2022 14:52:00 -0400 Subject: [PATCH] move extractbytes into CBOR module, renaming to extract_bytes_from_hex, so it can be re-used and others can parse pretty --- bin/pretty2cbor.rb | 7 +------ bin/pretty2diag.rb | 7 +------ lib/cbor-pretty.rb | 7 ++++++- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/bin/pretty2cbor.rb b/bin/pretty2cbor.rb index 61f4d7e..4958b7e 100755 --- a/bin/pretty2cbor.rb +++ b/bin/pretty2cbor.rb @@ -2,9 +2,4 @@ require 'cbor-pure' require 'cbor-pretty' - -def extractbytes(s) - 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 -end - -print(extractbytes(ARGF)) +print(CBOR.extract_bytes_from_hex(ARGF)) diff --git a/bin/pretty2diag.rb b/bin/pretty2diag.rb index 3d12651..ec0d6e8 100755 --- a/bin/pretty2diag.rb +++ b/bin/pretty2diag.rb @@ -3,15 +3,10 @@ require 'cbor-diagnostic' -def extractbytes(s) - 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 -end - - require 'cbor-diagnostic-helper' options = cbor_diagnostic_process_args("cdetpqu") -i = extractbytes(ARGF) +i = CBOR.extract_bytes_from_hex(ARGF) o = CBOR.decode(i) puts cbor_diagnostic_output(o, options) diff --git a/lib/cbor-pretty.rb b/lib/cbor-pretty.rb index 3c01f68..5545373 100644 --- a/lib/cbor-pretty.rb +++ b/lib/cbor-pretty.rb @@ -17,6 +17,11 @@ def hexbytes(sep = '') module CBOR + + def self.extract_bytes_from_hex(s) + 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 + end + def self.pretty(s, indent = 0, max_target = 40) Buffer.new(s).pretty_item_final(indent, max_target) end @@ -38,7 +43,7 @@ def take_and_print(n, prefix = '') @out << s.hexbytes s end - + def pretty_item_streaming(ib) res = nil @out << " # #{MT_NAMES[ib >> 5]}(*)\n"