Перейти к содержимому
Топаз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
    Модуль
    std.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)
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения run. Ненулевой код завершения.

    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)
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения run. Ненулевой код завершения.

    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)
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения run. Ненулевой код завершения.

    Diagnostics

    Ссылка

    Index out of bounds →

    Меню

    Загрузка →