|
- 默认情况下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[name] */
- 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[name] = 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[name] = 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;
- }
- }
复制代码
|
本帖子中包含更多资源
您需要 登录
才可以下载或查看,没有帐号?立即注册
x
|