Files
marqdo-ext-ai/lib/bytes.mq.md
cflmy fd570ae545 feat: add controlled inference package core
- explicit Package manifest with pinned marqdo-ext-http dependency
- public ai.infer/classify/select API over ordinary data
- layered lib: bytes, strict JSON, contract subset, context, protocol, transport, orchestration
- white-box regression entry for internal modules
2026-11-29 23:45:21 +08:00

2.3 KiB

import
import
utf8 format
std:text/utf8 std:format

@exec bytes

编码

params(内容)

把 UTF-8 文本编码为字节列表。按实际字节数申请最小 Memory 页数,不在每次 调用中固定占用 8 MiB;空文本返回空列表。

长度值 = format.字节长度(内容)

if 长度值 == 0

return []

end 页 = (长度值 + 65535) // 65536

内存 = memory.new(页, 页)

编码长度 = utf8.编码(内容, 内存, 0)

结果 = []

下标 = 0

while 下标 < 编码长度

结果 = list.append(结果, 内存[下标])

下标 = 下标 + 1

end return 结果

解码

params(字节)

把字节列表按 UTF-8 解码。成功返回 ok true value 文本,非法 UTF-8 返回 ok false code bytes.utf8。空字节返回空文本。

长度值 = 长度(字节)

if 长度值 == 0

return {ok: true, value: ""}

end 页 = (长度值 + 65535) // 65536

内存 = memory.new(页, 页)

下标 = 0

while 下标 < 长度值

if 类型(字节[下标]) != "Int"

return {ok: false, code: "bytes.type", message: "字节列表元素必须是 Int"}

end 内存[下标] = 字节[下标]

下标 = 下标 + 1

end return utf8.解码(内存, 0, 长度值)

字节数

params(内容)

返回文本的 UTF-8 字节数。

return format.字节长度(内容)

ascii小写

params(文本)

只折叠 ASCII A-Z;用于 HTTP 头名的 ASCII 大小写无关比较,不做 Unicode 大小写映射。

点 = text.codepoints(文本)

结果 = ""

下标 = 0

while 下标 < 长度(点)

码点 = 点[下标]

if 码点 >= 65 and 码点 <= 90

结果 = 结果 + text.from_codepoint(码点 + 32)

else if true

结果 = 结果 + text.from_codepoint(码点)

end end 下标 = 下标 + 1

end return 结果

头查找

params(头, 名称)

在 HTTP 头 Map 中按 ASCII 大小写无关语义查找一个头;命中返回 ok true 与文本值,缺失返回 ok false code http.header_missing。

目标 = ascii小写(名称)

键 = map.keys(头)

下标 = 0

while 下标 < 长度(键)

if ascii小写(键[下标]) == 目标

return {ok: true, value: 头[键[下标]]}

end 下标 = 下标 + 1

end return {ok: false, code: "http.header_missing", message: "缺少响应头 " + 名称}