mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-05 23:18:08 +01:00
libft: add functions to get the first character matching or not matching a predicate
This commit is contained in:
parent
8defbf4d13
commit
ac10c3b4de
4 changed files with 67 additions and 1 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/17 16:06:09 by jguelen #+# #+# */
|
||||
/* Updated: 2025/02/20 14:48:28 by khais ### ########.fr */
|
||||
/* Updated: 2025/02/20 14:59:22 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,6 +14,7 @@
|
|||
# define LIBFT_H
|
||||
# include <unistd.h>
|
||||
# include <ctype.h>
|
||||
# include <stdbool.h>
|
||||
# include <string.h>
|
||||
# include <strings.h>
|
||||
# include <stdlib.h>
|
||||
|
|
@ -132,6 +133,8 @@ int ft_tolower(int c);
|
|||
char *ft_strchr(const char *s, int c);
|
||||
size_t ft_strchridx(const char *s, char c);
|
||||
size_t ft_strnchridx(const char *s, char c);
|
||||
size_t ft_strfchridx(const char *s, bool f(char));
|
||||
size_t ft_strnfchridx(const char *s, bool f(char));
|
||||
char *ft_strrchr(const char *s, int c);
|
||||
int ft_strcmp(const char *s1, const char *s2);
|
||||
int ft_strncmp(const char *s1, const char *s2, size_t n);
|
||||
|
|
|
|||
|
|
@ -48,6 +48,7 @@ SOURCES = \
|
|||
ft_strmapi.c \
|
||||
ft_strnchridx.c \
|
||||
ft_strncmp.c \
|
||||
ft_strnfchridx.c \
|
||||
ft_strnstr.c \
|
||||
ft_strrchr.c \
|
||||
ft_strtrim.c \
|
||||
|
|
|
|||
30
libft/libft/ft_strfchridx.c
Normal file
30
libft/libft/ft_strfchridx.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strfchridx.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/14 15:10:03 by khais #+# #+# */
|
||||
/* Updated: 2025/02/14 15:16:39 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/*
|
||||
** return the index of the first character c in the string s where f(c) returns
|
||||
** true, or 0 if s is null, or length of s if c is not found.
|
||||
*/
|
||||
size_t ft_strfchridx(const char *s, bool f(char))
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (s == NULL)
|
||||
return (0);
|
||||
i = 0;
|
||||
while (f(s[i]) == false && s[i] != '\0')
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
32
libft/libft/ft_strnfchridx.c
Normal file
32
libft/libft/ft_strnfchridx.c
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strnfchridx.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/14 15:16:49 by khais #+# #+# */
|
||||
/* Updated: 2025/02/14 15:17:34 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/*
|
||||
** return the index of the first character c in the string s where f(c) returns
|
||||
** false, or 0 if s is null or c is not found.
|
||||
*/
|
||||
size_t ft_strnfchridx(const char *s, bool f(char))
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (s == NULL)
|
||||
return (0);
|
||||
i = 0;
|
||||
while (f(s[i]) == true && s[i] != '\0')
|
||||
i++;
|
||||
if (s[i] == '\0')
|
||||
return (0);
|
||||
return (i);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue