aboutsummaryrefslogtreecommitdiff
path: root/gem/gem.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-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}