class String

Public Instance Methods

b() click to toggle source
# File lib/cbor-diagnostic.rb, line 42
def b
  dup.force_encoding(Encoding::BINARY)
end
cbor_diagnostic(options = {}) click to toggle source
# File lib/cbor-diagnostic.rb, line 49
def cbor_diagnostic(options = {})
  if lengths = cbor_stream?
    pos = 0
    "(_ #{lengths.map{|l| r = self[pos, l].cbor_diagnostic(options); pos += l; r}.join(", ")})"
  else
    if encoding == Encoding::BINARY
      if options[:try_decode_embedded] && self != ''
        begin
          rest = self
          result = []
          while rest != ''
            dv, rest = CBOR.decode_with_rest(rest)
            result << dv
          end
          return "<< #{result.map{|x| x.cbor_diagnostic(options)}.join(", ")} >>"
        rescue StandardError => e
          # that didn't work out, so continue with other options
          # puts e.backtrace
        end
      end
      if options[:bytes_as_text] && (u8 = dup.force_encoding(Encoding::UTF_8)).valid_encoding?
        "'#{u8.cbor_diagnostic(options)[1..-2].gsub("'", "\\\\'")}'" # \' is a backref, so needs \\'
      else
        "h'#{hexbytes}'"
      end
    else
      if options[:utf8]
        to_json
      else
        to_json.encode(Encoding::UTF_16BE).bytes.each_slice(2).map {
          |c1, c2| c = (c1 << 8)+c2; c < 128 ? c.chr : '\u%04x' % c }.join
      end
    end
  end
end
hexbytes(sep = '') click to toggle source
# File lib/cbor-diagnostic.rb, line 46
def hexbytes(sep = '')
  bytes.map{|x| "%02X" % x}.join(sep)
end
to_json_514_workaround() click to toggle source
# File lib/cbor-pretty.rb, line 15
def to_json_514_workaround
  ret = to_json
  ret.encode(Encoding::UTF_16BE) # exception if bad
  ret
end