slice
Returns a section of a byte slice or hex string given start/end byte offsets
Loading...
Returns a section of a byte slice or hex string given start/end byte offsets
Returns a section of a byte slice or hex string given start/end byte offsets.
import "github.com/ChefBingbong/viem-go/utils/data"import "github.com/ChefBingbong/viem-go/utils/data"
// Slice bytesresult, _ := data.SliceBytes([]byte{0x01, 0x02, 0x03, 0x04}, 1, 3)// []byte{0x02, 0x03}
// Slice hex stringhexResult, _ := data.SliceHex("0x01020304", 1, 3)// "0x0203"
// Slice from start to endfromStart, _ := data.SliceBytesStart([]byte{0x01, 0x02, 0x03}, 1)// []byte{0x02, 0x03}([]byte, error) for SliceBytes, (string, error) for SliceHexThe sliced section. Returns an error if the start offset is out of bounds.
[]byte for SliceBytes, string for SliceHexThe byte slice or hex string to slice.
result, _ := data.SliceBytes([]byte{0x01, 0x02, 0x03, 0x04}, 1, 3)intThe start byte offset (inclusive). Use 0 for the beginning. Negative values are treated as 0.
// Start from byte 1result, _ := data.SliceBytes([]byte{0x01, 0x02, 0x03, 0x04}, 1, 3)// []byte{0x02, 0x03}
// Start from beginningresult, _ := data.SliceBytes([]byte{0x01, 0x02, 0x03, 0x04}, 0, 2)// []byte{0x01, 0x02}intThe end byte offset (exclusive). Use 0 or a value greater than the length to slice to the end.
// Slice from byte 1 to byte 3 (exclusive)result, _ := data.SliceBytes([]byte{0x01, 0x02, 0x03, 0x04}, 1, 3)// []byte{0x02, 0x03}
// Slice to the endresult, _ := data.SliceBytes([]byte{0x01, 0x02, 0x03}, 1, 0)// []byte{0x02, 0x03}boolfalseIf true, validates that the slice produces the expected size.
// Strict mode validates result sizeresult, err := data.SliceBytes( []byte{0x01, 0x02, 0x03}, 0, 2, data.SliceOptions{Strict: true},)// []byte{0x01, 0x02}, nilSlices from a start position to the end of the value:
result, _ := data.SliceBytesStart([]byte{0x01, 0x02, 0x03}, 1)
// []byte{0x02, 0x03}
Slices a hex string from a start position to the end:
result, _ := data.SliceHexStart("0x01020304", 1)
// "0x020304"