Annotate

util-src/strbitop.c @ 11163:37a6a535343e 0.11

util.strbitop: Library for bitwise operations on strings
author Kim Alvefur <zash@zash.se>
date Sat, 07 Sep 2019 13:37:47 +0200
child 11167:ba32b9a6d75b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 /*
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 * This project is MIT licensed. Please see the
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 * COPYING file in the source package for more information.
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 *
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 * Copyright (C) 2016 Kim Alvefur
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 */
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 #include <lua.h>
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 #include <lauxlib.h>
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 #if (LUA_VERSION_NUM == 501)
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 #endif
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 /* TODO Deduplicate code somehow */
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 int strop_and(lua_State* L) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 luaL_Buffer buf;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 size_t a, b, i;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 const char* str_a = luaL_checklstring(L, 1, &a);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 const char* str_b = luaL_checklstring(L, 2, &b);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 luaL_buffinit(L, &buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 for(i = 0; i < a; i++) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 luaL_addchar(&buf, str_a[i] & str_b[i % b]);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 int strop_or(lua_State* L) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 luaL_Buffer buf;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 size_t a, b, i;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 const char* str_a = luaL_checklstring(L, 1, &a);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 const char* str_b = luaL_checklstring(L, 2, &b);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 luaL_buffinit(L, &buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 for(i = 0; i < a; i++) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 luaL_addchar(&buf, str_a[i] | str_b[i % b]);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 int strop_xor(lua_State* L) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 luaL_Buffer buf;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 size_t a, b, i;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 const char* str_a = luaL_checklstring(L, 1, &a);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 const char* str_b = luaL_checklstring(L, 2, &b);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 luaL_buffinit(L, &buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 for(i = 0; i < a; i++) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 luaL_addchar(&buf, str_a[i] ^ str_b[i % b]);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 LUA_API int luaopen_util_strbitop(lua_State *L) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 luaL_Reg exports[] = {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 { "sand", strop_and },
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 { "sor", strop_or },
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 { "sxor", strop_xor },
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 { NULL, NULL }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 };
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 lua_newtable(L);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 luaL_setfuncs(L, exports, 0);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 }