# HG changeset patch # User Kim Alvefur # Date 1519479906 -3600 # Node ID e7214441523b449c62ddeeb9d8aa29ff147aff07 # Parent 0e1d8f2f02bf93f2cd2bac86bddd33e8b5e0d84b util.ringbuffer: Add method for discarding buffered data without returning it to lua diff -r 0e1d8f2f02bf -r e7214441523b util-src/ringbuffer.c --- a/util-src/ringbuffer.c Sat Feb 24 14:44:46 2018 +0100 +++ b/util-src/ringbuffer.c Sat Feb 24 14:45:06 2018 +0100 @@ -79,6 +79,27 @@ } /* + * 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 @@ { 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);