Software /
code /
prosody
Comparison
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 |
comparison
equal
deleted
inserted
replaced
11168:cde600e2fdf9 | 11169:6dde2c9fa272 |
---|---|
1 /* | 1 /* |
2 * This project is MIT licensed. Please see the | 2 * This project is MIT licensed. Please see the |
3 * COPYING file in the source package for more information. | 3 * COPYING file in the source package for more information. |
4 * | 4 * |
5 * Copyright (C) 2016 Kim Alvefur | 5 * Copyright (C) 2016-2020 Kim Alvefur |
6 */ | 6 */ |
7 | 7 |
8 #include <lua.h> | 8 #include <lua.h> |
9 #include <lauxlib.h> | 9 #include <lauxlib.h> |
10 | 10 |
25 if(a == 0 || b == 0) { | 25 if(a == 0 || b == 0) { |
26 lua_settop(L, 1); | 26 lua_settop(L, 1); |
27 return 1; | 27 return 1; |
28 } | 28 } |
29 | 29 |
30 char *cbuf = luaL_buffinitsize(L, &buf, a); | |
31 | |
30 for(i = 0; i < a; i++) { | 32 for(i = 0; i < a; i++) { |
31 luaL_addchar(&buf, str_a[i] & str_b[i % b]); | 33 cbuf[i] = str_a[i] & str_b[i % b]; |
32 } | 34 } |
33 | 35 |
36 luaL_addsize(&buf, a); | |
34 luaL_pushresult(&buf); | 37 luaL_pushresult(&buf); |
35 return 1; | 38 return 1; |
36 } | 39 } |
37 | 40 |
38 int strop_or(lua_State *L) { | 41 int strop_or(lua_State *L) { |
46 if(a == 0 || b == 0) { | 49 if(a == 0 || b == 0) { |
47 lua_settop(L, 1); | 50 lua_settop(L, 1); |
48 return 1; | 51 return 1; |
49 } | 52 } |
50 | 53 |
54 char *cbuf = luaL_buffinitsize(L, &buf, a); | |
55 | |
51 for(i = 0; i < a; i++) { | 56 for(i = 0; i < a; i++) { |
52 luaL_addchar(&buf, str_a[i] | str_b[i % b]); | 57 cbuf[i] = str_a[i] | str_b[i % b]; |
53 } | 58 } |
54 | 59 |
60 luaL_addsize(&buf, a); | |
55 luaL_pushresult(&buf); | 61 luaL_pushresult(&buf); |
56 return 1; | 62 return 1; |
57 } | 63 } |
58 | 64 |
59 int strop_xor(lua_State *L) { | 65 int strop_xor(lua_State *L) { |
60 luaL_Buffer buf; | 66 luaL_Buffer buf; |
61 size_t a, b, i; | 67 size_t a, b, i; |
62 const char *str_a = luaL_checklstring(L, 1, &a); | 68 const char *str_a = luaL_checklstring(L, 1, &a); |
63 const char *str_b = luaL_checklstring(L, 2, &b); | 69 const char *str_b = luaL_checklstring(L, 2, &b); |
64 | 70 |
65 luaL_buffinit(L, &buf); | |
66 | |
67 if(a == 0 || b == 0) { | 71 if(a == 0 || b == 0) { |
68 lua_settop(L, 1); | 72 lua_settop(L, 1); |
69 return 1; | 73 return 1; |
70 } | 74 } |
71 | 75 |
76 char *cbuf = luaL_buffinitsize(L, &buf, a); | |
77 | |
72 for(i = 0; i < a; i++) { | 78 for(i = 0; i < a; i++) { |
73 luaL_addchar(&buf, str_a[i] ^ str_b[i % b]); | 79 cbuf[i] = str_a[i] ^ str_b[i % b]; |
74 } | 80 } |
75 | 81 |
82 luaL_addsize(&buf, a); | |
76 luaL_pushresult(&buf); | 83 luaL_pushresult(&buf); |
77 return 1; | 84 return 1; |
78 } | 85 } |
79 | 86 |
80 LUA_API int luaopen_util_strbitop(lua_State *L) { | 87 LUA_API int luaopen_util_strbitop(lua_State *L) { |