Annotate

util-src/strbitop.c @ 12948:29983f09c913

prosody.loader: Incorporate search path rewrite patch from Debian packages Nice to drop that patch. Will allow loading this to do something both when installed under a prosody directory or from a source checkout.
author Kim Alvefur <zash@zash.se>
date Fri, 17 Mar 2023 14:36:02 +0100
parent 12575:1f6f05a98fcd
child 12976:a187600ec7d6
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
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 /* TODO Deduplicate code somehow */
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
12469
2b3adaa6d38e util.strbitop: Reduce scope of functions
Kim Alvefur <zash@zash.se>
parents: 11175
diff changeset
14 static int strop_and(lua_State *L) {
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 luaL_Buffer buf;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 size_t a, b, i;
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
17 const char *str_a = luaL_checklstring(L, 1, &a);
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
18 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
19
11175
235537247aa3 Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents: 11172
diff changeset
20 luaL_buffinit(L, &buf);
235537247aa3 Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents: 11172
diff changeset
21
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 }
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 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
28 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
29 }
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 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 return 1;
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
12469
2b3adaa6d38e util.strbitop: Reduce scope of functions
Kim Alvefur <zash@zash.se>
parents: 11175
diff changeset
35 static int strop_or(lua_State *L) {
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 luaL_Buffer buf;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 size_t a, b, i;
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
38 const char *str_a = luaL_checklstring(L, 1, &a);
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
39 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
40
11175
235537247aa3 Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents: 11172
diff changeset
41 luaL_buffinit(L, &buf);
235537247aa3 Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents: 11172
diff changeset
42
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 return 1;
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
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 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
49 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
50 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 return 1;
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
12469
2b3adaa6d38e util.strbitop: Reduce scope of functions
Kim Alvefur <zash@zash.se>
parents: 11175
diff changeset
56 static int strop_xor(lua_State *L) {
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 luaL_Buffer buf;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 size_t a, b, i;
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
59 const char *str_a = luaL_checklstring(L, 1, &a);
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
60 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
61
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
62 luaL_buffinit(L, &buf);
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
63
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 return 1;
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
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 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
70 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
71 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 return 1;
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
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 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
78 luaL_Reg exports[] = {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 { "sand", strop_and },
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 { "sor", strop_or },
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 { "sxor", strop_xor },
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 { NULL, NULL }
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
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 lua_newtable(L);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 luaL_setfuncs(L, exports, 0);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 }