decode Utf8() - > Result<string, string>- Модуль
- std.
bytes → - Получатель
- Bytes
- Минимальное число аргументов
- 0
- Максимальное число аргументов
- 0
Declaration and parameters
СсылкаДоступность
Включено по умолчанию · Не определено
Import std.bytes in language mode 5.21.
The current compiler selects std/bytes.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Получатель
Bytes
Ограничения параметров
Bytes.decodeUtf8 returns Ok with the exact Unicode scalar sequence encoded by valid UTF-8 bytes.
Empty Bytes returns Ok with an empty string. A leading UTF-8 encoding of U+FEFF remains in the returned string.
Overlong encodings, isolated continuation bytes, incomplete sequences, surrogate encodings, and values above U+10FFFF return Err with a string.
Decoding does not insert replacement characters, normalize scalars, alter newline sequences, or discard a byte order mark.
For a string, encodeUtf8 followed by decodeUtf8 returns the original string. Re-encoding any successfully decoded sequence recovers its original bytes.
The receiver evaluates once and remains unchanged. No text is exposed before the entire sequence has passed validation.
Связанное правило · Decode UTF-8 without replacing data →Результат
The declared result type is Result<string, string>.
Эффекты
Ошибки
Result and effects
СсылкаReturn the exact decoded scalar sequence in Ok. Malformed UTF-8 returns Err without replacement text or a decoded prefix.
Resolved contracts
СсылкаRelated entries
СсылкаDecode UTF-8 without replacing data
СсылкаDecode UTF-8 without replacing data
Bytes.decodeUtf8 returns Ok with the exact Unicode scalar sequence encoded by valid UTF-8 bytes.
Empty Bytes returns Ok with an empty string. A leading UTF-8 encoding of U+FEFF remains in the returned string.
Overlong encodings, isolated continuation bytes, incomplete sequences, surrogate encodings, and values above U+10FFFF return Err with a string.
Decoding does not insert replacement characters, normalize scalars, alter newline sequences, or discard a byte order mark.
For a string, encodeUtf8 followed by decodeUtf8 returns the original string. Re-encoding any successfully decoded sequence recovers its original bytes.
The receiver evaluates once and remains unchanged. No text is exposed before the entire sequence has passed validation.
Причина
Lossless decoding keeps malformed input visible and preserves distinctions needed by file formats, checksums, and text layout.
Retain a byte order mark and combining sequence
Ссылкаimport std.bytes { Bytes }import std.ioimport std.testfor hex in (["", "efbbbf", "65cc81", "0d0a", "f09f9880"]) {let decoded = test.assertOk(test.assertOk(Bytes.fromHex(hex)).decodeUtf8())io.writeLine(decoded.encodeUtf8().toHex())}
efbbbf
65cc81
0d0a
f09f9880
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
efbbbf65cc810d0af09f9880
Reject malformed UTF-8 categories
Ссылкаimport std.bytes { Bytes }import std.ioimport std.testfor hex in (["80", "c080", "e282", "eda080", "f4908080"]) {match (test.assertOk(Bytes.fromHex(hex)).decodeUtf8()) {case Err(_) => io.writeLine("invalid UTF-8")case Ok(_) => io.writeLine("accepted")}}
invalid UTF-8
invalid UTF-8
invalid UTF-8
invalid UTF-8
invalid UTF-8
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
invalid UTF-8invalid UTF-8invalid UTF-8invalid UTF-8invalid UTF-8