From a2cea3da193bdbf0e2754109982befafd8db7858 Mon Sep 17 00:00:00 2001 From: Thomas Harning Jr Date: Fri, 7 Sep 2007 23:55:20 -0400 Subject: event_buffer: drain learned spec notes, get_data learned negative values, many tests written The additional tests added were tests for: * Obtaining partial data using get_data * Reading lines using various line ending mixes/etc * Draining values w/ negative value --- src/event_buffer.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src/event_buffer.c') diff --git a/src/event_buffer.c b/src/event_buffer.c index 110a41c..72c6abe 100644 --- a/src/event_buffer.c +++ b/src/event_buffer.c @@ -117,6 +117,9 @@ static int event_buffer_get_length(lua_State* L) { () - Returns all data in buffer (len) - Returns data up to 'len' bytes long (begin,len) - Returns data beginning at 'begin' up to 'len' bytes long + If begin < 0, wraps at data length + ex: (-1, 1) returns last character + (-2,2) returns last 2 chars [length meaning does not get inverted] */ static int event_buffer_get_data(lua_State* L) { le_buffer* buf = event_buffer_check(L, 1); @@ -135,8 +138,18 @@ static int event_buffer_get_data(lua_State* L) { break; case 3: default: + /* - 1 to map it to Lua's 1-based indexing + * If begin < 0 add length to cause position wrapping + */ begin = luaL_checkinteger(L, 2); + if(begin < 0) + begin += EVBUFFER_LENGTH(buf->buffer); + else + begin--; len = luaL_checkinteger(L, 3); + /* If length is less than zero, capture entire remaining string */ + + if(len < 0) len = EVBUFFER_LENGTH(buf->buffer); if(begin > EVBUFFER_LENGTH(buf->buffer)) begin = EVBUFFER_LENGTH(buf->buffer); if(begin + len > EVBUFFER_LENGTH(buf->buffer)) @@ -150,7 +163,7 @@ static int event_buffer_get_data(lua_State* L) { /* LUA: buffer:readline() Returns a line terminated by either '\r\n','\n\r' or '\r' or '\n' Returns nil and leaves data alone if no terminator is found - TODO: Evaluate whether or not the newline is included + Newline is not present in the captured string. */ static int event_buffer_readline(lua_State* L) { le_buffer* buf = event_buffer_check(L, 1); @@ -164,6 +177,8 @@ static int event_buffer_readline(lua_State* L) { /* LUA: buffer:drain(amt) Drains 'amt' bytes from the buffer + If amt < 0, drains all data + (Due to auto-casting to unsigned int and automatic capping) */ static int event_buffer_drain(lua_State* L) { le_buffer* buf = event_buffer_check(L, 1); -- cgit v1.2.3