mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-15 01:55:03 +00:00
Backported from xs.
This commit is contained in:
parent
fcf8328503
commit
37dd6a631c
1 changed files with 14 additions and 22 deletions
36
xs.h
36
xs.h
|
@ -50,7 +50,8 @@ d_char *xs_insert_m(d_char *data, int offset, const char *mem, int size);
|
||||||
#define xs_append_m(data, mem, size) xs_insert_m(data, xs_size(data) - 1, mem, size)
|
#define xs_append_m(data, mem, size) xs_insert_m(data, xs_size(data) - 1, mem, size)
|
||||||
d_char *xs_str_new(const char *str);
|
d_char *xs_str_new(const char *str);
|
||||||
#define xs_str_cat(str1, str2) xs_insert(str1, xs_size(str1) - 1, str2)
|
#define xs_str_cat(str1, str2) xs_insert(str1, xs_size(str1) - 1, str2)
|
||||||
d_char *xs_replace(const char *str, const char *sfrom, const char *sto);
|
d_char *xs_replace_i(d_char *str, const char *sfrom, const char *sto);
|
||||||
|
#define xs_replace(str, sfrom, sto) xs_replace_i(xs_dup(str), sfrom, sto)
|
||||||
d_char *xs_fmt(const char *fmt, ...);
|
d_char *xs_fmt(const char *fmt, ...);
|
||||||
int xs_str_in(char *haystack, char *needle);
|
int xs_str_in(char *haystack, char *needle);
|
||||||
int xs_startswith(char *str, char *prefix);
|
int xs_startswith(char *str, char *prefix);
|
||||||
|
@ -262,34 +263,25 @@ d_char *xs_str_new(const char *str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
d_char *xs_replace(const char *str, const char *sfrom, const char *sto)
|
d_char *xs_replace_i(d_char *str, const char *sfrom, const char *sto)
|
||||||
/* replaces all occurrences of sfrom with sto in str */
|
/* replaces inline all sfrom with sto */
|
||||||
{
|
{
|
||||||
d_char *s;
|
int sfsz = strlen(sfrom);
|
||||||
|
int stsz = strlen(sto);
|
||||||
char *ss;
|
char *ss;
|
||||||
int sfsz;
|
int offset = 0;
|
||||||
|
|
||||||
/* cache the sizes */
|
while ((ss = strstr(str + offset, sfrom)) != NULL) {
|
||||||
sfsz = strlen(sfrom);
|
int n_offset = ss - str;
|
||||||
|
|
||||||
/* create the new string */
|
str = xs_collapse(str, n_offset, sfsz);
|
||||||
s = xs_str_new(NULL);
|
str = xs_expand(str, n_offset, stsz);
|
||||||
|
memcpy(str + n_offset, sto, stsz);
|
||||||
|
|
||||||
while ((ss = strstr(str, sfrom)) != NULL) {
|
offset = n_offset;
|
||||||
/* copy the first part */
|
|
||||||
s = xs_append_m(s, str, ss - str);
|
|
||||||
|
|
||||||
/* copy sto */
|
|
||||||
s = xs_str_cat(s, sto);
|
|
||||||
|
|
||||||
/* move forward */
|
|
||||||
str = ss + sfsz;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* copy the rest */
|
return str;
|
||||||
s = xs_str_cat(s, str);
|
|
||||||
|
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue