Class: ICU::CharDet::Detector

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi-icu/chardet.rb

Defined Under Namespace

Classes: Match

Instance Method Summary collapse

Constructor Details

#initializeDetector

Returns a new instance of Detector.



11
12
13
14
# 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

Instance Method Details

#declared_encoding=(str) ⇒ Object



24
25
26
27
28
# 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) ⇒ Object



30
31
32
33
34
35
# 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) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# 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_charsetsObject



51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



20
21
22
# File 'lib/ffi-icu/chardet.rb', line 20

def input_filter_enabled=(bool)
  Lib.ucsdet_enableInputFilter(@detector, !!bool)
end

#input_filter_enabled?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/ffi-icu/chardet.rb', line 16

def input_filter_enabled?
  Lib.ucsdet_isInputFilterEnabled @detector
end