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



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

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_normalized?(input) ⇒ Boolean

rubocop:disable Naming/PredicatePrefix

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/ffi-icu/normalizer.rb', line 48

def is_normalized?(input) # rubocop:disable Naming/PredicatePrefix
  Warning.warn('is_normalized? is deprecated and will be removed after v0.7. Please use normalized? instead.')
  normalized?(input)
end

#normailzed?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def normailzed?(input)
  input_length  = input.size
  in_ptr        = UCharPointer.from_string(input)

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

  result
end

#normalize(input) ⇒ Object



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

def normalize(input)
  input_length  = input.size
  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