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.
voidmx_printchar(charc);Outputs a string of characters to the standard output.
voidmx_printstr(constchar*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.
voidmx_print_strarr(char**arr, constchar*delim);Outputs integer values to the standard output.
voidmx_printint(intn);Converts an unsigned long number into a hexadecimal string.
Returns the number converted to a hexadecimal string.
char*mx_nbr_to_hex(unsigned longnbr);Sorts an array of integers in place in ascending order using the bubble sort algorithm.
Returns the number of swap operations.
intmx_bubble_sort(int*arr, intsize);Sorts an array of integers in ascending order using the quick sort algorithm.
Returns:
- the number of swaps;
-1ifarrdoes not exist.
intmx_quicksort(int*arr, intleft, intright);Takes an integer and converts it to a string.
Returns the number as a NULL-terminated string.
char*mx_itoa(intnumber);Outputs ASCII and multibyte characters to the standard output.
voidmx_print_unicode(wchar_tc);Computes n raised to the power of zero or a positive integer pow.
Returns the result of n to the power of pow.
doublemx_pow(doublen, unsigned intpow);Computes the non-negative square root of x.
Returns the square root of the numberx if it is natural, and 0 otherwise.
intmx_sqrt(intx);Converts a hexadecimal string into an unsigned long number.
Returns the unsigned long number.
unsigned longmx_hex_to_nbr(constchar*hex);Applies the function f for each element of the array arr given size.
voidmx_foreach(int*arr, intsize, 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.
intmx_binary_search(char**arr, intsize, constchar*s, int*count);Has the same behavior as the corresponding standard libc function strlen.
intmx_strlen(constchar*s);Swaps the characters of a string using pointers. Do nothing if s1 or s2 does not exist.
voidmx_swap_char(char*s1, char*s2);Has the same behavior as the standard libc function strcpy.
char*mx_strcpy(char*dst, constchar*src);Has the same behavior as the standard libc function strcmp.
intmx_strcmp(constchar*s1, constchar*s2);Has the same behavior as the standard libc function strcat.
char*mx_strcat(char*restrict s1, constchar*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(constintsize);Has the same behavior as the standard libc function strdup.
char*mx_strdup(constchar*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(constchar*s1, constchar*s2);Takes a pointer to a string, frees string memory with free and sets the string to NULL.
voidmx_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.
voidmx_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(constchar*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.
intmx_read_line(char**lineptr, size_tbuf_size, chardelim, constintfd);Has the same behavior as the standard libc function strncpy.
char*mx_strncpy(char*dst, constchar*src, intlen);Reverses a string using pointers. Do nothing if a string does not exist.
voidmx_str_reverse(char*s);Has the same behavior as the standard libc function strndup.
char*mx_strndup(constchar*s1, size_tn);Has the same behavior as the standard libc function strstr.
char*mx_strstr(constchar*haystack, constchar*needle);Counts words in a string.
Returns the number of words in the string.
intmx_count_words(constchar*str, charc);Counts the substrings sub in the string str.
Returns:
- the count of
subinstr; 0ifsubis an empty string;-1ifstrand/orsubdo not exist.
intmx_count_substr(constchar*str, constchar*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.
intmx_get_char_index(constchar*str, charc);Finds the index of a substring.
Returns:
- the index of the first character of
subinstr; -1ifsubis not found instr;-2ifstrorsubdoes not exist.
intmx_get_substr_index(constchar*str, constchar*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(constchar*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(constchar*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(constchar*s, charc);Replaces all occurrences of sub in str with replace.
Returns:
- a new string where substrings are replaced;
NULLifsuborstrorreplacedoes not exist.
char*mx_replace_substr(constchar*str, constchar*sub, constchar*replace);Has the same behavior as the standard libc function memset.
void*mx_memset(void*b, intc, size_tlen);Has the same behavior as the standard libc function memcpy.
void*mx_memcpy(void*restrict dst, constvoid*restrict src, size_tn);Has the same behavior as the standard stdlib function memcmp.
intmx_memcmp(constvoid*s1, constvoid*s2, size_tn);Has the same behavior as the standard stdlib function realloc.
void*mx_realloc(void*ptr, size_tsize);Has the same behavior as the standard libc function memmove.
void*mx_memmove(void*dst, constvoid*src, size_tlen);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(constvoid*s, intc, size_tn);Has the same behavior as the standard stdlib function memccpy.
void*mx_memccpy(void*restrict dst, constvoid* restrict src, intc, size_tn);Has the same behavior as the standard stdlib function memchr.
void*mx_memchr(constvoid*s, intc, size_tn);Has the same behavior as the standard libc function memmem.
void*mx_memmem(constvoid*big, size_tbig_len, constvoid*little, size_tlittle_len);Some functions to work with aingly linked list which contains void pointer.
typedefstructs_list {
void*data;
structs_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.
voidmx_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.
voidmx_push_back(t_list**list, void*data);Removes the first node of the linked list and frees the memory allocated for the node.
voidmx_pop_front(t_list**head);Removes the last node of the linked list and frees the memory allocated for the node.
voidmx_pop_back(t_list**head);Calculates the number of nodes in a linked list.
Returns the amount of nodes in the linked list.
intmx_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_tmx_malloc_size(void*p);Has the same behaviour as the standard libc function isdigit.
boolmx_isdigit(intc);Has the same behaviour as the standard libc function isspace.
boolmx_isspace(intc);Has the same behaviour as the standard libc function tolower.
intmx_tolower(intc);Has the same behaviour as the standard libc function toupper.
intmx_toupper(intc);Has the same behaviour as the standard libc function isalpha.
boolmx_isalpha(intc);Create a function that has the same behaviour as the standard libc function strchr.
char*mx_strchr(constchar*s, intc);Converts an ASCII string to an integer as the standard libc function atoi does.
intmx_atoi(constchar*str);Has the same behaviour as the standard libc function strncmp.
intmx_strncmp(constchar*s1, constchar*s2, intn);