认识 Aero / 字符串

字符串

Aero 的 str 是 NUL 结尾的 C 字符串(malloc 管理)。这是和 C 互操作最顺的表示。

基本操作

let s = "hello";
print(len(s));          // 5
print(s + " world");    // "hello world"(字面量拼接零成本)
print(s[0]);            // 104('h' 的数值)
len(s)字节长度
s1 + s2拼接(字面量零成本;变量参与需运行时分配)
== != < > <= >=字典序比较
s[i]第 i 字节的数值
int_to_str(n)整数 → 字符串(需 str_free)
str_to_int(s)字符串 → 整数(失败返回 0)
substr(s, start, end)截取 [start, end)(需 str_free)
str_contains(hay, needle)是否包含
str_find(hay, needle)首次出现位置,找不到 -1
str_cmp(a, b)C 风格比较:负 / 0 / 正
str_free(s)释放运行时分配的字符串

运行时分配必须释放

凡是运行时分配的字符串(int_to_str、substr、变量拼接),用完要 str_free,否则泄漏。

let s = int_to_str(12345);
print(s);        // 12345
str_free(s);     // 释放

文件读写

let text = read_file("data.txt");     // 读整个文件,失败返回空串
write_file("out.txt", text);          // 写文件,返回字节数,失败 -1
Aero 文档 · 基于 Aero 1.2.6 文档整理 · MIT
SereinTeam