Skip to content

Latest commit

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

LIBMX Documentation

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!

Table of contents


Utils pack

Print character

Outputs a single character to the standard output.

voidmx_printchar(charc);

Print string

Outputs a string of characters to the standard output.

voidmx_printstr(constchar*s);

Print array of strings

Outputs:

  • an array of strings arr to the standard output with a delimiter delim between the elements of an array;
  • nothing if arr or delim do 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);

Print integer

Outputs integer values to the standard output.

voidmx_printint(intn);

Decimal to hex

Converts an unsigned long number into a hexadecimal string.

Returns the number converted to a hexadecimal string.

char*mx_nbr_to_hex(unsigned longnbr);

Bubble sort

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);

Quick sort

Sorts an array of integers in ascending order using the quick sort algorithm.

Returns:

  • the number of swaps;
  • -1 if arr does not exist.
intmx_quicksort(int*arr, intleft, intright);

Integer to ASCII

Takes an integer and converts it to a string.

Returns the number as a NULL-terminated string.

char*mx_itoa(intnumber);

Print multibyte characters

Outputs ASCII and multibyte characters to the standard output.

voidmx_print_unicode(wchar_tc);

Exponentiation

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);

Square root

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);

Hex to decimal

Converts a hexadecimal string into an unsigned long number.

Returns the unsigned long number.

unsigned longmx_hex_to_nbr(constchar*hex);

For each

Applies the function f for each element of the array arr given size.

voidmx_foreach(int*arr, intsize, void(*f)(int));

Binary search

Searches the strings in the array arr with the given size of array using the binary search algorithm.

Returns:

  • the index of the found string in the array
  • -1 in case of errors or if the string has not been found
  • assigns the number of required iterations to the count pointer.
intmx_binary_search(char**arr, intsize, constchar*s, int*count);

Strings pack

String length

Has the same behavior as the corresponding standard libc function strlen.

intmx_strlen(constchar*s);

Swap characters

Swaps the characters of a string using pointers. Do nothing if s1 or s2 does not exist.

voidmx_swap_char(char*s1, char*s2);

Copy string

Has the same behavior as the standard libc function strcpy.

char*mx_strcpy(char*dst, constchar*src);

Compare strings

Has the same behavior as the standard libc function strcmp.

intmx_strcmp(constchar*s1, constchar*s2);

Concatenate strings

Has the same behavior as the standard libc function strcat.

char*mx_strcat(char*restrict s1, constchar*restrict s2);

New string

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);

Duplicate string

Has the same behavior as the standard libc function strdup.

char*mx_strdup(constchar*s1);

Join strings

Concatenates strings s1 and s2 into a new string. Terminates the new string with '\0'.

Returns:

  • the string as a result of concatenation s1 and s2;
  • the new copy of non-NULL parameter if one and only one of the parameters is NULL;
  • NULL if the concatenation fails.
char*mx_strjoin(constchar*s1, constchar*s2);

Delete string

Takes a pointer to a string, frees string memory with free and sets the string to NULL.

voidmx_strdel(char**str);

Delete array of strings

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);

File to string

Takes a filename as a parameter and reads data from the file into a string.

Returns:

  • NULL-terminated string;
  • NULL in case of any errors.
char*mx_file_to_str(constchar*file);

Readline

Reads the line from the given fd into the lineptr until it:

  • reaches a delim character. The delimiter must not be returned with lineptr;
  • reaches the End Of File (EOF);

Returns:

  • the number of bytes that are written into lineptr;
  • -1 if EOF is reached and there is nothing to write in lineptr;
  • -2 in case of errors or fd is invalid.
intmx_read_line(char**lineptr, size_tbuf_size, chardelim, constintfd);

Copy them all

Has the same behavior as the standard libc function strncpy.

char*mx_strncpy(char*dst, constchar*src, intlen);

Reverse string

Reverses a string using pointers. Do nothing if a string does not exist.

voidmx_str_reverse(char*s);

Duplicate part of string

Has the same behavior as the standard libc function strndup.

char*mx_strndup(constchar*s1, size_tn);

Locate substring

Has the same behavior as the standard libc function strstr.

char*mx_strstr(constchar*haystack, constchar*needle);

Count words

Counts words in a string.

Returns the number of words in the string.

intmx_count_words(constchar*str, charc);

Count substrings

Counts the substrings sub in the string str.

Returns:

  • the count of sub in str;
  • 0 if sub is an empty string;
  • -1 if str and/or sub do not exist.
intmx_count_substr(constchar*str, constchar*sub);

Get character index

Finds the index of the first occurrence of the character c in a string str.

Returns:

  • the index of the first occurrence;
  • -1 if no occurrence is found;
  • -2 if the string does not exist.
intmx_get_char_index(constchar*str, charc);

Get substring index

Finds the index of a substring.

Returns:

  • the index of the first character of sub in str;
  • -1 if sub is not found in str;
  • -2 if str or sub does not exist.
intmx_get_substr_index(constchar*str, constchar*sub);

Trim strings

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;
  • NULL if the string str does not exist or string trim fails.
char*mx_strtrim(constchar*str);

Clean string

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;
  • NULL if the string str does not exist or string creation fails.
char*mx_del_extra_spaces(constchar*str);

Split string

Converts a strings to a NULL-terminated array of words.

Returns:

  • the NULL-terminated array of strings;
  • NULL if the strings does not exist or conversion fails.
char**mx_strsplit(constchar*s, charc);

Replace substrings

Replaces all occurrences of sub in str with replace.

Returns:

  • a new string where substrings are replaced;
  • NULL if sub or str or replace does not exist.
char*mx_replace_substr(constchar*str, constchar*sub, constchar*replace);

Memory pack

Fill memory

Has the same behavior as the standard libc function memset.

void*mx_memset(void*b, intc, size_tlen);

Copy memory

Has the same behavior as the standard libc function memcpy.

void*mx_memcpy(void*restrict dst, constvoid*restrict src, size_tn);

Compare memory

Has the same behavior as the standard stdlib function memcmp.

intmx_memcmp(constvoid*s1, constvoid*s2, size_tn);

Reallocate memory

Has the same behavior as the standard stdlib function realloc.

void*mx_realloc(void*ptr, size_tsize);

Non-overlapping memory copy

Has the same behavior as the standard libc function memmove.

void*mx_memmove(void*dst, constvoid*src, size_tlen);

Locate byte from end

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);

Copy memory to ...

Has the same behavior as the standard stdlib function memccpy.

void*mx_memccpy(void*restrict dst, constvoid* restrict src, intc, size_tn);

Locate byte from start

Has the same behavior as the standard stdlib function memchr.

void*mx_memchr(constvoid*s, intc, size_tn);

Locate block of bytes

Has the same behavior as the standard libc function memmem.

void*mx_memmem(constvoid*big, size_tbig_len, constvoid*little, size_tlittle_len);

List pack

Some functions to work with aingly linked list which contains void pointer.

typedefstructs_list {
void*data;
structs_list*next;
} t_list;

Create node

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);

Push front

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);

Push back

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);

Pop front

Removes the first node of the linked list and frees the memory allocated for the node.

voidmx_pop_front(t_list**head);

Pop back

Removes the last node of the linked list and frees the memory allocated for the node.

voidmx_pop_back(t_list**head);

Size of list

Calculates the number of nodes in a linked list.

Returns the amount of nodes in the linked list.

intmx_list_size(t_list*list);

Sort 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*));

Extra pack

Allocated memory size

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);

Is digit?

Has the same behaviour as the standard libc function isdigit.

boolmx_isdigit(intc);

Is white-space?

Has the same behaviour as the standard libc function isspace.

boolmx_isspace(intc);

To lower case

Has the same behaviour as the standard libc function tolower.

intmx_tolower(intc);

To upper case

Has the same behaviour as the standard libc function toupper.

intmx_toupper(intc);

Is alphabetic?

Has the same behaviour as the standard libc function isalpha.

boolmx_isalpha(intc);

Locate character

Create a function that has the same behaviour as the standard libc function strchr.

char*mx_strchr(constchar*s, intc);

ASCII to integer

Converts an ASCII string to an integer as the standard libc function atoi does.

intmx_atoi(constchar*str);

Compare strings N

Has the same behaviour as the standard libc function strncmp.

intmx_strncmp(constchar*s1, constchar*s2, intn);

About

This is training project for Ucode IT Academy. An ANSI-compatible set of general-purpose functions.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages