summaryrefslogtreecommitdiff
path: root/ThirdParty/luasocket/gem/gem.c
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-20 13:40:34 +0800
committerchai <chaifix@163.com>2021-10-20 13:40:34 +0800
commitff0f488c97fe8b554b909a0057cebc4c860eac8f (patch)
tree4e47262b52ffce7e9cfeaaeeab46371243bcaa78 /ThirdParty/luasocket/gem/gem.c
parentdde719dd575090b36aaa3ad85bb3cabf33f36c5a (diff)
+luasocket src
Diffstat (limited to 'ThirdParty/luasocket/gem/gem.c')
-rw-r--r--ThirdParty/luasocket/gem/gem.c54
1 files changed, 54 insertions, 0 deletions
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;
+}