BinaryFormat.Choice

语法

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

关于

返回一个二进制格式,它基于已读取的值选择下一个二进制格式。 此函数按如下步骤生成二进制格式值:

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

可选的 type 参数指示选择函数将返回的二进制格式的类型。 可以指定 type anytype listtype binary 中的任何一个。 如果未指定 type 参数,则使用 type 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 }

results matching ""

    No results matching ""