diff options
| author | Quentin Young <qlyoung@cumulusnetworks.com> | 2019-05-06 22:38:10 +0000 |
|---|---|---|
| committer | Quentin Young <qlyoung@cumulusnetworks.com> | 2019-05-06 22:38:10 +0000 |
| commit | ed1809c9258b65b40bb3a498ea3b142f7025464f (patch) | |
| tree | 378767d9396ca744b51afa40dd27d55fd555a881 /lib/frrstr.c | |
| parent | 9a968a1d40bfe5980b082c70675957db27ac2f30 (diff) | |
lib: add string replace function
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
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) |
