본문으로 건너뛰기
토파즈6.0 · 출시 전
다운로드 →

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    라이브러리 · std.bytes

    Bytes.decodeUtf8

    Decodes this byte sequence as UTF-8 text.

    decodeUtf8() -> 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.

    결과

    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

    링크

    std.bytes →

    Decode UTF-8 without replacing data

    링크
    규칙topaz.stdlib.contract.bytes-utf8.rule

    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

    링크
    main.tpzRetain a byte order mark and combining sequence플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. import std.io
    3. import std.test
    4. for hex in (["", "efbbbf", "65cc81", "0d0a", "f09f9880"]) {
    5. let decoded = test.assertOk(test.assertOk(Bytes.fromHex(hex)).decodeUtf8())
    6. io.writeLine(decoded.encodeUtf8().toHex())
    7. }
    기대 출력종료 코드 0
    
    efbbbf
    65cc81
    0d0a
    f09f9880
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. efbbbf
    2. 65cc81
    3. 0d0a
    4. f09f9880

    Reject malformed UTF-8 categories

    링크
    main.tpzReject malformed UTF-8 categories플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. import std.io
    3. import std.test
    4. for hex in (["80", "c080", "e282", "eda080", "f4908080"]) {
    5. match (test.assertOk(Bytes.fromHex(hex)).decodeUtf8()) {
    6. case Err(_) => io.writeLine("invalid UTF-8")
    7. case Ok(_) => io.writeLine("accepted")
    8. }
    9. }
    기대 출력종료 코드 0
    invalid UTF-8
    invalid UTF-8
    invalid UTF-8
    invalid UTF-8
    invalid UTF-8
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. invalid UTF-8
    2. invalid UTF-8
    3. invalid UTF-8
    4. invalid UTF-8
    5. invalid UTF-8

    메뉴

    다운로드 →