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