2
* An implementation of replacing substrings in a string.
4
* Source: http://creativeandcritical.net/str-replace-c
6
* License: Public domain
8
* Brought on 2016-02-24
18
/* An optimised string replacement function that caches string positions so that
19
* strstr() doesn't need to be called twice for each position.
21
* This code is entirely by me, and released into the public domain, so have no
22
* doubts about your right to use it.
24
char *replace_substr(const char *str, const char *old, const char *new) {
26
/* Adjust each of the below values to suit your needs. */
28
/* Increment positions cache size initially by this number. */
29
size_t cache_sz_inc = 16;
30
/* Thereafter, each time capacity needs to be increased,
31
* multiply the increment by this factor. */
32
const size_t cache_sz_inc_factor = 3;
33
/* But never increment capacity by more than this number. */
34
const size_t cache_sz_inc_max = 1048576;
36
char *pret, *ret = NULL;
37
const char *pstr2, *pstr = str;
39
ptrdiff_t *pos_cache = NULL;
41
size_t cpylen, orglen, retlen, newlen, oldlen = strlen(old);
43
/* Find all matches and cache their positions. */
44
while ((pstr2 = strstr(pstr, old)) != NULL) {
47
/* Increase the cache size when necessary. */
48
if (cache_sz < count) {
49
cache_sz += cache_sz_inc;
50
pos_cache = realloc(pos_cache, sizeof(*pos_cache) * cache_sz);
51
if (pos_cache == NULL) {
54
cache_sz_inc *= cache_sz_inc_factor;
55
if (cache_sz_inc > cache_sz_inc_max) {
56
cache_sz_inc = cache_sz_inc_max;
60
pos_cache[count-1] = pstr2 - str;
61
pstr = pstr2 + oldlen;
64
orglen = pstr - str + strlen(pstr);
66
/* Allocate memory for the post-replacement string. */
69
retlen = orglen + (newlen - oldlen) * count;
70
} else retlen = orglen;
71
ret = malloc(retlen + 1);
77
/* If no matches, then just duplicate the string. */
80
/* Otherwise, duplicate the string whilst performing
81
* the replacements using the position cache. */
83
memcpy(pret, str, pos_cache[0]);
85
for (i = 0; i < count; i++) {
86
memcpy(pret, new, newlen);
88
pstr = str + pos_cache[i] + oldlen;
89
cpylen = (i == count-1 ? orglen : pos_cache[i+1]) - pos_cache[i] - oldlen;
90
memcpy(pret, pstr, cpylen);
97
/* Free the cache and return the post-replacement string,
98
* which will be NULL in the event of an error. */