Skip to content
Iva edited this page Sep 20, 2024 · 6 revisions

Strings

MemoryUtils

ft_strlen

Mimics strlen function:
Calculates the length of a string, i.e., the number of characters before the null terminator (\0).
The null terminator is not included in the length.

Original Function:

Library <string.h>
Signature size_t strlen(const char *s);
Parameters s: Pointer to the string whose length will be counted.
Return The length of the string as a size_t value, excluding the null terminator (\0).
Manual strlen(3) - Linux Manual

Usage Example:

size_t len = strlen("Hello, World!"); // Returns 13
size_t len_empty = strlen("");        // Returns 0
size_t len_null_char = strlen("\0");  // Returns 0

Specifics:

No explicit check for NULL is made in the original function.

char *str = NULL;
size_t len = strlen(str); // Undefined behavior

No explicit check for the null terminator (\0) at the end of the buffer is made in the original function.

char buffer[] = {'H', 'e', 'l', 'l', 'o'}; // Without '\0' at the end
size_t len = strlen(buffer); // Undefined behavior

ft_strlcpy

Mimics strlcpy function:
Copies a string from src to dst, ensuring the destination buffer doesn't overflow (dstsize - 1)
and guaranteeing null-termination if dstsize is not 0.

Original Function:

Library <bsd/string.h>
Signature size_t strlcpy(char *dst, const char *src, size_t dstsize);
Parameters dst: Pointer to the destination buffer.
src: Pointer to the source string to be copied.
dstsize: Size of the destination buffer.
Return The total length of the source string (src).
Manual strlcpy(3) - Linux Manual

Usage Example:

char dest[5];
size_t len = strlcpy(dest, "Hello", sizeof(dest)); // Copies "Hell", len returns 5

Specifics:

Ensures null-termination if dstsize > 0.

char dest[5];
size_t len = strlcpy(dest, "Hello", sizeof(dest)); // Copies "Hell\0"

ft_strlcat

Mimics strlcat function:
Appends a string from src to dst, ensuring the destination buffer doesn't overflow (dstsize - 1)
and guaranteeing null-termination if dstsize is not 0.

Original Function:

Library <bsd/string.h>
Signature size_t strlcat(char *dst, const char *src, size_t dstsize);
Parameters dst: Pointer to the destination string buffer.
src: Pointer to the source string.
dstsize: Size of the destination buffer.
Return The total length of the string it tried to create.
Manual strlcat(3) - Linux Manual

Usage Example:

char dest[10] = "Hi";
size_t len = strlcat(dest, " there", sizeof(dest)); // Returns total length attempted (8)

Specifics:

Ensures null-termination if dstsize > 0 and destination space is available.

char dest[5];
size_t len = strlcat(dest, "Hello", sizeof(dest)); // Copies "Hel\0"

ft_strchr

Mimics strchr function:
Locates the first occurrence of a character c in the string s.

Original Function:

Library <string.h>
Signature char *strchr(const char *s, int c);
Parameters s: Pointer to the string to search.
c: Character to locate.
Return A pointer to the first occurrence of c, or NULL if not found.
Manual strchr(3) - Linux Manual

Usage Example:

char *ptr = strchr("Hello", 'e'); // Returns pointer to 'e'

Specifics:

If c is '\0', strchr returns a pointer to the null terminator.

char *ptr = strchr("Hello", '\0'); // Returns pointer to null terminator

ft_strrchr

Mimics strrchr function:
Locates the last occurrence of a character c in the string s.

Original Function:

Library <string.h>
Signature char *strrchr(const char *s, int c);
Parameters s: Pointer to the string to search.
c: Character to locate.
Return A pointer to the last occurrence of c, or NULL if not found.
Manual strrchr(3) - Linux Manual

Usage Example:

char *ptr = strrchr("Hello", 'l'); // Returns pointer to the second 'l'

Specifics:

If c is '\0', strrchr returns a pointer to the null terminator.

char *ptr = strrchr("Hello", '\0'); // Returns pointer to null terminator

ft_strncmp

Mimics strncmp function:
Compares up to n characters of two strings.

Original Function:

Library <string.h>
Signature int strncmp(const char *s1, const char *s2, size_t n);
Parameters s1: First string.
s2: Second string.
n: Maximum number of characters to compare.
Return Integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2.
Manual strncmp(3) - Linux Manual

Usage Example:

int result = strncmp("Hello", "World", 3); // Compares first 3 chars, returns negative

ft_strnstr

Mimics strnstr function:
Locates the first occurrence of the substring little in big, searching only the first len characters.

Original Function:

Library <string.h>
Signature strnstr(const char *big, const char *little, size_t len);
Parameters big: String to search in.
little: Substring to find.
len: Maximum number of characters to search.
Return Pointer to the beginning of the located substring, or NULL if not found.
Manual strnstr(3) - FreeBSD Manual

Usage Example:

char *result = strnstr("Hello, World!", "World", 12); // Finds "World"

ft_strdup

Mimics strdup function:
Allocates memory and duplicates the given string s.

Original Function:

Library <string.h>
Signature char *strdup(const char *s);
Parameters s: String to duplicate.
Return Pointer to the newly allocated string, or NULL if memory allocation fails.
Manual strdup(3) - Linux Manual

Usage Example:

char *copy = strdup("Hello"); // Allocates and copies "Hello" into a new string

Specifics:

strdup allocates memory, so the returned string must be freed after use.

char *copy = strdup("Hello");
free(copy); // Freeing the allocated memory

Memory

StringsUtils Top ⬆️

ft_memset

Mimics memset function:
Fills a block of memory with a specified value.

Original Function:

Library <string.h>
Signature void *memset(void *s, int c, size_t n);
Parameters s: Pointer to the memory block.
c: Value to set (converted to unsigned char).
n: Number of bytes to set.
Return Pointer to the memory block s.
Manual memset(3) - Linux Manual

Usage Example:

char buffer[10];
memset(buffer, 0, 10); // Fills buffer with zeros

ft_bzero

Mimics bzero function:
Zeroes out a block of memory.

Original Function:

Library <strings.h>
Signature void bzero(void *s, size_t n);
Parameters s: Pointer to the memory block.
n: Number of bytes to set to zero.
Return None.
Manual bzero(3) - Linux Manual

Usage Example:

char buffer[10];
bzero(buffer, 10); // Sets all bytes in buffer to zero

Specifics:

bzero is considered deprecated in favor of memset, which achieves the same result.

Copiar código
bzero(buffer, 10);  // Equivalent to memset(buffer, 0, 10);

ft_memcpy

Mimics memcpy function:
Copies n bytes from source to destination.

Original Function:

Library <string.h>
Signature void *memcpy(void *dest, const void *src, size_t n);
Parameters dest: Destination memory block.
src: Source memory block.
n: Number of bytes to copy.
Return Pointer to the destination memory block dest.
Manual memcpy(3) - Linux Manual

Usage Example:

char src[10] = "Hello";
char dest[10];
memcpy(dest, src, 5); // Copies "Hello" to dest

Specifics:

memcpy has undefined behavior if source and destination overlap. Use memmove instead for overlapping memory.

memcpy(src + 1, src, 5); // Undefined behavior

ft_memmove

Mimics memmove function:
Safely copies n bytes from source to destination, even if memory regions overlap.

Original Function:

Library <string.h>
Signature void *memmove(void *dest, const void *src, size_t n);
Parameters dest: Destination memory block.
src: Source memory block.
n: Number of bytes to copy.
Return Pointer to the destination memory block dest.
Manual memmove(3) - Linux Manual

Usage Example:

char src[10] = "Hello";
memmove(src + 1, src, 5); // Safely copies "Hello" to "HHello"

ft_memchr

Mimics memchr function:
Searches for the first occurrence of a byte in a memory block.

Original Function:

Library <string.h>
Signature void *memchr(const void *s, int c, size_t n);
Parameters s: Pointer to the memory block.
c: Byte to search for (converted to unsigned char).
n: Number of bytes to search.
Return Pointer to the first occurrence of the byte c, or NULL if not found.
Manual memchr(3) - Linux Manual

Usage Example:

char data[] = "Hello, World!";
char *ptr = memchr(data, 'W', sizeof(data)); // Returns pointer to 'W'

ft_memcmp

Mimics memcmp function:
Compares the first n bytes of two memory blocks.

Original Function:

Library <string.h>
Signature int memcmp(const void *s1, const void *s2, size_t n);
Parameters s1: First memory block.
s2: Second memory block.
n: Number of bytes to compare.
Return Integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2.
Manual memcmp(3) - Linux Manual

Usage Example:

int result = memcmp("ABC", "ABD", 3); // Returns a negative value

ft_calloc

Mimics calloc function:
Allocates memory for an array of nmemb elements of size bytes each and initializes the allocated memory to zero.

Original Function:

Library <stdlib.h>
Signature void *calloc(size_t nmemb, size_t size);
Parameters nmemb: Number of elements to allocate.
size: Size of each element.
Return Pointer to the allocated memory, or NULL if the allocation fails.
Manual calloc(3) - Linux Manual

Usage Example:

int *arr = calloc(5, sizeof(int)); // Allocates and zeroes memory for an array of 5 integers

Specifics:

If nmemb or size is 0, calloc returns a unique pointer that can be passed to free().

int *arr = calloc(0, sizeof(int)); // Returns a unique pointer, safe to pass to free()

If the multiplication of nmemb and size results in an integer overflow, calloc returns NULL.

size_t nmemb = SIZE_MAX;
size_t size = 2;
void *ptr = calloc(nmemb, size); // Returns NULL due to integer overflow

Utils

StringsMemory Top ⬆️

ft_isalpha

Mimics isalpha function:
Checks whether the passed character is an alphabetic letter.

Original Function:

Library <ctype.h>
Signature int isalpha(int c);
Parameters c: Character to be checked.
Return Non-zero if the character is an alphabetic letter, zero otherwise.
Manual isalpha(3) - Linux Manual

Usage Example:

int result = isalpha('A'); // Returns non-zero
int result = isalpha('1'); // Returns 0

ft_isdigit

Mimics isdigit function:
Checks whether the passed character is a decimal digit (0-9).

Original Function:

Library <ctype.h>
Signature int isdigit(int c);
Parameters c: Character to be checked.
Return Non-zero if the character is a decimal digit (0-9), zero otherwise.
Manual isdigit(3) - Linux Manual

Usage Example:

int result = isdigit('5'); // Returns non-zero
int result = isdigit('a'); // Returns 0

ft_isalnum

Mimics isalnum function:
Checks whether the passed character is alphanumeric (either a digit or a letter).

Original Function:

Library <ctype.h>
Signature int isalnum(int c);
Parameters c: Character to be checked.
Return Non-zero if the character is alphanumeric, zero otherwise.
Manual isalnum(3) - Linux Manual

Usage Example:

int result = isalnum('A'); // Returns non-zero
int result = isalnum('1'); // Returns non-zero
int result = isalnum('$'); // Returns 0

ft_isascii

Mimics isascii function:
Checks whether the passed character is an ASCII character (0-127).

Original Function:

Library <ctype.h>
Signature int isascii(int c);
Parameters c: Character to be checked.
Return Non-zero if the character is an ASCII character, zero otherwise.
Manual isascii(3) - Linux Manual

Usage Example:

int result = isascii(65);  // Returns non-zero for 'A'
int result = isascii(200); // Returns 0

ft_isprint

Mimics isprint function:
Checks whether the passed character is printable, including space.

Original Function:

Library <ctype.h>
Signature int isprint(int c);
Parameters c: Character to be checked.
Return Non-zero if the character is printable, zero otherwise.
Manual isprint(3) - Linux Manual

Usage Example:

int result = isprint(65);  // Returns non-zero for 'A'
int result = isprint(127); // Returns 0 (non-printable)

ft_toupper

Mimics toupper function:
Converts a lowercase alphabetic character to uppercase.

Original Function:

Library <ctype.h>
Signature int toupper(int c);
Parameters c: Character to be converted.
Return Uppercase equivalent if c is lowercase; otherwise returns c.
Manual toupper(3) - Linux Manual

Usage Example:

int upper = toupper('a'); // Returns 'A'
int unchanged = toupper('A'); // Returns 'A'

ft_tolower

Mimics tolower function:
Converts an uppercase alphabetic character to lowercase.

Original Function:

Library <ctype.h>
Signature int tolower(int c);
Parameters c: Character to be converted.
Return Lowercase equivalent if c is uppercase; otherwise returns c.
Manual tolower(3) - Linux Manual

Usage Example:

int lower = tolower('A'); // Returns 'a'
int unchanged = tolower('a'); // Returns 'a'

ft_atoi

Mimics atoi function:
Converts a string to an integer, ignoring leading whitespace and stopping conversion at the first non-numeric character.

Original Function:

Library <stdlib.h>
Signature int atoi(const char *str);
Parameters str: The string to convert.
Return The integer value of the string, or 0 if no valid conversion is performed.
Manual atoi(3) - Linux Manual

Usage Example:

int number = atoi("42");      // Returns 42
int number = atoi("   -42");  // Returns -42
int number = atoi("42abc");   // Returns 42

Specifics:

atoi does not handle overflow or underflow. For large inputs, it may result in undefined behavior.

int number = atoi("999999999999999999999999"); // Undefined behavior, may overflow

Previous ⬅️ Top ⬆️ Next ➡️