Module: ICU::Lib::Util

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

Class Method Summary collapse

Class Method Details

.read_null_terminated_array_of_strings(pointer) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ffi-icu/lib/util.rb', line 6

def self.read_null_terminated_array_of_strings(pointer)
  offset = 0
  result = []

  until (ptr = pointer.get_pointer(offset)).null?
    result << ptr.read_string
    offset += FFI::Pointer.size
  end

  result
end

.read_string_buffer(length) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ffi-icu/lib/util.rb', line 18

def self.read_string_buffer(length)
  attempts = 0

  begin
    result = FFI::MemoryPointer.new(:char, length)
    Lib.check_error { |status| length = yield(result, status) }
  rescue BufferOverflowError
    attempts += 1
    retry if attempts < 2
    raise(BufferOverflowError, "needed: #{length}")
  end

  result.read_string(length)
end

.read_uchar_buffer(length) ⇒ Object



33
34
35
36
# File 'lib/ffi-icu/lib/util.rb', line 33

def self.read_uchar_buffer(length, &)
  buf, len = read_uchar_buffer_as_ptr_impl(length, &)
  buf.string(len)
end

.read_uchar_buffer_as_ptr(length) ⇒ Object



38
39
40
41
# File 'lib/ffi-icu/lib/util.rb', line 38

def self.read_uchar_buffer_as_ptr(length, &)
  buf, = read_uchar_buffer_as_ptr_impl(length, &)
  buf
end

.read_uchar_buffer_as_ptr_impl(length) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ffi-icu/lib/util.rb', line 43

def self.read_uchar_buffer_as_ptr_impl(length)
  attempts = 0

  begin
    result = UCharPointer.new(length)
    Lib.check_error { |status| length = yield(result, status) }
  rescue BufferOverflowError
    attempts += 1
    retry if attempts < 2
    raise(BufferOverflowError, "needed: #{length}")
  end

  [result, length]
end