# HG changeset patch # User Kim Alvefur # Date 1427117250 -3600 # Node ID 141afe8a167bd2f14383ada3a1aff60f6513c1d8 # Parent fe3018a2f187b238cf08637210a2e803427980a3 util.encodings: Expose UTF-8 validation and length checking functions diff -r fe3018a2f187 -r 141afe8a167b util-src/encodings.c --- a/util-src/encodings.c Mon Mar 23 14:26:34 2015 +0100 +++ b/util-src/encodings.c Mon Mar 23 14:27:30 2015 +0100 @@ -175,6 +175,29 @@ return s; } +static int Lutf8_valid(lua_State *L) { + lua_pushboolean(L, check_utf8(L, 1, NULL) != NULL); + return 1; +} + +static int Lutf8_length(lua_State *L) { + size_t len; + if(!check_utf8(L, 1, &len)) { + lua_pushnil(L); + lua_pushliteral(L, "invalid utf8"); + return 2; + } + lua_pushinteger(L, len); + return 1; +} + +static const luaL_Reg Reg_utf8[] = +{ + { "valid", Lutf8_valid }, + { "length", Lutf8_length }, + { NULL, NULL } +}; + /***************** STRINGPREP *****************/ #ifdef USE_STRINGPREP_ICU @@ -452,6 +475,11 @@ luaL_register(L, NULL, Reg_idna); lua_settable(L,-3); + lua_pushliteral(L, "utf8"); + lua_newtable(L); + luaL_register(L, NULL, Reg_utf8); + lua_settable(L, -3); + lua_pushliteral(L, "version"); /** version */ lua_pushliteral(L, "-3.14"); lua_settable(L,-3);