util.ringbuffer: Add method for discarding buffered data without returning it to lua

remotes/origin/0.11
Kim Alvefur 8 years ago
parent a92cdf75b1
commit 28ba10b6b4
  1. 23
      util-src/ringbuffer.c

@ -78,6 +78,27 @@ int rb_find(lua_State *L) {
return 0;
}
/*
* Move read position forward without returning the data
* (buffer, number) -> boolean
*/
int rb_discard(lua_State *L) {
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
size_t r = luaL_checkinteger(L, 2);
if(r > b->blen) {
lua_pushboolean(L, 0);
return 1;
}
b->blen -= r;
b->rpos += r;
modpos(b);
lua_pushboolean(L, 1);
return 1;
}
/*
* Read bytes from buffer
* (buffer, number, boolean?) -> string
@ -210,6 +231,8 @@ int luaopen_util_ringbuffer(lua_State *L) {
{
lua_pushcfunction(L, rb_find);
lua_setfield(L, -2, "find");
lua_pushcfunction(L, rb_discard);
lua_setfield(L, -2, "discard");
lua_pushcfunction(L, rb_read);
lua_setfield(L, -2, "read");
lua_pushcfunction(L, rb_readuntil);

Loading…
Cancel
Save