class ICU::CharDet::Detector

Constants

Match

Public Class Methods

new() click to toggle source
# File lib/ffi-icu/chardet.rb, line 11
def initialize
  ptr = Lib.check_error { |err| Lib.ucsdet_open err }
  @detector = FFI::AutoPointer.new(ptr, Lib.method(:ucsdet_close))
end

Public Instance Methods

declared_encoding=(str) click to toggle source
# File lib/ffi-icu/chardet.rb, line 24
def declared_encoding=(str)
  Lib.check_error do |ptr|
    Lib.ucsdet_setDeclaredEncoding(@detector, str, str.bytesize, ptr)
  end
end
detect(str) click to toggle source
# File lib/ffi-icu/chardet.rb, line 30
def detect(str)
  set_text(str)

  match_ptr = Lib.check_error { |ptr| Lib.ucsdet_detect(@detector, ptr) }
  match_ptr_to_ruby(match_ptr) unless match_ptr.null?
end
detect_all(str) click to toggle source
# File lib/ffi-icu/chardet.rb, line 37
def detect_all(str)
  set_text(str)

  matches_found_ptr = FFI::MemoryPointer.new :int32_t
  array_ptr = Lib.check_error do |status|
    Lib.ucsdet_detectAll(@detector, matches_found_ptr, status)
  end

  length = matches_found_ptr.read_int
  array_ptr.read_array_of_pointer(length).map do |match|
    match_ptr_to_ruby(match)
  end
end
detectable_charsets() click to toggle source
# File lib/ffi-icu/chardet.rb, line 51
def detectable_charsets
  enum_ptr = Lib.check_error do |ptr|
    Lib.ucsdet_getAllDetectableCharsets(@detector, ptr)
  end

  result = Lib.enum_ptr_to_array(enum_ptr)
  Lib.uenum_close(enum_ptr)

  result
end
input_filter_enabled=(bool) click to toggle source
# File lib/ffi-icu/chardet.rb, line 20
def input_filter_enabled=(bool)
  Lib.ucsdet_enableInputFilter(@detector, !!bool)
end
input_filter_enabled?() click to toggle source
# File lib/ffi-icu/chardet.rb, line 16
def input_filter_enabled?
  Lib.ucsdet_isInputFilterEnabled @detector
end

Private Instance Methods

match_ptr_to_ruby(match_ptr) click to toggle source
# File lib/ffi-icu/chardet.rb, line 64
def match_ptr_to_ruby(match_ptr)
  result = Match.new

  result.name       = Lib.check_error { |ptr| Lib.ucsdet_getName(match_ptr, ptr) }
  result.confidence = Lib.check_error { |ptr| Lib.ucsdet_getConfidence(match_ptr, ptr) }
  result.language   = Lib.check_error { |ptr| Lib.ucsdet_getLanguage(match_ptr, ptr) }

  result
end
set_text(text) click to toggle source
# File lib/ffi-icu/chardet.rb, line 74
def set_text(text)
  Lib.check_error do |status|
    data = FFI::MemoryPointer.from_string(text)
    Lib.ucsdet_setText(@detector, data, text.bytesize, status)
  end
end