From be9b1404e66157ac00bf3ae488ad2af1becffe25 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Tue, 18 Nov 2025 15:54:47 +0100 Subject: common: New function replace_substr. * common/stringhelp.c (replace_substr): New. * common/t-stringhelp.c (test_replace_substr): New test. --- common/t-stringhelp.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) (limited to 'common/t-stringhelp.c') diff --git a/common/t-stringhelp.c b/common/t-stringhelp.c index b43bb7932..3bd9ba928 100644 --- a/common/t-stringhelp.c +++ b/common/t-stringhelp.c @@ -1201,6 +1201,117 @@ test_compare_version_strings (void) } +static void +test_replace_substr (void) +{ + struct { + const char *string; + const char *substr; + const char *replace; + const char *result; + } t[] = { + { "Look afar and see the end from the beginning.", + "see ", + "view ", + "Look afar and view the end from the beginning." + }, + { "Look afar and see the end from the beginning.", + "see ", + "xxx ", + "Look afar and xxx the end from the beginning." + }, + { "Look afar and see the end from the beginning.", + "see ", + "xx ", + "Look afar and xx the end from the beginning." + }, + { "Look afar and see the end from the beginning.", + "see ", + "x ", + "Look afar and x the end from the beginning." + }, + { "Look afar and see the end from the beginning.", + "see ", + " ", + "Look afar and the end from the beginning." + }, + { "Look afar and see the end from the beginning.", + "see ", + "", + "Look afar and the end from the beginning." + }, + { "Be different: conform.", + "", + "xxx", + "xxxBe different: conform." + }, + { "Be different: conform.", + "foo", + "bar", + "Be different: conform." + }, + { "Be different: conform.", + "different", + "unlike", + "Be unlike: conform." + }, + { "Be different: conform.", + "B", + "Bee", + "Beee different: conform." + }, + { "Be different: conform.", + ".", + "", + "Be different: conform" + }, + { "Be different: conform.", + ".", + "!", + "Be different: conform!" + }, + { "Be different: conform.", + ".", + "...", + "Be different: conform..." + }, + { "Be different: conform.", + ":", + " - this is a very long replacement string - ", + "Be different - this is a very long replacement string - conform." + }, + { "", + "", + "", + "" + } + }; + int idx; + char *res; + + for (idx=0; idx < DIM(t); idx++) + { + res = replace_substr (t[idx].string, t[idx].substr, t[idx].replace); + if (!res) + { + fprintf (stderr,"error replacing in '%s' (test %d): %s\n", + t[idx].string, idx, strerror (errno)); + exit (2); + } + if (strcmp (res, t[idx].result)) + { + fprintf (stderr, "string is '%s'\n", t[idx].string); + fprintf (stderr, " substr '%s'\n", t[idx].substr); + fprintf (stderr, " replace '%s'\n", t[idx].replace); + fprintf (stderr, " expected '%s'\n", t[idx].result); + fprintf (stderr, " got '%s'\n", res); + fail (idx); + } + xfree (res); + } +} + + static void test_substitute_envvars (void) { @@ -1317,6 +1428,7 @@ main (int argc, char **argv) test_split_fields_colon (); test_compare_version_strings (); test_format_text (); + test_replace_substr (); test_substitute_envvars (); xfree (home_buffer); -- cgit