|
libft
|
Functions for handling and manipulating C strings. More...
Files | |
| file | ft_split.c |
| Splits a string into substrings using a single delimiter character. | |
| file | ft_split_charset.c |
| Splits a string into substrings based on a set of delimiters. | |
| file | ft_string.c |
| Returns the number of characters in a string. | |
| file | ft_string.h |
| String manipulation utility functions. | |
| file | ft_strtrim.c |
| Trims characters from the beginning and end of a string. | |
| file | skip_whitespace.c |
| Utility functions to skip leading whitespace in C strings. | |
Functions | |
| char * | ft_itoa (int n) |
| Converts an integer to a string. | |
| char ** | ft_split (const char *s, char c) |
| Splits a string into substrings based on a delimiter character. | |
| char ** | ft_split_charset (char *s, char *charset) |
| Splits a string into substrings based on a character set. | |
| char * | ft_strcat (char *dest, const char *src) |
Appends the src string to the end of the dest string. | |
| char * | ft_strchr (const char *str, int c) |
| Finds the first occurrence of a character in a string. | |
| int | ft_strcmp (const char *s1, const char *s2) |
| Compares two strings lexicographically. | |
| char * | ft_strcpy (char *dst, const char *src) |
| Compares two strings lexicographically. | |
| char * | ft_strdup (const char *src) |
| Allocates and duplicates a null-terminated string. | |
| void | ft_striteri (char *s, void(*f)(unsigned int, char *)) |
| Applies a function to each character of a string, passing its index. | |
| char * | ft_strjoin (const char *s1, const char *s2) |
| Concatenates two strings into a newly allocated string. | |
| char * | ft_strjoin_and_free (char *s1, char *s2, int free_s1, int free_s2) |
| size_t | ft_strlcat (char *dest, const char *src, size_t size) |
Concatenates src to the end of dest while ensuring null-termination. | |
| size_t | ft_strlcpy (char *dest, const char *src, size_t size) |
Copies a string from src to dest, up to size - 1 characters. | |
| size_t | ft_strlen (const char *str) |
| char * | ft_strmapi (char const *s, char(*f)(unsigned int, char)) |
| Applies a function to each character of a string to create a new string. | |
| int | ft_strncmp (const char *s1, const char *s2, size_t n) |
Compares two strings up to n characters. | |
| char * | ft_strndup (const char *src, size_t n) |
Creates a copy of a string, up to n characters. | |
| char * | ft_strnstr (const char *big, const char *little, size_t len) |
| Searches for a substring in a string within a max length. | |
| char * | ft_strrchr (const char *str, int c) |
| Returns a pointer to the last occurrence of a character in a string. | |
| char * | ft_strtrim (const char *s1, const char *set) |
| Trims characters from both ends of a string. | |
| char * | ft_substr (const char *s, unsigned int start, size_t len) |
| Extracts a substring from a string. | |
| int | ft_tolower (int c) |
| Converts a character to lowercase. | |
| int | ft_toupper (int c) |
| Converts a character to uppercase. | |
| void | skip_whitespace_index (const char *input, int *i) |
| Skips leading whitespace by incrementing a pointer index. | |
| char * | skip_whitespace_ptr (const char *s) |
| Returns a pointer to the first non-whitespace character. | |
Functions for handling and manipulating C strings.
This group includes common string operations such as copying, concatenation, trimming, splitting, searching, and case conversion.
Useful for tasks like:
| char * ft_itoa | ( | int | n | ) |
#include <srcs/convert/ft_number_conversion.c>
Converts an integer to a string.
Allocates and returns a new string representing the integer n. Handles negative numbers and returns NULL if allocation fails.
| n | The integer to convert. |
n, or NULL on failure. | char ** ft_split | ( | const char * | s, |
| char | c | ||
| ) |
#include <include/ft_string.h>
Splits a string into substrings based on a delimiter character.
Splits the input string s into substrings separated by the delimiter c. The resulting substrings are stored in a newly allocated NULL-terminated array.
| s | The string to split. |
| c | The delimiter character. |
ft_free_array.| char ** ft_split_charset | ( | char * | s, |
| char * | charset | ||
| ) |
#include <include/ft_string.h>
Splits a string into substrings based on a character set.
This function splits the input string s into substrings using the set of delimiter characters defined in charset. Each substring is allocated dynamically and stored in a NULL-terminated array.
| s | The string to split. |
| charset | The set of delimiter characters. |
| char * ft_strcat | ( | char * | dest, |
| const char * | src | ||
| ) |
#include <include/ft_string.h>
Appends the src string to the end of the dest string.
This function assumes that dest points to a buffer large enough to hold the resulting string, including the null terminator.
| dest | The destination string to which src will be appended. |
| src | The source string to append. |
dest).| char * ft_strchr | ( | const char * | str, |
| int | c | ||
| ) |
#include <include/ft_string.h>
Finds the first occurrence of a character in a string.
Searches for the first occurrence of the character c in the string str. The terminating null byte (\0) is considered part of the string.
| str | The null-terminated string to search in. |
| c | The character to search for, interpreted as an unsigned char. |
c in str, or NULL if c is not found. | int ft_strcmp | ( | const char * | s1, |
| const char * | s2 | ||
| ) |
#include <include/ft_string.h>
Compares two strings lexicographically.
This function compares the characters of s1 and s2 one by one. The comparison stops at the first differing character or when a null byte is reached.
| s1 | Pointer to the first null-terminated string. |
| s2 | Pointer to the second null-terminated string. |
s1 is found to be less than, equal to, or greater than s2, respectively.| char * ft_strcpy | ( | char * | dst, |
| const char * | src | ||
| ) |
#include <include/ft_string.h>
Compares two strings lexicographically.
This function compares the characters of s1 and s2 one by one. The comparison stops at the first differing character or when a null byte is reached.
| s1 | Pointer to the first null-terminated string. |
| s2 | Pointer to the second null-terminated string. |
s1 is found to be less than, equal to, or greater than s2, respectively. | char * ft_strdup | ( | const char * | src | ) |
#include <include/ft_string.h>
Allocates and duplicates a null-terminated string.
Allocates memory for a copy of the string src, copies its contents (including the null terminator), and returns a pointer to the new string.
| src | The source string to duplicate. |
| void ft_striteri | ( | char * | s, |
| void(*)(unsigned int, char *) | f | ||
| ) |
#include <include/ft_string.h>
Applies a function to each character of a string, passing its index.
Iterates over the string s and applies the function f to each character. The function receives the index of the character and a pointer to it, allowing in-place modification.
| s | The string to iterate over. |
| f | The function to apply to each character. |
s or f is NULL, the function does nothing. | char * ft_strjoin | ( | const char * | s1, |
| const char * | s2 | ||
| ) |
#include <include/ft_string.h>
Concatenates two strings into a newly allocated string.
Allocates a new string containing the contents of s1 followed by s2. If either string is NULL, it is treated as an empty string.
| s1 | The first string. |
| s2 | The second string. |
s1 and s2, or NULL if memory allocation fails.| char * ft_strjoin_and_free | ( | char * | s1, |
| char * | s2, | ||
| int | free_s1, | ||
| int | free_s2 | ||
| ) |
#include <include/ft_string.h>
| size_t ft_strlcat | ( | char * | dest, |
| const char * | src, | ||
| size_t | size | ||
| ) |
#include <include/ft_string.h>
Concatenates src to the end of dest while ensuring null-termination.
This function appends the src string to the end of the dest string. It ensures that the result fits within the buffer size given by size, including the null-terminator. If size is less than or equal to the length of dest, the function returns size + strlen(src) without appending.
| dest | The destination buffer. |
| src | The source string to append. |
| size | The total size of the destination buffer. |
strlen(dest) + strlen(src). This value can be used to detect truncation.| size_t ft_strlcpy | ( | char * | dest, |
| const char * | src, | ||
| size_t | size | ||
| ) |
#include <include/ft_string.h>
Copies a string from src to dest, up to size - 1 characters.
Copies up to size - 1 characters from the null-terminated string src to dest, null-terminating the result. If size is 0, nothing is written to dest, but the length of src is still returned.
| dest | Destination buffer. |
| src | Source null-terminated string to copy. |
| size | Total size of destination buffer. |
src. This allows detection of truncation: if the return value is >= size, truncation occurred. | size_t ft_strlen | ( | const char * | str | ) |
#include <include/ft_string.h>
| char * ft_strmapi | ( | char const * | s, |
| char(*)(unsigned int, char) | f | ||
| ) |
#include <include/ft_string.h>
Applies a function to each character of a string to create a new string.
Allocates a new string where each character is the result of applying the given function f to the corresponding character of the input string s. The function f receives the index of each character as its first argument.
| s | The original null-terminated string to map. |
| f | The function to apply to each character, receiving the index and the character. |
| int ft_strncmp | ( | const char * | s1, |
| const char * | s2, | ||
| size_t | n | ||
| ) |
#include <include/ft_string.h>
Compares two strings up to n characters.
This function compares the first n characters of the strings s1 and s2. The comparison is done using unsigned characters.
| s1 | First string to compare. |
| s2 | Second string to compare. |
| n | Maximum number of characters to compare. |
s1 is found, respectively, to be less than, to match, or be greater than s2.n characters have been compared. | char * ft_strndup | ( | const char * | src, |
| size_t | n | ||
| ) |
#include <include/ft_string.h>
Creates a copy of a string, up to n characters.
This function allocates memory and copies at most n characters from the string src to a new string. The new string is always null-terminated.
| src | The source string to duplicate. |
| n | The maximum number of characters to copy. |
| char * ft_strnstr | ( | const char * | big, |
| const char * | little, | ||
| size_t | len | ||
| ) |
#include <include/ft_string.h>
Searches for a substring in a string within a max length.
This function searches for the first occurrence of the null-terminated string little in the string big, where not more than len characters are searched. Characters after a \0 character are not searched.
If little is an empty string, big is returned. If little occurs nowhere in big within the first len characters, NULL is returned.
| big | The main string to be searched. |
| little | The substring to search for. |
| len | The maximum number of characters to search in big. |
little in big within len characters, or NULL if not found. | char * ft_strrchr | ( | const char * | str, |
| int | c | ||
| ) |
#include <include/ft_string.h>
Returns a pointer to the last occurrence of a character in a string.
Scans the string str for the last occurrence of the character c (interpreted as an unsigned char). If found, a pointer to the character is returned. If not, NULL is returned.
If c is '\0', a pointer to the null terminator at the end of the string is returned.
| str | The null-terminated string to be searched. |
| c | The character to search for (casted to unsigned char). |
c in str, or NULL if not found. | char * ft_strtrim | ( | const char * | s1, |
| const char * | set | ||
| ) |
#include <include/ft_string.h>
Trims characters from both ends of a string.
Allocates and returns a new string derived from s1 with all characters found in set removed from both the beginning and the end. The returned string is NULL-terminated and dynamically allocated.
| s1 | The original null-terminated string to trim. |
| set | The set of characters to remove. |
| char * ft_substr | ( | const char * | s, |
| unsigned int | start, | ||
| size_t | len | ||
| ) |
#include <include/ft_string.h>
Extracts a substring from a string.
Allocates and returns a new string, which is a substring of s starting from index start and up to len characters long. If start is greater than or equal to the length of s, an empty string is returned. The length of the returned substring is either len or the remaining length from start, whichever is smaller.
| s | The source string. |
| start | The starting index in the source string. |
| len | The maximum number of characters to copy. |
start is out of bounds, an empty string is returned.| int ft_tolower | ( | int | c | ) |
#include <include/ft_string.h>
Converts a character to lowercase.
If the character c is an uppercase letter (between 'A' and 'Z'), it is converted to its lowercase equivalent by adding the difference between the ASCII values of 'a' and 'A'. If c is not an uppercase letter, the function returns c unchanged.
| c | The character to convert. |
c if it is an uppercase letter; otherwise, returns c unchanged.| int ft_toupper | ( | int | c | ) |
#include <include/ft_string.h>
Converts a character to uppercase.
If the character c is a lowercase letter (between 'a' and 'z'), it is converted to its uppercase equivalent by subtracting the difference between the ASCII values of 'a' and 'A'. If c is not a lowercase letter, the function returns c unchanged.
| c | The character to convert. |
c if it is a lowercase letter; otherwise, returns c unchanged.| void skip_whitespace_index | ( | const char * | input, |
| int * | i | ||
| ) |
#include <include/ft_string.h>
Skips leading whitespace by incrementing a pointer index.
This function iterates through the input string from the position indicated by the index *i, incrementing the index until a non-whitespace character is reached. Whitespace is detected using ft_isspace.
| input | The string to search for whitespace. |
| i | Pointer to the current index, which will be updated in-place. |
| char * skip_whitespace_ptr | ( | const char * | s | ) |
#include <include/ft_string.h>
Returns a pointer to the first non-whitespace character.
Iterates through the string s and returns a pointer to the first character that is not a whitespace character. If all characters are whitespace, returns a pointer to the null-terminator.
| s | The string to scan. |