Comparison

util-src/ringbuffer.c @ 10921:6eb5d2bb11af

util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions Actually just an alias of pushnil, but it does make it more obvious where the failure conditions are, which is good for readability.
author Kim Alvefur <zash@zash.se>
date Sun, 07 Jun 2020 02:25:56 +0200
parent 10901:5e33926f4b43
child 10953:c3b3ac63f4c3
comparison
equal deleted inserted replaced
10920:c171b4c59bd1 10921:6eb5d2bb11af
3 #include <unistd.h> 3 #include <unistd.h>
4 #include <string.h> 4 #include <string.h>
5 5
6 #include <lua.h> 6 #include <lua.h>
7 #include <lauxlib.h> 7 #include <lauxlib.h>
8
9 #if (LUA_VERSION_NUM < 504)
10 #define luaL_pushfail lua_pushnil
11 #endif
8 12
9 typedef struct { 13 typedef struct {
10 size_t rpos; /* read position */ 14 size_t rpos; /* read position */
11 size_t wpos; /* write position */ 15 size_t wpos; /* write position */
12 size_t alen; /* allocated size */ 16 size_t alen; /* allocated size */
150 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); 154 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
151 size_t r = luaL_checkinteger(L, 2); 155 size_t r = luaL_checkinteger(L, 2);
152 int peek = lua_toboolean(L, 3); 156 int peek = lua_toboolean(L, 3);
153 157
154 if(r > b->blen) { 158 if(r > b->blen) {
155 lua_pushnil(L); 159 luaL_pushfail(L);
156 return 1; 160 return 1;
157 } 161 }
158 162
159 if((b->rpos + r) > b->alen) { 163 if((b->rpos + r) > b->alen) {
160 /* Substring wraps around to the beginning of the buffer */ 164 /* Substring wraps around to the beginning of the buffer */
202 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); 206 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
203 const char *s = luaL_checklstring(L, 2, &l); 207 const char *s = luaL_checklstring(L, 2, &l);
204 208
205 /* Does `l` bytes fit? */ 209 /* Does `l` bytes fit? */
206 if((l + b->blen) > b->alen) { 210 if((l + b->blen) > b->alen) {
207 lua_pushnil(L); 211 luaL_pushfail(L);
208 return 1; 212 return 1;
209 } 213 }
210 214
211 while(l-- > 0) { 215 while(l-- > 0) {
212 writechar(b, *s++); 216 writechar(b, *s++);