<commit_before>

module CF
  extend FFI::Library
  ffi_lib '/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation'

  if FFI::Platform::ARCH == 'x86_64'
    typedef :long_long, :cfindex
    typedef :long_long, :cfcomparisonresult
    typedef :ulong_long, :cfoptionflags
    typedef :ulong_long, :cftypeid
    typedef :ulong_long, :cfhashcode
  else
    typedef :long, :cfindex
    typedef :long, :cfcomparisonresult
    typedef :ulong, :cfoptionflags
    typedef :ulong, :cftypeid
    typedef :ulong, :cfhashcode
  end


  class Range < FFI::Struct
    layout :location, :cfindex,
           :length, :cfindex
  end

  typedef :pointer, :cftyperef

  #general utility functions
  attach_function :show, 'CFShow', [:cftyperef], :void
  attach_function :release, 'CFRelease', [:cftyperef], :void
  attach_function :retain, 'CFRetain', [:cftyperef], :cftyperef
  attach_function 'CFEqual', [:cftyperef, :cftyperef], :char
  attach_function 'CFHash', [:cftyperef], :cfhashcode
  attach_function 'CFCopyDescription', [:cftyperef], :cftyperef
  attach_function 'CFGetTypeID', [:cftyperef], :cftypeid

  class Base < FFI::AutoPointer  
    @@type_map = {}


    class << self
      def check_cftype(cftyperef)
        raise TypeError, "#{cftyperef.inspect} is not a cftype" unless cftyperef.is_a?(CF::Base)
      end

      def register_type(type_name)
        CF.attach_function "#{type_name}GetTypeID", [], :cftypeid
        @@type_map[CF.send("#{type_name}GetTypeID")] = self
      end


      def typecast_wrap(cftyperef)
        klass = klass_from_cf_type cftyperef
        klass.wrap(cftyperef)
      end

      def typecast_wrap_retaining(cftyperef)
        klass = klass_from_cf_type cftyperef
        klass.wrap_retaining(cftyperef)
      end

      def klass_from_cf_type cftyperef
        klass = @@type_map[CF.CFGetTypeID(cftyperef)]
        if !klass
          raise TypeError, "No class registered for cf type #{cftyperef.inspect}"
        end
        klass
      end

      def wrap(cftyperef)
        new(cftyperef, CF.method(:release))
      end

      def wrap_retaining(cftyperef)
        new(CF.retain(cftyperef), CF.method(:release))
      end
    end

    def inspect
      CF::String.wrap(CF.CFCopyDescription(self)).to_s
    end

    def hash
      CF.CFHash(self)
    end

    def eql?(other)
      if other.is_a?(CF::Base)
        CF.CFEqual(self, other) != 0
      else
        false
      end
    end
    alias_method :==, :eql?

    
  end

  attach_variable 'kCFBooleanTrue', :pointer
  attach_variable 'kCFBooleanFalse', :pointer

  class Boolean < Base
    register_type("CFBoolean")
    TRUE = wrap_retaining(CF.kCFBooleanTrue)
    FALSE = wrap_retaining(CF.kCFBooleanFalse)
  end

end

require 'keychain/cf/string'
require 'keychain/cf/data'
require 'keychain/cf/array'
require 'keychain/cf/dictionary'<commit_msg>define .equals? as whether the pointer has same address<commit_after>

module CF
  extend FFI::Library
  ffi_lib '/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation'

  if FFI::Platform::ARCH == 'x86_64'
    typedef :long_long, :cfindex
    typedef :long_long, :cfcomparisonresult
    typedef :ulong_long, :cfoptionflags
    typedef :ulong_long, :cftypeid
    typedef :ulong_long, :cfhashcode
  else
    typedef :long, :cfindex
    typedef :long, :cfcomparisonresult
    typedef :ulong, :cfoptionflags
    typedef :ulong, :cftypeid
    typedef :ulong, :cfhashcode
  end


  class Range < FFI::Struct
    layout :location, :cfindex,
           :length, :cfindex
  end

  typedef :pointer, :cftyperef

  #general utility functions
  attach_function :show, 'CFShow', [:cftyperef], :void
  attach_function :release, 'CFRelease', [:cftyperef], :void
  attach_function :retain, 'CFRetain', [:cftyperef], :cftyperef
  attach_function 'CFEqual', [:cftyperef, :cftyperef], :char
  attach_function 'CFHash', [:cftyperef], :cfhashcode
  attach_function 'CFCopyDescription', [:cftyperef], :cftyperef
  attach_function 'CFGetTypeID', [:cftyperef], :cftypeid

  class Base < FFI::AutoPointer  
    @@type_map = {}


    class << self
      def check_cftype(cftyperef)
        raise TypeError, "#{cftyperef.inspect} is not a cftype" unless cftyperef.is_a?(CF::Base)
      end

      def register_type(type_name)
        CF.attach_function "#{type_name}GetTypeID", [], :cftypeid
        @@type_map[CF.send("#{type_name}GetTypeID")] = self
      end


      def typecast_wrap(cftyperef)
        klass = klass_from_cf_type cftyperef
        klass.wrap(cftyperef)
      end

      def typecast_wrap_retaining(cftyperef)
        klass = klass_from_cf_type cftyperef
        klass.wrap_retaining(cftyperef)
      end

      def klass_from_cf_type cftyperef
        klass = @@type_map[CF.CFGetTypeID(cftyperef)]
        if !klass
          raise TypeError, "No class registered for cf type #{cftyperef.inspect}"
        end
        klass
      end

      def wrap(cftyperef)
        new(cftyperef, CF.method(:release))
      end

      def wrap_retaining(cftyperef)
        new(CF.retain(cftyperef), CF.method(:release))
      end
    end

    def inspect
      CF::String.wrap(CF.CFCopyDescription(self)).to_s
    end

    def hash
      CF.CFHash(self)
    end

    def eql?(other)
      if other.is_a?(CF::Base)
        CF.CFEqual(self, other) != 0
      else
        false
      end
    end
    
    def equals?(other)
      if other.is_a?(CF::Base)
        address == other.address
      else
        false
      end
    end
    
    alias_method :==, :eql?


  end

  attach_variable 'kCFBooleanTrue', :pointer
  attach_variable 'kCFBooleanFalse', :pointer

  class Boolean < Base
    register_type("CFBoolean")
    TRUE = wrap_retaining(CF.kCFBooleanTrue)
    FALSE = wrap_retaining(CF.kCFBooleanFalse)
  end

end

require 'keychain/cf/string'
require 'keychain/cf/data'
require 'keychain/cf/array'
require 'keychain/cf/dictionary'
{'fraction_non_alphanumeric': 0.10397553516819572, 'fraction_numerical': 0.0016989466530750934, 'mean_word_length': 3.119664100769769, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '187819', 'n_tokens_mistral': 2117, 'n_tokens_neox': 1965, 'n_words': 427}