#include #include #include #include "lua.h" #include "lauxlib.h" inline static int _steps(uint8_t c) { if(c < 0x80) return 1; if(c < 0xc0) return 0; if(c < 0xe0) return 2; if(c < 0xf0) return 3; if(c < 0xf8) return 4; return 0; } inline static int _bytes(uint32_t rune) { if(rune < 0x80) return 1; if(rune < 0x800) return 2; if(rune < 0x10000) return 3; if(rune < 0x110000) return 4; return 0; } inline static uint32_t _decode(const char *str, int i, int step) { uint8_t c = str[i]; uint32_t v = c & (0xff >> step); int j = 1; for(;j>= 6; inline static uint8_t* _encode(uint32_t rune, int bytes, uint8_t* str) { if (bytes == 1) { str[0] = rune & 0x7f; } else if(bytes == 2) { FILL_LOW_BITS(str, 1, rune) str[0] = rune | 0xc0; } else if(bytes == 3) { FILL_LOW_BITS(str, 2, rune) FILL_LOW_BITS(str, 1, rune) str[0] = rune | 0xe0; } else { FILL_LOW_BITS(str, 3, rune) FILL_LOW_BITS(str, 2, rune) FILL_LOW_BITS(str, 1, rune) str[0] = rune | 0xf0; } return str + bytes; } static int _toutf32(lua_State *L) { size_t len; const char* str = luaL_checklstring(L, 1, &len); luaL_checktype(L, 2, LUA_TTABLE); int count = 0; int i, step; uint8_t c; for(i=0;i