More broken collation code

This commit is contained in:
Jari Bakken 2010-05-10 21:39:28 +02:00
parent 93bdd92fad
commit a0241bf6e6
6 changed files with 104 additions and 47 deletions

View File

@ -1,4 +1,9 @@
require "ffi"
require "ffi-icu/lib"
require "ffi-icu/uchar"
require "ffi-icu/chardet"
require "ffi-icu/collate"
require "ffi-icu/collation"
module ICU
end

View File

@ -1,39 +0,0 @@
module ICU
module Collate
def self.collate(locale, arr)
collator = new(locale)
res = collator.collate(arr)
collator.close
res
end
def self.available_locales
enum_ptr = Lib.check_error { |err| Lib.ucol_openAvailableLocales(err) }
res = Lib.enum_ptr_to_array(enum_ptr)
Lib.enum_close(enum_ptr)
res
end
class Collator
def initialize(locale)
@c = Lib.check_error { |error| Lib.ucol_open(locale, error) }
end
def collate(array)
array.sort { |a,b| r=Lib.ucol_strcoll(@c, a, a.bytesize, b, b.bytesize); p [a,b,r]; r }
end
def close
Libu.ucol_close(@c)
end
end
end # Collate
end # ICU

66
lib/ffi-icu/collation.rb Normal file
View File

@ -0,0 +1,66 @@
module ICU
module Collation
def self.collate(locale, arr)
collator = new(locale)
res = collator.collate(arr)
collator.close
res
end
def self.keywords
enum_ptr = Lib.check_error { |error| Lib.ucol_getKeywords(error) }
keywords = Lib.enum_ptr_to_array(enum_ptr)
Lib.uenum_close enum_ptr
hash = {}
keywords.each do |keyword|
enum_ptr = Lib.check_error { |error| Lib.ucol_getKeywordValues(keyword, error) }
hash[keyword] = Lib.enum_ptr_to_array(enum_ptr)
Lib.uenum_close(enum_ptr)
end
hash
end
def self.keyword_values()
end
#
# def self.available_locales
# enum_ptr = Lib.check_error { |err| Lib.ucol_openAvailableLocales(err) }
# res = Lib.enum_ptr_to_array(enum_ptr)
# Lib.enum_close(enum_ptr)
#
# res
# end
class Collator
def initialize(locale)
@c = Lib.check_error { |error| Lib.ucol_open(locale, error) }
end
def collate(array)
array.sort do |a,b|
r = Lib.ucol_strcollIter(
@c,
UCharIteratorPointer.from_string(a), a.bytesize,
UCharIteratorPointer.from_string(b), b.bytesize
)
p [a,b,r]
r
end
end
def close
Libu.ucol_close(@c)
end
end
end # Collate
end # ICU

View File

@ -46,9 +46,14 @@ module ICU
attach_function "ucol_open#{suffix}", :ucol_open, [:string, :pointer], :pointer
attach_function "ucol_close#{suffix}", :ucol_close, [:pointer], :void
attach_function "ucol_strcoll#{suffix}", :ucol_strcoll, [:pointer, :string, :int32, :string, :int32], :int
# attach_function "ucol_openAvailableLocales#{suffix}", :ucol_openAvailableLocales, [:pointer], :pointer
attach_function "ucol_strcoll#{suffix}", :ucol_strcoll, [:pointer, :pointer, :int32, :pointer, :int32], :int
attach_function "ucol_strcollIter#{suffix}", :ucol_strcollIter, [:pointer, :pointer, :pointer], :int
attach_function "ucol_getKeywords#{suffix}", :ucol_getKeywords, [:pointer], :pointer
attach_function "ucol_getKeywordValues#{suffix}", :ucol_getKeywords, [:string, :pointer], :pointer
attach_function "ucol_openAvailableLocales#{suffix}", :ucol_openAvailableLocales, [:pointer], :pointer
attach_function "uiter_setUTF8#{suffix}", :uiter_setUTF8, [:pointer, :string, :int32], :void
def self.check_error
ptr = FFI::MemoryPointer.new(:int)
@ -72,6 +77,5 @@ module ICU
end
end
end # Functions
end # Lib
end # ICU

21
lib/ffi-icu/uchar.rb Normal file
View File

@ -0,0 +1,21 @@
module ICU
class UCharPointer < FFI::MemoryPointer
def self.from_string(str)
bytes = str.bytes.to_a
ptr = new(:uint16, bytes.size)
ptr.put_array_of_uint16(0, bytes)
ptr
end
end
# class UCharIteratorPointer < FFI::MemoryPointer
# def self.from_string(str)
# ptr = new(:pointer, 4)
# Lib.uiter_setUTF8(ptr, str, str.length)
# ptr
# end
# end
end

View File

@ -2,10 +2,10 @@
require 'spec_helper'
module ICU::Collate
module ICU::Collation
describe Collator do
before { @d = Collator.new("no") }
before { @d = Collator.new("NO") }
after { @d.close }
it "should collate an array of strings" do