1.8 KiB
import
| import | ||||||
|---|---|---|---|---|---|---|
|
读取行
()
从已授权的 stdio/stdin 资源读取一行。按有界块读取并增量解码 UTF-8;
EOF 返回 {ok:false, code:"io.eof"},无效 UTF-8 返回可恢复错误。
流 = resource.open("stdio", "stdin", ["read"])
内存 = memory.new(1, 1)
字节 = []
结束 = false
错误 = ""
-
not 结束读取 = resource.read(流, 内存, 0, 1)
-
not 读取.ok结束 = true
错误 = 读取.code
-
读取.value.eof and 读取.value.count == 0结束 = true
-
内存[0] == 10结束 = true
-
true字节 = list.append(字节, 内存[0])
-
关闭 = resource.close(流)
-
错误 != ""{ok: false, code: 错误, message: "stdin read failed"}
-
not 关闭.ok关闭
-
长度(字节) == 0{ok: false, code: "io.eof", message: "end of input"}
-
true
数据长度 = 长度(字节)
-
数据长度 > 0 and 字节[数据长度 - 1] == 13数据长度 = 数据长度 - 1
-
true忽略 = 0
页 = (数据长度 + 65535) // 65536
数据 = memory.new(页, 页)
下标 = 0
-
下标 < 数据长度数据[下标] = 字节[下标]下标 = 下标 + 1
解码 = utf8.解码(数据, 0, 数据长度)
-
not 解码.ok解码
-
true{ok: true, value: 解码.value}
打印
(值)
结果 = stream.写文本(format.显示(值) + "\n")
-
not 结果.ok结果
-
true{ok: true, value: None}
写文本
(文本)
stream.写文本(文本)
写全部
(流, 内存, 起点, 长度)
stream.写全部(流, 内存, 起点, 长度)