BinaryFormat.Choice

Syntax

BinaryFormat.Choice(
    binaryFormat as function,
    chooseFunction as function,
    optional type as nullable type,
    optional combineFunction as nullable function
) as function

About

返回一种二进制格式,该格式基于已读取的值选择下一个二进制格式。 此函数生成的二进制格式值分阶段工作:

  • 参数指定的 binaryFormat 二进制格式用于读取值。
  • 该值传递给参数指定的 chooseFunction 选择函数。
  • 选择函数检查值并返回第二个二进制格式。
  • 第二个二进制格式用于读取第二个值。
  • 如果指定了合并函数,则将第一个和第二个值传递给合并函数,并返回结果值。
  • 如果未指定合并函数,则返回第二个值。
  • 返回第二个值。

可选 type 参数指示选项函数将返回的二进制格式的类型。 type any可以指定,type list也可以type binary指定。 如果未指定参数 typetype any 则使用此参数。 如果 type listtype binary 已使用,则系统可能能够返回流式处理 binarylist 值而不是缓冲值,这可以减少读取格式所需的内存量。

示例 1

读取字节列表,其中元素数由第一个字节确定。

用法

let
    binaryData = #binary({2, 3, 4, 5}),
    listFormat = BinaryFormat.Choice(
        BinaryFormat.Byte,
        (length) => BinaryFormat.List(BinaryFormat.Byte, length)
    )
in
    listFormat(binaryData)

输出

{3,4}

示例 2

读取字节列表,其中元素数由第一个字节确定,并保留第一个字节读取。

用法

let
    binaryData = #binary({2, 3, 4, 5}),
    listFormat = BinaryFormat.Choice(
        BinaryFormat.Byte,
        (length) => BinaryFormat.Record([
            length = length,
            list = BinaryFormat.List(BinaryFormat.Byte, length)
        ])
    )
in
    listFormat(binaryData)

输出

[length = 2, list = {3, 4}]

示例 3

读取字节列表,其中元素数由第一个字节使用流式处理列表确定。

用法

let
    binaryData = #binary({2, 3, 4, 5}),
    listFormat = BinaryFormat.Choice(
        BinaryFormat.Byte,
        (length) => BinaryFormat.List(BinaryFormat.Byte, length),
        type list
    )
in
    listFormat(binaryData)

输出

{3, 4}