Class: ICU::Normalizer

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

Instance Method Summary collapse

Constructor Details

#initialize(package_name = nil, name = 'nfc', mode = :decompose) ⇒ Normalizer

support for newer ICU normalization API



5
6
7
8
9
# File 'lib/ffi-icu/normalizer.rb', line 5

def initialize(package_name = nil, name = 'nfc', mode = :decompose)
  Lib.check_error do |error|
    @instance = Lib.unorm2_getInstance(package_name, name, mode, error)
  end
end

Instance Method Details

#is_normailzed?(input) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
# File 'lib/ffi-icu/normalizer.rb', line 35

def is_normailzed?(input)
  input_length  = input.jlength
  in_ptr        = UCharPointer.from_string(input)

  Lib.check_error do |error|
    result = Lib.unorm2_isNormalized(@instance, in_ptr, input_length, error)
  end

  result
end

#normalize(input) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ffi-icu/normalizer.rb', line 11

def normalize(input)
  input_length  = input.jlength
  in_ptr        = UCharPointer.from_string(input)
  needed_length = capacity = 0
  out_ptr       = UCharPointer.new(needed_length)

  retried = false
  begin
    Lib.check_error do |error|
      needed_length = Lib.unorm2_normalize(@instance, in_ptr, input_length, out_ptr, capacity, error)
    end
  rescue BufferOverflowError
    raise BufferOverflowError, "needed: #{needed_length}" if retried

    capacity = needed_length
    out_ptr = out_ptr.resized_to needed_length

    retried = true
    retry
  end

  out_ptr.string
end