replace.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Functions for replacing substrings in strings.
  3. Copyright (C) 2013-2024
  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/strutil.h"
  22. /*** global variables ****************************************************************************/
  23. /*** file scope macro definitions ****************************************************************/
  24. /*** file scope type declarations ****************************************************************/
  25. /*** forward declarations (file scope functions) *************************************************/
  26. /*** file scope variables ************************************************************************/
  27. /* --------------------------------------------------------------------------------------------- */
  28. /*** file scope functions ************************************************************************/
  29. /* --------------------------------------------------------------------------------------------- */
  30. /* --------------------------------------------------------------------------------------------- */
  31. /*** public functions ****************************************************************************/
  32. /* --------------------------------------------------------------------------------------------- */
  33. /**
  34. * Replace all substrings 'needle' in string 'haystack' by 'replacement'.
  35. * If the 'needle' in the 'haystack' is escaped by backslash,
  36. * then this occurrence isn't be replaced.
  37. *
  38. * @param haystack string contains substrings for replacement. Cannot be NULL.
  39. * @param needle string for search. Cannot be NULL.
  40. * @param replacement string for replace. Cannot be NULL.
  41. * @return newly allocated string with replaced substrings or NULL if @haystack is empty.
  42. */
  43. char *
  44. str_replace_all (const char *haystack, const char *needle, const char *replacement)
  45. {
  46. size_t needle_len, replacement_len;
  47. GString *return_str = NULL;
  48. char *needle_in_str;
  49. needle_len = strlen (needle);
  50. replacement_len = strlen (replacement);
  51. while ((needle_in_str = strstr (haystack, needle)) != NULL)
  52. {
  53. if (return_str == NULL)
  54. return_str = g_string_sized_new (32);
  55. if (str_is_char_escaped (haystack, needle_in_str))
  56. {
  57. char *backslash = needle_in_str - 1;
  58. if (haystack != backslash)
  59. g_string_append_len (return_str, haystack, backslash - haystack);
  60. g_string_append_len (return_str, needle_in_str, needle_in_str - backslash);
  61. haystack = needle_in_str + 1;
  62. }
  63. else
  64. {
  65. if (needle_in_str != haystack)
  66. g_string_append_len (return_str, haystack, needle_in_str - haystack);
  67. g_string_append_len (return_str, replacement, replacement_len);
  68. haystack = needle_in_str + needle_len;
  69. }
  70. }
  71. if (*haystack != '\0')
  72. {
  73. if (return_str == NULL)
  74. return strdup (haystack);
  75. g_string_append (return_str, haystack);
  76. }
  77. return (return_str != NULL ? g_string_free (return_str, FALSE) : NULL);
  78. }
  79. /* --------------------------------------------------------------------------------------------- */