More collation functions + fix README

This commit is contained in:
Jari Bakken 2010-05-11 01:34:41 +02:00
parent bad7dae65c
commit 02cca9a9c8
4 changed files with 60 additions and 10 deletions

View File

@ -30,7 +30,16 @@ or
== Locale Sensitive Collation
Todo.
=== Examples:
ICU::Collation.collate("nb", %w[å æ ø]) == %w[æ ø å] #=> true
or
collator = ICU::Collation::Collator.new("nb")
collator.compare("a", "b") #=> -1
collator.greater?("z", "a") #=> true
collator.collate(%w[å æ ø]) #=> ["æ", "ø", "å"]
= Tested on:

View File

@ -41,14 +41,32 @@ module ICU
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
)
end
def greater?(a, b)
Lib.ucol_greater(@c, UCharPointer.from_string(a), a.length,
UCharPointer.from_string(b), b.length)
end
def greater_or_equal?(a, b)
Lib.ucol_greaterOrEqual(@c, UCharPointer.from_string(a), a.length,
UCharPointer.from_string(b), b.length)
end
# can't override Object#equal? - suggestions welcome
def same?(a, b)
Lib.ucol_equal(@c, UCharPointer.from_string(a), a.length,
UCharPointer.from_string(b), b.length)
end
def collate(array)
array.sort do |a,b|
Lib.ucol_strcoll(
@c,
UCharPointer.from_string(a), a.size,
UCharPointer.from_string(b), b.size
)
end
array.sort { |a,b| compare a, b }
end
def close

View File

@ -104,7 +104,9 @@ module ICU
attach_function :ucol_getAvailable, "ucol_getAvailable#{suffix}", [:int32], :string
attach_function :ucol_countAvailable, "ucol_countAvailable#{suffix}", [], :int32
attach_function :ucol_getLocale, "ucol_getLocale#{suffix}", [:pointer, :int, :pointer], :string
attach_function :ucol_greater, "ucol_greater#{suffix}", [:pointer, :pointer, :int32, :pointer, :int32], :bool
attach_function :ucol_greaterOrEqual, "ucol_greaterOrEqual#{suffix}", [:pointer, :pointer, :int32, :pointer, :int32], :bool
attach_function :ucol_equal, "ucol_equal#{suffix}", [:pointer, :pointer, :int32, :pointer, :int32], :bool
end # Lib
end # ICU

View File

@ -25,6 +25,27 @@ module ICU
l.should == "nb"
end
it "should compare two strings" do
@c.compare("blåbærsyltetøy", "blah").should == 1
@c.compare("blah", "blah").should == 0
@c.compare("baah", "blah").should == -1
end
it "should know if a string is greater than another" do
@c.should be_greater("z", "a")
@c.should_not be_greater("a", "z")
end
it "should know if a string is greater or equal to another" do
@c.should be_greater_or_equal("z", "a")
@c.should be_greater_or_equal("z", "z")
@c.should_not be_greater_or_equal("a", "z")
end
it "should know if a string is equal to another" do
@c.should be_same("a", "a")
end
end
end # Collate
end # ICU
end # ICU