60c256f91b 2024-06-27 1: local ffi = require("ffi")
60c256f91b 2024-06-27 2: local s = {}
60c256f91b 2024-06-27 3: ffi.cdef([[
60c256f91b 2024-06-27 4: typedef unsigned int UINT;
60c256f91b 2024-06-27 5: typedef unsigned long DWORD;
60c256f91b 2024-06-27 6: typedef wchar_t WCHAR;
60c256f91b 2024-06-27 7: typedef WCHAR* LPWSTR;
60c256f91b 2024-06-27 8: typedef wchar_t* LPCWCH;
60c256f91b 2024-06-27 9: typedef char CHAR;
60c256f91b 2024-06-27 10: typedef CHAR* LPSTR;
60c256f91b 2024-06-27 11: typedef const char* LPCCH;
60c256f91b 2024-06-27 12: typedef int BOOL;
60c256f91b 2024-06-27 13: typedef BOOL* LPBOOL;
60c256f91b 2024-06-27 14: int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCCH lpMultiByteStr, int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar);
60c256f91b 2024-06-27 15: int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWCH lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr, int cchMultiByte, LPCCH lpDefaultChar, LPBOOL pfUsedDefaultChar);
60c256f91b 2024-06-27 16: ]])
60c256f91b 2024-06-27 17: s.CP_ACP = 0
60c256f91b 2024-06-27 18: s.CP_UTF8 = 65001
60c256f91b 2024-06-27 19: --~ 算机_基统_窗群
60c256f91b 2024-06-27 20: function s.m2w(input, u1)
60c256f91b 2024-06-27 21: local wlen = ffi.C.MultiByteToWideChar(u1 or s.CP_ACP, 0, input, #input, nil, 0)
60c256f91b 2024-06-27 22: local wstr = ffi.new("wchar_t[?]", wlen + 1)
60c256f91b 2024-06-27 23: ffi.C.MultiByteToWideChar(u1 or s.CP_ACP, 0, input, #input, wstr, wlen)
60c256f91b 2024-06-27 24: return wstr, wlen
60c256f91b 2024-06-27 25: end
60c256f91b 2024-06-27 26: function s.w2m(wstr, wlen, u1)
60c256f91b 2024-06-27 27: local len = ffi.C.WideCharToMultiByte(u1 or s.CP_ACP, 0, wstr, wlen or -1, nil, 0, nil, nil)
60c256f91b 2024-06-27 28: local str = ffi.new("char[?]", len)
60c256f91b 2024-06-27 29: ffi.C.WideCharToMultiByte(u1 or s.CP_ACP, 0, wstr, wlen or -1, str, len, nil, nil)
60c256f91b 2024-06-27 30: return ffi.string(str, len)
60c256f91b 2024-06-27 31: end
60c256f91b 2024-06-27 32: --~ 通用大字集_八位
60c256f91b 2024-06-27 33: function s.u8_to_a(input)
60c256f91b 2024-06-27 34: return s.w2m(s.m2w(input, s.CP_UTF8))
60c256f91b 2024-06-27 35: end
60c256f91b 2024-06-27 36: function s.a_to_u8(input)
60c256f91b 2024-06-27 37: local h1, h2 = s.m2w(input)
60c256f91b 2024-06-27 38: return s.w2m(h1, h2, s.CP_UTF8)
60c256f91b 2024-06-27 39: end
60c256f91b 2024-06-27 40: return s