From 3a9c6cc403443a33951f428e4fa6567f4c049e41 Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Mon, 20 Apr 2015 23:45:32 +0200 Subject: [PATCH] Introduce string_new_replace(3) --- include/System/string.h | 2 ++ src/string.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/include/System/string.h b/include/System/string.h index 3d45bca..5717566 100644 --- a/include/System/string.h +++ b/include/System/string.h @@ -36,6 +36,8 @@ String * string_new(String const * string); String * string_new_append(String const * string, ...); String * string_new_format(String const * format, ...); String * string_new_length(String const * string, size_t length); +String * string_new_replace(String const * string, String const * what, + String const * by); void string_delete(String * string); /* accessors */ diff --git a/src/string.c b/src/string.c index 52e5d27..9db8b07 100644 --- a/src/string.c +++ b/src/string.c @@ -114,6 +114,23 @@ String * string_new_length(String const * string, size_t length) } +/* string_new_replace */ +String * string_new_replace(String const * string, String const * what, + String const * by) +{ + String * ret; + + if((ret = string_new(string)) == NULL) + return NULL; + if(string_replace(&ret, what, by) != 0) + { + string_delete(ret); + return NULL; + } + return ret; +} + + /* string_delete */ void string_delete(String * string) {