Libft
Custom implementation of core libc functions with additional utility helpers.
Loading...
Searching...
No Matches
String Functions

String manipulation utilities. More...

Functions

char ** str_split (char const *s, char c)
 Splits a string into an array of strings using a delimiter.
char * str_chr (const char *s, int c)
 Locates the first occurrence of a character in a string.
char * str_dup (const char *s1)
 Duplicates a string.
void str_iteri (char *s, void(*f)(unsigned int, char *))
 Applies a function to each character of a string with its index.
char * str_join (char const *s1, char const *s2)
 Concatenates two strings into a new string.
size_t str_lcat (char *dst, const char *src, size_t dstsize)
 Appends src to dst with size limit.
size_t str_lcpy (char *dst, const char *src, size_t dstsize)
 Copies src to dst with size limit.
size_t str_len (const char *s)
 Calculates the length of a string.
char * str_mapi (char const *s, char(*f)(unsigned int, char))
 Creates a new string by applying a function to each character.
int str_ncmp (const char *s1, const char *s2, size_t n)
 Compares at most n characters of two strings.
char * str_nstr (const char *haystack, const char *needle, size_t len)
 Locates a substring within a string, limited by length.
char * str_rchr (const char *s, int c)
 Locates the last occurrence of a character in a string.
char * str_trim (char const *s1, char const *set)
 Trims characters from the beginning and end of a string.
char * str_sub (char const *s, unsigned int start, size_t len)
 Extracts a substring from a string.

Detailed Description

String manipulation utilities.

Functions to search, copy, compare and transform strings.

Function Documentation

◆ str_chr()

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

Locates the first occurrence of a character in a string.

Note
Returned pointer is borrowed from s. Do not free it directly.
If c is '\0', returns pointer to the terminating null.
Parameters
sString to search (borrowed).
cCharacter to find.
Returns
Pointer to the character (borrowed), or NULL if not found.
Here is the caller graph for this function:

◆ str_dup()

char * str_dup ( const char * s1)

Duplicates a string.

Note
Caller owns the returned string and must free it.
Parameters
s1String to duplicate (borrowed).
Returns
Newly allocated copy (owned), or NULL on failure.
Here is the call graph for this function:

◆ str_iteri()

void str_iteri ( char * s,
void(* )(unsigned int, char *) )

Applies a function to each character of a string with its index.

Parameters
sString to iterate (modified in place).
fFunction taking index and character pointer.

◆ str_join()

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

Concatenates two strings into a new string.

Note
Caller owns the returned string and must free it.
Parameters
s1First string (borrowed).
s2Second string (borrowed).
Returns
Newly allocated concatenated string (owned), or NULL on failure.
Here is the call graph for this function:

◆ str_lcat()

size_t str_lcat ( char * dst,
const char * src,
size_t dstsize )

Appends src to dst with size limit.

Parameters
dstDestination buffer (must be null-terminated).
srcSource string.
dstsizeTotal size of destination buffer.
Returns
Total length of string it tried to create.
Here is the call graph for this function:

◆ str_lcpy()

size_t str_lcpy ( char * dst,
const char * src,
size_t dstsize )

Copies src to dst with size limit.

Parameters
dstDestination buffer.
srcSource string.
dstsizeSize of destination buffer.
Returns
Length of src.
Here is the call graph for this function:

◆ str_len()

size_t str_len ( const char * s)

Calculates the length of a string.

Parameters
sString to measure.
Returns
Length of the string (not including null terminator).
Here is the caller graph for this function:

◆ str_mapi()

char * str_mapi ( char const * s,
char(* )(unsigned int, char) )

Creates a new string by applying a function to each character.

Note
Caller owns the returned string and must free it.
Parameters
sString to transform (borrowed).
fFunction taking index and character, returning new character.
Returns
Newly allocated transformed string (owned), or NULL on failure.
Here is the call graph for this function:

◆ str_ncmp()

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

Compares at most n characters of two strings.

Parameters
s1First string.
s2Second string.
nMaximum number of characters to compare.
Returns
Difference of first differing characters, or 0 if equal.

◆ str_nstr()

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

Locates a substring within a string, limited by length.

Note
Returned pointer is borrowed from haystack. Do not free it directly.
Parameters
haystackString to search in (borrowed).
needleSubstring to find (borrowed).
lenMaximum characters to search.
Returns
Pointer to start of substring (borrowed), or NULL if not found.

◆ str_rchr()

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

Locates the last occurrence of a character in a string.

Note
Returned pointer is borrowed from s. Do not free it directly.
If c is '\0', returns pointer to the terminating null.
Parameters
sString to search (borrowed).
cCharacter to find.
Returns
Pointer to the character (borrowed), or NULL if not found.

◆ str_split()

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

Splits a string into an array of strings using a delimiter.

Note
Caller owns the returned array and each string within it. Free each string then free the array itself.
Parameters
sString to split (borrowed).
cDelimiter character.
Returns
NULL-terminated array of strings (owned), or NULL on failure.
Here is the call graph for this function:

◆ str_sub()

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

Extracts a substring from a string.

Note
Caller owns the returned string and must free it.
Parameters
sSource string (borrowed).
startStarting index.
lenMaximum length of substring.
Returns
Newly allocated substring (owned), or NULL on failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ str_trim()

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

Trims characters from the beginning and end of a string.

Note
Caller owns the returned string and must free it.
Parameters
s1String to trim (borrowed).
setCharacters to trim (borrowed).
Returns
Newly allocated trimmed string (owned), or NULL on failure.
Here is the call graph for this function: