lib/ffi-icu/collation.rb in ffi-icu-0.0.5 vs lib/ffi-icu/collation.rb in ffi-icu-0.0.6

- old
+ new

@@ -1,14 +1,10 @@ module ICU module Collation def self.collate(locale, arr) - collator = Collator.new(locale) - res = collator.collate(arr) - collator.close - - res + Collator.new(locale).collate(arr) end def self.keywords enum_ptr = Lib.check_error { |error| Lib.ucol_getKeywords(error) } keywords = Lib.enum_ptr_to_array(enum_ptr) @@ -32,33 +28,34 @@ class Collator ULOC_VALID_LOCALE = 1 def initialize(locale) - @c = Lib.check_error { |error| Lib.ucol_open(locale, error) } + ptr = Lib.check_error { |error| Lib.ucol_open(locale, error) } + @c = FFI::AutoPointer.new(ptr, Lib.method(:ucol_close)) end def locale Lib.check_error { |error| Lib.ucol_getLocale(@c, ULOC_VALID_LOCALE, error) } end def compare(a, b) Lib.ucol_strcoll( @c, - UCharPointer.from_string(a), a.length, - UCharPointer.from_string(b), b.length + UCharPointer.from_string(a), a.jlength, + UCharPointer.from_string(b), b.jlength ) end def greater?(a, b) - Lib.ucol_greater(@c, UCharPointer.from_string(a), a.length, - UCharPointer.from_string(b), b.length) + Lib.ucol_greater(@c, UCharPointer.from_string(a), a.jlength, + UCharPointer.from_string(b), b.jlength) end def greater_or_equal?(a, b) - Lib.ucol_greaterOrEqual(@c, UCharPointer.from_string(a), a.length, - UCharPointer.from_string(b), b.length) + Lib.ucol_greaterOrEqual(@c, UCharPointer.from_string(a), a.jlength, + UCharPointer.from_string(b), b.jlength) end def equal?(*args) return super() if args.empty? @@ -66,19 +63,19 @@ raise ArgumentError, "wrong number of arguments (#{args.size} for 2)" end a, b = args - Lib.ucol_equal(@c, UCharPointer.from_string(a), a.length, - UCharPointer.from_string(b), b.length) + Lib.ucol_equal(@c, UCharPointer.from_string(a), a.jlength, + UCharPointer.from_string(b), b.jlength) end - def collate(array) - array.sort { |a,b| compare a, b } - end + def collate(sortable) + unless sortable.respond_to?(:sort) + raise ArgumentError, "argument must respond to :sort with arity of 2" + end - def close - Lib.ucol_close(@c) + sortable.sort { |a, b| compare a, b } end end # Collator end # Collate end # ICU