mirror of https://github.com/erickguan/ffi-icu.git
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:
parent
246b13f4eb
commit
e1f9626bd1
|
@ -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
|
||||
|
|
|
@ -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
|
||||
#
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue