Libft
42 Libft library documentation
Loading...
Searching...
No Matches
libft.h File Reference
#include <unistd.h>
#include <stdlib.h>
#include <stddef.h>
#include "types.h"
+ Include dependency graph for libft.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int ft_atoi (const char *str)
 Converts an ASCII string to an int value.
 
void ft_bzero (void *s, size_t n)
 Sets a memory area to zero bytes.
 
void * ft_calloc (size_t count, size_t size)
 Allocates and zero-initializes an array.
 
int ft_isalnum (int c)
 Checks whether a character is alphanumeric.
 
int ft_isalpha (int c)
 
int ft_isascii (int c)
 Checks whether a character value is a valid ASCII byte.
 
int ft_isdigit (int c)
 Checks whether a character is a decimal digit.
 
int ft_isprint (int c)
 Checks whether a character is printable in ASCII.
 
char * ft_itoa (int n)
 Converts an integer to a newly allocated decimal string.
 
void ft_lstadd_back (t_list **lst, t_list *new)
 Adds a new element to the end of a linked list. This function takes a pointer to the first element of a linked list and a new element, then adds the new element to the end of the list. If the list is empty, the new element becomes the first element.
 
void ft_lstadd_front (t_list **lst, t_list *new)
 Adds a new element to the front of a linked list. This function takes a pointer to the first element of a linked list and a new element, then adds the new element to the front of the list. The new element becomes the new head of the list.
 
void ft_lstclear (t_list **lst, void(*del)(void *))
 Clears a linked list. This function takes a pointer to the first element of a linked list and a function pointer to a deletion function, then removes and frees all elements in the list.
 
void ft_lstdelone (t_list *lst, void(*del)(void *))
 Deletes and frees a single element from a linked list. This function takes an element from a linked list and a function pointer to a deletion function, then removes and frees the element.
 
void ft_lstiter (t_list *lst, void(*f)(void *))
 Iterates over a linked list and applies a function to each element. This function takes a linked list and a function pointer, then applies the function to the content of each element in the list.
 
t_listft_lstlast (t_list *lst)
 Returns the last element of a linked list. This function takes a linked list and returns a pointer to the last element.
 
t_listft_lstmap (t_list *lst, void *(*f)(void *), void(*del)(void *))
 Creates a new linked list with the results of applying a function to each element of the original list. This function takes a linked list and two function pointers, then applies the first function to the content of each element in the list and creates a new list with the results.
 
t_listft_lstnew (void *content)
 Creates a new linked-list node.
 
int ft_lstsize (t_list *lst)
 Counts the number of nodes in a linked list.
 
void * ft_memchr (const void *s, int c, size_t n)
 Scans a memory area for a byte value.
 
int ft_memcmp (const void *s1, const void *s2, size_t n)
 Compares two memory areas byte by byte.
 
void * ft_memcpy (void *dest, const void *src, size_t n)
 Copies bytes from source to destination.
 
void * ft_memmove (void *dst, const void *src, size_t len)
 Copies bytes between potentially overlapping memory areas.
 
void * ft_memset (void *s, int c, size_t len)
 Fills a memory area with a byte value.
 
void ft_putchar_fd (char c, int fd)
 Writes one character to a file descriptor.
 
void ft_putendl_fd (char *s, int fd)
 Writes a string followed by a newline to a file descriptor.
 
void ft_putnbr_fd (int n, int fd)
 Writes an integer in decimal format to a file descriptor.
 
void ft_putstr_fd (char *s, int fd)
 Writes a string to a file descriptor.
 
char ** ft_split (char const *s, char c)
 Splits a string into an array of substrings.
 
char * ft_strchr (const char *s, int c)
 Finds the first occurrence of a character in a string.
 
char * ft_strdup (const char *s1)
 Duplicates a C string into newly allocated memory.
 
void ft_striteri (char *s, void(*f)(unsigned int, char *))
 Applies a callback to each character of a string in place.
 
char * ft_strjoin (char const *s1, char const *s2)
 Concatenates two strings into a newly allocated string.
 
size_t ft_strlcat (char *dest, const char *src, size_t len)
 Appends a string to a bounded destination buffer.
 
size_t ft_strlcpy (char *dest, const char *src, size_t size)
 Copies a string into a bounded destination buffer.
 
size_t ft_strlen (const char *str)
 Returns the length of a null-terminated string.
 
char * ft_strmapi (char const *s, char(*f)(unsigned int, char))
 Maps a function over a string into a new allocated string.
 
int ft_strncmp (const char *s1, const char *s2, size_t n)
 Compares two strings up to a maximum number of characters.
 
char * ft_strnstr (const char *haystack, const char *needle, size_t len)
 Finds a substring within a bounded string region.
 
char * ft_strrchr (const char *s, int c)
 Finds the last occurrence of a character in a string.
 
char * ft_strtrim (char const *s1, char const *set)
 Trims leading and trailing characters from a set.
 
char * ft_substr (char const *s, unsigned int start, size_t len)
 Extracts a substring from a string.
 
int ft_tolower (int c)
 Converts an uppercase ASCII letter to lowercase.
 
int ft_toupper (int c)
 Converts a lowercase ASCII letter to uppercase.
 

Function Documentation

◆ ft_atoi()

int ft_atoi ( const char *  str)

Converts an ASCII string to an int value.

Skips leading whitespace, parses an optional sign, then accumulates consecutive decimal digits.

Parameters
strInput C string.
Returns
Parsed integer value.

Definition at line 22 of file ft_atoi.c.

23{
24 int res;
25 int sign;
26 int i;
27
28 res = 0;
29 sign = 1;
30 i = 0;
31 while ((str[i] >= 9 && str[i] <= 13) || str[i] == 32)
32 i++;
33 if (str[i] == '-' || str[i] == '+')
34 {
35 if (str[i] == '-')
36 sign = -1;
37 i++;
38 }
39 while (str[i] >= '0' && str[i] <= '9')
40 {
41 res = res * 10 + (str[i] - '0');
42 i++;
43 }
44 return (res * sign);
45}

◆ ft_bzero()

void ft_bzero ( void *  void_ptr,
size_t  n 
)

Sets a memory area to zero bytes.

Parameters
void_ptrPointer to the memory block.
nNumber of bytes to zero.

Definition at line 21 of file ft_bzero.c.

22{
23 ft_memset(void_ptr, 0, n);
24}
void * ft_memset(void *s, int c, size_t len)
Fills a memory area with a byte value.
Definition ft_memset.c:23

References ft_memset().

+ Here is the call graph for this function:

◆ ft_calloc()

void * ft_calloc ( size_t  nmemb,
size_t  size 
)

Allocates and zero-initializes an array.

Allocates memory for nmemb elements of size bytes each and sets all allocated bytes to zero. Returns NULL on overflow or allocation failure.

Parameters
nmembNumber of elements.
sizeSize of each element in bytes.
Returns
Pointer to allocated memory, or NULL on failure.

Definition at line 26 of file ft_calloc.c.

27{
28 void *ptr;
29 size_t total_size;
30
31 if (nmemb == 0 || size == 0)
32 return (malloc(0));
33 if (nmemb > (size_t)-1 / size)
34 return (NULL);
35 total_size = nmemb * size;
36 ptr = malloc(total_size);
37 if (!ptr)
38 return (NULL);
39 ft_memset(ptr, 0, total_size);
40 return (ptr);
41}

References ft_memset().

+ Here is the call graph for this function:

◆ ft_isalnum()

int ft_isalnum ( int  c)

Checks whether a character is alphanumeric.

Parameters
cCharacter value to test.
Returns
Non-zero if c is a letter or a digit, 0 otherwise.

Definition at line 19 of file ft_isalnum.c.

20{
21 return (((unsigned int)(c - '0') < 10)
22 || ((unsigned int)((c | 32) - 'a') < 26));
23}

◆ ft_isalpha()

int ft_isalpha ( int  c)

◆ ft_isascii()

int ft_isascii ( int  c)

Checks whether a character value is a valid ASCII byte.

Parameters
cCharacter value to test.
Returns
Non-zero if c is in [0, 127], 0 otherwise.

Definition at line 19 of file ft_isascii.c.

20{
21 return ((unsigned int)c < 128);
22}

◆ ft_isdigit()

int ft_isdigit ( int  c)

Checks whether a character is a decimal digit.

Parameters
cCharacter value to test.
Returns
Non-zero if c is in ['0', '9'], 0 otherwise.

Definition at line 19 of file ft_isdigit.c.

20{
21 return ((unsigned int)(c - '0') < 10);
22}

◆ ft_isprint()

int ft_isprint ( int  c)

Checks whether a character is printable in ASCII.

Parameters
cCharacter value to test.
Returns
Non-zero if c is in [32, 126], 0 otherwise.

Definition at line 19 of file ft_isprint.c.

20{
21 return ((unsigned int)(c - 32) < 95);
22}

◆ ft_itoa()

char * ft_itoa ( int  n)

Converts an integer to a newly allocated decimal string.

Parameters
nInteger value to convert.
Returns
Newly allocated string, or NULL on allocation failure.

Definition at line 23 of file ft_itoa.c.

24{
25 char *str;
26 size_t len;
27 unsigned int nb;
28
29 len = ft_num_len(n);
30 str = (char *)malloc(sizeof(char) * (len + 1));
31 if (!str)
32 return (NULL);
33 str[len] = '\0';
34 if (n < 0)
35 {
36 str[0] = '-';
37 nb = -n;
38 }
39 else
40 nb = n;
41 if (nb == 0)
42 str[0] = '0';
43 while (nb != 0)
44 {
45 str[--len] = (nb % 10) + '0';
46 nb /= 10;
47 }
48 return (str);
49}
static size_t ft_num_len(int n)
Computes the number of characters needed for an int string.
Definition ft_itoa.c:59

References ft_num_len().

+ Here is the call graph for this function:

◆ ft_lstadd_back()

void ft_lstadd_back ( t_list **  lst,
t_list new 
)

Adds a new element to the end of a linked list. This function takes a pointer to the first element of a linked list and a new element, then adds the new element to the end of the list. If the list is empty, the new element becomes the first element.

Parameters
lstA pointer to the pointer to the first element of the list.
newThe new element to add to the end of the list.

Definition at line 25 of file ft_lstadd_back.c.

26{
27 t_list *last;
28
29 if (!lst || !new)
30 return ;
31 if (!*lst)
32 {
33 *lst = new;
34 return ;
35 }
36 last = ft_lstlast(*lst);
37 last->next = new;
38}
t_list * ft_lstlast(t_list *lst)
Returns the last element of a linked list. This function takes a linked list and returns a pointer to...
Definition ft_lstlast.c:23
Definition types.h:4
struct s_list * next
Definition types.h:6

References ft_lstlast(), and s_list::next.

Referenced by ft_lstmap().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ft_lstadd_front()

void ft_lstadd_front ( t_list **  lst,
t_list new 
)

Adds a new element to the front of a linked list. This function takes a pointer to the first element of a linked list and a new element, then adds the new element to the front of the list. The new element becomes the new head of the list.

Parameters
lstA pointer to the pointer to the first element of the list.
newThe new element to add to the front of the list.

Definition at line 24 of file ft_lstadd_front.c.

25{
26 if (!lst || !new)
27 return ;
28 new->next = *lst;
29 *lst = new;
30}

◆ ft_lstclear()

void ft_lstclear ( t_list **  lst,
void(*)(void *)  del 
)

Clears a linked list. This function takes a pointer to the first element of a linked list and a function pointer to a deletion function, then removes and frees all elements in the list.

Parameters
lstA pointer to the pointer to the first element of the list.
delA function pointer to the deletion function.

Definition at line 25 of file ft_lstclear.c.

26{
27 t_list *tmp;
28
29 if (!lst || !del)
30 return ;
31 while (*lst)
32 {
33 tmp = (*lst)->next;
34 ft_lstdelone(*lst, del);
35 *lst = tmp;
36 }
37}
void ft_lstdelone(t_list *lst, void(*del)(void *))
Deletes and frees a single element from a linked list. This function takes an element from a linked l...

References ft_lstdelone(), and s_list::next.

Referenced by ft_lstmap().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ft_lstdelone()

void ft_lstdelone ( t_list lst,
void(*)(void *)  del 
)

Deletes and frees a single element from a linked list. This function takes an element from a linked list and a function pointer to a deletion function, then removes and frees the element.

Parameters
lstThe element to delete and free.
delA function pointer to the deletion function.

Definition at line 24 of file ft_lstdelone.c.

25{
26 if (!lst || !del)
27 return ;
28 del(lst->content);
29 free(lst);
30}
void * content
Definition types.h:5

References s_list::content.

Referenced by ft_lstclear().

+ Here is the caller graph for this function:

◆ ft_lstiter()

void ft_lstiter ( t_list lst,
void(*)(void *)  f 
)

Iterates over a linked list and applies a function to each element. This function takes a linked list and a function pointer, then applies the function to the content of each element in the list.

Parameters
lstThe linked list to iterate over.
fThe function to apply to each element.

Definition at line 23 of file ft_lstiter.c.

24{
25 if (!lst || !f)
26 return ;
27 while (lst)
28 {
29 f(lst->content);
30 lst = lst->next;
31 }
32}

References s_list::content, and s_list::next.

◆ ft_lstlast()

t_list * ft_lstlast ( t_list lst)

Returns the last element of a linked list. This function takes a linked list and returns a pointer to the last element.

Parameters
lstThe linked list to search.
Returns
A pointer to the last element of the list, or NULL if the list is empty.

Definition at line 23 of file ft_lstlast.c.

24{
25 if (!lst)
26 return (NULL);
27 while (lst->next)
28 lst = lst->next;
29 return (lst);
30}

References s_list::next.

Referenced by ft_lstadd_back().

+ Here is the caller graph for this function:

◆ ft_lstmap()

t_list * ft_lstmap ( t_list lst,
void *(*)(void *)  f,
void(*)(void *)  del 
)

Creates a new linked list with the results of applying a function to each element of the original list. This function takes a linked list and two function pointers, then applies the first function to the content of each element in the list and creates a new list with the results.

Parameters
lstThe linked list to iterate over.
fThe function to apply to each element.
delThe function to delete each element.
Returns
A pointer to the new linked list, or NULL if an error occurs.

Definition at line 26 of file ft_lstmap.c.

27{
28 t_list *new_list;
29 t_list *new_elem;
30 void *content;
31
32 if (!lst || !f || !del)
33 return (NULL);
34 new_list = NULL;
35 while (lst)
36 {
37 content = f(lst->content);
38 new_elem = ft_lstnew(content);
39 if (!new_elem)
40 {
41 del(content);
42 ft_lstclear(&new_list, del);
43 return (NULL);
44 }
45 ft_lstadd_back(&new_list, new_elem);
46 lst = lst->next;
47 }
48 return (new_list);
49}
void ft_lstadd_back(t_list **lst, t_list *new)
Adds a new element to the end of a linked list. This function takes a pointer to the first element of...
void ft_lstclear(t_list **lst, void(*del)(void *))
Clears a linked list. This function takes a pointer to the first element of a linked list and a funct...
Definition ft_lstclear.c:25
t_list * ft_lstnew(void *content)
Creates a new linked-list node.
Definition ft_lstnew.c:24

References s_list::content, ft_lstadd_back(), ft_lstclear(), ft_lstnew(), and s_list::next.

+ Here is the call graph for this function:

◆ ft_lstnew()

t_list * ft_lstnew ( void *  content)

Creates a new linked-list node.

The node is initialized with content and its next pointer is set to NULL.

Parameters
contentPointer stored in the node.
Returns
Newly allocated node, or NULL on allocation failure.

Definition at line 24 of file ft_lstnew.c.

25{
26 t_list *new_elem;
27
28 new_elem = (t_list *)malloc(sizeof(t_list));
29 if (!new_elem)
30 return (NULL);
31 new_elem->content = content;
32 new_elem->next = NULL;
33 return (new_elem);
34}

References s_list::content, and s_list::next.

Referenced by ft_lstmap().

+ Here is the caller graph for this function:

◆ ft_lstsize()

int ft_lstsize ( t_list lst)

Counts the number of nodes in a linked list.

Parameters
lstHead of the list.
Returns
Number of nodes.

Definition at line 21 of file ft_lstsize.c.

22{
23 int count;
24
25 count = 0;
26 while (lst)
27 {
28 lst = lst->next;
29 count++;
30 }
31 return (count);
32}

References s_list::next.

◆ ft_memchr()

void * ft_memchr ( const void *  s,
int  c,
size_t  n 
)

Scans a memory area for a byte value.

Parameters
sPointer to the memory area.
cByte value to search for.
nNumber of bytes to inspect.
Returns
Pointer to the first matching byte, or NULL if not found.

Definition at line 23 of file ft_memchr.c.

24{
25 const unsigned char *ptr;
26
27 ptr = (const unsigned char *)s;
28 while (n--)
29 {
30 if (*ptr == (unsigned char)c)
31 return ((void *)ptr);
32 ptr++;
33 }
34 return (NULL);
35}

◆ ft_memcmp()

int ft_memcmp ( const void *  s1,
const void *  s2,
size_t  n 
)

Compares two memory areas byte by byte.

Parameters
s1First memory area.
s2Second memory area.
nNumber of bytes to compare.
Returns
Negative, zero, or positive difference at first mismatch.

Definition at line 23 of file ft_memcmp.c.

24{
25 const unsigned char *p1;
26 const unsigned char *p2;
27
28 p1 = (const unsigned char *)s1;
29 p2 = (const unsigned char *)s2;
30 while (n--)
31 {
32 if (*p1 != *p2)
33 return (*p1 - *p2);
34 p1++;
35 p2++;
36 }
37 return (0);
38}

◆ ft_memcpy()

void * ft_memcpy ( void *  dest,
const void *  src,
size_t  n 
)

Copies bytes from source to destination.

Behavior is undefined for overlapping regions; use ft_memmove for overlap.

Parameters
destDestination memory area.
srcSource memory area.
nNumber of bytes to copy.
Returns
Original dest pointer, or NULL when both pointers are NULL.

Definition at line 25 of file ft_memcpy.c.

26{
27 unsigned char *d;
28 const unsigned char *s;
29
30 if (!dest && !src)
31 return (NULL);
32 d = (unsigned char *)dest;
33 s = (const unsigned char *)src;
34 while (n--)
35 *d++ = *s++;
36 return (dest);
37}

Referenced by ft_strdup(), and ft_strjoin().

+ Here is the caller graph for this function:

◆ ft_memmove()

void * ft_memmove ( void *  dst,
const void *  src,
size_t  len 
)

Copies bytes between potentially overlapping memory areas.

Parameters
dstDestination memory area.
srcSource memory area.
lenNumber of bytes to copy.
Returns
Original dst pointer, or NULL when both pointers are NULL.

Definition at line 23 of file ft_memmove.c.

24{
25 unsigned char *d;
26 const unsigned char *s;
27
28 if (!dst && !src)
29 return (NULL);
30 d = (unsigned char *)dst;
31 s = (const unsigned char *)src;
32 if (d > s)
33 {
34 while (len--)
35 d[len] = s[len];
36 }
37 else
38 {
39 while (len--)
40 *d++ = *s++;
41 }
42 return (dst);
43}

◆ ft_memset()

void * ft_memset ( void *  s,
int  c,
size_t  len 
)

Fills a memory area with a byte value.

Parameters
sPointer to the memory area.
cByte value used for filling.
lenNumber of bytes to set.
Returns
Original s pointer.

Definition at line 23 of file ft_memset.c.

24{
25 unsigned char *ptr;
26
27 ptr = (unsigned char *)s;
28 while (len--)
29 *ptr++ = (unsigned char)c;
30 return (s);
31}

Referenced by ft_bzero(), and ft_calloc().

+ Here is the caller graph for this function:

◆ ft_putchar_fd()

void ft_putchar_fd ( char  c,
int  fd 
)

Writes one character to a file descriptor.

Parameters
cCharacter to write.
fdDestination file descriptor.

Definition at line 21 of file ft_putchar_fd.c.

22{
23 write(fd, &c, 1);
24}

Referenced by ft_putnbr_fd().

+ Here is the caller graph for this function:

◆ ft_putendl_fd()

void ft_putendl_fd ( char *  s,
int  fd 
)

Writes a string followed by a newline to a file descriptor.

Does nothing when s is NULL.

Parameters
sString to write.
fdDestination file descriptor.

Definition at line 23 of file ft_putendl_fd.c.

24{
25 if (!s)
26 return ;
27 ft_putstr_fd(s, fd);
28 write(fd, "\n", 1);
29}
void ft_putstr_fd(char *s, int fd)
Writes a string to a file descriptor.

References ft_putstr_fd().

+ Here is the call graph for this function:

◆ ft_putnbr_fd()

void ft_putnbr_fd ( int  n,
int  fd 
)

Writes an integer in decimal format to a file descriptor.

Handles negative values and the full int range.

Parameters
nValue to write.
fdDestination file descriptor.

Definition at line 23 of file ft_putnbr_fd.c.

24{
25 long nb;
26
27 nb = n;
28 if (nb < 0)
29 {
30 ft_putchar_fd('-', fd);
31 nb = -nb;
32 }
33 if (nb >= 10)
34 ft_putnbr_fd(nb / 10, fd);
35 ft_putchar_fd((nb % 10) + '0', fd);
36}
void ft_putnbr_fd(int n, int fd)
Writes an integer in decimal format to a file descriptor.
void ft_putchar_fd(char c, int fd)
Writes one character to a file descriptor.

References ft_putchar_fd(), and ft_putnbr_fd().

Referenced by ft_putnbr_fd().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ft_putstr_fd()

void ft_putstr_fd ( char *  s,
int  fd 
)

Writes a string to a file descriptor.

Does nothing when s is NULL.

Parameters
sString to write.
fdDestination file descriptor.

Definition at line 23 of file ft_putstr_fd.c.

24{
25 if (!s)
26 return ;
27 write(fd, s, ft_strlen(s));
28}
size_t ft_strlen(const char *str)
Returns the length of a null-terminated string.
Definition ft_strlen.c:21

References ft_strlen().

Referenced by ft_putendl_fd().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ft_split()

char ** ft_split ( char const *  s,
char  c 
)

Splits a string into an array of substrings.

Uses the delimiter c to separate words. The returned array is NULL-terminated.

Parameters
sInput string.
cDelimiter character.
Returns
Newly allocated NULL-terminated array, or NULL on failure.

Definition at line 30 of file ft_split.c.

31{
32 char **split;
33
34 if (!s)
35 return (NULL);
36 split = malloc(sizeof(char *) * (ft_count_words(s, c) + 1));
37 if (!split)
38 return (NULL);
39 return (ft_fill_split(split, s, c));
40}
static size_t ft_count_words(char const *s, char c)
Counts words separated by a delimiter in a string.
Definition ft_split.c:83
static char ** ft_fill_split(char **split, char const *s, char c)
Fills the split array with allocated word substrings.
Definition ft_split.c:50

References ft_count_words(), and ft_fill_split().

+ Here is the call graph for this function:

◆ ft_strchr()

char * ft_strchr ( const char *  s,
int  c 
)

Finds the first occurrence of a character in a string.

The terminating '\0' is considered part of the string.

Parameters
sInput string.
cCharacter to search for.
Returns
Pointer to the first match, or NULL if not found.

Definition at line 24 of file ft_strchr.c.

25{
26 while (*s != (char)c)
27 {
28 if (!*s)
29 return (NULL);
30 s++;
31 }
32 return ((char *)s);
33}

Referenced by ft_strtrim().

+ Here is the caller graph for this function:

◆ ft_strdup()

char * ft_strdup ( const char *  s)

Duplicates a C string into newly allocated memory.

Parameters
sSource string.
Returns
Newly allocated duplicate, or NULL on allocation failure.

Definition at line 21 of file ft_strdup.c.

22{
23 char *dup;
24 size_t len;
25
26 len = ft_strlen(s) + 1;
27 dup = (char *)malloc(len);
28 if (!dup)
29 return (NULL);
30 ft_memcpy(dup, s, len);
31 return (dup);
32}
void * ft_memcpy(void *dest, const void *src, size_t n)
Copies bytes from source to destination.
Definition ft_memcpy.c:25

References ft_memcpy(), and ft_strlen().

Referenced by ft_substr().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ft_striteri()

void ft_striteri ( char *  s,
void(*)(unsigned int, char *)  f 
)

Applies a callback to each character of a string in place.

The callback receives the character index and a pointer to the character.

Parameters
sString to modify.
fCallback applied to each character.

Definition at line 21 of file ft_striteri.c.

22{
23 unsigned int i;
24
25 if (!s || !f)
26 return ;
27 i = 0;
28 while (s[i])
29 {
30 f(i, &s[i]);
31 i++;
32 }
33}

◆ ft_strjoin()

char * ft_strjoin ( char const *  s1,
char const *  s2 
)

Concatenates two strings into a newly allocated string.

Parameters
s1First input string.
s2Second input string.
Returns
Newly allocated concatenation, or NULL on failure.

Definition at line 22 of file ft_strjoin.c.

23{
24 char *join;
25 size_t len1;
26 size_t len2;
27
28 if (!s1 || !s2)
29 return (NULL);
30 len1 = ft_strlen(s1);
31 len2 = ft_strlen(s2);
32 join = (char *)malloc(sizeof(char) * (len1 + len2 + 1));
33 if (!join)
34 return (NULL);
35 ft_memcpy(join, s1, len1);
36 ft_memcpy(join + len1, s2, len2);
37 join[len1 + len2] = '\0';
38 return (join);
39}

References ft_memcpy(), and ft_strlen().

+ Here is the call graph for this function:

◆ ft_strlcat()

size_t ft_strlcat ( char *  dest,
const char *  src,
size_t  size 
)

Appends a string to a bounded destination buffer.

Parameters
destDestination buffer.
srcSource string.
sizeFull size of dest buffer.
Returns
Length of the string it tried to create.

Definition at line 23 of file ft_strlcat.c.

24{
25 size_t dest_len;
26 size_t src_len;
27 size_t i;
28
29 dest_len = 0;
30 while (dest[dest_len] && dest_len < size)
31 dest_len++;
32 src_len = ft_strlen(src);
33 if (dest_len == size)
34 return (size + src_len);
35 i = 0;
36 while (src[i] && (dest_len + i) < (size - 1))
37 {
38 dest[dest_len + i] = src[i];
39 i++;
40 }
41 dest[dest_len + i] = '\0';
42 return (dest_len + src_len);
43}

References ft_strlen().

+ Here is the call graph for this function:

◆ ft_strlcpy()

size_t ft_strlcpy ( char *  dest,
const char *  src,
size_t  size 
)

Copies a string into a bounded destination buffer.

Parameters
destDestination buffer.
srcSource string.
sizeSize of dest buffer.
Returns
Total length of src.

Definition at line 23 of file ft_strlcpy.c.

24{
25 size_t src_len;
26 size_t i;
27
28 src_len = ft_strlen(src);
29 if (size > 0)
30 {
31 i = 0;
32 while (src[i] && i < (size - 1))
33 {
34 dest[i] = src[i];
35 i++;
36 }
37 dest[i] = '\0';
38 }
39 return (src_len);
40}

References ft_strlen().

Referenced by ft_substr().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ft_strlen()

size_t ft_strlen ( const char *  str)

Returns the length of a null-terminated string.

Parameters
strInput string.
Returns
Number of characters before the terminating null byte.

Definition at line 21 of file ft_strlen.c.

22{
23 const char *s;
24
25 s = str;
26 while (*s)
27 s++;
28 return (s - str);
29}

Referenced by ft_putstr_fd(), ft_strdup(), ft_strjoin(), ft_strlcat(), ft_strlcpy(), ft_strmapi(), ft_strrchr(), ft_strtrim(), and ft_substr().

+ Here is the caller graph for this function:

◆ ft_strmapi()

char * ft_strmapi ( char const *  s,
char(*)(unsigned int, char)  f 
)

Maps a function over a string into a new allocated string.

Parameters
sInput string.
fMapping callback receiving index and character.
Returns
Newly allocated mapped string, or NULL on failure.

Definition at line 22 of file ft_strmapi.c.

23{
24 char *str;
25 unsigned int i;
26 size_t len;
27
28 if (!s || !f)
29 return (NULL);
30 len = ft_strlen(s);
31 str = (char *)malloc(sizeof(char) * (len + 1));
32 if (!str)
33 return (NULL);
34 i = 0;
35 while (s[i])
36 {
37 str[i] = f(i, s[i]);
38 i++;
39 }
40 str[i] = '\0';
41 return (str);
42}

References ft_strlen().

+ Here is the call graph for this function:

◆ ft_strncmp()

int ft_strncmp ( const char *  s1,
const char *  s2,
size_t  n 
)

Compares two strings up to a maximum number of characters.

Parameters
s1First string.
s2Second string.
nMaximum number of characters to compare.
Returns
Negative, zero, or positive difference at first mismatch.

Definition at line 23 of file ft_strncmp.c.

24{
25 size_t i;
26
27 i = 0;
28 if (n == 0)
29 return (0);
30 while (s1[i] && s2[i] && s1[i] == s2[i] && i < n - 1)
31 i++;
32 return ((unsigned char)s1[i] - (unsigned char)s2[i]);
33}

◆ ft_strnstr()

char * ft_strnstr ( const char *  haystack,
const char *  needle,
size_t  len 
)

Finds a substring within a bounded string region.

Searches needle in haystack but not beyond len bytes.

Parameters
haystackString to search in.
needleSubstring to search for.
lenMaximum number of bytes to inspect in haystack.
Returns
Pointer to first match, or NULL when not found.

Definition at line 25 of file ft_strnstr.c.

26{
27 size_t i;
28 size_t j;
29
30 if (!*needle)
31 return ((char *)haystack);
32 i = 0;
33 while (haystack[i] && i < len)
34 {
35 j = 0;
36 while (haystack[i + j] && needle[j] && i + j < len
37 && haystack[i + j] == needle[j])
38 j++;
39 if (!needle[j])
40 return ((char *)&haystack[i]);
41 i++;
42 }
43 return (NULL);
44}

◆ ft_strrchr()

char * ft_strrchr ( const char *  s,
int  c 
)

Finds the last occurrence of a character in a string.

Parameters
sInput string.
cCharacter to search for.
Returns
Pointer to the last match, or NULL if not found.

Definition at line 22 of file ft_strrchr.c.

23{
24 int i;
25
26 i = ft_strlen(s);
27 if ((char)c == '\0')
28 return ((char *)&s[i]);
29 while (i >= 0)
30 {
31 if (s[i] == (char)c)
32 return ((char *)&s[i]);
33 i--;
34 }
35 return (NULL);
36}

References ft_strlen().

+ Here is the call graph for this function:

◆ ft_strtrim()

char * ft_strtrim ( char const *  s1,
char const *  set 
)

Trims leading and trailing characters from a set.

Parameters
s1Source string.
setSet of characters to trim.
Returns
Newly allocated trimmed string, or NULL on failure.

Definition at line 22 of file ft_strtrim.c.

23{
24 size_t start;
25 size_t end;
26
27 if (!s1 || !set)
28 return (NULL);
29 start = 0;
30 while (s1[start] && ft_strchr(set, s1[start]))
31 start++;
32 end = ft_strlen(s1);
33 while (end > start && ft_strchr(set, s1[end - 1]))
34 end--;
35 return (ft_substr(s1, start, end - start));
36}
char * ft_substr(char const *s, unsigned int start, size_t len)
Extracts a substring from a string.
Definition ft_substr.c:23
char * ft_strchr(const char *s, int c)
Finds the first occurrence of a character in a string.
Definition ft_strchr.c:24

References ft_strchr(), ft_strlen(), and ft_substr().

+ Here is the call graph for this function:

◆ ft_substr()

char * ft_substr ( char const *  s,
unsigned int  start,
size_t  len 
)

Extracts a substring from a string.

Parameters
sSource string.
startStart index in s.
lenMaximum substring length.
Returns
Newly allocated substring, or NULL on failure.

Definition at line 23 of file ft_substr.c.

24{
25 char *sub;
26 size_t s_len;
27
28 if (!s)
29 return (NULL);
30 s_len = ft_strlen(s);
31 if (start >= s_len)
32 return (ft_strdup(""));
33 if (len > s_len - start)
34 len = s_len - start;
35 sub = (char *)malloc(sizeof(char) * (len + 1));
36 if (!sub)
37 return (NULL);
38 ft_strlcpy(sub, s + start, len + 1);
39 return (sub);
40}
char * ft_strdup(const char *s1)
Duplicates a C string into newly allocated memory.
Definition ft_strdup.c:21
size_t ft_strlcpy(char *dest, const char *src, size_t size)
Copies a string into a bounded destination buffer.
Definition ft_strlcpy.c:23

References ft_strdup(), ft_strlcpy(), and ft_strlen().

Referenced by ft_fill_split(), and ft_strtrim().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ft_tolower()

int ft_tolower ( int  c)

Converts an uppercase ASCII letter to lowercase.

Parameters
cCharacter value.
Returns
Lowercase ASCII equivalent when applicable, otherwise unchanged.

Definition at line 19 of file ft_tolower.c.

20{
21 return (c + 32 * ((unsigned int)(c - 'A') < 26));
22}

◆ ft_toupper()

int ft_toupper ( int  c)

Converts a lowercase ASCII letter to uppercase.

Parameters
cCharacter value.
Returns
Uppercase ASCII equivalent when applicable, otherwise unchanged.

Definition at line 19 of file ft_toupper.c.

20{
21 return (c - 32 * ((unsigned int)(c - 'a') < 26));
22}