From: Austin Ray Date: Sat, 18 Aug 2018 22:14:04 +0000 (-0400) Subject: Implement substring removal function X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5ee2d0c363636997c75922bc995029b120c36e72;p=neomutt Implement substring removal function Implemented a method that removes all instances of a substring from a given string. The method has protections against trying to remove a non-existent substring. --- diff --git a/mutt/string.c b/mutt/string.c index 046483b34..919d2a1c2 100644 --- a/mutt/string.c +++ b/mutt/string.c @@ -1017,6 +1017,28 @@ bool mutt_str_inline_replace(char *buf, size_t buflen, size_t xlen, const char * return true; } +/** + * mutt_str_remall_strcasestr - Remove all occurrences of substring, ignoring case + * @param str String containing the substring + * @param target Target substring for removal + * @retval 0 String contained substring and substring was removed successfully + * @retval 1 String did not contain substring + */ +int mutt_str_remall_strcasestr(char *str, const char *target) +{ + int retval = 1; + + // Look through an ensure all instances of the substring are gone. + while ((str = (char *) mutt_str_strcasestr(str, target))) + { + size_t target_len = mutt_str_strlen(target); + memmove(str, str + target_len, 1 + strlen(str + target_len)); + retval = 0; // If we got here, then a substring existed and has been removed. + } + + return retval; +} + /** * mutt_str_strcasestr - Find a substring within a string without worrying about case * @param haystack String that may or may not contain the substring diff --git a/mutt/string2.h b/mutt/string2.h index 557c7673f..7e4501f68 100644 --- a/mutt/string2.h +++ b/mutt/string2.h @@ -79,6 +79,7 @@ size_t mutt_str_lws_len(const char *s, size_t n); size_t mutt_str_lws_rlen(const char *s, size_t n); const char *mutt_str_next_word(const char *s); void mutt_str_pretty_size(char *buf, size_t buflen, size_t num); +int mutt_str_remall_strcasestr(char *str, const char *target); void mutt_str_remove_trailing_ws(char *s); void mutt_str_replace(char **p, const char *s); const char *mutt_str_rstrnstr(const char *haystack, size_t haystack_length, const char *needle);