Skip to content

lua5.4 or lua JIT中文lua编程(中文变量,中文函数,中文lua文件名)最佳实践 #193

@qqq694637644

Description

@qqq694637644

首先,lua5.4 or lua JIT 宏定义LUA_UCID后是可以支持中文变量,函数

(这条存疑)如果vscode显示文件名不为乱码不用修改
修改LuaPanda.lua取路径函数,处理输入gbk编码时vscode显示文件名为乱码问题

function this.getPath( info )
在函数最后三行,加入编码转换filePath = GbkToUtf8(filePath)

    filePath = GbkToUtf8(filePath)
    --放入Cache中缓存
    this.setCacheFormatPath(originalPath, filePath);
    print("filePath " .. filePath)
    return filePath;
end
    //使用sol2进行函数绑定
	lua.set_function(u8"GbkToUtf8", [](const std::string& gbkStr) -> std::string {
		return 字符串::GbkToUtf8(gbkStr);
		});

	export std::string GbkToUtf8(std::string src_str)
	{
		int len = MultiByteToWideChar(CP_ACP, 0, src_str.c_str(), -1, NULL, 0);
		wchar_t* wstr = new wchar_t[len + 1];
		memset(wstr, 0, len + 1);
		MultiByteToWideChar(CP_ACP, 0, src_str.c_str(), -1, wstr, len);
		len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
		char* str = new char[len + 1];
		memset(str, 0, len + 1);
		WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
		std::string strTemp = str;
		if (wstr) delete[] wstr;
		if (str) delete[] str;
		return strTemp;
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions