libft
Loading...
Searching...
No Matches
Functions
ft_strtrim.c File Reference

Trims characters from the beginning and end of a string. More...

#include "libft.h"
+ Include dependency graph for ft_strtrim.c:

Functions

static int ft_is_in_set (char c, const char *set)
 Checks if a character is present in a given set.
 
char * ft_strtrim (const char *s1, const char *set)
 Trims characters from both ends of a string.
 
static size_t ft_trim_end (const char *s1, const char *set)
 Finds the ending index before trimming trailing characters.
 
static size_t ft_trim_start (const char *s1, const char *set)
 Finds the starting index after trimming leading characters.
 

Detailed Description

Trims characters from the beginning and end of a string.

Author
Toonsa
Date
2025/04/05

This file implements the ft_strtrim function, which removes all leading and trailing characters from a string based on a given set. It also defines helper functions to locate trim boundaries efficiently.

Function Documentation

◆ ft_is_in_set()

static int ft_is_in_set ( char  c,
const char *  set 
)
static

Checks if a character is present in a given set.

Iterates through the set and returns 1 if character c is found, 0 otherwise.

Parameters
cThe character to check.
setThe null-terminated string containing the set of characters.
Returns
1 if c is in set, 0 otherwise.

◆ ft_trim_end()

static size_t ft_trim_end ( const char *  s1,
const char *  set 
)
static

Finds the ending index before trimming trailing characters.

Scans backward from the end of s1 and returns the index of the last character not in set + 1.

Parameters
s1The string to trim.
setThe set of characters to remove.
Returns
Index of the first trailing character (exclusive).

◆ ft_trim_start()

static size_t ft_trim_start ( const char *  s1,
const char *  set 
)
static

Finds the starting index after trimming leading characters.

Scans from the beginning of s1 and returns the index of the first character not included in set.

Parameters
s1The string to trim.
setThe set of characters to remove.
Returns
Index of the first non-trimmed character.