1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #include <string.h>
- #include <stdlib.h>
- #include <stddef.h>
- #ifndef STRING_MISC_H
- #define STRING_MISC_H
- #if (__STDC_VERSION__ >= 199901L)
- #include <stdint.h>
- #endif
- char *repl_str(const char *str, const char *from, const char *to) {
-
-
- size_t cache_sz_inc = 16;
-
- const size_t cache_sz_inc_factor = 3;
-
- const size_t cache_sz_inc_max = 1048576;
- char *pret, *ret = NULL;
- const char *pstr2, *pstr = str;
- size_t i, count = 0;
- #if (__STDC_VERSION__ >= 199901L)
- uintptr_t *pos_cache_tmp, *pos_cache = NULL;
- #else
- ptrdiff_t *pos_cache_tmp, *pos_cache = NULL;
- #endif
- size_t cache_sz = 0;
- size_t cpylen, orglen, retlen, tolen, fromlen = strlen(from);
-
- while ((pstr2 = strstr(pstr, from)) != NULL) {
- count++;
-
- if (cache_sz < count) {
- cache_sz += cache_sz_inc;
- pos_cache_tmp = realloc(pos_cache, sizeof(*pos_cache) * cache_sz);
- if (pos_cache_tmp == NULL) {
- goto end_repl_str;
- } else pos_cache = pos_cache_tmp;
- cache_sz_inc *= cache_sz_inc_factor;
- if (cache_sz_inc > cache_sz_inc_max) {
- cache_sz_inc = cache_sz_inc_max;
- }
- }
- pos_cache[count-1] = pstr2 - str;
- pstr = pstr2 + fromlen;
- }
- orglen = pstr - str + strlen(pstr);
-
- if (count > 0) {
- tolen = strlen(to);
- retlen = orglen + (tolen - fromlen) * count;
- } else retlen = orglen;
- ret = malloc(retlen + 1);
- if (ret == NULL) {
- goto end_repl_str;
- }
- if (count == 0) {
-
- strcpy(ret, str);
- } else {
-
- pret = ret;
- memcpy(pret, str, pos_cache[0]);
- pret += pos_cache[0];
- for (i = 0; i < count; i++) {
- memcpy(pret, to, tolen);
- pret += tolen;
- pstr = str + pos_cache[i] + fromlen;
- cpylen = (i == count-1 ? orglen : pos_cache[i+1]) - pos_cache[i] - fromlen;
- memcpy(pret, pstr, cpylen);
- pret += cpylen;
- }
- ret[retlen] = '\0';
- }
- end_repl_str:
-
- free(pos_cache);
- return ret;
- }
- #endif
|