本文简要介绍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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。