add Collator#collation_key method

similar to direct comparison, but more efficient if you need to compare
the same string multiple times
This commit is contained in:
Cody Cutrer 2016-01-29 09:18:52 -07:00
parent 246b13f4eb
commit e1f9626bd1
3 changed files with 13 additions and 1 deletions

View File

@ -80,10 +80,18 @@ module ICU
def rules
@rules ||= begin
length = FFI::MemoryPointer.new(:int)
ptr = ICU::Lib.ucol_getRules(@c, length)
ptr = Lib.ucol_getRules(@c, length)
ptr.read_array_of_uint16(length.read_int).pack("U*")
end
end
def collation_key(string)
ptr = UCharPointer.from_string(string)
size = Lib.ucol_getSortKey(@c, ptr, string.jlength, nil, 0)
buffer = FFI::MemoryPointer.new(:char, size)
Lib.ucol_getSortKey(@c, ptr, string.jlength, buffer, size)
buffer.read_bytes(size - 1)
end
end # Collator
end # Collate

View File

@ -290,6 +290,7 @@ module ICU
attach_function :ucol_greaterOrEqual, "ucol_greaterOrEqual#{suffix}", [:pointer, :pointer, :int32_t, :pointer, :int32_t], :bool
attach_function :ucol_equal, "ucol_equal#{suffix}", [:pointer, :pointer, :int32_t, :pointer, :int32_t], :bool
attach_function :ucol_getRules, "ucol_getRules#{suffix}", [:pointer, :pointer], :pointer
attach_function :ucol_getSortKey, "ucol_getSortKey#{suffix}", [:pointer, :pointer, :int, :pointer, :int], :int
# Transliteration
#

View File

@ -61,6 +61,9 @@ module ICU
collator.rules.include?('ö<<<Ö').should be_true
end
it "returns usable collation keys" do
collator.collation_key("abc").should be < collator.collation_key("xyz")
end
end
end # Collate
end # ICU