The main idea staying behind this challenge is to create own static library of most common functions uning only some low-level functions, such as: malloc, malloc_size/malloc_usable_size, free, open,read, write, close, exit.
Library is compiled with clang -std=c11 -Wall -Wextra -Werror -Wpedantic.
To compile library simply use make or make libmx.a in your shell. There are also some test cases included, which can be compiled by make test and they executed by ./test.
Feel free to use it if you need, ask questions and so on. As this is mostly educational project, feedback is highly appreciated :)
Enjoy!
- Print character
- Print string
- Print array of strings
- Print integer
- Decimal to hex
- Bubble sort
- Quick sort
- Integer to ASCII
- Print multibyte characters
- Exponentiation
- Square root
- Hex to decimal
- For each
- Binary search
- String length
- Swap characters
- Copy string
- Compare strings
- Concatenate strings
- New string
- Duplicate string
- Join strings
- Delete string
- Delete array of strings
- File to string
- Readline
- Copy them all
- Reverse string
- Duplicate part of string
- Locate substring
- Print character
- Count words
- Count substrings
- Get character index
- Get substring index
- Trim strings
- Clean string
- Split string
- Replace substrings
- Fill memory
- Copy memory
- Compare memory
- Reallocate memory
- Non-overlapping memory copy
- Locate byte from end
- Copy memory to ...
- Locate byte from start
- Locate block of bytes
- Allocated memory size
- Is digit?
- Is white-space?
- To lower case
- To upper case
- Is aplhabetic?
- Locate character
- ASCII to integer
- Compare strings N
Outputs a single character to the standard output.
void mx_printchar(char c);Outputs a string of characters to the standard output.
void mx_printstr(const char *s);Outputs:
- an array of strings
arrto the standard output with a delimiterdelimbetween the elements of an array; - nothing if
arrordelimdo not exist; - a newline at the end of the output.
arr must be NULL-terminated, in other cases the behavior is undefined.
void mx_print_strarr(char **arr, const char *delim);Outputs integer values to the standard output.
void mx_printint(int n);Converts an unsigned long number into a hexadecimal string.
Returns the number converted to a hexadecimal string.
char *mx_nbr_to_hex(unsigned long nbr);Sorts an array of integers in place in ascending order using the bubble sort algorithm.
Returns the number of swap operations.
int mx_bubble_sort(int *arr, int size);Sorts an array of integers in ascending order using the quick sort algorithm.
Returns:
- the number of swaps;
-1ifarrdoes not exist.
int mx_quicksort(int *arr, int left, int right);Takes an integer and converts it to a string.
Returns the number as a NULL-terminated string.
char *mx_itoa(int number);Outputs ASCII and multibyte characters to the standard output.
void mx_print_unicode(wchar_t c);Computes n raised to the power of zero or a positive integer pow.
Returns the result of n to the power of pow.
double mx_pow(double n, unsigned int pow);Computes the non-negative square root of x.
Returns the square root of the numberx if it is natural, and 0 otherwise.
int mx_sqrt(int x);Converts a hexadecimal string into an unsigned long number.
Returns the unsigned long number.
unsigned long mx_hex_to_nbr(const char *hex);Applies the function f for each element of the array arr given size.
void mx_foreach(int *arr, int size, void(*f)(int));Searches the strings in the array arr with the given size of array using the binary search algorithm.
Returns:
- the
indexof the found string in the array -1in case of errors or if the string has not been found- assigns the number of required iterations to the
countpointer.
int mx_binary_search(char **arr, int size, const char *s, int *count);Has the same behavior as the corresponding standard libc function strlen.
int mx_strlen(const char *s);Swaps the characters of a string using pointers. Do nothing if s1 or s2 does not exist.
void mx_swap_char(char *s1, char *s2);Has the same behavior as the standard libc function strcpy.
char *mx_strcpy(char *dst, const char *src);Has the same behavior as the standard libc function strcmp.
int mx_strcmp(const char *s1, const char *s2);Has the same behavior as the standard libc function strcat.
char *mx_strcat(char *restrict s1, const char *restrict s2);Allocates memory for a string of a specific size and one additional byte for the terminating '\0'. Initializes each character with '\0'.
Returns the string of a specific size and terminated by '\0' or NULL if creation fails.
char *mx_strnew(const int size);Has the same behavior as the standard libc function strdup.
char *mx_strdup(const char *s1);Concatenates strings s1 and s2 into a new string. Terminates the new string with '\0'.
Returns:
- the string as a result of concatenation
s1ands2; - the new copy of non-
NULLparameter if one and only one of the parameters isNULL; NULLif the concatenation fails.
char *mx_strjoin(const char *s1, const char *s2);Takes a pointer to a string, frees string memory with free and sets the string to NULL.
void mx_strdel(char **str);Takes a pointer to a NULL-terminated array of strings, deletes the contents of the array, frees array memory with free and sets a pointer to NULL.
void mx_del_strarr(char ***arr);Takes a filename as a parameter and reads data from the file into a string.
Returns:
NULL-terminated string;NULLin case of any errors.
char *mx_file_to_str(const char *file);Reads the line from the given fd into the lineptr until it:
- reaches a
delimcharacter. The delimiter must not be returned withlineptr; - reaches the End Of File (EOF);
Returns:
- the number of bytes that are written into
lineptr; -1if EOF is reached and there is nothing to write inlineptr;-2in case of errors orfdis invalid.
int mx_read_line(char **lineptr, size_t buf_size, char delim, const int fd);Has the same behavior as the standard libc function strncpy.
char *mx_strncpy(char *dst, const char *src, int len);Reverses a string using pointers. Do nothing if a string does not exist.
void mx_str_reverse(char *s);Has the same behavior as the standard libc function strndup.
char *mx_strndup(const char *s1, size_t n);Has the same behavior as the standard libc function strstr.
char *mx_strstr(const char *haystack, const char *needle);Counts words in a string.
Returns the number of words in the string.
int mx_count_words(const char *str, char c);Counts the substrings sub in the string str.
Returns:
- the count of
subinstr; 0ifsubis an empty string;-1ifstrand/orsubdo not exist.
int mx_count_substr(const char *str, const char *sub);Finds the index of the first occurrence of the character c in a string str.
Returns:
- the index of the first occurrence;
-1if no occurrence is found;-2if the string does not exist.
int mx_get_char_index(const char *str, char c);Finds the index of a substring.
Returns:
- the index of the first character of
subinstr; -1ifsubis not found instr;-2ifstrorsubdoes not exist.
int mx_get_substr_index(const char *str, const char *sub);Takes a string, and creates a new one from it without whitespace characters at the beginning and the end of the string.
Returns:
- a new trimmed string;
NULLif the stringstrdoes not exist or string trim fails.
char *mx_strtrim(const char *str);Takes a string, and creates a new one from it without whitespace characters in thebeginning and/or at the end of the string. Separates words in the new string with exactly one space character.
Returns:
- a new created string;
NULLif the stringstrdoes not exist or string creation fails.
char *mx_del_extra_spaces(const char *str);Converts a strings to a NULL-terminated array of words.
Returns:
- the
NULL-terminated array of strings; NULLif the strings does not exist or conversion fails.
char **mx_strsplit(const char *s, char c);Replaces all occurrences of sub in str with replace.
Returns:
- a new string where substrings are replaced;
NULLifsuborstrorreplacedoes not exist.
char *mx_replace_substr(const char *str, const char *sub, const char *replace);Has the same behavior as the standard libc function memset.
void *mx_memset(void *b, int c, size_t len);Has the same behavior as the standard libc function memcpy.
void *mx_memcpy(void *restrict dst, const void *restrict src, size_t n);Has the same behavior as the standard stdlib function memcmp.
int mx_memcmp(const void *s1, const void *s2, size_t n);Has the same behavior as the standard stdlib function realloc.
void *mx_realloc(void *ptr, size_t size);Has the same behavior as the standard libc function memmove.
void *mx_memmove(void *dst, const void *src, size_t len);Similar to the function mx_memchr, except that it searches in the opposite direction from the end of the bytes n points to s instead of directly from the beginning.
void *mx_memrchr(const void *s, int c, size_t n);Has the same behavior as the standard stdlib function memccpy.
void *mx_memccpy(void *restrict dst, const void* restrict src, int c, size_t n);Has the same behavior as the standard stdlib function memchr.
void *mx_memchr(const void *s, int c, size_t n);Has the same behavior as the standard libc function memmem.
void *mx_memmem(const void *big, size_t big_len, const void *little, size_t little_len);Some functions to work with aingly linked list which contains void pointer.
typedef struct s_list {
void *data;
struct s_list *next;
} t_list;Creates a new node of a linked list t_list. The function assigns a parameter data to the list variable data and assigns next to NULL.
t_list *mx_create_node(void *data);Inserts a new node of t_list type with the given parameter data at the beginning of the linked list.
void mx_push_front(t_list **list, void *data);Inserts a node of t_list type with the given parameter data at the end of the linked list.
void mx_push_back(t_list **list, void *data);Removes the first node of the linked list and frees the memory allocated for the node.
void mx_pop_front(t_list **head);Removes the last node of the linked list and frees the memory allocated for the node.
void mx_pop_back(t_list **head);Calculates the number of nodes in a linked list.
Returns the amount of nodes in the linked list.
int mx_list_size(t_list *list);Sorts a list's contents in ascending order. The function cmp returns true if a > b and false in other cases.
Returns a pointer to the first element of the sorted list.
t_list *mx_sort_list(t_list *lst, bool(*cmp)(void*, void*));Platform-indepent function which return size of previously allocated memory.
Special credits to https://stackoverflow.com/users/1424877/quuxplusone
size_t mx_malloc_size(void *p);Has the same behaviour as the standard libc function isdigit.
bool mx_isdigit(int c);Has the same behaviour as the standard libc function isspace.
bool mx_isspace(int c);Has the same behaviour as the standard libc function tolower.
int mx_tolower(int c);Has the same behaviour as the standard libc function toupper.
int mx_toupper(int c);Has the same behaviour as the standard libc function isalpha.
bool mx_isalpha(int c);Create a function that has the same behaviour as the standard libc function strchr.
char *mx_strchr(const char *s, int c);Converts an ASCII string to an integer as the standard libc function atoi does.
int mx_atoi(const char *str);Has the same behaviour as the standard libc function strncmp.
int mx_strncmp(const char *s1, const char *s2, int n);