diff options
Diffstat (limited to 'xs.h')
| -rw-r--r-- | xs.h | 10 |
1 files changed, 6 insertions, 4 deletions
| @@ -449,16 +449,18 @@ xs_str *xs_replace_in(xs_str *str, const char *sfrom, const char *sto, int times | |||
| 449 | 449 | ||
| 450 | int sfsz = strlen(sfrom); | 450 | int sfsz = strlen(sfrom); |
| 451 | int stsz = strlen(sto); | 451 | int stsz = strlen(sto); |
| 452 | int diff = stsz - sfsz; | ||
| 452 | char *ss; | 453 | char *ss; |
| 453 | int offset = 0; | 454 | int offset = 0; |
| 454 | 455 | ||
| 455 | while (times > 0 && (ss = strstr(str + offset, sfrom)) != NULL) { | 456 | while (times > 0 && (ss = strstr(str + offset, sfrom)) != NULL) { |
| 456 | int n_offset = ss - str; | 457 | int n_offset = ss - str; |
| 457 | 458 | ||
| 458 | if (sfsz != stsz) { | 459 | if (diff < 0) |
| 459 | str = xs_collapse(str, n_offset, sfsz); | 460 | str = xs_collapse(str, n_offset, -diff); |
| 460 | str = xs_expand(str, n_offset, stsz); | 461 | else |
| 461 | } | 462 | if (diff > 0) |
| 463 | str = xs_expand(str, n_offset, diff); | ||
| 462 | 464 | ||
| 463 | memcpy(str + n_offset, sto, stsz); | 465 | memcpy(str + n_offset, sto, stsz); |
| 464 | 466 | ||