Sha256: f392eda97a55a884266f3fbd17f7b14f5019a56e752b0bb11d16f392f002af07

Contents?: true

Size: 1.71 KB

Versions: 24

Compression:

Stored size: 1.71 KB

Contents

module ICU
  class BreakIterator
    include Enumerable

    attr_reader :text

    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)
      @text = str

      Lib.check_error { |err|
        Lib.ubrk_setText @iterator, UCharPointer.from_string(str), str.jlength, err
      }
    end

    def each(&blk)
      return to_enum(:each) unless block_given?

      int = first

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

      self
    end

    def each_substring(&blk)
      return to_enum(:each_substring) unless block_given?

      # each_char needed for 1.8, where String#[] works on bytes, not characters
      chars = text.each_char.to_a
      low   = first

      while (high = self.next) != DONE
        yield chars[low...high].join
        low = high
      end

      self
    end

    def substrings
      each_substring.to_a
    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

24 entries across 24 versions & 1 rubygems

Version Path
ffi-icu-0.5.3 lib/ffi-icu/break_iterator.rb
ffi-icu-0.5.2 lib/ffi-icu/break_iterator.rb
ffi-icu-0.5.1 lib/ffi-icu/break_iterator.rb
ffi-icu-0.5.0 lib/ffi-icu/break_iterator.rb
ffi-icu-0.4.3 lib/ffi-icu/break_iterator.rb
ffi-icu-0.4.2 lib/ffi-icu/break_iterator.rb
ffi-icu-0.4.1 lib/ffi-icu/break_iterator.rb
ffi-icu-0.4.0 lib/ffi-icu/break_iterator.rb
ffi-icu-0.3.0 lib/ffi-icu/break_iterator.rb
ffi-icu-0.2.0 lib/ffi-icu/break_iterator.rb
ffi-icu-0.1.10 lib/ffi-icu/break_iterator.rb
ffi-icu-0.1.9 lib/ffi-icu/break_iterator.rb
ffi-icu-0.1.8 lib/ffi-icu/break_iterator.rb
ffi-icu-0.1.7 lib/ffi-icu/break_iterator.rb
ffi-icu-0.1.6 lib/ffi-icu/break_iterator.rb
ffi-icu-0.1.5 lib/ffi-icu/break_iterator.rb
ffi-icu-0.1.4 lib/ffi-icu/break_iterator.rb
ffi-icu-0.1.3 lib/ffi-icu/break_iterator.rb
ffi-icu-0.1.2 lib/ffi-icu/break_iterator.rb
ffi-icu-0.1.1 lib/ffi-icu/break_iterator.rb