Module: CBOR::StringHelper

Defined in:
lib/libcbor/helpers.rb

Overview

Provides the #to_cbor (or equivalent) method for Stringss

Instance Method Summary collapse

Instance Method Details

#__libcbor_to_cborString

Encodes Strings. The result is always a definite string.

The string is assumed to be a valid UTF-8 string. The precondition is not verified.

Returns:

  • (String)

    The CBOR representation



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/libcbor/helpers.rb', line 36

def __libcbor_to_cbor
  @@item ||= LibCBOR.cbor_new_definite_string
  string = FFI::MemoryPointer.from_string(self)
  out_bfr = FFI::MemoryPointer.new :pointer
  out_bfr_len = FFI::MemoryPointer.new :size_t
  LibCBOR.cbor_string_set_handle(@@item, string, bytes.length)
  res_len = LibCBOR.cbor_serialize_alloc(@@item, out_bfr, out_bfr_len)
  out_bfr.read_pointer.get_bytes(0, res_len).tap do
    LibC.free(out_bfr.read_pointer)
  end
end