Skip to content

Utility expressions¤

Overview¤

  • !CAST: Converts type of the argument into another.
  • !HASH: Calculates a digest.
  • !HEX.BYTES: Converts hexadecimal string into binary data.

!CAST¤

Convert type of the argument into another.

Type: Mapping.

Synopsis:

!CAST
what: <input>
type: <type>
default: <value>
base: <integer>
  • default is optional. Returned when conversion fails.
  • base is optional. Used when converting strings to integers (radix).

Explicitly convert type of what into the type of type.

SP-Lang automatically converts types of arguments so that the user doesn't need to think about types at all. This feature is called implicit casting.

In case of explicit need for a type conversion, use !CAST expression. It is very powerful method that do a lot of heavy-lifting.

For more details, see chapter about types.

Example

!CAST
what: "10.3"
type: fp64

This is an explicit casting of the string into a floating-point number.


!HASH¤

Calculate a digest.

Type: Mapping.

Synopsis:

!HASH
what: <input>
seed: <integer>
type: <type of hash>

Calculate the hash for a what value and return the digest as a list of bytes (list[ui8]).

seed specifies the initial hash seed.

type specifies a hashing function, the default value is XXH64.

Supported hashing functions¤

  • XXH64: xxHash, 64bit, non-cryptographic, extremely fast hash algorithm
  • XXH3: xxHash, 64bit, non-cryptographic, further optimized for small inputs

More information about xxHash are at xxhash.com.

Example

!HASH
what: "Hello world!"
seed: 5

!HEX.BYTES¤

Convert hexadecimal string into binary data.

Type: Mapping.

Synopsis:

!HEX.BYTES
what: <string>

Example

!HEX.BYTES
what: "8F3A12"
Output: b'\x8f:\x12'