Class: CoreFoundation::CFStringRef

Inherits:
FFI::Pointer
  • Object
show all
Defined in:
lib/core_foundation.rb

Instance Method Summary collapse

Instance Method Details

#lengthCFIndex

Returns the length of the referenced CFString.

Returns:

  • (CFIndex)

    the length of the referenced CFString



47
48
49
# File 'lib/core_foundation.rb', line 47

def length
    CoreFoundation.CFStringGetLength(self)
end

#max_sizeCFIndex

Returns the maximum size of the buffer that will hold the string.

Returns:

  • (CFIndex)

    the maximum size of the buffer that will hold the string



52
53
54
# File 'lib/core_foundation.rb', line 52

def max_size
    CoreFoundation.CFStringGetMaximumSizeForEncoding(length, CFStringEncoding[:UTF8])
end

#to_sString

Returns the CFString, converted to a UTF-8 string.

Returns:

  • (String)

    the CFString, converted to a UTF-8 string



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/core_foundation.rb', line 57

def to_s
    buffer = FFI::MemoryPointer.new(:char, max_size)
    used_bytes = FFI::MemoryPointer.new(CFIndex)
    CoreFoundation.CFStringGetBytes(self,
            CFRange.make(location:0, length:length),
            CFStringEncoding[:UTF8],
            0,
            false,
            buffer,
            buffer.size,
            used_bytes)

    used_bytes = if CFIndex == CoreFoundation.find_type(:long_long)
  used_bytes.read_long_long
    else
  used_bytes.read_long
    end

    buffer.read_string(used_bytes).force_encoding(Encoding::UTF_8)
end