mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
Expansion : text replacement in a string
This commit is contained in:
parent
d30a39d907
commit
ed1d8b18fe
3 changed files with 176 additions and 0 deletions
28
interesting_tests.md
Normal file
28
interesting_tests.md
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
jguelen@c2r12p1 ~()% bash
|
||||||
|
jguelen@c2r12p1:~$ export test=flagada$PATH
|
||||||
|
jguelen@c2r12p1:~$ echo $test
|
||||||
|
flagada/home/jguelen/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin
|
||||||
|
jguelen@c2r12p1:~$ export test="flagada"$PATH
|
||||||
|
jguelen@c2r12p1:~$ echo $test
|
||||||
|
flagada/home/jguelen/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin
|
||||||
|
jguelen@c2r12p1:~$ export test=flagada"$PATHhihi"
|
||||||
|
jguelen@c2r12p1:~$ echo $test
|
||||||
|
flagada
|
||||||
|
jguelen@c2r12p1:~$ export test=flagada"$PATH hihi"
|
||||||
|
jguelen@c2r12p1:~$ echo $test
|
||||||
|
flagada/home/jguelen/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin hihi
|
||||||
|
jguelen@c2r12p1:~$ export test=flagada"$PATH"hihi
|
||||||
|
jguelen@c2r12p1:~$ echo $test
|
||||||
|
flagada/home/jguelen/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/binhihi
|
||||||
|
jguelen@c2r12p1:~$ export test=flagada"$'PA'TH"hihi
|
||||||
|
jguelen@c2r12p1:~$ echo $test
|
||||||
|
flagada$'PA'THhihi
|
||||||
|
jguelen@c2r12p1:~$ export test=flagada$'P'ATH
|
||||||
|
jguelen@c2r12p1:~$ echo $test
|
||||||
|
flagadaPATH
|
||||||
|
jguelen@c2r12p1:~$ export test=flagada$P'A'TH hihi
|
||||||
|
jguelen@c2r12p1:~$ echo $test
|
||||||
|
flagadaATH
|
||||||
|
jguelen@c2r12p1:~/Common_Core/minishell$ echo "tests$"PATH""
|
||||||
|
tests$PATH
|
||||||
|
jguelen@c2r12p1:~$
|
||||||
131
src/subst/replace_substr.c
Normal file
131
src/subst/replace_substr.c
Normal file
|
|
@ -0,0 +1,131 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* replace_substr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/02/25 13:02:59 by jguelen #+# #+# */
|
||||||
|
/* Updated: 2025/02/25 18:50:00 by jguelen ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "replace_substr.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
** @RETURN Returns a new C-compliant malloc-allocated string corresponding to
|
||||||
|
** text where the substring starting in text at index index_start and ending
|
||||||
|
** at index_end is replaced in totality by the C-compliant string replacement
|
||||||
|
** excluding its NULL-byte.
|
||||||
|
** Returns NULL if an allocation error occurred.
|
||||||
|
*/
|
||||||
|
char *replace_in_str(const char *text, size_t index_start, size_t index_end,
|
||||||
|
char *replacement)
|
||||||
|
{
|
||||||
|
char *new;
|
||||||
|
size_t len_text;
|
||||||
|
size_t len_rep;
|
||||||
|
size_t new_size;
|
||||||
|
|
||||||
|
len_text = ft_strlen(text);
|
||||||
|
len_rep = ft_strlen(replacement);
|
||||||
|
new_size = len_text + index_start - index_end + len_rep;
|
||||||
|
if (index_end >= index_start)
|
||||||
|
{
|
||||||
|
new = malloc(new_size * sizeof(char));
|
||||||
|
if (!new)
|
||||||
|
return (NULL);
|
||||||
|
ft_memmove(new, text, index_start);
|
||||||
|
ft_memmove(new + index_start, replacement, len_rep);
|
||||||
|
ft_memmove(new + index_start + len_rep, text + index_end + 1,
|
||||||
|
len_text - index_end - 1);
|
||||||
|
new[new_size - 1] = '\0';
|
||||||
|
return (new);
|
||||||
|
}
|
||||||
|
return (ft_strdup(text));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** word is presumed not to be NULL
|
||||||
|
*/
|
||||||
|
int *better_prefix_array(char *word, size_t word_len)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
int *better_prefix;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
better_prefix = ft_calloc(word_len + 1, sizeof(int));
|
||||||
|
if (!better_prefix)
|
||||||
|
return (NULL);
|
||||||
|
better_prefix[0] = -1;
|
||||||
|
j = 0;
|
||||||
|
while (++j < m)
|
||||||
|
{
|
||||||
|
if (word[i] == word[j])
|
||||||
|
better_prefix[j] = better_prefix[i];
|
||||||
|
else
|
||||||
|
{
|
||||||
|
better_prefix[j] = i;
|
||||||
|
i = better_prefix[i];
|
||||||
|
while (i >= 0 && word[j] != word[i])
|
||||||
|
i = better_prefix[i];
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
better_prefix[word_len] = i;
|
||||||
|
return (better_prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Return the index at which the word needle begins in haystack if found,
|
||||||
|
** -1 if needle is not to be found in haystack.
|
||||||
|
*/
|
||||||
|
ssize_t find_word_index(const char *haystack, char *needle)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
size_t len_n;
|
||||||
|
int *better_prefix;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
len_n = ft_strlen(needle);
|
||||||
|
better_prefix = better_prefix_array(needle, len_n);
|
||||||
|
while (haystack[j])
|
||||||
|
{
|
||||||
|
while (i >= 0 && needle[i] != haystack[j])
|
||||||
|
i = better_prefix[i];
|
||||||
|
i++;
|
||||||
|
if (i == len_n)
|
||||||
|
return (free(better_prefix), j);
|
||||||
|
i = better_prefix[i];
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
free(better_prefix);
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** @RETURN Returns a new C-compliant malloc-allocated string corresponding to
|
||||||
|
** the C-compliant string text where the first occurrence of the C-compliant
|
||||||
|
** string to_replace (NULL-byte excluded) found in text is replaced by the
|
||||||
|
** C-compliant string replacement.
|
||||||
|
** Returns NULL if an allocation error occurred.
|
||||||
|
*/
|
||||||
|
char *replace_substr(const char *text, char *to_replace, char *replacement)
|
||||||
|
{
|
||||||
|
char *new;
|
||||||
|
size_t start_index;
|
||||||
|
size_t len_torep;
|
||||||
|
|
||||||
|
len_torep = ft_strlen(to_replace);
|
||||||
|
if (len_torep)
|
||||||
|
{
|
||||||
|
start_index = find_word_index(text, to_replace);
|
||||||
|
if (start_index != -1)
|
||||||
|
return (replace_in_str(text, index_start,
|
||||||
|
(size_t)start_index + len_torep - 1, replacement));
|
||||||
|
}
|
||||||
|
return (ft_strdup(text));
|
||||||
|
}
|
||||||
17
src/subst/replace_substr.h
Normal file
17
src/subst/replace_substr.h
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* replace_substr.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/02/25 12:48:39 by jguelen #+# #+# */
|
||||||
|
/* Updated: 2025/02/25 13:03:51 by jguelen ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char *replace_in_str(char *text, size_t index_start, size_t index_end,
|
||||||
|
char *replacement);
|
||||||
|
char *replace_substr(char *text, char *to_replace, char *replacement);
|
||||||
Loading…
Add table
Add a link
Reference in a new issue