Module: ICU::Normalization

Defined in:
lib/ffi-icu/normalization.rb

Class Method Summary collapse

Class Method Details

.normalize(input, mode = :default) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ffi-icu/normalization.rb', line 4

def self.normalize(input, mode = :default)
  input_length  = input.jlength
  needed_length = out_length = options = 0
  in_ptr        = UCharPointer.from_string(input)
  out_ptr       = UCharPointer.new(out_length)

  retried = false

  begin
    Lib.check_error do |error|
      needed_length = Lib.unorm_normalize(in_ptr, input_length, mode, options, out_ptr, out_length, error)
    end
  rescue BufferOverflowError
    raise BufferOverflowError, "needed: #{needed_length}" if retried

    out_ptr       = out_ptr.resized_to needed_length
    out_length    = needed_length + 1

    retried = true
    retry
  end

  out_ptr.string
end