trim
Removes leading or trailing zero bytes/characters from a byte slice or hex string
Loading...
Removes leading or trailing zero bytes/characters from a byte slice or hex string
Removes leading or trailing zero bytes/characters from a byte slice or hex string.
import "github.com/ChefBingbong/viem-go/utils/data"import "github.com/ChefBingbong/viem-go/utils/data"
// Trim leading zeros from bytesresult := data.TrimBytes([]byte{0x00, 0x00, 0x01, 0x02}, data.TrimLeft)// []byte{0x01, 0x02}
// Trim trailing zeros from bytesresult := data.TrimBytes([]byte{0x01, 0x02, 0x00, 0x00}, data.TrimRight)// []byte{0x01, 0x02}
// Trim leading zeros from hex stringhexResult := data.TrimHex("0x00000102", data.TrimLeft)// "0x0102"
// Trim trailing zeros from hex stringhexResult := data.TrimHex("0x01020000", data.TrimRight)// "0x0102"[]byte for TrimBytes, string for TrimHexThe trimmed value with leading or trailing zeros removed.
[]byte for TrimBytes, string for TrimHexThe byte slice or hex string to trim.
result := data.TrimBytes([]byte{0x00, 0x00, 0x01, 0x02}, data.TrimLeft)data.TrimDirection (TrimLeft or TrimRight)The direction in which to trim zeros – either leading (left) or trailing (right).
// Trim leading zerosresult := data.TrimBytes([]byte{0x00, 0x00, 0x01, 0x02}, data.TrimLeft)// []byte{0x01, 0x02}
// Trim trailing zerosresult := data.TrimBytes([]byte{0x01, 0x02, 0x00, 0x00}, data.TrimRight)// []byte{0x01, 0x02}
// Trim hex leading zeroshexResult := data.TrimHex("0x00000102", data.TrimLeft)// "0x0102"
// Trim hex trailing zeroshexResult := data.TrimHex("0x01020000", data.TrimRight)// "0x0102"Convenience functions for trimming bytes in a specific direction:
result := data.TrimLeftBytes([]byte{0x00, 0x00, 0x01, 0x02})
// []byte{0x01, 0x02}
result := data.TrimRightBytes([]byte{0x01, 0x02, 0x00, 0x00})
// []byte{0x01, 0x02}
Convenience functions for trimming hex strings in a specific direction:
result := data.TrimLeftHex("0x00000102")
// "0x0102"
result := data.TrimRightHex("0x01020000")
// "0x0102"