الفرق بين المراجعتين لصفحة: «Ruby/Encoding/Converter/primitive errinfo»
جميل-بيلوني (نقاش | مساهمات) طلا ملخص تعديل |
طلا ملخص تعديل |
||
سطر 29: | سطر 29: | ||
مثالٌ عن استخدام التابع <code>primitive_errinfo</code>:<syntaxhighlight lang="ruby"> | مثالٌ عن استخدام التابع <code>primitive_errinfo</code>:<syntaxhighlight lang="ruby"> | ||
# \xff is invalid as EUC-JP. | # \xff is invalid as EUC-JP. | ||
ec = Encoding::Converter.new("EUC-JP", "Shift_JIS") | ec = Encoding::Converter.new("EUC-JP", "Shift_JIS") | ||
ec.primitive_convert(src="\xff", dst="", nil, 10) | ec.primitive_convert(src="\xff", dst="", nil, 10) | ||
p ec.primitive_errinfo | p ec.primitive_errinfo | ||
#=> [:invalid_byte_sequence, "EUC-JP", "UTF-8", "\xFF", ""] | #=> [:invalid_byte_sequence, "EUC-JP", "UTF-8", "\xFF", ""] | ||
# HIRAGANA LETTER A (\xa4\xa2 in EUC-JP) is not representable in ISO-8859-1. | # 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, | # 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). | # error_bytes is HIRAGANA LETTER A in UTF-8 (\xE3\x81\x82). | ||
ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1") | ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1") | ||
ec.primitive_convert(src="\xa4\xa2", dst="", nil, 10) | ec.primitive_convert(src="\xa4\xa2", dst="", nil, 10) | ||
p ec.primitive_errinfo | p ec.primitive_errinfo | ||
#=> [:undefined_conversion, "UTF-8", "ISO-8859-1", "\xE3\x81\x82", ""] | #=> [:undefined_conversion, "UTF-8", "ISO-8859-1", "\xE3\x81\x82", ""] | ||
# partial character is invalid | # partial character is invalid | ||
ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1") | ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1") | ||
ec.primitive_convert(src="\xa4", dst="", nil, 10) | ec.primitive_convert(src="\xa4", dst="", nil, 10) | ||
p ec.primitive_errinfo | p ec.primitive_errinfo | ||
#=> [:incomplete_input, "EUC-JP", "UTF-8", "\xA4", ""] | #=> [:incomplete_input, "EUC-JP", "UTF-8", "\xA4", ""] | ||
# Encoding::Converter::PARTIAL_INPUT prevents invalid errors by | # Encoding::Converter::PARTIAL_INPUT prevents invalid errors by | ||
# partial characters. | # partial characters. | ||
ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1") | ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1") | ||
ec.primitive_convert(src="\xa4", dst="", nil, 10, Encoding::Converter::PARTIAL_INPUT) | ec.primitive_convert(src="\xa4", dst="", nil, 10, Encoding::Converter::PARTIAL_INPUT) | ||
p ec.primitive_errinfo | p ec.primitive_errinfo | ||
#=> [:source_buffer_empty, nil, nil, nil, nil] | #=> [:source_buffer_empty, nil, nil, nil, nil] | ||
# \xd8\x00\x00@ is invalid as UTF-16BE because | # \xd8\x00\x00@ is invalid as UTF-16BE because | ||
# no low surrogate after high surrogate (\xd8\x00). | # no low surrogate after high surrogate (\xd8\x00). | ||
# It is detected by 3rd byte (\00) which is part of next character. | # It is detected by 3rd byte (\00) which is part of next character. | ||
# So the high surrogate (\xd8\x00) is discarded and | # So the high surrogate (\xd8\x00) is discarded and | ||
# the 3rd byte is read again later. | # the 3rd byte is read again later. | ||
# Since the byte is buffered in ec, it is dropped from src. | # Since the byte is buffered in ec, it is dropped from src. | ||
ec = Encoding::Converter.new("UTF-16BE", "UTF-8") | ec = Encoding::Converter.new("UTF-16BE", "UTF-8") | ||
ec.primitive_convert(src="\xd8\x00\x00@", dst="", nil, 10) | ec.primitive_convert(src="\xd8\x00\x00@", dst="", nil, 10) | ||
p ec.primitive_errinfo | p ec.primitive_errinfo | ||
#=> [:invalid_byte_sequence, "UTF-16BE", "UTF-8", "\xD8\x00", "\x00"] | #=> [:invalid_byte_sequence, "UTF-16BE", "UTF-8", "\xD8\x00", "\x00"] | ||
p src | p src | ||
#=> "@" | #=> "@" | ||
# Similar to UTF-16BE, \x00\xd8@\x00 is invalid as UTF-16LE. | # Similar to UTF-16BE, \x00\xd8@\x00 is invalid as UTF-16LE. | ||
# The problem is detected by 4th byte. | # The problem is detected by 4th byte. | ||
ec = Encoding::Converter.new("UTF-16LE", "UTF-8") | ec = Encoding::Converter.new("UTF-16LE", "UTF-8") | ||
ec.primitive_convert(src="\x00\xd8@\x00", dst="", nil, 10) | ec.primitive_convert(src="\x00\xd8@\x00", dst="", nil, 10) | ||
p ec.primitive_errinfo | p ec.primitive_errinfo | ||
#=> [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "@\x00"] | #=> [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "@\x00"] | ||
p src | p src | ||
#=> "" | #=> "" | ||
</syntaxhighlight> | </syntaxhighlight> |
المراجعة الحالية بتاريخ 10:43، 18 أكتوبر 2018
يُعيد التّابع primitive_errinfo
معلوماتٍ مهمّةً حول آخر خطأٍ حدث في مصفوفة مكونة من خمسة عناصر مثل:
[result, enc1, enc2, error_bytes, readagain_bytes]
تكون result
هي آخر نتيجة تحويل للتّابع primitive_convert
.
تكون العناصر الأخرى ذات معنى فقط عندما تكون result
هي invalid_byte_sequence:
، أو incomplete_input:
، أو undefined_conversion:
.
تدلُّ القيمتان enc1
و enc2
على خطوة التحويل على شكل زوجٍ من السلاسل النصيّة. على سبيل المثال، يحول محولٌ (converter) من التّرميز 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
هو الجزء المخزَّن مؤقتًا الذي سيُقرأ مجدّدًا في عملية التحويل التالية.
البنية العامة
primitive_errinfo → array
القيم المعادة
تُعاد مصفوفة ذات 5 عناصر:
[result, enc1, enc2, error_bytes, readagain_bytes]
القيمة result
: آخر نتيجة من التّابع primitive_errinfo
.
القيمتان enc1
و enc2
: يدلّان على خطوة التحويل على شكل زوجٍ من السلاسل النصيّة.
القيمتان error_bytes
و readagain_bytes
: تشيران إلى سلاسل البايت التي سببت الخطأ.
أمثلة
مثالٌ عن استخدام التابع primitive_errinfo
:
# \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", "UTF-8", "\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
#=> ""
انظر أيضًا
- التابع
primitive_convert
: يحوّل ترميز جميع المحتويات المخزَّنة مؤقتًا (buffer) في مخزن معين أو مجال محدد منها ثم يضع الناتج في مخزَّن محدَّد. - التّابع
asciicompat_encoding
: يُعيد التّرميز المتوافق معASCII
المقابل للترميز المعطى. - التّابع
destination_encoding
: يُعيد التّرميز الوجهة على شكل كائنٍ من الصنفEncoding
. - التّابع
source_encoding
: يُعيد الترميز المصدر على شكل كائن من الصنفEncoding
. - التّابع
insert_output
: يضيف سلسلةً نصيّةً إلى محوِّل التّرميز، إذ سيحويل ترميز هذه السلسلة إلى ترميز الوجهة نفسه وتوضع في نهاية مخرجات عملية التحويل. - التّابع
finish
: يُنهي محوّل الترميز، ويُعيد الجزء الأخير من السلسلة النصيّة التي يحول ترميزها حينذاك.