decode Utf8() - > Result<string, string>- 수신자
- 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