Fix string methods

This commit is contained in:
Gregory Lirent 2022-08-17 22:21:35 +03:00
parent ee987964a1
commit 21e5c0fd8c
2 changed files with 17 additions and 12 deletions

View File

@ -200,6 +200,8 @@ size_t string_reverse(str_t* x) {
p = memcpy(p - cs, v, cs);
v += cs;
} else *(--p) = *(v++);
++n;
}
free(x->buffer);

View File

@ -32,31 +32,34 @@ char* string_at(const str_t* s, ssize_t i) {
}
bool string_slice(str_t* x, str_t* s, ssize_t i, size_t c, bool cut) {
size_t string_slice(str_t* x, str_t* s, ssize_t i, size_t c, bool cut) {
char *e, *p, *v;
size_t n = 0;
memset(x, 0, sizeof(*x));
if (!c) return true;
if (!c) return n;
p = string_at(s, i);
if (is_null(p) || (e = p + strlen(p)) > p + c)
return false;
if (is_null(p))
return n;
e = p + strlen(p);
v = p;
do { v = next_char(v); } while (--c && v < e);
while (c-- && v < e) {
v = next_char(v);
++n;
}
if (!c) {
x->buffer = strndup(p, v - p);
x->buffer = strndup(p, v - p);
if (cut) {
memmove(p, v, strlen(v) + 1);
}
if (cut) {
memmove(p, v, strlen(v) + 1);
}
return true;
} else return false;
return n;
}