Skip to content

Commit 5effaf8

Browse files
committed
add unsafe interface for decode
1 parent 687dd09 commit 5effaf8

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

pb.c

+25-3
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,13 @@ static int Lslice_leave(lua_State *L) {
10771077
return 2;
10781078
}
10791079

1080+
LUALIB_API int lpb_newslice(lua_State *L, const char *s, size_t len) {
1081+
lpb_SliceEx *S = (lpb_SliceEx*)lua_newuserdata(L, sizeof(lpb_SliceEx));
1082+
*S = lpb_initext(pb_lslice(s, len));
1083+
luaL_setmetatable(L, PB_SLICE);
1084+
return 1;
1085+
}
1086+
10801087
LUALIB_API int luaopen_pb_slice(lua_State *L) {
10811088
luaL_Reg libs[] = {
10821089
{ "__tostring", Lslice_tostring },
@@ -1672,11 +1679,9 @@ static int lpb_decode(lpb_Env *e, pb_Type *t) {
16721679
return 1;
16731680
}
16741681

1675-
static int Lpb_decode(lua_State *L) {
1682+
static int lpb_decode_ex(lua_State *L, lpb_SliceEx s) {
16761683
lpb_State *LS = default_lstate(L);
16771684
pb_Type *t = lpb_type(&LS->base, luaL_checkstring(L, 1));
1678-
lpb_SliceEx s = lua_isnoneornil(L, 2) ? lpb_initext(pb_lslice(NULL, 0))
1679-
: lpb_initext(lpb_checkslice(L, 2));
16801685
lpb_Env e;
16811686
argcheck(L, t!=NULL, 1, "type '%s' does not exists", lua_tostring(L, 1));
16821687
lua_settop(L, 3);
@@ -1688,6 +1693,11 @@ static int Lpb_decode(lua_State *L) {
16881693
return lpb_decode(&e, t);
16891694
}
16901695

1696+
static int Lpb_decode(lua_State *L) {
1697+
lpb_SliceEx s = lua_isnoneornil(L, 2) ? lpb_initext(pb_lslice(NULL, 0))
1698+
: lpb_initext(lpb_checkslice(L, 2));
1699+
return lpb_decode_ex(L, s);
1700+
}
16911701

16921702
/* pb module interface */
16931703

@@ -1756,6 +1766,18 @@ LUALIB_API int luaopen_pb(lua_State *L) {
17561766
return 1;
17571767
}
17581768

1769+
static int Lpb_decode_unsafe(lua_State *L) {
1770+
return lpb_decode_ex(L,
1771+
lpb_initext(pb_lslice(
1772+
(const char*)lua_touserdata(L, 2),
1773+
(size_t)luaL_checkinteger(L, 3))));
1774+
}
1775+
1776+
LUALIB_API int luaopen_pb_decode_unsafe(lua_State *L) {
1777+
lua_pushcfunction(L, Lpb_decode_unsafe);
1778+
return 1;
1779+
}
1780+
17591781

17601782
PB_NS_END
17611783

0 commit comments

Comments
 (0)