summaryrefslogtreecommitdiff
path: root/xs.h
diff options
context:
space:
mode:
authordefault <nobody@localhost>2023-08-14 09:03:05 +0200
committerdefault <nobody@localhost>2023-08-14 09:03:05 +0200
commita1d083ff27f78ce41a24b132adde2a8fb406a222 (patch)
treef49a12d24b360ddd105aea23bda22ff0bd2dd419 /xs.h
parentc8d535a154a8bf6a7bc97a921afeb3dbbf51c204 (diff)
Backport from xs.
Diffstat (limited to 'xs.h')
-rw-r--r--xs.h10
1 files changed, 6 insertions, 4 deletions
diff --git a/xs.h b/xs.h
index 1f714b6..ff86500 100644
--- a/xs.h
+++ b/xs.h
@@ -449,16 +449,18 @@ xs_str *xs_replace_in(xs_str *str, const char *sfrom, const char *sto, int times
int sfsz = strlen(sfrom);
int stsz = strlen(sto);
+ int diff = stsz - sfsz;
char *ss;
int offset = 0;
while (times > 0 && (ss = strstr(str + offset, sfrom)) != NULL) {
int n_offset = ss - str;
- if (sfsz != stsz) {
- str = xs_collapse(str, n_offset, sfsz);
- str = xs_expand(str, n_offset, stsz);
- }
+ if (diff < 0)
+ str = xs_collapse(str, n_offset, -diff);
+ else
+ if (diff > 0)
+ str = xs_expand(str, n_offset, diff);
memcpy(str + n_offset, sto, stsz);