diff options
Diffstat (limited to 'lib/frrstr.c')
| -rw-r--r-- | lib/frrstr.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/frrstr.c b/lib/frrstr.c index fd337073f8..fbbc890ec6 100644 --- a/lib/frrstr.c +++ b/lib/frrstr.c @@ -152,6 +152,32 @@ void frrstr_strvec_free(vector v) vector_free(v); } +char *frrstr_replace(const char *str, const char *find, const char *replace) +{ + char *ch; + char *nustr = XSTRDUP(MTYPE_TMP, str); + + size_t findlen = strlen(find); + size_t repllen = strlen(replace); + + while ((ch = strstr(nustr, find))) { + if (repllen > findlen) { + size_t nusz = strlen(nustr) + repllen - findlen + 1; + nustr = XREALLOC(MTYPE_TMP, nustr, nusz); + ch = strstr(nustr, find); + } + + size_t nustrlen = strlen(nustr); + size_t taillen = (nustr + nustrlen) - (ch + findlen); + + memmove(ch + findlen + (repllen - findlen), ch + findlen, + taillen + 1); + memcpy(ch, replace, repllen); + } + + return nustr; +} + bool begins_with(const char *str, const char *prefix) { if (!str || !prefix) |
