diff options
| author | 2022-09-27 10:14:29 +0200 | |
|---|---|---|
| committer | 2022-09-27 10:14:29 +0200 | |
| commit | 37dd6a631c7327a7bde9e70208758f06e31a0032 (patch) | |
| tree | f0f74301f4cc2d5c8264fbceb27995b4987b6a40 | |
| parent | Backported xs_regex.h from xs. (diff) | |
| download | snac2-37dd6a631c7327a7bde9e70208758f06e31a0032.tar.gz snac2-37dd6a631c7327a7bde9e70208758f06e31a0032.tar.xz snac2-37dd6a631c7327a7bde9e70208758f06e31a0032.zip | |
Backported from xs.
| -rw-r--r-- | xs.h | 36 |
1 files changed, 14 insertions, 22 deletions
| @@ -50,7 +50,8 @@ d_char *xs_insert_m(d_char *data, int offset, const char *mem, int size); | |||
| 50 | #define xs_append_m(data, mem, size) xs_insert_m(data, xs_size(data) - 1, mem, size) | 50 | #define xs_append_m(data, mem, size) xs_insert_m(data, xs_size(data) - 1, mem, size) |
| 51 | d_char *xs_str_new(const char *str); | 51 | d_char *xs_str_new(const char *str); |
| 52 | #define xs_str_cat(str1, str2) xs_insert(str1, xs_size(str1) - 1, str2) | 52 | #define xs_str_cat(str1, str2) xs_insert(str1, xs_size(str1) - 1, str2) |
| 53 | d_char *xs_replace(const char *str, const char *sfrom, const char *sto); | 53 | d_char *xs_replace_i(d_char *str, const char *sfrom, const char *sto); |
| 54 | #define xs_replace(str, sfrom, sto) xs_replace_i(xs_dup(str), sfrom, sto) | ||
| 54 | d_char *xs_fmt(const char *fmt, ...); | 55 | d_char *xs_fmt(const char *fmt, ...); |
| 55 | int xs_str_in(char *haystack, char *needle); | 56 | int xs_str_in(char *haystack, char *needle); |
| 56 | int xs_startswith(char *str, char *prefix); | 57 | int xs_startswith(char *str, char *prefix); |
| @@ -262,34 +263,25 @@ d_char *xs_str_new(const char *str) | |||
| 262 | } | 263 | } |
| 263 | 264 | ||
| 264 | 265 | ||
| 265 | d_char *xs_replace(const char *str, const char *sfrom, const char *sto) | 266 | d_char *xs_replace_i(d_char *str, const char *sfrom, const char *sto) |
| 266 | /* replaces all occurrences of sfrom with sto in str */ | 267 | /* replaces inline all sfrom with sto */ |
| 267 | { | 268 | { |
| 268 | d_char *s; | 269 | int sfsz = strlen(sfrom); |
| 270 | int stsz = strlen(sto); | ||
| 269 | char *ss; | 271 | char *ss; |
| 270 | int sfsz; | 272 | int offset = 0; |
| 271 | |||
| 272 | /* cache the sizes */ | ||
| 273 | sfsz = strlen(sfrom); | ||
| 274 | |||
| 275 | /* create the new string */ | ||
| 276 | s = xs_str_new(NULL); | ||
| 277 | 273 | ||
| 278 | while ((ss = strstr(str, sfrom)) != NULL) { | 274 | while ((ss = strstr(str + offset, sfrom)) != NULL) { |
| 279 | /* copy the first part */ | 275 | int n_offset = ss - str; |
| 280 | s = xs_append_m(s, str, ss - str); | ||
| 281 | 276 | ||
| 282 | /* copy sto */ | 277 | str = xs_collapse(str, n_offset, sfsz); |
| 283 | s = xs_str_cat(s, sto); | 278 | str = xs_expand(str, n_offset, stsz); |
| 279 | memcpy(str + n_offset, sto, stsz); | ||
| 284 | 280 | ||
| 285 | /* move forward */ | 281 | offset = n_offset; |
| 286 | str = ss + sfsz; | ||
| 287 | } | 282 | } |
| 288 | 283 | ||
| 289 | /* copy the rest */ | 284 | return str; |
| 290 | s = xs_str_cat(s, str); | ||
| 291 | |||
| 292 | return s; | ||
| 293 | } | 285 | } |
| 294 | 286 | ||
| 295 | 287 | ||