silverbullet/common/space_lua/stdlib/string.ts

95 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-10-10 02:35:07 +08:00
import {
2024-10-11 21:34:27 +08:00
LuaBuiltinFunction,
LuaMultiRes,
LuaTable,
luaToString,
2024-10-10 02:35:07 +08:00
} from "$common/space_lua/runtime.ts";
export const stringApi = new LuaTable({
2024-10-20 21:06:23 +08:00
byte: new LuaBuiltinFunction((_sf, s: string, i?: number, j?: number) => {
2024-10-11 21:34:27 +08:00
i = i ?? 1;
j = j ?? i;
const result = [];
for (let k = i; k <= j; k++) {
result.push(s.charCodeAt(k - 1));
}
return new LuaMultiRes(result);
}),
2024-10-20 21:06:23 +08:00
char: new LuaBuiltinFunction((_sf, ...args: number[]) => {
2024-10-11 21:34:27 +08:00
return String.fromCharCode(...args);
}),
find: new LuaBuiltinFunction(
2024-10-20 21:06:23 +08:00
(_sf, s: string, pattern: string, init?: number, plain?: boolean) => {
2024-10-11 21:34:27 +08:00
init = init ?? 1;
plain = plain ?? false;
const result = s.slice(init - 1).match(pattern);
if (!result) {
return new LuaMultiRes([]);
}
return new LuaMultiRes([
result.index! + 1,
result.index! + result[0].length,
]);
},
),
2024-10-20 21:06:23 +08:00
gmatch: new LuaBuiltinFunction((_sf, s: string, pattern: string) => {
2024-10-11 21:34:27 +08:00
const regex = new RegExp(pattern, "g");
return () => {
const result = regex.exec(s);
if (!result) {
return;
}
return new LuaMultiRes(result.slice(1));
};
}),
gsub: new LuaBuiltinFunction(
2024-10-20 21:06:23 +08:00
(_sf, s: string, pattern: string, repl: string, n?: number) => {
2024-10-11 21:34:27 +08:00
n = n ?? Infinity;
const regex = new RegExp(pattern, "g");
let result = s;
let match: RegExpExecArray | null;
for (let i = 0; i < n; i++) {
match = regex.exec(result);
if (!match) {
break;
2024-10-10 02:35:07 +08:00
}
2024-10-11 21:34:27 +08:00
result = result.replace(match[0], repl);
}
return result;
},
),
2024-10-20 21:06:23 +08:00
len: new LuaBuiltinFunction((_sf, s: string) => {
2024-10-11 21:34:27 +08:00
return s.length;
}),
2024-10-20 21:06:23 +08:00
lower: new LuaBuiltinFunction((_sf, s: string) => {
2024-10-11 21:34:27 +08:00
return luaToString(s.toLowerCase());
}),
2024-10-20 21:06:23 +08:00
upper: new LuaBuiltinFunction((_sf, s: string) => {
2024-10-11 21:34:27 +08:00
return luaToString(s.toUpperCase());
}),
match: new LuaBuiltinFunction(
2024-10-20 21:06:23 +08:00
(_sf, s: string, pattern: string, init?: number) => {
2024-10-11 21:34:27 +08:00
init = init ?? 1;
const result = s.slice(init - 1).match(pattern);
if (!result) {
return new LuaMultiRes([]);
}
return new LuaMultiRes(result.slice(1));
},
),
2024-10-20 21:06:23 +08:00
rep: new LuaBuiltinFunction((_sf, s: string, n: number, sep?: string) => {
2024-10-11 21:34:27 +08:00
sep = sep ?? "";
return s.repeat(n) + sep;
}),
2024-10-20 21:06:23 +08:00
reverse: new LuaBuiltinFunction((_sf, s: string) => {
2024-10-11 21:34:27 +08:00
return s.split("").reverse().join("");
}),
2024-10-20 21:06:23 +08:00
sub: new LuaBuiltinFunction((_sf, s: string, i: number, j?: number) => {
2024-10-11 21:34:27 +08:00
j = j ?? s.length;
return s.slice(i - 1, j);
}),
2024-10-20 21:06:23 +08:00
split: new LuaBuiltinFunction((_sf, s: string, sep: string) => {
2024-10-11 21:34:27 +08:00
return s.split(sep);
}),
2024-10-10 02:35:07 +08:00
});