Software / code / prosody
Comparison
util-src/strbitop.c @ 11173:cbe1edecb8fa
Merge 0.11->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 15 Oct 2020 17:14:03 +0200 |
| parent | 11172:712b2e6a09d9 |
| child | 11175:235537247aa3 |
comparison
equal
deleted
inserted
replaced
| 11170:4bda303d54ed | 11173:cbe1edecb8fa |
|---|---|
| 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-2020 Kim Alvefur | 5 * Copyright (C) 2016 Kim Alvefur |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include <lua.h> | 8 #include <lua.h> |
| 9 #include <lauxlib.h> | 9 #include <lauxlib.h> |
| 10 | 10 |
| 18 luaL_Buffer buf; | 18 luaL_Buffer buf; |
| 19 size_t a, b, i; | 19 size_t a, b, i; |
| 20 const char *str_a = luaL_checklstring(L, 1, &a); | 20 const char *str_a = luaL_checklstring(L, 1, &a); |
| 21 const char *str_b = luaL_checklstring(L, 2, &b); | 21 const char *str_b = luaL_checklstring(L, 2, &b); |
| 22 | 22 |
| 23 luaL_buffinit(L, &buf); | 23 if(a == 0 || b == 0) { |
| 24 lua_settop(L, 1); | |
| 25 return 1; | |
| 26 } | |
| 27 | |
| 28 for(i = 0; i < a; i++) { | |
| 29 luaL_addchar(&buf, str_a[i] & str_b[i % b]); | |
| 30 } | |
| 31 | |
| 32 luaL_pushresult(&buf); | |
| 33 return 1; | |
| 34 } | |
| 35 | |
| 36 int strop_or(lua_State *L) { | |
| 37 luaL_Buffer buf; | |
| 38 size_t a, b, i; | |
| 39 const char *str_a = luaL_checklstring(L, 1, &a); | |
| 40 const char *str_b = luaL_checklstring(L, 2, &b); | |
| 24 | 41 |
| 25 if(a == 0 || b == 0) { | 42 if(a == 0 || b == 0) { |
| 26 lua_settop(L, 1); | 43 lua_settop(L, 1); |
| 27 return 1; | 44 return 1; |
| 28 } | 45 } |
| 29 | 46 |
| 30 char *cbuf = luaL_buffinitsize(L, &buf, a); | |
| 31 | |
| 32 for(i = 0; i < a; i++) { | 47 for(i = 0; i < a; i++) { |
| 33 cbuf[i] = str_a[i] & str_b[i % b]; | 48 luaL_addchar(&buf, str_a[i] | str_b[i % b]); |
| 34 } | 49 } |
| 35 | 50 |
| 36 luaL_addsize(&buf, a); | |
| 37 luaL_pushresult(&buf); | 51 luaL_pushresult(&buf); |
| 38 return 1; | 52 return 1; |
| 39 } | 53 } |
| 40 | 54 |
| 41 int strop_or(lua_State *L) { | 55 int strop_xor(lua_State *L) { |
| 42 luaL_Buffer buf; | 56 luaL_Buffer buf; |
| 43 size_t a, b, i; | 57 size_t a, b, i; |
| 44 const char *str_a = luaL_checklstring(L, 1, &a); | 58 const char *str_a = luaL_checklstring(L, 1, &a); |
| 45 const char *str_b = luaL_checklstring(L, 2, &b); | 59 const char *str_b = luaL_checklstring(L, 2, &b); |
| 46 | 60 |
| 49 if(a == 0 || b == 0) { | 63 if(a == 0 || b == 0) { |
| 50 lua_settop(L, 1); | 64 lua_settop(L, 1); |
| 51 return 1; | 65 return 1; |
| 52 } | 66 } |
| 53 | 67 |
| 54 char *cbuf = luaL_buffinitsize(L, &buf, a); | |
| 55 | |
| 56 for(i = 0; i < a; i++) { | 68 for(i = 0; i < a; i++) { |
| 57 cbuf[i] = str_a[i] | str_b[i % b]; | 69 luaL_addchar(&buf, str_a[i] ^ str_b[i % b]); |
| 58 } | 70 } |
| 59 | 71 |
| 60 luaL_addsize(&buf, a); | |
| 61 luaL_pushresult(&buf); | |
| 62 return 1; | |
| 63 } | |
| 64 | |
| 65 int strop_xor(lua_State *L) { | |
| 66 luaL_Buffer buf; | |
| 67 size_t a, b, i; | |
| 68 const char *str_a = luaL_checklstring(L, 1, &a); | |
| 69 const char *str_b = luaL_checklstring(L, 2, &b); | |
| 70 | |
| 71 if(a == 0 || b == 0) { | |
| 72 lua_settop(L, 1); | |
| 73 return 1; | |
| 74 } | |
| 75 | |
| 76 char *cbuf = luaL_buffinitsize(L, &buf, a); | |
| 77 | |
| 78 for(i = 0; i < a; i++) { | |
| 79 cbuf[i] = str_a[i] ^ str_b[i % b]; | |
| 80 } | |
| 81 | |
| 82 luaL_addsize(&buf, a); | |
| 83 luaL_pushresult(&buf); | 72 luaL_pushresult(&buf); |
| 84 return 1; | 73 return 1; |
| 85 } | 74 } |
| 86 | 75 |
| 87 LUA_API int luaopen_util_strbitop(lua_State *L) { | 76 LUA_API int luaopen_util_strbitop(lua_State *L) { |