Mercurial > prosody-hg
annotate util-src/strbitop.c @ 11171:2c1583bb0e0f 0.11
util.strbitop: Remove redundant init function
When you have 3 almost identical functions, you tend to edit one and
then copypaste. Forgot to remove this line from the other two.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 15 Oct 2020 17:05:53 +0200 |
| parents | 6dde2c9fa272 |
| children | 712b2e6a09d9 |
| 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 * |
|
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
5 * Copyright (C) 2016-2020 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 | 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 | 20 const char *str_a = luaL_checklstring(L, 1, &a); |
| 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 |
|
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
28 char *cbuf = luaL_buffinitsize(L, &buf, a); |
|
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
29 |
|
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 for(i = 0; i < a; i++) { |
|
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
31 cbuf[i] = 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
|
32 } |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 |
|
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
34 luaL_addsize(&buf, a); |
|
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 luaL_pushresult(&buf); |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 return 1; |
|
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 |
| 11167 | 39 int strop_or(lua_State *L) { |
|
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 luaL_Buffer buf; |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 size_t a, b, i; |
| 11167 | 42 const char *str_a = luaL_checklstring(L, 1, &a); |
| 43 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
|
44 |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 if(a == 0 || b == 0) { |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 lua_settop(L, 1); |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 return 1; |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 } |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 |
|
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
50 char *cbuf = luaL_buffinitsize(L, &buf, a); |
|
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
51 |
|
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 for(i = 0; i < a; i++) { |
|
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
53 cbuf[i] = 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
|
54 } |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 |
|
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
56 luaL_addsize(&buf, a); |
|
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 luaL_pushresult(&buf); |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 return 1; |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 } |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 |
| 11167 | 61 int strop_xor(lua_State *L) { |
|
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 luaL_Buffer buf; |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 size_t a, b, i; |
| 11167 | 64 const char *str_a = luaL_checklstring(L, 1, &a); |
| 65 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
|
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 |
|
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
72 char *cbuf = luaL_buffinitsize(L, &buf, a); |
|
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
73 |
|
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 for(i = 0; i < a; i++) { |
|
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
75 cbuf[i] = 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
|
76 } |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 |
|
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
78 luaL_addsize(&buf, a); |
|
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 luaL_pushresult(&buf); |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 return 1; |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 } |
|
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 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
|
84 luaL_Reg exports[] = { |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 { "sand", strop_and }, |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 { "sor", strop_or }, |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 { "sxor", strop_xor }, |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 { NULL, NULL } |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 }; |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 lua_newtable(L); |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 luaL_setfuncs(L, exports, 0); |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 return 1; |
|
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 } |
