Skip to content

Instantly share code, notes, and snippets.

View denisdemaisbr's full-sized avatar

DENIS DOS SANTOS SILVA denisdemaisbr

View GitHub Profile
@denisdemaisbr
denisdemaisbr / memory-align.c
Last active March 16, 2024 06:37
one simple way to align memory alloc'ed
/*
one simple way to align memory alloc'ed
based on sqlite3 macro
+7 = 8 (64-bit)
+3 = 4 (32-bit)
HINT: you can try ptrdiff_t too =)
*/
@denisdemaisbr
denisdemaisbr / str_replace.c
Created December 12, 2023 00:25
c function to replace char in string
static void str_replace(void *s, unsigned char target, unsigned char to) {
unsigned char* str = (unsigned char*) s;
while (*str) {
if (*str == target) {
*str = to;
}
str++;
}
}
@denisdemaisbr
denisdemaisbr / str_remove.c
Created December 12, 2023 00:25
c function to remove character in string
static void str_remove(void *s, unsigned char target) {
unsigned char* str = (unsigned char*) s;
int len = strlen(str);
int i, j = 0;
for (i = 0; i < len; i++) {
if (str[i] != target) {
str[j++] = str[i];
}
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>
double get_time()
{
struct timeval t;
struct timezone tzp;
gettimeofday(&t, &tzp);
function in_array(value, array)
for _, v in ipairs(array) do
if v == value then
return true
end
end
return false
end
@denisdemaisbr
denisdemaisbr / strdup_lower.c
Created October 8, 2023 22:26
a simple function to duplicate string and lower it.
char* strdup_lower(char* str) {
char *b;
char *s;
char *buf;
errno = 0;
buf = strdup(str);
if (!buf)
return NULL;
@denisdemaisbr
denisdemaisbr / strdup_upper.c
Created October 8, 2023 22:23
a simple function to duplicate string and upper it.
/*
a simple function to duplicate string and upper it.
sample:
...
char *upper = strdup_upper("HELLO world");
if (!upper) { perror("ops!"); exit(1); }
puts(upper);
free(upper);
@denisdemaisbr
denisdemaisbr / strstr.lua
Created September 24, 2023 02:01
strstr() lua implementation
-- works like c strstr()
-- retorn index if found pattern =)
function strstr(haystack, needle)
if (not haystack) then return nil; end
if (not needle) then return nil; end
local h, n = #haystack, #needle;
for i = 1, h - n + 1 do
@denisdemaisbr
denisdemaisbr / install.sh
Created August 5, 2023 20:34
how install docker on raspiberry pi 3/3b+
# based on 2023-05-03-raspios-bullseye-arm64-lite.img.xz
#
su [your password]
apt-get update && sudo apt-get upgrade
curl -fsSL test.docker.com -o get-docker.sh && sh get-docker.sh
usermod -aG docker [user_name]
# fix 'memory/swap' error
echo "cgroup_enable=memory swapaccount=1 cgroup_memory=1 cgroup_enable=cpuset" >> /boot/cmdline.txt
--! lua.org
--! get a argument from cli in lua-5.1
if not arg or #arg == 0 then
print('lua args.lua [nome]');
os.exit(1);
end
print( _VERSION )
print( string.format('ola %q', arg[1]) )