class CF::String

Wrapper class for CFString

Unlike ruby, CFString is not an arbitrary bag of bytes - the data will be converted to a collection of unicode characters

Constants

UTF8

The cfstring encoding for UTF8

Public Class Methods

from_string(s) click to toggle source

Creates a string from a ruby string The string must be convertable to UTF-8

@param [String] s @return [CF::String]

# File lib/corefoundation/string.rb, line 28
def self.from_string(s)
  s_utf = s.encode("UTF-8")
  raw = CF.CFStringCreateWithBytes(nil, s_utf, s_utf.bytesize, UTF8, 0)
  raw.null? ? nil : new(raw)
end

Public Instance Methods

<=>(other) click to toggle source

Compares the receiver with the argument @param [CF::String] other @return [Integer]

# File lib/corefoundation/string.rb, line 43
def <=>(other)
  Base.check_cftype(other)
  CF.CFStringCompare(self,other,0)
end
length() click to toggle source

Returns the length, in unicode characters of the string @return [Integer]

# File lib/corefoundation/string.rb, line 36
def length
  CF.CFStringGetLength(self)
end
to_ruby()
Alias for: to_s
to_s() click to toggle source

Converts the CF::String to a UTF-8 ruby string

@return [String]

# File lib/corefoundation/string.rb, line 51
def to_s
  max_size = CF.CFStringGetMaximumSizeForEncoding(length, UTF8)
  range = CF::Range.new
  range[:location] = 0
  range[:length] = length
  buffer = FFI::MemoryPointer.new(:char, max_size)
  cfindex = CF.find_type(:cfindex)
  bytes_used_buffer = FFI::MemoryPointer.new(cfindex)

  CF.CFStringGetBytes(self, range, UTF8, 0, 0, buffer, max_size, bytes_used_buffer)
  len = bytes_used_buffer.send(cfindex == CF.find_type(:long_long) ? :read_long_long : :read_long)

  buffer.read_string(len).force_encoding(Encoding::UTF_8)
end
Also aliased as: to_ruby