Annotate

util-src/strbitop.c @ 11172:712b2e6a09d9 0.11

Back out 6dde2c9fa272: Doesn't work on Lua 5.1
author Kim Alvefur <zash@zash.se>
date Thu, 15 Oct 2020 17:12:33 +0200
parent 11171:2c1583bb0e0f
child 11175:235537247aa3
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 *
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
5 * Copyright (C) 2016 Kim Alvefur
11163
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
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
17 int strop_and(lua_State *L) {
11163
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;
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
20 const char *str_a = luaL_checklstring(L, 1, &a);
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
21 const char *str_b = luaL_checklstring(L, 2, &b);
11163
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 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 for(i = 0; i < a; i++) {
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
29 luaL_addchar(&buf, str_a[i] & str_b[i % b]);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
36 int strop_or(lua_State *L) {
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 luaL_Buffer buf;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 size_t a, b, i;
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
39 const char *str_a = luaL_checklstring(L, 1, &a);
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
40 const char *str_b = luaL_checklstring(L, 2, &b);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 return 1;
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
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 for(i = 0; i < a; i++) {
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
48 luaL_addchar(&buf, str_a[i] | str_b[i % b]);
11163
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 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 return 1;
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
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
55 int strop_xor(lua_State *L) {
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 luaL_Buffer buf;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 size_t a, b, i;
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
58 const char *str_a = luaL_checklstring(L, 1, &a);
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
59 const char *str_b = luaL_checklstring(L, 2, &b);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
61 luaL_buffinit(L, &buf);
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
62
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 return 1;
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
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 for(i = 0; i < a; i++) {
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
69 luaL_addchar(&buf, str_a[i] ^ str_b[i % b]);
11163
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 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 return 1;
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 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
77 luaL_Reg exports[] = {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 { "sand", strop_and },
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 { "sor", strop_or },
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 { "sxor", strop_xor },
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 { NULL, NULL }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 };
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 lua_newtable(L);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 luaL_setfuncs(L, exports, 0);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 }