Sha256: 53055c9893b582934f498b9168f47c6856cbe1ae0f9d7187166c4a51a6320e6d

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

module ICU
  class BreakIterator
    include Enumerable

    UBRK_DONE = -1

    def self.available_locales
      (0...Lib.ubrk_countAvailable).map do |idx|
        Lib.ubrk_getAvailable idx
      end
    end

    def initialize(type, locale)
      ptr = Lib.check_error { |err| Lib.ubrk_open(type, locale, nil, 0, err) }

      @iterator = FFI::AutoPointer.new(ptr, Lib.method(:ubrk_close))
    end

    def text=(str)
      Lib.check_error { |err|
        Lib.ubrk_setText @iterator, UCharPointer.from_string(str), str.length, err
      }
    end

    def each(&blk)
      int = first

      while int != UBRK_DONE
        yield int
        int = self.next
      end
    end

    def next
      Lib.ubrk_next @iterator
    end

    def previous
      Lib.ubrk_next @iterator
    end

    def first
      Lib.ubrk_first @iterator
    end

    def last
      Lib.ubrk_last @iterator
    end

    def preceding(offset)
      Lib.ubrk_preceding @iterator, Integer(offset)
    end

    def following(offset)
      Lib.ubrk_following @iterator, Integer(offset)
    end

    def current
      Lib.ubrk_current @iterator
    end

    def boundary?(offset)
      Lib.ubrk_isBoundary(@iterator, Integer(offset)) != 0
    end

  end # BreakIterator
end # ICU

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ffi-icu-0.0.5 lib/ffi-icu/break_iterator.rb
ffi-icu-0.0.4 lib/ffi-icu/break_iterator.rb