1.1 --- a/src/ucx/string.h Fri Dec 27 11:48:55 2019 +0100 1.2 +++ b/src/ucx/string.h Sun Dec 29 11:29:17 2019 +0100 1.3 @@ -1072,6 +1072,128 @@ 1.4 */ 1.5 #define sstrupper_a(allocator, string) scstrupper_a(allocator, string) 1.6 1.7 + 1.8 +/** 1.9 + * Replaces a pattern in a string with another string. 1.10 + * 1.11 + * The pattern is taken literally and is no regular expression. 1.12 + * Replaces at most <code>replmax</code> occurrences. 1.13 + * 1.14 + * The resulting string is allocated by the specified allocator. I.e. it 1.15 + * depends on the used allocator, whether the sstr_t.ptr must be freed 1.16 + * manually. 1.17 + * 1.18 + * If allocation fails, the sstr_t.ptr of the return value is NULL. 1.19 + * 1.20 + * @param allocator the allocator to use 1.21 + * @param str the string where replacements should be applied 1.22 + * @param pattern the pattern to search for 1.23 + * @param replacement the replacement string 1.24 + * @param replmax maximum number of replacements 1.25 + * @return the resulting string after applying the replacements 1.26 + */ 1.27 +sstr_t scstrreplacen_a(UcxAllocator *allocator, scstr_t str, 1.28 + scstr_t pattern, scstr_t replacement, size_t replmax); 1.29 + 1.30 +/** 1.31 + * Replaces a pattern in a string with another string. 1.32 + * 1.33 + * The pattern is taken literally and is no regular expression. 1.34 + * Replaces at most <code>replmax</code> occurrences. 1.35 + * 1.36 + * The sstr_t.ptr of the resulting string must be freed manually. 1.37 + * 1.38 + * If allocation fails, the sstr_t.ptr of the return value is NULL. 1.39 + * 1.40 + * @param str the string where replacements should be applied 1.41 + * @param pattern the pattern to search for 1.42 + * @param replacement the replacement string 1.43 + * @param replmax maximum number of replacements 1.44 + * @return the resulting string after applying the replacements 1.45 + */ 1.46 +sstr_t scstrreplacen(scstr_t str, scstr_t pattern, 1.47 + scstr_t replacement, size_t replmax); 1.48 + 1.49 +/** 1.50 + * Replaces a pattern in a string with another string. 1.51 + * 1.52 + * The pattern is taken literally and is no regular expression. 1.53 + * Replaces at most <code>replmax</code> occurrences. 1.54 + * 1.55 + * The resulting string is allocated by the specified allocator. I.e. it 1.56 + * depends on the used allocator, whether the sstr_t.ptr must be freed 1.57 + * manually. 1.58 + * 1.59 + * @param allocator the allocator to use 1.60 + * @param str the string where replacements should be applied 1.61 + * @param pattern the pattern to search for 1.62 + * @param replacement the replacement string 1.63 + * @param replmax maximum number of replacements 1.64 + * @return the resulting string after applying the replacements 1.65 + */ 1.66 +#define sstrreplacen_a(allocator, str, pattern, replacement, replmax) \ 1.67 + scstrreplacen_a(allocator, SCSTR(str), SCSTR(pattern), \ 1.68 + SCSTR(replacement), replmax) 1.69 + 1.70 +/** 1.71 + * Replaces a pattern in a string with another string. 1.72 + * 1.73 + * The pattern is taken literally and is no regular expression. 1.74 + * Replaces at most <code>replmax</code> occurrences. 1.75 + * 1.76 + * The sstr_t.ptr of the resulting string must be freed manually. 1.77 + * 1.78 + * If allocation fails, the sstr_t.ptr of the return value is NULL. 1.79 + * 1.80 + * @param str the string where replacements should be applied 1.81 + * @param pattern the pattern to search for 1.82 + * @param replacement the replacement string 1.83 + * @param replmax maximum number of replacements 1.84 + * @return the resulting string after applying the replacements 1.85 + */ 1.86 +#define sstrreplacen(str, pattern, replacement, replmax) \ 1.87 + scstrreplacen(SCSTR(str), SCSTR(pattern), SCSTR(replacement), replmax) 1.88 + 1.89 +/** 1.90 + * Replaces a pattern in a string with another string. 1.91 + * 1.92 + * The pattern is taken literally and is no regular expression. 1.93 + * Replaces at most <code>replmax</code> occurrences. 1.94 + * 1.95 + * The resulting string is allocated by the specified allocator. I.e. it 1.96 + * depends on the used allocator, whether the sstr_t.ptr must be freed 1.97 + * manually. 1.98 + * 1.99 + * If allocation fails, the sstr_t.ptr of the return value is NULL. 1.100 + * 1.101 + * @param allocator the allocator to use 1.102 + * @param str the string where replacements should be applied 1.103 + * @param pattern the pattern to search for 1.104 + * @param replacement the replacement string 1.105 + * @return the resulting string after applying the replacements 1.106 + */ 1.107 +#define sstrreplace_a(allocator, str, pattern, replacement) \ 1.108 + scstrreplacen_a(allocator, SCSTR(str), SCSTR(pattern), \ 1.109 + SCSTR(replacement), SIZE_MAX) 1.110 + 1.111 +/** 1.112 + * Replaces a pattern in a string with another string. 1.113 + * 1.114 + * The pattern is taken literally and is no regular expression. 1.115 + * Replaces at most <code>replmax</code> occurrences. 1.116 + * 1.117 + * The sstr_t.ptr of the resulting string must be freed manually. 1.118 + * 1.119 + * If allocation fails, the sstr_t.ptr of the return value is NULL. 1.120 + * 1.121 + * @param str the string where replacements should be applied 1.122 + * @param pattern the pattern to search for 1.123 + * @param replacement the replacement string 1.124 + * @return the resulting string after applying the replacements 1.125 + */ 1.126 +#define sstrreplace(str, pattern, replacement) \ 1.127 + scstrreplacen(SCSTR(str), SCSTR(pattern), SCSTR(replacement), SIZE_MAX) 1.128 + 1.129 #ifdef __cplusplus 1.130 } 1.131 #endif