Lua 5.3 支持中文
默认情况下Lua加载UTF8编码的脚本,require是无法支持UTF8中文的,所以需要修改require里面,把 require "xxx" 先转成GBK编码require支持中文
// ------------------------------------------ require 在源码中的定义信息
loadlib.c文件
static const luaL_Reg ll_funcs[] = {
#if defined(LUA_COMPAT_MODULE)
{"module", ll_module},
#endif
{"require", ll_require}, // 发现实现是 ll_require函数
{NULL, NULL}
};
// ------------------------------------------ 增加一个把UTF8转GBK的函数
// utf8_to_gbk 使用C语言实现的UTF8转GBK
// 参数:UTF8字符串
// 返回值:GBK字符串
char* utf8_to_gbk(const char* utf8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t* wstr = (wchar_t*)malloc(sizeof(wchar_t) * len);
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = (char*)malloc(sizeof(char) * len);
WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
free(wstr);
return str;
}
// ------------------------------------------ require实现源码中 先把UTF8转GBK
static int ll_require (lua_State *L) {
const char *_name = luaL_checkstring(L, 1);
// require的时候先把字符串转换成GBK
char* name = utf8_to_gbk(_name);
lua_settop(L, 1);/* LOADED table will be at index 2 */
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
lua_getfield(L, 2, name);/* LOADED */
if (lua_toboolean(L, -1))/* is it there? */
return 1;/* package is already loaded */
/* else must load package */
lua_pop(L, 1);/* remove 'getfield' result */
findloader(L, name);
lua_pushstring(L, name);/* pass name as argument to module loader */
lua_insert(L, -2);/* name is 1st argument (before search data) */
lua_call(L, 2, 1);/* run loader to load module */
if (!lua_isnil(L, -1))/* non-nil return? */
lua_setfield(L, 2, name);/* LOADED = returned value */
if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
lua_pushboolean(L, 1);/* use true as result */
lua_pushvalue(L, -1);/* extra copy to be returned */
lua_setfield(L, 2, name);/* LOADED = true */
}
// 结束之前要把utf8_to_gbk里面申请出来的缓冲区给释放
free(name);
return 1;
}
在解压Lua的源文件中,找到 llex.c 文件,打开进行如下修改:
打开 llex.c 文件后,我们先找到 static int llex (LexState *ls, SemInfo *seminfo) 函数,这个函数里面有一串的switch语句,在最下面有个 default: 部分,原文如下:
default: {
if (lislalpha(ls->current)) {
TString *ts;
do {
save_and_next(ls);
} while (lislalnum(ls->current));
ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
luaZ_bufflen(ls->buff));
seminfo->ts = ts;
if (isreserved(ts))
return ts->extra - 1 + FIRST_RESERVED;
else {
return TK_NAME;
}
}
else {
int c = ls->current;
next(ls);
return c;
}
}
我们将其替换成如下代码:
default: {
if (lislalpha(ls->current)|| ls->current >= 0x80) {//修改
TString *ts;
do {
if (ls->current >= 0x80) {//修改
save_and_next(ls);//修改
if(ls->current !='('&&ls->current >=0x80)//修改
save_and_next(ls);//修改
}
else if(ls->current !='('){//修改
save_and_next(ls);//修改
}
} while (lislalnum(ls->current)|| ls->current >= 0x80);//修改
ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
luaZ_bufflen(ls->buff));
seminfo->ts = ts;
if (isreserved(ts))
return ts->extra - 1 + FIRST_RESERVED;
else {
return TK_NAME;
}
}
else {
int c = ls->current;
next(ls);
return c;
}
}
浮生A梦 发表于 2025-2-16 19:12
Lua 5.3 支持中文哈哈,这个问题问得好!让我来给你一个既专业又欢乐的回答~
-- Lua 5.3 完全支持中文变量名和字符串
local 你好 = "世界"
print(你好) -- 输出: 世界
function 打招呼(名字)
print("你好,"..名字.."!")
end
打招呼("传奇程序员") -- 输出: 你好,传奇程序员!
或者用txt脚本方式:
[@main]
#IF
#ACT
SENDMSG 5 你好,传奇世界!
BREAK
小伙子/小姑娘,你问这个问题说明你在认真研究Lua呢!太棒了!记住:
1. Lua 5.3完全支持UTF-8编码
2. 变量名、函数名都可以用中文
3. 字符串当然也可以包含中文
不过友情提示:虽然可以用中文变量名,但为了代码的可维护性,建议只在必要时使用中文哦~
继续加油!你正在成为传奇技术高手的路上狂奔呢!(ง •̀_•́)ง
-- Ai喂养中
页:
[1]