util-src/strbitop.c
branch0.11
changeset 11176 712b2e6a09d9
parent 11175 2c1583bb0e0f
child 11179 235537247aa3
equal deleted inserted replaced
11175:2c1583bb0e0f 11176:712b2e6a09d9
     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 
    23 	if(a == 0 || b == 0) {
    23 	if(a == 0 || b == 0) {
    24 		lua_settop(L, 1);
    24 		lua_settop(L, 1);
    25 		return 1;
    25 		return 1;
    26 	}
    26 	}
    27 
    27 
    28 	char *cbuf = luaL_buffinitsize(L, &buf, a);
       
    29 
       
    30 	for(i = 0; i < a; i++) {
    28 	for(i = 0; i < a; i++) {
    31 		cbuf[i] = str_a[i] & str_b[i % b];
    29 		luaL_addchar(&buf, str_a[i] & str_b[i % b]);
    32 	}
    30 	}
    33 
    31 
    34 	luaL_addsize(&buf, a);
       
    35 	luaL_pushresult(&buf);
    32 	luaL_pushresult(&buf);
    36 	return 1;
    33 	return 1;
    37 }
    34 }
    38 
    35 
    39 int strop_or(lua_State *L) {
    36 int strop_or(lua_State *L) {
    45 	if(a == 0 || b == 0) {
    42 	if(a == 0 || b == 0) {
    46 		lua_settop(L, 1);
    43 		lua_settop(L, 1);
    47 		return 1;
    44 		return 1;
    48 	}
    45 	}
    49 
    46 
    50 	char *cbuf = luaL_buffinitsize(L, &buf, a);
       
    51 
       
    52 	for(i = 0; i < a; i++) {
    47 	for(i = 0; i < a; i++) {
    53 		cbuf[i] = str_a[i] | str_b[i % b];
    48 		luaL_addchar(&buf, str_a[i] | str_b[i % b]);
    54 	}
    49 	}
    55 
    50 
    56 	luaL_addsize(&buf, a);
       
    57 	luaL_pushresult(&buf);
    51 	luaL_pushresult(&buf);
    58 	return 1;
    52 	return 1;
    59 }
    53 }
    60 
    54 
    61 int strop_xor(lua_State *L) {
    55 int strop_xor(lua_State *L) {
    62 	luaL_Buffer buf;
    56 	luaL_Buffer buf;
    63 	size_t a, b, i;
    57 	size_t a, b, i;
    64 	const char *str_a = luaL_checklstring(L, 1, &a);
    58 	const char *str_a = luaL_checklstring(L, 1, &a);
    65 	const char *str_b = luaL_checklstring(L, 2, &b);
    59 	const char *str_b = luaL_checklstring(L, 2, &b);
    66 
    60 
       
    61 	luaL_buffinit(L, &buf);
       
    62 
    67 	if(a == 0 || b == 0) {
    63 	if(a == 0 || b == 0) {
    68 		lua_settop(L, 1);
    64 		lua_settop(L, 1);
    69 		return 1;
    65 		return 1;
    70 	}
    66 	}
    71 
    67 
    72 	char *cbuf = luaL_buffinitsize(L, &buf, a);
       
    73 
       
    74 	for(i = 0; i < a; i++) {
    68 	for(i = 0; i < a; i++) {
    75 		cbuf[i] = str_a[i] ^ str_b[i % b];
    69 		luaL_addchar(&buf, str_a[i] ^ str_b[i % b]);
    76 	}
    70 	}
    77 
    71 
    78 	luaL_addsize(&buf, a);
       
    79 	luaL_pushresult(&buf);
    72 	luaL_pushresult(&buf);
    80 	return 1;
    73 	return 1;
    81 }
    74 }
    82 
    75 
    83 LUA_API int luaopen_util_strbitop(lua_State *L) {
    76 LUA_API int luaopen_util_strbitop(lua_State *L) {