diff --git a/interesting_tests.md b/interesting_tests.md new file mode 100644 index 0000000..22358b9 --- /dev/null +++ b/interesting_tests.md @@ -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:~$ diff --git a/src/subst/replace_substr.c b/src/subst/replace_substr.c new file mode 100644 index 0000000..dc8c7c4 --- /dev/null +++ b/src/subst/replace_substr.c @@ -0,0 +1,131 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* replace_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jguelen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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)); +} diff --git a/src/subst/replace_substr.h b/src/subst/replace_substr.h new file mode 100644 index 0000000..ee704e7 --- /dev/null +++ b/src/subst/replace_substr.h @@ -0,0 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* replace_substr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jguelen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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);