Files
Viper_VRTE/viperio.vp
T
TermiNexus e25e8958f0 WIP 快照:includes 现有改动(含 M2.4 new 描述符的语法支持)
语法侧(M2.4):ast/base.vp 枚举尾部追加 ASTKind.New;ast/exprs.vp 新增 NEW_SRC_*/NEW_LIFE_* 与 class New;ast/parser.vp 新增 _is_new_expr/_parse_new_expr(new 为软关键字,不进 Keyword 表,new=1/new.x/new(1) 仍是标识符);ast/__init__.vp 再导出 New 与常量。

其余为既有 WIP 改动一并快照,保持基线一致(39 文件,+312/-237)。

未纳入:ctraits.vpi(已删除)、ctraits.vpnci、tomllib/(均未跟踪)。
2026-10-08 17:45:31 +08:00

47 lines
1.4 KiB
Plaintext

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