mirror of https://github.com/erickguan/ffi-icu.git
Fix int32_t, simpler spec for transliteration
This commit is contained in:
parent
721a60c3b1
commit
cbabb433e9
|
@ -44,7 +44,7 @@ module ICU
|
|||
def detect_all(str)
|
||||
set_text(str)
|
||||
|
||||
matches_found_ptr = FFI::MemoryPointer.new :int32
|
||||
matches_found_ptr = FFI::MemoryPointer.new :int32_t
|
||||
array_ptr = Lib.check_error do |status|
|
||||
Lib.ucsdet_detectAll(@detector, matches_found_ptr, status)
|
||||
end
|
||||
|
|
|
@ -81,8 +81,8 @@ module ICU
|
|||
attach_function :uenum_count, "uenum_count#{suffix}", [:pointer, :pointer], :int
|
||||
attach_function :uenum_close, "uenum_close#{suffix}", [:pointer], :void
|
||||
attach_function :uenum_next, "uenum_next#{suffix}", [:pointer, :pointer, :pointer], :string
|
||||
attach_function :u_charsToUChars, "u_charsToUChars#{suffix}", [:string, :pointer, :int32], :void
|
||||
attach_function :u_UCharsToChars, "u_UCharsToChars#{suffix}", [:pointer, :string, :int32], :void
|
||||
attach_function :u_charsToUChars, "u_charsToUChars#{suffix}", [:string, :pointer, :int32_t], :void
|
||||
attach_function :u_UCharsToChars, "u_UCharsToChars#{suffix}", [:pointer, :string, :int32_t], :void
|
||||
|
||||
# CharDet
|
||||
#
|
||||
|
@ -91,12 +91,12 @@ module ICU
|
|||
|
||||
attach_function :ucsdet_open, "ucsdet_open#{suffix}", [:pointer], :pointer
|
||||
attach_function :ucsdet_close, "ucsdet_close#{suffix}", [:pointer], :void
|
||||
attach_function :ucsdet_setText, "ucsdet_setText#{suffix}", [:pointer, :string, :int32, :pointer], :void
|
||||
attach_function :ucsdet_setDeclaredEncoding, "ucsdet_setDeclaredEncoding#{suffix}", [:pointer, :string, :int32, :pointer], :void
|
||||
attach_function :ucsdet_setText, "ucsdet_setText#{suffix}", [:pointer, :string, :int32_t, :pointer], :void
|
||||
attach_function :ucsdet_setDeclaredEncoding, "ucsdet_setDeclaredEncoding#{suffix}", [:pointer, :string, :int32_t, :pointer], :void
|
||||
attach_function :ucsdet_detect, "ucsdet_detect#{suffix}", [:pointer, :pointer], :pointer
|
||||
attach_function :ucsdet_detectAll, "ucsdet_detectAll#{suffix}", [:pointer, :pointer, :pointer], :pointer
|
||||
attach_function :ucsdet_getName, "ucsdet_getName#{suffix}", [:pointer, :pointer], :string
|
||||
attach_function :ucsdet_getConfidence, "ucsdet_getConfidence#{suffix}", [:pointer, :pointer], :int32
|
||||
attach_function :ucsdet_getConfidence, "ucsdet_getConfidence#{suffix}", [:pointer, :pointer], :int32_t
|
||||
attach_function :ucsdet_getLanguage, "ucsdet_getLanguage#{suffix}", [:pointer, :pointer], :string
|
||||
attach_function :ucsdet_getAllDetectableCharsets, "ucsdet_getAllDetectableCharsets#{suffix}", [:pointer, :pointer], :pointer
|
||||
attach_function :ucsdet_isInputFilterEnabled, "ucsdet_isInputFilterEnabled#{suffix}", [:pointer], :bool
|
||||
|
@ -109,15 +109,15 @@ module ICU
|
|||
|
||||
attach_function :ucol_open, "ucol_open#{suffix}", [:string, :pointer], :pointer
|
||||
attach_function :ucol_close, "ucol_close#{suffix}", [:pointer], :void
|
||||
attach_function :ucol_strcoll, "ucol_strcoll#{suffix}", [:pointer, :pointer, :int32, :pointer, :int32], :int
|
||||
attach_function :ucol_strcoll, "ucol_strcoll#{suffix}", [:pointer, :pointer, :int32_t, :pointer, :int32_t], :int
|
||||
attach_function :ucol_getKeywords, "ucol_getKeywords#{suffix}", [:pointer], :pointer
|
||||
attach_function :ucol_getKeywordValues, "ucol_getKeywordValues#{suffix}", [:string, :pointer], :pointer
|
||||
attach_function :ucol_getAvailable, "ucol_getAvailable#{suffix}", [:int32], :string
|
||||
attach_function :ucol_countAvailable, "ucol_countAvailable#{suffix}", [], :int32
|
||||
attach_function :ucol_getAvailable, "ucol_getAvailable#{suffix}", [:int32_t], :string
|
||||
attach_function :ucol_countAvailable, "ucol_countAvailable#{suffix}", [], :int32_t
|
||||
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
|
||||
attach_function :ucol_greater, "ucol_greater#{suffix}", [:pointer, :pointer, :int32_t, :pointer, :int32_t], :bool
|
||||
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
|
||||
|
||||
# Transliteration
|
||||
#
|
||||
|
@ -125,8 +125,8 @@ module ICU
|
|||
#
|
||||
|
||||
class UParseError < FFI::Struct
|
||||
layout :line, :int32,
|
||||
:offset, :int32,
|
||||
layout :line, :int32_t,
|
||||
:offset, :int32_t,
|
||||
:pre_context, :pointer,
|
||||
:post_context, :pointer
|
||||
|
||||
|
@ -134,19 +134,19 @@ module ICU
|
|||
end
|
||||
|
||||
class UTransPosition < FFI::Struct
|
||||
layout :context_start, :int32,
|
||||
:context_limit, :int32,
|
||||
:start, :int32,
|
||||
:end, :int32
|
||||
layout :context_start, :int32_t,
|
||||
:context_limit, :int32_t,
|
||||
:start, :int32_t,
|
||||
:end, :int32_t
|
||||
|
||||
end
|
||||
|
||||
enum :trans_direction, [:forward, :reverse]
|
||||
|
||||
attach_function :utrans_openIDs, "utrans_openIDs#{suffix}", [:pointer], :pointer
|
||||
attach_function :utrans_openU, "utrans_openU#{suffix}", [:pointer, :int32, :trans_direction, :pointer, :int32, :pointer, :pointer], :pointer
|
||||
attach_function :utrans_open, "utrans_open#{suffix}", [:string, :trans_direction, :pointer, :int32, :pointer, :pointer], :pointer
|
||||
attach_function :utrans_transUChars, "utrans_transUChars#{suffix}", [:pointer, :pointer, :pointer, :int32, :int32, :pointer, :pointer], :void
|
||||
attach_function :utrans_openU, "utrans_openU#{suffix}", [:pointer, :int32_t, :trans_direction, :pointer, :int32_t, :pointer, :pointer], :pointer
|
||||
attach_function :utrans_open, "utrans_open#{suffix}", [:string, :trans_direction, :pointer, :int32_t, :pointer, :pointer], :pointer
|
||||
attach_function :utrans_transUChars, "utrans_transUChars#{suffix}", [:pointer, :pointer, :pointer, :int32_t, :int32_t, :pointer, :pointer], :void
|
||||
|
||||
# Normalization
|
||||
#
|
||||
|
@ -162,6 +162,6 @@ module ICU
|
|||
:fcd, 6
|
||||
]
|
||||
|
||||
attach_function :unorm_normalize, "unorm_normalize#{suffix}", [:pointer, :int32, :normalization_mode, :int32, :pointer, :int32, :pointer], :int32
|
||||
attach_function :unorm_normalize, "unorm_normalize#{suffix}", [:pointer, :int32_t, :normalization_mode, :int32_t, :pointer, :int32_t, :pointer], :int32_t
|
||||
end # Lib
|
||||
end # ICU
|
||||
|
|
|
@ -26,33 +26,29 @@ module ICU
|
|||
@parse_error = Lib::UParseError.new
|
||||
Lib.check_error do |status|
|
||||
# couldn't get utrans_openU to work properly, so using deprecated utrans_open for now
|
||||
@tr = Lib.utrans_open(id, direction, nil, 0, nil, status)
|
||||
@tr = Lib.utrans_open(id, direction, nil, -1, @parse_error, status)
|
||||
end
|
||||
end
|
||||
|
||||
def transliterate(string)
|
||||
capacity = string.length + 1
|
||||
def transliterate(from)
|
||||
capacity = from.bytesize + 1
|
||||
limit = FFI::MemoryPointer.new :int32_t
|
||||
|
||||
text_length = FFI::MemoryPointer.new :int32
|
||||
text_length.put_int32(0, string.length)
|
||||
text_length = FFI::MemoryPointer.new :int32_t
|
||||
text_length.put_int32(0, from.length)
|
||||
|
||||
limit = FFI::MemoryPointer.new :int32
|
||||
|
||||
uchar_ptr = UCharPointer.from_string(string)
|
||||
# pos = Lib::UTransPosition.new
|
||||
# pos[:context_start] = pos[:start] = 0
|
||||
# pos[:context_limit] = pos[:end] = length
|
||||
buf = UCharPointer.from_string(from)
|
||||
|
||||
Lib.check_error do |error|
|
||||
Lib.utrans_transUChars(@tr, uchar_ptr, text_length, capacity, 0, limit, error)
|
||||
Lib.utrans_transUChars(@tr, buf, text_length, capacity, 0, limit, error)
|
||||
end
|
||||
|
||||
# TODO: clean up UCharPointer
|
||||
uchar_ptr.string
|
||||
buf.string
|
||||
end
|
||||
|
||||
def close
|
||||
|
||||
Lib.utrans_close @tr
|
||||
end
|
||||
end # Transliterator
|
||||
|
||||
|
|
|
@ -13,8 +13,10 @@ module ICU
|
|||
ptr
|
||||
end
|
||||
|
||||
def string
|
||||
wstring = get_array_of_uint16(0, size / FFI.type_size(UCHAR_TYPE))
|
||||
def string(length = nil)
|
||||
length ||= size / FFI.type_size(UCHAR_TYPE)
|
||||
|
||||
wstring = get_array_of_uint16(0, length)
|
||||
wstring.pack("U*")
|
||||
end
|
||||
|
||||
|
|
|
@ -5,11 +5,17 @@ module ICU
|
|||
|
||||
describe Transliterator do
|
||||
|
||||
before { @t = Transliterator.new("Greek-Latin", :reverse) }
|
||||
after { @t.close }
|
||||
def transliterator(*args)
|
||||
@t = Transliterator.new(*args)
|
||||
end
|
||||
after { @t.close if @t }
|
||||
|
||||
it "should transliterate a string" do
|
||||
@t.transliterate("Hello World").should == "Χελλο Ωορλδ"
|
||||
it "should transliterate Greek-Latin" do
|
||||
transliterator("Greek-Latin").transliterate("Hello World").should == "Χελλο Ωορλδ"
|
||||
end
|
||||
|
||||
it "should transliterate Lower" do
|
||||
transliterator("Lower").transliterate("ABC").should == "abc"
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue