Update string

This commit is contained in:
Gregory Lirent 2022-06-03 19:32:04 +03:00
parent f1314e8bef
commit e54906d316
3 changed files with 128 additions and 8 deletions

View File

@ -56,4 +56,8 @@ extern void libcdsb_string_replace(str_t* x, char* p, size_t n, const char* v, s
#define string_split_cstring libcdsb_string_split_cstring
#define string_split_char libcdsb_string_split_char
#define string_replace_cstring_cstring_r libcdsb_string_replace_r_cstring_cstring
#define string_replace_cstring_char_r libcdsb_string_replace_r_cstring_char
#define string_replace_char_cstring_r libcdsb_string_replace_r_char_cstring
#endif /* LIBCDSB_SRC_STRING_INCLUDE_H */

View File

@ -4,8 +4,10 @@
#include "include.h"
size_t string_replace_cstring_cstring(str_t* x, const char* a, const char* d, size_t maxn) {
char* p;
size_t c, an, dn;
char *p, *t, *r;
size_t c, n, an, dn;
if (a == d) return string_count(x, a);
if (is_null(x->buffer) || is_null(a) || !*x->buffer || !*a) {
return 0;
@ -13,8 +15,26 @@ size_t string_replace_cstring_cstring(str_t* x, const char* a, const char* d, si
an = strlen(a);
dn = (!is_null(d)) ? strlen(d) : 0;
n = strlen(x->buffer);
p = x->buffer;
c = 0;
t = 0;
r = 0;
if (x->buffer == a) {
x->buffer = realloc(x->buffer, dn + 1);
memcpy(x->buffer, d, dn);
return 1;
}
if (x->buffer < a && a < x->buffer + n) {
a = r = memndup(a, an);
}
if (x->buffer <= d && d < x->buffer + n) {
d = t = memndup(d, dn);
}
while (maxn-- && !is_null(p = strstr(p, a))) {
string_replace(x, p, an, d, dn);
@ -22,14 +42,16 @@ size_t string_replace_cstring_cstring(str_t* x, const char* a, const char* d, si
++c;
}
free(r);
free(t);
return c;
}
size_t string_replace_cstring_char(str_t* x, const char* a, int dc, size_t maxn) {
char* p;
char *p, *t;
char d[4];
size_t c, an, dn;
size_t c, n, an, dn;
if (is_null(x->buffer) || is_null(a) || !*x->buffer || !*a) {
return 0;
@ -38,8 +60,19 @@ size_t string_replace_cstring_char(str_t* x, const char* a, int dc, size_t maxn)
an = strlen(a);
p = tochar_unicode(d, dc);
dn = (!is_null(p)) ? p - d : 0;
n = strlen(x->buffer);
p = x->buffer;
c = 0;
t = 0;
if (x->buffer == a) {
x->buffer = realloc(x->buffer, dn + 1);
memcpy(x->buffer, d, dn);
return 1;
} else if (x->buffer < a && a < x->buffer + n) {
a = t = memndup(a, an);
}
while (maxn-- && !is_null(p = strstr(p, a))) {
string_replace(x, p, an, d, dn);
@ -47,14 +80,15 @@ size_t string_replace_cstring_char(str_t* x, const char* a, int dc, size_t maxn)
++c;
}
free(t);
return c;
}
size_t string_replace_char_cstring(str_t* x, int ac, const char* d, size_t maxn) {
char* p;
char *p, *t;
char a[4];
size_t c, an, dn;
size_t c, n, an, dn;
p = tochar_unicode(a, ac);
@ -64,8 +98,14 @@ size_t string_replace_char_cstring(str_t* x, int ac, const char* d, size_t maxn)
an = p - a;
dn = (!is_null(d)) ? strlen(d) : 0;
n = strlen(x->buffer);
p = x->buffer;
c = 0;
t = 0;
if (x->buffer <= d && d < x->buffer + n) {
d = t = memndup(d, dn);
}
while (maxn-- && !is_null(p = strstr(p, a))) {
string_replace(x, p, an, d, dn);
@ -73,6 +113,7 @@ size_t string_replace_char_cstring(str_t* x, int ac, const char* d, size_t maxn)
++c;
}
free(t);
return c;
}
@ -103,3 +144,78 @@ size_t string_replace_char_char(str_t* x, int ac, int dc, size_t maxn) {
return c;
}
size_t string_replace_cstring_cstring_r(str_t* x, const char*restrict a, const char*restrict d, size_t maxn) {
char *restrict p;
size_t c, an, dn;
if (is_null(x->buffer) || is_null(a) || !*x->buffer || !*a) {
return 0;
}
an = strlen(a);
dn = (!is_null(d)) ? strlen(d) : 0;
p = x->buffer;
c = 0;
while (maxn-- && !is_null(p = strstr(p, a))) {
string_replace(x, p, an, d, dn);
p += an;
++c;
}
return c;
}
size_t string_replace_cstring_char_r(str_t* x, const char*restrict a, int dc, size_t maxn) {
char *restrict p;
char d[4];
size_t c, an, dn;
if (is_null(x->buffer) || is_null(a) || !*x->buffer || !*a) {
return 0;
}
an = strlen(a);
p = tochar_unicode(d, dc);
dn = (!is_null(p)) ? p - d : 0;
p = x->buffer;
c = 0;
while (maxn-- && !is_null(p = strstr(p, a))) {
string_replace(x, p, an, d, dn);
p += an;
++c;
}
return c;
}
size_t string_replace_char_cstring_r(str_t* x, int ac, const char*restrict d, size_t maxn) {
char *restrict p;
char a[4];
size_t c, an, dn;
p = tochar_unicode(a, ac);
if (is_null(x->buffer) || is_null(p) || !*x->buffer || !*p) {
return 0;
}
an = p - a;
dn = (!is_null(d)) ? strlen(d) : 0;
p = x->buffer;
c = 0;
while (maxn-- && !is_null(p = strstr(p, a))) {
string_replace(x, p, an, d, dn);
p += an;
++c;
}
return c;
}

View File

@ -35,9 +35,9 @@ static void th_init(th_t* x, const char* s) {
if (n && n == strasciilen(s)) {
for (int i = 0; i < n; ++i) {
#ifndef STRING_TRIM_MEMOPT
x->x[s[i]] = 1;
x->x[((unsigned char*)s)[i]] = 1;
#else
x->x[s[i]/(8*sizeof(size_t))] |= (size_t)1<<(s[i]%(8*sizeof(size_t)));
x->x[((unsigned char*)s)[i]/(8*sizeof(size_t))] |= (size_t)1<<(((unsigned char*)s)[i]%(8*sizeof(size_t)));
#endif
}
} else if (n) {