Skip to content

Commit

Permalink
fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
simo981 committed Apr 15, 2024
1 parent 1a8d101 commit 656ca4f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 56 deletions.
90 changes: 34 additions & 56 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
Expand All @@ -19,15 +20,9 @@ static char **last = NULL;
static size_t connectors_size = 0;
static size_t last_size = 0;
static const char *connector_placeholder = "|";
static bool leet_vowel = false;
static bool leet_full = false;
static bool upper_first = false;
static bool upper_full = false;
static bool only_transform = false;
static bool reverse_words = false;
static bool reverse_full = false;
static modifiers_t bool_modifiers = {0x0};

unsigned **binomialCoefficient(size_t n, size_t k)
unsigned **binomial_coefficient(size_t n, size_t k)
{
unsigned **C = (unsigned **)calloc(n + 1, sizeof(unsigned *));
for (size_t i = 0; i <= n; i++)
Expand Down Expand Up @@ -88,26 +83,21 @@ inline bool palindrome(char *str, size_t len)
{
char *p1 = str;
char *p2 = str + len - 1;
char *mid = str + (size_t)len / 2;
while (p1 != mid && p2 != mid)
char *mid = str + (size_t)(len / 2);
while (p1 < mid && p2 >= mid)
{
if (*p1++ != *p2--)
{
return false;
}
}
if (p1 == mid && p2 == mid)
{
return true;
}
return false;
return true;
}

inline void reverse(char *str, size_t len)
{
char *p1 = str;
char *p2 = str + len - 1;

while (p1 < p2)
{
char tmp = *p1;
Expand All @@ -131,7 +121,7 @@ inline void print_out(char **arr, size_t size)
run_len += lengths[i];
}
strings_len = add_string(all_strings, strings_len, finalString, run_len);
if (reverse_words || reverse_full)
if (bool_modifiers.reverse_words || bool_modifiers.reverse_full)
{
size_t one = 0, palindrome_count = 0;
for (size_t i = 0; i < size; i++)
Expand All @@ -149,15 +139,13 @@ inline void print_out(char **arr, size_t size)
{
reverse_make_sense = true;
}
if (reverse_make_sense && reverse_words)
if (reverse_make_sense && bool_modifiers.reverse_words)
{
saved_len = strings_len;
for (size_t i = 0; i < saved_len; i++)
{
const char *my_str = all_strings[i];
size_t copy_len = strlen(my_str);
char copy[BUFF] = {0x0};
memccpy(copy, my_str, '\0', copy_len);
size_t copy_len = COPY_STRING(copy, all_strings[i]);
reverse(copy, copy_len);
strings_len = add_string(all_strings, strings_len, copy, copy_len);
}
Expand Down Expand Up @@ -186,10 +174,8 @@ inline void print_out(char **arr, size_t size)
for (size_t i = 0; i < saved_len; i++)
{
size_t size_of_number;
const char *my_str = all_strings[i];
size_t copy_len = strlen(my_str);
char copy[BUFF] = {0x0};
memccpy(copy, my_str, '\0', copy_len);
size_t copy_len = COPY_STRING(copy, all_strings[i]);
for (size_t j = 0; j < last_size; j++)
{
size_of_number = strlen(last[j]);
Expand All @@ -198,50 +184,44 @@ inline void print_out(char **arr, size_t size)
}
}
}
if (leet_full || leet_vowel)
if (bool_modifiers.leet_full || bool_modifiers.leet_vowel)
{
saved_len = strings_len;
for (size_t i = 0; i < saved_len; i++)
{
const char *my_str = all_strings[i];
size_t copy_len = strlen(my_str);
char copy[BUFF] = {0x0};
memccpy(copy, my_str, '\0', copy_len);
size_t copy_len = COPY_STRING(copy, all_strings[i]);
if (leet_encode(copy))
{
strings_len = add_string(all_strings, strings_len, copy, copy_len);
}
}
}
if (upper_first || upper_full)
if (bool_modifiers.upper_first || bool_modifiers.upper_full)
{
saved_len = strings_len;
for (size_t i = 0; i < saved_len; i++)
{
const char *my_str = all_strings[i];
size_t copy_len = strlen(my_str);
char copy[BUFF] = {0x0};
memccpy(copy, my_str, '\0', copy_len);
size_t copy_len = COPY_STRING(copy, all_strings[i]);
if (upper_encode(copy))
{
strings_len = add_string(all_strings, strings_len, copy, copy_len);
}
}
}
if (reverse_make_sense && reverse_full && last)
if (reverse_make_sense && bool_modifiers.reverse_full)
{
saved_len = strings_len;
for (size_t i = 0; i < saved_len; i++)
{
const char *my_str = all_strings[i];
size_t copy_len = strlen(my_str);
char copy[BUFF] = {0x0};
memccpy(copy, my_str, '\0', copy_len);
size_t copy_len = COPY_STRING(copy, all_strings[i]);
reverse(copy, copy_len);
strings_len = add_string(all_strings, strings_len, copy, copy_len);
}
}
saved_len = only_transform ? saved_len : 0;
saved_len = bool_modifiers.only_transform ? saved_len : 0;
for (size_t i = 0; i < strings_len; i++)
{
if (i >= saved_len)
Expand Down Expand Up @@ -270,20 +250,20 @@ inline bool leet_encode(char *str)
inline bool upper_encode(char *str)
{
bool encoded = false;
if (upper_full)
if (bool_modifiers.upper_full)
{
for (size_t j = 0; j < strlen(str); j++)
for (char *ptr = str; *ptr != '\0'; ptr++)
{
if (str[j] >= 97 && str[j] <= 122)
if (islower(*ptr))
{
str[j] -= 32;
*ptr = toupper((unsigned char)*ptr);
encoded = true;
}
}
}
else if (upper_first && str[0] >= 97 && str[0] <= 122)
else if (bool_modifiers.upper_first && islower((unsigned char)str[0]))
{
str[0] -= 32;
str[0] = toupper((unsigned char)str[0]);
encoded = true;
}
return encoded;
Expand Down Expand Up @@ -312,8 +292,7 @@ void seq_perm(char **arr, size_t size)
}
else
{
P[i] = 0;
i++;
P[i++] = 0;
}
}
free(P);
Expand Down Expand Up @@ -355,8 +334,7 @@ void gen_bin_perms(unsigned short *arr, size_t size, size_t idx, size_t max, siz
if (arr[j] == 1)
{
auxPerm[pointer] = (char *)calloc(strlen(dict[j]) + 1, sizeof(char));
memccpy(auxPerm[pointer], dict[j], '\0', strlen(dict[j]));
pointer++;
memccpy(auxPerm[pointer++], dict[j], '\0', strlen(dict[j]));
}
}
push_queue(all_queues[cur - min], auxPerm, cur);
Expand Down Expand Up @@ -384,43 +362,43 @@ int main(int argc, char **argv)
{
if (strcmp(optarg, "full") == 0)
{
reverse_full = true;
bool_modifiers.reverse_full = true;
}
else if (strcmp(optarg, "words") == 0)
{
reverse_words = true;
bool_modifiers.reverse_words = true;
}
break;
}
case 'k':
{
if (strcmp(optarg, "full") == 0)
{
leet_full = true;
bool_modifiers.leet_full = true;
}
else if (strcmp(optarg, "vowel") == 0)
{
leet_vowel = true;
bool_modifiers.leet_vowel = true;
}
break;
}
case 'u':
{
if (strcmp(optarg, "full") == 0)
{
upper_full = true;
bool_modifiers.upper_full = true;
}
else if (strcmp(optarg, "first") == 0)
{
upper_first = true;
bool_modifiers.upper_first = true;
}
break;
}
case 'p':
{
if (optarg[0] == 'Y' || optarg[0] == 'y')
{
only_transform = true;
bool_modifiers.only_transform = true;
}
break;
}
Expand Down Expand Up @@ -499,7 +477,7 @@ int main(int argc, char **argv)
leet_map['e'] = leet_map['E'] = '3';
leet_map['i'] = leet_map['I'] = '1';
leet_map['o'] = leet_map['O'] = '0';
if (leet_full)
if (bool_modifiers.leet_full)
{
leet_map['s'] = leet_map['S'] = '5';
leet_map['t'] = leet_map['T'] = '7';
Expand All @@ -511,7 +489,7 @@ int main(int argc, char **argv)
queue_n = max_len - min_len + 1;
thread_n = (size_t)(max_len * (max_len + 1)) / 2;
all_queues = (Queue_t **)malloc(sizeof(Queue_t *) * queue_n);
unsigned **C = binomialCoefficient(word_size, max_len);
unsigned **C = binomial_coefficient(word_size, max_len);
for (size_t i = 0; i < queue_n; i++)
{
all_queues[i] = init_queue(C[word_size][min_len + i]);
Expand Down
21 changes: 21 additions & 0 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@
#include <string.h>
#define BUFF 512

#define COPY_STRING(dest, src) \
({ \
size_t copy_len = strlen(src); \
memccpy(dest, src, '\0', copy_len); \
dest[copy_len] = '\0'; \
copy_len; \
})

typedef struct bool_t
{
uint8_t leet_vowel : 1;
uint8_t leet_full : 1;
uint8_t upper_first : 1;
uint8_t upper_full : 1;
uint8_t only_transform : 1;
uint8_t reverse_words : 1;
uint8_t reverse_full : 1;
uint8_t __padding : 1;
} modifiers_t;

void print_out(char **arr, size_t size);
void print_f_maiusc(char **arr, char *string);
void print_number(char **arr, char *finalString, size_t run_len);
Expand All @@ -16,6 +36,7 @@ void seq_perm(char **arr, size_t size);
void *thread_perm(void *in);
void gen_bin_perms(unsigned short *arr, size_t size, size_t idx, size_t max, size_t cur, size_t min);
size_t add_string(char *buff[BUFF], size_t idx, char *to_push, size_t to_push_len);
unsigned **binomial_coefficient(size_t n, size_t k);
void free_inputs_optind(void);
void exit_usage(char *plus);
bool leet_encode(char *str);
Expand Down

0 comments on commit 656ca4f

Please sign in to comment.