Software /
code /
prosody
Comparison
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 |
comparison
equal
deleted
inserted
replaced
8542:8c39ec8e6018 | 8543:0e1d8f2f02bf |
---|---|
37 | 37 |
38 if(b->rpos == b->wpos) { /* empty */ | 38 if(b->rpos == b->wpos) { /* empty */ |
39 return 0; | 39 return 0; |
40 } | 40 } |
41 | 41 |
42 /* look for a matching first byte */ | |
42 for(i = 0; i <= b->blen - l; i++) { | 43 for(i = 0; i <= b->blen - l; i++) { |
43 if(b->buffer[(b->rpos + i) % b->alen] == *s) { | 44 if(b->buffer[(b->rpos + i) % b->alen] == *s) { |
44 m = 1; | 45 m = 1; |
45 | 46 |
47 /* check if the following byte also match */ | |
46 for(j = 1; j < l; j++) | 48 for(j = 1; j < l; j++) |
47 if(b->buffer[(b->rpos + i + j) % b->alen] != s[j]) { | 49 if(b->buffer[(b->rpos + i + j) % b->alen] != s[j]) { |
48 m = 0; | 50 m = 0; |
49 break; | 51 break; |
50 } | 52 } |
56 } | 58 } |
57 | 59 |
58 return 0; | 60 return 0; |
59 } | 61 } |
60 | 62 |
63 /* | |
64 * Find first position of a substring in buffer | |
65 * (buffer, string) -> number | |
66 */ | |
61 int rb_find(lua_State *L) { | 67 int rb_find(lua_State *L) { |
62 size_t l, m; | 68 size_t l, m; |
63 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); | 69 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); |
64 const char *s = luaL_checklstring(L, 2, &l); | 70 const char *s = luaL_checklstring(L, 2, &l); |
65 m = find(b, s, l); | 71 m = find(b, s, l); |
70 } | 76 } |
71 | 77 |
72 return 0; | 78 return 0; |
73 } | 79 } |
74 | 80 |
81 /* | |
82 * Read bytes from buffer | |
83 * (buffer, number, boolean?) -> string | |
84 */ | |
75 int rb_read(lua_State *L) { | 85 int rb_read(lua_State *L) { |
76 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); | 86 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); |
77 size_t r = luaL_checkinteger(L, 2); | 87 size_t r = luaL_checkinteger(L, 2); |
78 int peek = lua_toboolean(L, 3); | 88 int peek = lua_toboolean(L, 3); |
79 | 89 |
81 lua_pushnil(L); | 91 lua_pushnil(L); |
82 return 1; | 92 return 1; |
83 } | 93 } |
84 | 94 |
85 if((b->rpos + r) > b->alen) { | 95 if((b->rpos + r) > b->alen) { |
96 /* Substring wraps around to the beginning of the buffer */ | |
86 lua_pushlstring(L, &b->buffer[b->rpos], b->alen - b->rpos); | 97 lua_pushlstring(L, &b->buffer[b->rpos], b->alen - b->rpos); |
87 lua_pushlstring(L, b->buffer, r - (b->alen - b->rpos)); | 98 lua_pushlstring(L, b->buffer, r - (b->alen - b->rpos)); |
88 lua_concat(L, 2); | 99 lua_concat(L, 2); |
89 } else { | 100 } else { |
90 lua_pushlstring(L, &b->buffer[b->rpos], r); | 101 lua_pushlstring(L, &b->buffer[b->rpos], r); |
97 } | 108 } |
98 | 109 |
99 return 1; | 110 return 1; |
100 } | 111 } |
101 | 112 |
113 /* | |
114 * Read buffer until first occurence of a substring | |
115 * (buffer, string) -> string | |
116 */ | |
102 int rb_readuntil(lua_State *L) { | 117 int rb_readuntil(lua_State *L) { |
103 size_t l, m; | 118 size_t l, m; |
104 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); | 119 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); |
105 const char *s = luaL_checklstring(L, 2, &l); | 120 const char *s = luaL_checklstring(L, 2, &l); |
106 m = find(b, s, l); | 121 m = find(b, s, l); |
112 } | 127 } |
113 | 128 |
114 return 0; | 129 return 0; |
115 } | 130 } |
116 | 131 |
132 /* | |
133 * Write bytes into the buffer | |
134 * (buffer, string) -> integer | |
135 */ | |
117 int rb_write(lua_State *L) { | 136 int rb_write(lua_State *L) { |
118 size_t l, w = 0; | 137 size_t l, w = 0; |
119 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); | 138 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); |
120 const char *s = luaL_checklstring(L, 2, &l); | 139 const char *s = luaL_checklstring(L, 2, &l); |
121 | 140 |