Yet another rename (more consistent with other FFI projects)

This commit is contained in:
Jari Bakken 2010-05-10 20:28:20 +02:00
parent c1bce6fe5b
commit 93bdd92fad
11 changed files with 62 additions and 47 deletions

View File

@ -1,4 +1,4 @@
= icu-ffi
= ffi-icu
Simple FFI wrappers for things I need from ICU.
@ -13,26 +13,24 @@ ICU - you might need to hack the ffi_lib call to make it work. Please send a pat
=== Examples:
match = ICU::CharDet.detect(str)
match.name # => String
match.confidence # => Integer
match.name # => "UTF-8"
match.confidence # => 80
or
detector = ICU::CharDet::Detector.new
detector.detect(str) #=> match
detector.detect(str)
detector.close
== Locale Sensitive Collation
Todo.
=== Why not just use rchardet?
* this is faster
* rchardet does not work well on 1.9
* none of the rchardet forks claiming to work on 1.9 actually does
== Locale Sensitive Collation
Todo.
= Tested on:

View File

@ -4,11 +4,11 @@ require 'rake'
begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "icu-ffi"
gem.name = "ffi-icu"
gem.summary = %Q{Simple FFI wrappers for things I need from ICU.}
gem.description = %Q{Simple FFI wrappers for things I need from ICU.}
gem.email = "jari.bakken@gmail.com"
gem.homepage = "http://github.com/jarib/icu-ffi"
gem.homepage = "http://github.com/jarib/ffi-icu"
gem.authors = ["Jari Bakken"]
gem.add_dependency "ffi", ">= 0.6.3"

View File

@ -3,12 +3,12 @@
require "benchmark"
$LOAD_PATH.unshift "lib"
require "icu-ffi"
require "ffi-icu"
require "rchardet"
TESTS = 1000
Benchmark.bmbm do |results|
results.report("rchardet:") { TESTS.times { CharDet.detect("æåø") } }
results.report("icu-ffi:") { TESTS.times { ICU::CharDet.detect("æåø") } }
results.report("ffi-icu:") { TESTS.times { ICU::CharDet.detect("æåø") } }
end

View File

@ -3,7 +3,7 @@
require "benchmark"
$LOAD_PATH.unshift "lib"
require "icu-ffi"
require "ffi-icu"
require "rchardet"
TESTS = 1000
@ -13,5 +13,5 @@ $icu = ICU::CharDet::Detector.new
Benchmark.bmbm do |results|
results.report("rchardet instance:") { TESTS.times { $rchardet.reset; $rchardet.feed("æåø"); $rchardet.result } }
results.report("icu-ffi instance:") { TESTS.times { $icu.detect("æåø") } }
results.report("ffi-icu instance:") { TESTS.times { $icu.detect("æåø") } }
end

4
lib/ffi-icu.rb Normal file
View File

@ -0,0 +1,4 @@
require "ffi"
require "ffi-icu/lib"
require "ffi-icu/chardet"
require "ffi-icu/collate"

View File

@ -44,7 +44,7 @@ module ICU
def detect_all(str)
set_text(str)
matches_found_ptr = FFI::MemoryPointer.new :int
matches_found_ptr = FFI::MemoryPointer.new :int32
array_ptr = Lib.check_error do |status|
Lib.ucsdet_detectAll(@detector, matches_found_ptr, status)
end

39
lib/ffi-icu/collate.rb Normal file
View File

@ -0,0 +1,39 @@
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

View File

@ -28,12 +28,12 @@ module ICU
attach_function "ucsdet_open#{suffix}", :ucsdet_open, [:pointer], :pointer
attach_function "ucsdet_close#{suffix}", :ucsdet_close, [:pointer], :void
attach_function "ucsdet_setText#{suffix}", :ucsdet_setText, [:pointer, :string, :int, :pointer], :void
attach_function "ucsdet_setDeclaredEncoding#{suffix}", :ucsdet_setDeclaredEncoding, [:pointer, :string, :int, :pointer], :void
attach_function "ucsdet_setText#{suffix}", :ucsdet_setText, [:pointer, :string, :int32, :pointer], :void
attach_function "ucsdet_setDeclaredEncoding#{suffix}", :ucsdet_setDeclaredEncoding, [:pointer, :string, :int32, :pointer], :void
attach_function "ucsdet_detect#{suffix}", :ucsdet_detect, [:pointer, :pointer], :pointer
attach_function "ucsdet_detectAll#{suffix}", :ucsdet_detectAll, [:pointer, :pointer, :pointer], :pointer
attach_function "ucsdet_getName#{suffix}", :ucsdet_getName, [:pointer, :pointer], :string
attach_function "ucsdet_getConfidence#{suffix}", :ucsdet_getConfidence, [:pointer, :pointer], :int
attach_function "ucsdet_getConfidence#{suffix}", :ucsdet_getConfidence, [:pointer, :pointer], :int32
attach_function "ucsdet_getLanguage#{suffix}", :ucsdet_getLanguage, [:pointer, :pointer], :string
attach_function "ucsdet_getAllDetectableCharsets#{suffix}", :ucsdet_getAllDetectableCharsets, [:pointer, :pointer], :pointer
attach_function "ucsdet_isInputFilterEnabled#{suffix}", :ucsdet_isInputFilterEnabled, [:pointer], :bool
@ -46,7 +46,8 @@ 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
def self.check_error

View File

@ -1,4 +0,0 @@
require "ffi"
require "icu-ffi/lib"
require "icu-ffi/chardet"
require "icu-ffi/collate"

View File

@ -1,23 +0,0 @@
module ICU
module Collate
def collate(locale, arr)
collator = new(locale)
res = collator.colate(arr)
collator.close
res
end
class Collator
def initialize(locale)
end
end
end # Collate
end # ICU

View File

@ -2,7 +2,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require "rubygems"
require 'icu-ffi'
require 'ffi-icu'
require 'spec'
require 'spec/autorun'