slice(start: int, end: int) - > Bytes- Module
- std.
bytes - Receiver
- Bytes
- Minimum arity
- 2
- Maximum arity
- 2
Declaration and parameters
LinkAvailability
Included by default: Pending
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.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 961 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 52 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 961 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 408 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296 - compiler/crates/topaz_value/src/value/members.rs at
b48527d0a174· 251
Receiver
Bytes
Parameter constraints
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.
Related rule: Select explicit byte intervalsResult
The declared result type is Bytes.
Effects
Failures
Result and effects
LinkCopy the half-open interval from start to end. Invalid or inverted bounds produce TPZ4001 at runtime.
Resolved contracts
LinkRelated entries
LinkSelect explicit byte intervals
LinkSelect 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.
Why
An explicit interval should identify exactly the requested payload. Rejecting an invalid interval makes a mistaken byte count visible before subsequent decoding.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 308 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 329 -
topaz.stdlib.contract.bytes-sliceatworking-tree - topaz.decision.bytes-slice-bounds.1 at
topaz.decisi
Select empty and nonempty intervals
Linkimport 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
Expected result
Expected result
Execution completes. Exit code 0.
626361626364
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 308 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 329 -
topaz.stdlib.contract.bytes-sliceatworking-tree - topaz.decision.bytes-slice-bounds.1 at
topaz.decisi
Keep byte slicing separate from UTF-8 validation
Linkimport 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
Expected result
Expected result
Execution completes. Exit code 0.
c3invalid UTF-8
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 308 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 329 -
topaz.stdlib.contract.bytes-sliceatworking-tree - topaz.decision.bytes-slice-bounds.1 at
topaz.decisi
Reject a negative slice start
Linkimport std.bytes { Bytes }let value = "abcd".encodeUtf8()value.slice(-1, 2)
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 308 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 329 -
topaz.stdlib.contract.bytes-sliceatworking-tree - topaz.decision.bytes-slice-bounds.1 at
topaz.decisi
Reject an inverted byte interval
Linkimport std.bytes { Bytes }let value = "abcd".encodeUtf8()value.slice(3, 2)
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 308 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 329 -
topaz.stdlib.contract.bytes-sliceatworking-tree - topaz.decision.bytes-slice-bounds.1 at
topaz.decisi
Reject an endpoint beyond the sequence
Linkimport std.bytes { Bytes }let value = "abcd".encodeUtf8()value.slice(0, 5)
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 308 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 329 -
topaz.stdlib.contract.bytes-sliceatworking-tree - topaz.decision.bytes-slice-bounds.1 at
topaz.decisi