Class: CoreFoundation::CFStringRef

Inherits:
FFI::Pointer
  • Object
show all
Defined in:
lib/macos/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



49
50
51
# File 'lib/macos/core_foundation.rb', line 49

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



54
55
56
# File 'lib/macos/core_foundation.rb', line 54

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



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

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