env_manip: add function to check if a str is a valid variable identifier

This commit is contained in:
Jérôme Guélen 2025-02-17 16:52:17 +01:00 committed by Khaïs COLIN
parent 532f9dc526
commit df8aec148f
2 changed files with 29 additions and 4 deletions

View file

@ -6,7 +6,7 @@
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/14 13:46:39 by jguelen #+# #+# */
/* Updated: 2025/02/18 16:47:34 by khais ### ########.fr */
/* Updated: 2025/02/19 13:07:32 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
# define ENV_MANIP_H
# include <stdlib.h>
# include <stdbool.h>
# include "libft.h"
typedef struct s_env
@ -39,5 +40,6 @@ void env_destroy_node(t_env *node);
void env_destroy(t_env *env);
void envp_destroy(char **envp);
t_env *env_find_node_bykey(t_env *env, char *key);
bool identifier_isvalid(char *id);
#endif

View file

@ -3,15 +3,16 @@
/* ::: :::::::: */
/* env_manip_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/14 18:31:38 by jguelen #+# #+# */
/* Updated: 2025/02/18 16:40:32 by khais ### ########.fr */
/* Created: 2025/02/19 13:16/49 by khais #+# #+# */
/* Updated: 2025/02/19 13:16:49 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "env_manip.h"
#include "libft.h"
#include <stdbool.h>
/*
** Free all memory related to a given envp structure
@ -79,3 +80,25 @@ t_env *env_find_node_bykey(t_env *env, char *key)
}
return (NULL);
}
/*
** Return true if id is a valid bash identifier, false otherwise.
**
** A valid identifier starts with one of a-zA-Z_
** It can contain any char a-zA-Z0-9_
** It must contain at least one char
*/
bool identifier_isvalid(char *id)
{
if (id == NULL || id[0] == '\0')
return (false);
if (!ft_isalpha(id[0]) && id[0] != '_')
return (false);
while (*id != '\0')
{
if (!ft_isalpha(*id) && !ft_isdigit(*id) && *id != '_')
return (false);
id++;
}
return (true);
}