replace.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Functions for replacing substrings in strings.
  3. Copyright (C) 2013-2021
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2013;
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include "lib/global.h"
  21. #include "lib/strescape.h"
  22. #include "lib/strutil.h"
  23. /*** global variables ****************************************************************************/
  24. /*** file scope macro definitions ****************************************************************/
  25. /*** file scope type declarations ****************************************************************/
  26. /*** file scope variables ************************************************************************/
  27. /* --------------------------------------------------------------------------------------------- */
  28. /*** file scope functions ************************************************************************/
  29. /* --------------------------------------------------------------------------------------------- */
  30. static GString *
  31. str_ptr_array_join (const GPtrArray * str_splints)
  32. {
  33. GString *return_str;
  34. guint i;
  35. return_str = g_string_sized_new (32);
  36. for (i = 0; i < str_splints->len; i++)
  37. g_string_append (return_str, g_ptr_array_index (str_splints, i));
  38. return return_str;
  39. }
  40. /* --------------------------------------------------------------------------------------------- */
  41. /*** public functions ****************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. /**
  44. * Replace all substrings 'needle' in string 'haystack' by 'replacement'.
  45. * If the 'needle' in the 'haystack' will be escaped by backslash,
  46. * then this occurrence isn't be replaced.
  47. *
  48. * @param haystack string contains substrings for replacement
  49. * @param needle string for search
  50. * @param replacement string for replace
  51. * @return newly allocated string with replaced substrings
  52. */
  53. char *
  54. str_replace_all (const char *haystack, const char *needle, const char *replacement)
  55. {
  56. size_t needle_len;
  57. GPtrArray *str_splints;
  58. GString *return_str;
  59. needle_len = strlen (needle);
  60. str_splints = g_ptr_array_new_with_free_func (g_free);
  61. while (TRUE)
  62. {
  63. char *needle_in_str;
  64. needle_in_str = strstr (haystack, needle);
  65. if (needle_in_str == NULL)
  66. {
  67. if (*haystack != '\0')
  68. g_ptr_array_add (str_splints, g_strdup (haystack));
  69. break;
  70. }
  71. if (strutils_is_char_escaped (haystack, needle_in_str))
  72. {
  73. char *backslash = needle_in_str - 1;
  74. if (haystack != backslash)
  75. g_ptr_array_add (str_splints, g_strndup (haystack, backslash - haystack));
  76. g_ptr_array_add (str_splints, g_strndup (backslash + 1, needle_in_str - backslash));
  77. haystack = needle_in_str + 1;
  78. continue;
  79. }
  80. if (needle_in_str - haystack > 0)
  81. g_ptr_array_add (str_splints, g_strndup (haystack, needle_in_str - haystack));
  82. g_ptr_array_add (str_splints, g_strdup (replacement));
  83. haystack = needle_in_str + needle_len;
  84. }
  85. return_str = str_ptr_array_join (str_splints);
  86. g_ptr_array_free (str_splints, TRUE);
  87. return g_string_free (return_str, FALSE);
  88. }
  89. /* --------------------------------------------------------------------------------------------- */