Files
marqdo-cli/tests/conformance/semantics/60-utf8-decoder.mq.md
T
cflmy 92bfbf2568 feat(rfc-cm): baseline P0-P3 compute machine, stdlib and resource slices
Implements the first vertical slices of RFC-CM-0001/0002 on top of a113d89:

- P1: opaque Memory regions (64 KiB pages, budgets, grow-only), byte
  read/write, Memory-index place assignment, wire/host-boundary rejection.
- CM-A04/A05: value-semantics List/Map/Text/Table primitives and the
  recoverable Result map convention.
- CM-A06: opaque Resource handles, resource.open/read/write/close, stdout
  write, granted project-file read and buffered write, strict fail-closed.
- CM-A07 Compat-A: explicit .mq.md format/utf8/io chain matching builtin
  print for the covered value types; builtin remains default.
- P2 fixtures: allocators, list/tree/graph/hash, integer codecs, UTF-8
  encode/decode, CSV/JSON subsets, table filter/sort/join, bytecode VM.
- ADRs 0022-0027, spec updates, evidence reports and 68 semantic goldens.

Gate: cargo fmt --check, clippy -D warnings, 407 tests passed.
2026-11-20 21:16:54 +08:00

3.2 KiB

成功

(值)

{ok: true, value: 值}

失败

(代码, 消息)

{ok: false, code: 代码, message: 消息}

续字节

(字节)

字节 >= 128 and 字节 < 192

解码2

(内存, 基址, 长度, 偏移, 文本)

  1. 偏移 + 1 >= 长度

    [文本, 偏移, "utf8.truncated"]

b1 = 内存[基址 + 偏移 + 1]

  1. not 续字节(b1)

    [文本, 偏移, "utf8.invalid_continuation"]

码点 = (内存[基址 + 偏移] - 192) * 64 + (b1 - 128)

  1. 码点 < 128

    [文本, 偏移, "utf8.invalid_scalar"]

[文本 + text.from_codepoint(码点), 偏移 + 2, ""]

解码3

(内存, 基址, 长度, 偏移, 文本)

  1. 偏移 + 2 >= 长度

    [文本, 偏移, "utf8.truncated"]

b1 = 内存[基址 + 偏移 + 1]

b2 = 内存[基址 + 偏移 + 2]

  1. not (续字节(b1) and 续字节(b2))

    [文本, 偏移, "utf8.invalid_continuation"]

码点 = (内存[基址 + 偏移] - 224) * 4096 + (b1 - 128) * 64 + (b2 - 128)

  1. 码点 < 2048 or (码点 >= 55296 and 码点 <= 57343)

    [文本, 偏移, "utf8.invalid_scalar"]

[文本 + text.from_codepoint(码点), 偏移 + 3, ""]

解码4

(内存, 基址, 长度, 偏移, 文本)

  1. 偏移 + 3 >= 长度

    [文本, 偏移, "utf8.truncated"]

b1 = 内存[基址 + 偏移 + 1]

b2 = 内存[基址 + 偏移 + 2]

b3 = 内存[基址 + 偏移 + 3]

  1. not (续字节(b1) and 续字节(b2) and 续字节(b3))

    [文本, 偏移, "utf8.invalid_continuation"]

码点 = (内存[基址 + 偏移] - 240) * 262144 + (b1 - 128) * 4096 + (b2 - 128) * 64 + (b3 - 128)

  1. 码点 < 65536 or 码点 > 1114111

    [文本, 偏移, "utf8.invalid_scalar"]

[文本 + text.from_codepoint(码点), 偏移 + 4, ""]

解码

(内存, 基址, 长度)

偏移 = 0

文本 = ""

错误 = ""

  • 偏移 < 长度 and 错误 == ""

    首 = 内存[基址 + 偏移]

    1. 首 < 128

      文本 = 文本 + text.from_codepoint(首)

      偏移 = 偏移 + 1

    2. 首 < 194

      错误 = "utf8.invalid_lead"

    3. 首 < 224

      结果 = 解码2(内存, 基址, 长度, 偏移, 文本)

      文本 = 结果[0]

      偏移 = 结果[1]

      错误 = 结果[2]

    4. 首 < 240

      结果 = 解码3(内存, 基址, 长度, 偏移, 文本)

      文本 = 结果[0]

      偏移 = 结果[1]

      错误 = 结果[2]

    5. 首 < 245

      结果 = 解码4(内存, 基址, 长度, 偏移, 文本)

      文本 = 结果[0]

      偏移 = 结果[1]

      错误 = 结果[2]

    6. true

      错误 = "utf8.invalid_lead"

  1. 错误 == ""

    成功(文本)

  2. true

    失败(错误, "UTF-8 解码失败")

写字节

(内存, 基址, 字节)

下标 = 0

  • 下标 < 长度(字节)

    内存[基址 + 下标] = 字节[下标]

    下标 = 下标 + 1

None

main

内存 = memory.new(1, 1)

写字节(内存, 0, [65, 195, 169, 228, 184, 173])

写字节(内存, 16, [228, 184])

写字节(内存, 32, [228, 65, 173])

写字节(内存, 48, [237, 160, 128])

写字节(内存, 64, [240, 157, 132, 158])

[解码(内存, 0, 6), 解码(内存, 16, 2), 解码(内存, 32, 3), 解码(内存, 48, 3), 解码(内存, 64, 4)]