forked from CriztianiX/lua-resty-nanoid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nanoid.c
46 lines (38 loc) · 876 Bytes
/
nanoid.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
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <sys/time.h>
#include "nanoid.h"
#if LUA_VERSION_NUM < 502
#define luaL_newlib(L, l) (lua_newtable(L), luaL_register(L, NULL, l))
#endif
static int l_generate( lua_State *L )
{
lua_Integer n = lua_tointeger(L, 1);
char *id = generate(n);
lua_pushstring(L, id);
return 1;
}
static int l_simple( lua_State *L )
{
char *id = simple();
lua_pushstring(L, id);
return 1;
}
static int l_safe_simple( lua_State *L )
{
char *id = safe_simple();
lua_pushstring(L, id);
return 1;
}
int luaopen_nanoid( lua_State *L )
{
static const luaL_Reg nanoid[] = {
{ "generate", l_generate },
{ "simple", l_simple },
{ "safe_simple", l_safe_simple },
{ NULL, NULL } /* Sentinel item */
};
luaL_newlib( L, nanoid );
return 1;
}