Skip to main content
Version: Next

Encoding Module

Hosted interpreter permissions: Every function and value on this page requires no host permissions. Higher-order functions still enforce the requirements of their callbacks. These checks do not apply to CLI execution or compilation. See Host permissions.

Bytes are represented as int list; every element must be in 0..255. Functions return Some result on success and None for invalid content. Inputs are never read from files, and no system encoding or current culture is consulted.

Encoding.utf8Encode​

None
Encoding.utf8Encode : string -> int list option

Encodes text as UTF-8 bytes without a BOM. Unpaired UTF-16 surrogates return None.

Encoding.utf8Encode "é"
// Some [195;169]

Encoding.utf8Decode​

None
Encoding.utf8Decode : int list -> string option

Strictly decodes UTF-8. Invalid byte values or malformed UTF-8 return None. A supplied BOM is preserved as U+FEFF.

Encoding.utf8Decode [195;169]
// Some "é"

Encoding.base64Encode​

None
Encoding.base64Encode : int list -> string option

Encodes bytes with the standard Base64 alphabet and padding, without line breaks.

Encoding.base64Encode [97;98;99]
// Some "YWJj"

Encoding.base64Decode​

None
Encoding.base64Decode : string -> int list option

Decodes standard padded Base64; .NET Base64 whitespace is accepted. Invalid input returns None.

Encoding.base64Decode "YWJj"
// Some [97;98;99]

Encoding.hexEncode​

None
Encoding.hexEncode : int list -> string option

Encodes bytes as lowercase hexadecimal, with two digits per byte.

Encoding.hexEncode [0;171;255]
// Some "00abff"

Encoding.hexDecode​

None
Encoding.hexDecode : string -> int list option

Decodes even-length hexadecimal; either case is accepted. Whitespace, odd lengths, and invalid digits return None.

Encoding.hexDecode "00AbFF"
// Some [0;171;255]

Encoding.uriEncode​

None
Encoding.uriEncode : string -> string option

Percent-encodes a URI component using UTF-8, leaving only RFC 3986 unreserved characters unchanged. Invalid UTF-16 returns None.

Encoding.uriEncode "a +/é"
// Some "a%20%2B%2F%C3%A9"

Encoding.uriDecode​

None
Encoding.uriDecode : string -> string option

Decodes percent escapes and strictly validates UTF-8. Invalid escapes or UTF-8 return None. A plus sign remains a plus sign; this is not form decoding.

Encoding.uriDecode "a%20%2B%2F%C3%A9"
// Some "a +/é"

Register only pure content helpers​

A .NET embedding host can expose all Encoding functions and SHA-256 functions without granting I/O permissions:

open FScript.Language
open FScript.Runtime

let registration = HostInterop.ofExterns PureExterns.functions
let result =
Parser.parseProgram "Hash.sha256 \"hello\""
|> TypeInfer.inferProgramWithRegistration registration
|> Eval.evalProgramWithRegistration registration