游戏王残局简化版

u8_to_a.lua at [5e3f812cd3]
Login

u8_to_a.lua at [5e3f812cd3]

File 残局文料/lua_lib/u8_to_a.lua artifact 78713afee6 part of check-in 5e3f812cd3


local ffi = require("ffi")
local s = {}
ffi.cdef([[
typedef unsigned int UINT;
typedef unsigned long DWORD;
typedef wchar_t WCHAR;
typedef WCHAR* LPWSTR;
typedef wchar_t* LPCWCH;
typedef char CHAR;
typedef CHAR* LPSTR;
typedef const char* LPCCH;
typedef int BOOL;
typedef BOOL* LPBOOL;
int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCCH lpMultiByteStr, int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar);
int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWCH lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr, int cchMultiByte, LPCCH lpDefaultChar, LPBOOL pfUsedDefaultChar);
]])
s.CP_ACP = 0
s.CP_UTF8 = 65001
--~ 算机_基统_窗群
function s.m2w(input, u1)
  local wlen = ffi.C.MultiByteToWideChar(u1 or s.CP_ACP, 0, input, #input, nil, 0)
  local wstr = ffi.new("wchar_t[?]", wlen + 1)
  ffi.C.MultiByteToWideChar(u1 or s.CP_ACP, 0, input, #input, wstr, wlen)
  return wstr, wlen
end
function s.w2m(wstr, wlen, u1)
  local len = ffi.C.WideCharToMultiByte(u1 or s.CP_ACP, 0, wstr, wlen or -1, nil, 0, nil, nil)
  local str = ffi.new("char[?]", len)
  ffi.C.WideCharToMultiByte(u1 or s.CP_ACP, 0, wstr, wlen or -1, str, len, nil, nil)
  return ffi.string(str, len)
end
--~ 通用大字集_八位
function s.u8_to_a(input)
  return s.w2m(s.m2w(input, s.CP_UTF8))
end
function s.a_to_u8(input)
  local h1, h2 = s.m2w(input)
  return s.w2m(h1, h2, s.CP_UTF8)
end
return s