Sha256: 100df95142baa4c71629d3896c78859f328a97df8903f7c2f52f70b205ef627d

Contents?: true

Size: 1.4 KB

Versions: 6

Compression:

Stored size: 1.4 KB

Contents

module ICU
  module Lib
    module Util
      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

      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

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

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

      private

      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
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ffi-icu-0.5.3 lib/ffi-icu/lib/util.rb
ffi-icu-0.5.2 lib/ffi-icu/lib/util.rb
ffi-icu-0.5.1 lib/ffi-icu/lib/util.rb
ffi-icu-0.5.0 lib/ffi-icu/lib/util.rb
ffi-icu-0.4.3 lib/ffi-icu/lib/util.rb
ffi-icu-0.4.2 lib/ffi-icu/lib/util.rb