本文簡要介紹ruby語言中 Encoding::Converter.primitive_errinfo
的用法。
用法
primitive_errinfo → array
primitive_errinfo
以 5 元素數組的形式返回有關最後一個錯誤的重要信息:
[result, enc1, enc2, error_bytes, readagain_bytes]
result 是primitive_convert 的最後一個結果。
其他元素僅在結果為:invalid_byte_sequence、:incomplete_input 或:undefined_conversion 時才有意義。
enc1 和 enc2 將轉換步驟表示為一對字符串。例如,從 EUC-JP 到 ISO-8859-1 的轉換器將字符串轉換如下:EUC-JP -> UTF-8 -> ISO-8859-1。所以 [enc1, enc2] 或者是 [“EUC-JP”, “UTF-8”] 或者是 [“UTF-8”, “ISO-8859-1”]。
error_bytes 和readagain_bytes 表示導致錯誤的字節序列。 error_bytes 是丟棄部分。 readagain_bytes 是緩衝部分,在下次轉換時再次讀取。
例子:
# \xff is invalid as EUC-JP.
ec = Encoding::Converter.new("EUC-JP", "Shift_JIS")
ec.primitive_convert(src="\xff", dst="", nil, 10)
p ec.primitive_errinfo
#=> [:invalid_byte_sequence, "EUC-JP", "Shift_JIS", "\xFF", ""]
# HIRAGANA LETTER A (\xa4\xa2 in EUC-JP) is not representable in ISO-8859-1.
# Since this error is occur in UTF-8 to ISO-8859-1 conversion,
# error_bytes is HIRAGANA LETTER A in UTF-8 (\xE3\x81\x82).
ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
ec.primitive_convert(src="\xa4\xa2", dst="", nil, 10)
p ec.primitive_errinfo
#=> [:undefined_conversion, "UTF-8", "ISO-8859-1", "\xE3\x81\x82", ""]
# partial character is invalid
ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
ec.primitive_convert(src="\xa4", dst="", nil, 10)
p ec.primitive_errinfo
#=> [:incomplete_input, "EUC-JP", "UTF-8", "\xA4", ""]
# Encoding::Converter::PARTIAL_INPUT prevents invalid errors by
# partial characters.
ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
ec.primitive_convert(src="\xa4", dst="", nil, 10, Encoding::Converter::PARTIAL_INPUT)
p ec.primitive_errinfo
#=> [:source_buffer_empty, nil, nil, nil, nil]
# \xd8\x00\x00@ is invalid as UTF-16BE because
# no low surrogate after high surrogate (\xd8\x00).
# It is detected by 3rd byte (\00) which is part of next character.
# So the high surrogate (\xd8\x00) is discarded and
# the 3rd byte is read again later.
# Since the byte is buffered in ec, it is dropped from src.
ec = Encoding::Converter.new("UTF-16BE", "UTF-8")
ec.primitive_convert(src="\xd8\x00\x00@", dst="", nil, 10)
p ec.primitive_errinfo
#=> [:invalid_byte_sequence, "UTF-16BE", "UTF-8", "\xD8\x00", "\x00"]
p src
#=> "@"
# Similar to UTF-16BE, \x00\xd8@\x00 is invalid as UTF-16LE.
# The problem is detected by 4th byte.
ec = Encoding::Converter.new("UTF-16LE", "UTF-8")
ec.primitive_convert(src="\x00\xd8@\x00", dst="", nil, 10)
p ec.primitive_errinfo
#=> [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "@\x00"]
p src
#=> ""
相關用法
- Ruby Converter.primitive_convert用法及代碼示例
- Ruby Converter.putback用法及代碼示例
- Ruby Converter.convert用法及代碼示例
- Ruby Converter.inspect用法及代碼示例
- Ruby Converter.insert_output用法及代碼示例
- Ruby Converter.search_convpath用法及代碼示例
- Ruby Converter.last_error用法及代碼示例
- Ruby Converter.replacement用法及代碼示例
- Ruby Converter.replacement =用法及代碼示例
- Ruby Converter.new用法及代碼示例
- Ruby Converter.finish用法及代碼示例
- Ruby Converter.convpath用法及代碼示例
- Ruby Converter.asciicompat_encoding用法及代碼示例
- Ruby Context.save_history=用法及代碼示例
- Ruby Constants模塊用法及代碼示例
- Ruby Continuation類用法及代碼示例
- Ruby Context.echo_on_assignment?用法及代碼示例
- Ruby Config.get_value用法及代碼示例
- Ruby Context.echo?用法及代碼示例
- Ruby Context.echo_on_assignment用法及代碼示例
- Ruby Config.to_s用法及代碼示例
- Ruby Constructive類用法及代碼示例
- Ruby Constructive.each用法及代碼示例
- Ruby Context.auto_indent_mode用法及代碼示例
- Ruby ConditionVariable類用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Converter.primitive_errinfo。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。