class ICU::Normalizer

Public Class Methods

new(package_name = nil, name = 'nfc', mode = :decompose) click to toggle source

support for newer ICU normalization API

# 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

Public Instance Methods

is_normailzed?(input) click to toggle source
# 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) click to toggle source
# 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