-
Notifications
You must be signed in to change notification settings - Fork 49
/
lua-utf8.c
169 lines (146 loc) · 3.36 KB
/
lua-utf8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#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<step; j++) {
v = v << 6;
v = v | (str[i+j] & 0x3f);
}
return v;
}
#define FILL_LOW_BITS(str, pos, rune) str[pos] = (rune & 0x3f) | 0x80; rune >>= 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<len;) {
c = str[i];
step = _steps(c);
if(step == 0 || len < i + step) {
count = -1;
break;
}
lua_pushinteger(L, _decode(str, i, step));
count = count + 1;
lua_rawseti(L, 2, count);
i = i + step;
}
if(count < 0) {
return 0;
}
lua_pushinteger(L, count);
return 1;
}
static int
_toutf8(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t sz = 0;
size_t len = lua_rawlen(L, 1);
size_t i;
for(i = 1; i <= len; i++) {
lua_rawgeti(L, 1, i);
int isnum;
uint32_t rune = (uint32_t)lua_tointegerx(L, -1, &isnum);
lua_pop(L, 1);
if(!isnum) {
return 0;
}
int bytes = _bytes(rune);
if(!bytes) {
return 0;
}
sz += bytes;
}
uint8_t *str = lua_newuserdata(L, sz);
uint8_t *tmp = str;
for(i = 1; i <= len; i++) {
lua_rawgeti(L, 1, i);
uint32_t rune = lua_tointeger(L, -1);
lua_pop(L, 1);
int bytes = _bytes(rune);
tmp = _encode(rune, bytes, tmp);
}
lua_pushlstring(L, (char*)str, sz);
return 1;
}
static int
_len(lua_State *L) {
size_t len;
const char* str = luaL_checklstring(L, 1, &len);
int count = 0;
int i, step;
uint8_t c;
for(i=0;i<len;) {
c = str[i];
step = _steps(c);
i = i + step;
if(!step || len < i) {
count = -1;
break;
}
count = count + 1;
}
if(count < 0) {
return 0;
}
lua_pushinteger(L, count);
return 1;
}
int
luaopen_utf8_c(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{"len", _len},
{"toutf32", _toutf32},
{"toutf8", _toutf8},
{NULL, NULL}
};
luaL_newlib(L, l);
return 1;
}