Software /
code /
prosody
Annotate
util-src/strbitop.c @ 11169:6dde2c9fa272 0.11
util.strbitop: Create buffer in the correct size (optimization)
This avoids dynamically growing the buffer as Lua does when luaL_addchar
is used, thus saving on realloc calls.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 15 Oct 2020 16:43:30 +0200 |
parent | 11167:ba32b9a6d75b |
child | 11171:2c1583bb0e0f |
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 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 |
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
30 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
|
31 |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 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
|
33 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
|
34 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 |
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
36 luaL_addsize(&buf, a); |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 luaL_pushresult(&buf); |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 return 1; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 |
11167 | 41 int strop_or(lua_State *L) { |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 luaL_Buffer buf; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 size_t a, b, i; |
11167 | 44 const char *str_a = luaL_checklstring(L, 1, &a); |
45 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
|
46 |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 luaL_buffinit(L, &buf); |
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 if(a == 0 || b == 0) { |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 lua_settop(L, 1); |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 return 1; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 |
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
54 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
|
55 |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 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
|
57 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
|
58 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 |
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
60 luaL_addsize(&buf, a); |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 luaL_pushresult(&buf); |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 return 1; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 |
11167 | 65 int strop_xor(lua_State *L) { |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 luaL_Buffer buf; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 size_t a, b, i; |
11167 | 68 const char *str_a = luaL_checklstring(L, 1, &a); |
69 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
|
70 |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 if(a == 0 || b == 0) { |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 lua_settop(L, 1); |
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 |
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
76 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
|
77 |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 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
|
79 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
|
80 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 |
11169
6dde2c9fa272
util.strbitop: Create buffer in the correct size (optimization)
Kim Alvefur <zash@zash.se>
parents:
11167
diff
changeset
|
82 luaL_addsize(&buf, a); |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 luaL_pushresult(&buf); |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 return 1; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 } |
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 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
|
88 luaL_Reg exports[] = { |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 { "sand", strop_and }, |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 { "sor", strop_or }, |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 { "sxor", strop_xor }, |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 { NULL, NULL } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 }; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 lua_newtable(L); |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 luaL_setfuncs(L, exports, 0); |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 return 1; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 } |