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