按规范「注释只讲解作用,不掺杂历史原因,尽量英文」:把 includes 下 75 个 .vp 的中文 # 注释与三引号 docstring 改写为英文,每个注释块压到 2 行以内。 保留要点:作用、约束、危险警告(不得/必须/注意)、ABI 与类型约定(如 BYTEPTR | t.CPtr = i8** 的跨模块坍缩坑)、位宽/长度约定、fail-fast 要求、内存与句柄所有权。 未动:文件头注释(规范豁免,仍是中文)、运行时中文字符串常量(printf / fatal_error / MlirFail 等用户可见消息,属代码字面量)。 验证:独立脚本比对 HEAD 与工作区的代码骨架(剥离 # 注释与三引号 docstring 后的非空行)——75 个文件逐字节一致;行尾 CRLF/LF 未被改动;全文剩余中文仅 547 行头注释、0 条行尾注释。bootstrap 三重重编 SUCCESS;全量 77/77 顺序回归通过。
53 lines
1.5 KiB
Plaintext
53 lines
1.5 KiB
Plaintext
import t, c
|
|
from stdint import *
|
|
|
|
|
|
class Buf:
|
|
data: char*
|
|
length: sizet
|
|
capacity: sizet
|
|
owned: bool
|
|
|
|
def __init__(self, data: char*, capacity: sizet, length: sizet = 0, owned: bool = False):
|
|
self.data = data
|
|
self.capacity = capacity
|
|
self.length = length
|
|
self.owned = owned
|
|
|
|
def clear(self):
|
|
self.length = 0
|
|
if self.data != None and self.capacity > 0: self.data[0] = 0
|
|
|
|
def write(self, src: char*, count: sizet) -> sizet:
|
|
if self.data == None: return 0
|
|
if src == None: return 0
|
|
if count == 0: return 0
|
|
avail: sizet = self.capacity - self.length
|
|
if avail <= 0: return 0
|
|
n: sizet = count if count < avail else avail
|
|
for i in range(n):
|
|
self.data[self.length + i] = src[i]
|
|
self.length += n
|
|
# When exactly full (n==avail), data[capacity] would overflow by 1 byte, so guard it
|
|
if self.length < self.capacity: self.data[self.length] = 0
|
|
return n
|
|
|
|
def cstr(self) -> char*:
|
|
return self.data
|
|
|
|
def reset(self):
|
|
self.length = 0
|
|
if self.data != None and self.capacity > 0:
|
|
self.data[0] = 0
|
|
|
|
def __enter__(self) -> Buf*: return self
|
|
def __exit__(self): self.reset()
|
|
|
|
def free(self):
|
|
if self.owned and self.data != None:
|
|
self.data[0] = 0
|
|
self.owned = False
|
|
self.data = None
|
|
self.length = 0
|
|
self.capacity = 0
|