Skip to content

Instantly share code, notes, and snippets.

View alsamitech's full-sized avatar

Sami Alameddine alsamitech

  • Alsami Technologies
  • United States of America
View GitHub Profile
@alsamitech
alsamitech / longpow.c
Created September 1, 2021 20:37
longpow - exponent function in 3 lines
long longpow(const long val, long pw){
long res=val;
while(--pw)res*=val;
return res;
}
@alsamitech
alsamitech / starts_with_5.c
Created August 27, 2021 20:52
the fifth iteration of starts_with
_Bool starts_with(const char* restrict str, const char* restrict sw){
while(*sw)
if(*sw++!=*str++)return 0;
return 1;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <memory.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
void reverse_bytes(char* restrict bytes, const size_t len){
for(size_t i=0;i<len/2;i++){
char tmp=bytes[i];
bytes[i]=bytes[len-1-i];
bytes[len-1-i]=tmp;
}
}
// str must have 11 bytes allocated to it
void int_to_str(int val, char* str){
void str_clear(char* restrict str){
while(*str)*str++=0;
}
void str_set(char* restrict str, const char set){
while(*str)*str++=set;
}
@alsamitech
alsamitech / file_count.c
Created August 7, 2021 07:11
file_count- lists the amount of files in a directory
// file_count will add one to the count because zero is reserved for errors
size_t file_count(const char* dir_nm){
DIR* dir=opendir(dir_nm);
if(!dir)return 0;
struct dirent* entity=readdir(dir);
size_t count=0;
for(;readdir(dir);count++);
closedir(dir);
return count;
}
@alsamitech
alsamitech / strcmp_until.c
Created July 27, 2021 04:30
strcmp_until - faster than doing strncmp with passing until_char
_Bool strcmp_until(const char* s1,const char* s2,const char until){
for(size_t i=0;s1[i]!=until;i++)
if(s1[i]!=s2[i])return 1;
return 0;
}
_Bool starts_with_word(const char* str, const char* word){
for(size_t i=0;;i++){
if(!((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')||str[i]=='_')){
if(word[i])return 1;
break;
}
if(str[i]!=word[i]){
return 1;
}
@alsamitech
alsamitech / starts_with_4.c
Created July 23, 2021 17:35
starts_with - starts with but even better (replace _Bool with char if you're using anything before C99)
_Bool starts_with(const char* in, const char* sw){
for(size_t i=0;sw[i];i++)
if(in[i]!=sw[i])
return 1;
return 0;
}
@alsamitech
alsamitech / get_last_ex.c
Created July 21, 2021 20:49
get_last_ex - get_last but throws excpetions
// returns position of the last byte in an array or string whose length is known.
size_t get_last_byte_ex(const char* const bytes, const char byte,size_t len, _Bool* const exists){
while(bytes[len]!=byte){
if(!len){
*exists=0;
return 0;
}
len--;
}