Add lib/strutil/replace:str_replace_all() function.
[midnight-commander.git] / lib / strutil / replace.c
blob6d1c5669164b052ae086046a04a78a0aaa02cafb
1 /*
2 Functions for replacing substrings in strings.
4 Copyright (C) 2013
5 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2013;
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <config.h>
28 #include "lib/strutil.h"
29 #include "lib/strescape.h"
31 /*** global variables ****************************************************************************/
33 /*** file scope macro definitions ****************************************************************/
35 /*** file scope type declarations ****************************************************************/
37 /*** file scope variables ************************************************************************/
39 /* --------------------------------------------------------------------------------------------- */
40 /*** file scope functions ************************************************************************/
41 /* --------------------------------------------------------------------------------------------- */
43 static char *
44 str_ptr_array_join (GPtrArray * str_splints)
46 GString *return_str;
47 guint i;
49 return_str = g_string_sized_new (32);
50 for (i = 0; i < str_splints->len; i++)
51 g_string_append (return_str, g_ptr_array_index (str_splints, i));
53 return g_string_free (return_str, FALSE);
56 /* --------------------------------------------------------------------------------------------- */
57 /*** public functions ****************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
59 /**
60 * Replace all substrings 'needle' in string 'haystack' by 'replacement'.
61 * If the 'needle' in the 'haystack' will be escaped by backslash,
62 * then this occurence isn't be replaced.
64 * @param haystack string contains substrings for replacement
65 * @param needle string for search
66 * @param replacement string for replace
67 * @return newly allocated string with replaced substrings
70 char *
71 str_replace_all (const char *haystack, const char *needle, const char *replacement)
73 size_t needle_len;
74 GPtrArray *str_splints;
75 char *return_str;
77 needle_len = strlen (needle);
79 str_splints = g_ptr_array_new ();
81 while (TRUE)
83 char *needle_in_str;
85 needle_in_str = strstr (haystack, needle);
86 if (needle_in_str == NULL)
88 if (*haystack != '\0')
89 g_ptr_array_add (str_splints, g_strdup (haystack));
90 break;
93 if (strutils_is_char_escaped (haystack, needle_in_str))
95 char *backslash = needle_in_str - 1;
97 if (haystack != backslash)
98 g_ptr_array_add (str_splints, g_strndup (haystack, backslash - haystack));
100 g_ptr_array_add (str_splints, g_strndup (backslash + 1, needle_in_str - backslash));
101 haystack = needle_in_str + 1;
102 continue;
104 if (needle_in_str - haystack > 0)
105 g_ptr_array_add (str_splints, g_strndup (haystack, needle_in_str - haystack));
106 g_ptr_array_add (str_splints, g_strdup (replacement));
107 haystack = needle_in_str + needle_len;
109 return_str = str_ptr_array_join (str_splints);
111 g_ptr_array_foreach (str_splints, (GFunc) g_free, NULL);
112 g_ptr_array_free (str_splints, TRUE);
114 return return_str;
117 /* --------------------------------------------------------------------------------------------- */