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

문서 검색

명세 6.0

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

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

    라이브러리 · std.bytes

    Bytes.slice

    Returns a byte sequence selected by start and end positions.

    slice(start: int, end: int) -> Bytes
    수신자
    Bytes
    최소 인자 수
    2
    최대 인자 수
    2

    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.slice uses a start position and an exclusive end position measured in bytes.

    Valid bounds satisfy 0 <= start <= end <= length. The result contains exactly the bytes from start through end minus one.

    Equal endpoints produce empty Bytes, including endpoints at zero and at length. A full interval preserves every byte.

    Negative bounds, inverted bounds, and endpoints beyond length produce TPZ4001 at runtime. Bounds are not clamped or interpreted from the end.

    Slicing never changes the receiver. The receiver, start, and end expressions evaluate once in that order before the bounds are checked.

    A byte interval may split a UTF-8 encoding. Bytes.slice preserves those selected octets and Bytes.decodeUtf8 reports invalid text when they form an incomplete sequence.

    결과

    The declared result type is Bytes.

    효과

    실패

    Result and effects

    링크

    Copy the half-open interval from start to end. Invalid or inverted bounds produce TPZ4001 at runtime.

    Resolved contracts

    링크

    Related entries

    링크

    std.bytes →

    Select explicit byte intervals

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

    Select explicit byte intervals

    Bytes.slice uses a start position and an exclusive end position measured in bytes.

    Valid bounds satisfy 0 <= start <= end <= length. The result contains exactly the bytes from start through end minus one.

    Equal endpoints produce empty Bytes, including endpoints at zero and at length. A full interval preserves every byte.

    Negative bounds, inverted bounds, and endpoints beyond length produce TPZ4001 at runtime. Bounds are not clamped or interpreted from the end.

    Slicing never changes the receiver. The receiver, start, and end expressions evaluate once in that order before the bounds are checked.

    A byte interval may split a UTF-8 encoding. Bytes.slice preserves those selected octets and Bytes.decodeUtf8 reports invalid text when they form an incomplete sequence.

    이유

    An explicit interval should identify exactly the requested payload. Rejecting an invalid interval makes a mistaken byte count visible before subsequent decoding.

    Select empty and nonempty intervals

    링크
    main.tpzSelect empty and nonempty intervals플레이그라운드 열기 →
    1. import std.io
    2. let value = "abcd".encodeUtf8()
    3. io.writeLine(value.slice(0, 0).toHex())
    4. io.writeLine(value.slice(1, 3).toHex())
    5. io.writeLine(value.slice(4, 4).toHex())
    6. io.writeLine(value.toHex())
    기대 출력종료 코드 0
    
    6263
    
    61626364
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

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

    기대 표준 출력
    1. 6263
    2. 61626364

    Keep byte slicing separate from UTF-8 validation

    링크
    main.tpzKeep byte slicing separate from UTF-8 validation플레이그라운드 열기 →
    1. import std.io
    2. let byte = "é".encodeUtf8().slice(0, 1)
    3. io.writeLine(byte.toHex())
    4. match (byte.decodeUtf8()) {
    5. case Err(_) => io.writeLine("invalid UTF-8")
    6. case Ok(_) => io.writeLine("accepted")
    7. }
    기대 출력종료 코드 0
    c3
    invalid UTF-8
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

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

    기대 표준 출력
    1. c3
    2. invalid UTF-8

    Reject a negative slice start

    링크
    main.tpzReject a negative slice start플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. let value = "abcd".encodeUtf8()
    3. value.slice(-1, 2)
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    Reject an inverted byte interval

    링크
    main.tpzReject an inverted byte interval플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. let value = "abcd".encodeUtf8()
    3. value.slice(3, 2)
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    Reject an endpoint beyond the sequence

    링크
    main.tpzReject an endpoint beyond the sequence플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. let value = "abcd".encodeUtf8()
    3. value.slice(0, 5)
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    Diagnostics

    링크

    Index out of bounds →

    메뉴

    다운로드 →