Software /
code /
prosody
Diff
util-src/ringbuffer.c @ 8543:0e1d8f2f02bf
util.ringbuffer: Add various comments
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 24 Feb 2018 14:44:46 +0100 |
parent | 7969:1c6a07606309 |
child | 8544:e7214441523b |
line wrap: on
line diff
--- a/util-src/ringbuffer.c Sat Feb 24 14:39:36 2018 +0100 +++ b/util-src/ringbuffer.c Sat Feb 24 14:44:46 2018 +0100 @@ -39,10 +39,12 @@ return 0; } + /* look for a matching first byte */ for(i = 0; i <= b->blen - l; i++) { if(b->buffer[(b->rpos + i) % b->alen] == *s) { m = 1; + /* check if the following byte also match */ for(j = 1; j < l; j++) if(b->buffer[(b->rpos + i + j) % b->alen] != s[j]) { m = 0; @@ -58,6 +60,10 @@ return 0; } +/* + * Find first position of a substring in buffer + * (buffer, string) -> number + */ int rb_find(lua_State *L) { size_t l, m; ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); @@ -72,6 +78,10 @@ return 0; } +/* + * Read bytes from buffer + * (buffer, number, boolean?) -> string + */ int rb_read(lua_State *L) { ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); size_t r = luaL_checkinteger(L, 2); @@ -83,6 +93,7 @@ } if((b->rpos + r) > b->alen) { + /* Substring wraps around to the beginning of the buffer */ lua_pushlstring(L, &b->buffer[b->rpos], b->alen - b->rpos); lua_pushlstring(L, b->buffer, r - (b->alen - b->rpos)); lua_concat(L, 2); @@ -99,6 +110,10 @@ return 1; } +/* + * Read buffer until first occurence of a substring + * (buffer, string) -> string + */ int rb_readuntil(lua_State *L) { size_t l, m; ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); @@ -114,6 +129,10 @@ return 0; } +/* + * Write bytes into the buffer + * (buffer, string) -> integer + */ int rb_write(lua_State *L) { size_t l, w = 0; ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");