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.
Связанное правило · Select explicit byte intervals →Результат
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
СсылкаSelect explicit byte intervals
Ссылка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
Ссылкаimport std.iolet value = "abcd".encodeUtf8()io.writeLine(value.slice(0, 0).toHex())io.writeLine(value.slice(1, 3).toHex())io.writeLine(value.slice(4, 4).toHex())io.writeLine(value.toHex())
6263
61626364
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
626361626364
Keep byte slicing separate from UTF-8 validation
Ссылкаimport std.iolet byte = "é".encodeUtf8().slice(0, 1)io.writeLine(byte.toHex())match (byte.decodeUtf8()) {case Err(_) => io.writeLine("invalid UTF-8")case Ok(_) => io.writeLine("accepted")}
c3
invalid UTF-8
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
c3invalid UTF-8
Reject a negative slice start
Ссылкаimport std.bytes { Bytes }let value = "abcd".encodeUtf8()value.slice(-1, 2)
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения run. Ненулевой код завершения.
bytes-slice-bounds at
main.tpz:3:1
Reject an inverted byte interval
Ссылкаimport std.bytes { Bytes }let value = "abcd".encodeUtf8()value.slice(3, 2)
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения run. Ненулевой код завершения.
bytes-slice-bounds at
main.tpz:3:1
Reject an endpoint beyond the sequence
Ссылкаimport std.bytes { Bytes }let value = "abcd".encodeUtf8()value.slice(0, 5)
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения run. Ненулевой код завершения.
bytes-slice-bounds at
main.tpz:3:1