From ff0f488c97fe8b554b909a0057cebc4c860eac8f Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 20 Oct 2021 13:40:34 +0800 Subject: +luasocket src --- ThirdParty/luasocket/gem/gem.c | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 ThirdParty/luasocket/gem/gem.c (limited to 'ThirdParty/luasocket/gem/gem.c') diff --git a/ThirdParty/luasocket/gem/gem.c b/ThirdParty/luasocket/gem/gem.c new file mode 100644 index 0000000..976f74d --- /dev/null +++ b/ThirdParty/luasocket/gem/gem.c @@ -0,0 +1,54 @@ +#include "lua.h" +#include "lauxlib.h" + +#define CR '\xD' +#define LF '\xA' +#define CRLF "\xD\xA" + +#define candidate(c) (c == CR || c == LF) +static int pushchar(int c, int last, const char *marker, + luaL_Buffer *buffer) { + if (candidate(c)) { + if (candidate(last)) { + if (c == last) + luaL_addstring(buffer, marker); + return 0; + } else { + luaL_addstring(buffer, marker); + return c; + } + } else { + luaL_putchar(buffer, c); + return 0; + } +} + +static int eol(lua_State *L) { + int context = luaL_checkint(L, 1); + size_t isize = 0; + const char *input = luaL_optlstring(L, 2, NULL, &isize); + const char *last = input + isize; + const char *marker = luaL_optstring(L, 3, CRLF); + luaL_Buffer buffer; + luaL_buffinit(L, &buffer); + if (!input) { + lua_pushnil(L); + lua_pushnumber(L, 0); + return 2; + } + while (input < last) + context = pushchar(*input++, context, marker, &buffer); + luaL_pushresult(&buffer); + lua_pushnumber(L, context); + return 2; +} + +static luaL_reg func[] = { + { "eol", eol }, + { NULL, NULL } +}; + +int luaopen_gem(lua_State *L) { + luaL_openlib(L, "gem", func, 0); + return 0; +} -- cgit v1.1-26-g67d0