aboutsummaryrefslogtreecommitdiff
path: root/gem/gem.c
diff options
context:
space:
mode:
authorThijs Schreijer <thijs@thijsschreijer.nl>2022-08-24 12:31:18 +0200
committerGitHub <noreply@github.com>2022-08-24 12:31:18 +0200
commit87c48f3e4ddba13d9c014067e62568ba906cc410 (patch)
treec99889ec0b9bbab2bc5ee14cd9046d0a122eb226 /gem/gem.c
parent95b7efa9da506ef968c1347edf3fc56370f0deed (diff)
parent97d5194f302d3fb9fe27874d9b5f73004a208d01 (diff)
downloadluasocket-87c48f3e4ddba13d9c014067e62568ba906cc410.tar.gz
luasocket-87c48f3e4ddba13d9c014067e62568ba906cc410.tar.bz2
luasocket-87c48f3e4ddba13d9c014067e62568ba906cc410.zip
Merge pull request #364 from lunarmodules/cleanup
Diffstat (limited to 'gem/gem.c')
-rw-r--r--gem/gem.c54
1 files changed, 0 insertions, 54 deletions
diff --git a/gem/gem.c b/gem/gem.c
deleted file mode 100644
index 976f74d..0000000
--- a/gem/gem.c
+++ /dev/null
@@ -1,54 +0,0 @@
1#include "lua.h"
2#include "lauxlib.h"
3
4#define CR '\xD'
5#define LF '\xA'
6#define CRLF "\xD\xA"
7
8#define candidate(c) (c == CR || c == LF)
9static int pushchar(int c, int last, const char *marker,
10 luaL_Buffer *buffer) {
11 if (candidate(c)) {
12 if (candidate(last)) {
13 if (c == last)
14 luaL_addstring(buffer, marker);
15 return 0;
16 } else {
17 luaL_addstring(buffer, marker);
18 return c;
19 }
20 } else {
21 luaL_putchar(buffer, c);
22 return 0;
23 }
24}
25
26static int eol(lua_State *L) {
27 int context = luaL_checkint(L, 1);
28 size_t isize = 0;
29 const char *input = luaL_optlstring(L, 2, NULL, &isize);
30 const char *last = input + isize;
31 const char *marker = luaL_optstring(L, 3, CRLF);
32 luaL_Buffer buffer;
33 luaL_buffinit(L, &buffer);
34 if (!input) {
35 lua_pushnil(L);
36 lua_pushnumber(L, 0);
37 return 2;
38 }
39 while (input < last)
40 context = pushchar(*input++, context, marker, &buffer);
41 luaL_pushresult(&buffer);
42 lua_pushnumber(L, context);
43 return 2;
44}
45
46static luaL_reg func[] = {
47 { "eol", eol },
48 { NULL, NULL }
49};
50
51int luaopen_gem(lua_State *L) {
52 luaL_openlib(L, "gem", func, 0);
53 return 0;
54}