aboutsummaryrefslogtreecommitdiffstats
path: root/util-src
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2018-02-24 14:45:06 +0100
committerKim Alvefur <zash@zash.se>2018-02-24 14:45:06 +0100
commit28ba10b6b49b37a6786fde91a9f2fd5b7cd3fa52 (patch)
tree210250b3b35f1f8cc84a608ace03148c78acffad /util-src
parenta92cdf75b1e145413fc0289fdb661b9850cef4c1 (diff)
downloadprosody-28ba10b6b49b37a6786fde91a9f2fd5b7cd3fa52.tar.gz
prosody-28ba10b6b49b37a6786fde91a9f2fd5b7cd3fa52.zip
util.ringbuffer: Add method for discarding buffered data without returning it to lua
Diffstat (limited to 'util-src')
-rw-r--r--util-src/ringbuffer.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/util-src/ringbuffer.c b/util-src/ringbuffer.c
index 233b9058..10fe0209 100644
--- a/util-src/ringbuffer.c
+++ b/util-src/ringbuffer.c
@@ -79,6 +79,27 @@ int rb_find(lua_State *L) {
}
/*
+ * 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);