Comparison

util-src/encodings.c @ 6592:141afe8a167b

util.encodings: Expose UTF-8 validation and length checking functions
author Kim Alvefur <zash@zash.se>
date Mon, 23 Mar 2015 14:27:30 +0100
parent 6591:fe3018a2f187
child 6602:61b6a4fc65f1
child 6604:478308ee29dd
child 6607:64e6b88b6b21
comparison
equal deleted inserted replaced
6591:fe3018a2f187 6592:141afe8a167b
173 *l = len; 173 *l = len;
174 } 174 }
175 return s; 175 return s;
176 } 176 }
177 177
178 static int Lutf8_valid(lua_State *L) {
179 lua_pushboolean(L, check_utf8(L, 1, NULL) != NULL);
180 return 1;
181 }
182
183 static int Lutf8_length(lua_State *L) {
184 size_t len;
185 if(!check_utf8(L, 1, &len)) {
186 lua_pushnil(L);
187 lua_pushliteral(L, "invalid utf8");
188 return 2;
189 }
190 lua_pushinteger(L, len);
191 return 1;
192 }
193
194 static const luaL_Reg Reg_utf8[] =
195 {
196 { "valid", Lutf8_valid },
197 { "length", Lutf8_length },
198 { NULL, NULL }
199 };
200
178 201
179 /***************** STRINGPREP *****************/ 202 /***************** STRINGPREP *****************/
180 #ifdef USE_STRINGPREP_ICU 203 #ifdef USE_STRINGPREP_ICU
181 204
182 #include <unicode/usprep.h> 205 #include <unicode/usprep.h>
450 lua_pushliteral(L, "idna"); 473 lua_pushliteral(L, "idna");
451 lua_newtable(L); 474 lua_newtable(L);
452 luaL_register(L, NULL, Reg_idna); 475 luaL_register(L, NULL, Reg_idna);
453 lua_settable(L,-3); 476 lua_settable(L,-3);
454 477
478 lua_pushliteral(L, "utf8");
479 lua_newtable(L);
480 luaL_register(L, NULL, Reg_utf8);
481 lua_settable(L, -3);
482
455 lua_pushliteral(L, "version"); /** version */ 483 lua_pushliteral(L, "version"); /** version */
456 lua_pushliteral(L, "-3.14"); 484 lua_pushliteral(L, "-3.14");
457 lua_settable(L,-3); 485 lua_settable(L,-3);
458 return 1; 486 return 1;
459 } 487 }