161 lines
3.3 KiB
C
161 lines
3.3 KiB
C
/* This software is licensed by the MIT License, see LICENSE file */
|
|
/* Copyright © 2022 Gregory Lirent */
|
|
|
|
#include "include.h"
|
|
|
|
|
|
/*#####################################################################################################################*/
|
|
|
|
|
|
void libcdsb_string_trim_spaces(str_t* x, int direction) {
|
|
static size_t m[32/(sizeof(size_t))] = {0};
|
|
|
|
u8_t* l;
|
|
u8_t* r;
|
|
|
|
if (sizeof(size_t) == 8) {
|
|
m[0] = 0x0000000100002e00UL;
|
|
} else {
|
|
m[0] = 0x00002e00UL;
|
|
m[1] = 0x00000001UL;
|
|
}
|
|
|
|
if (is_null(x->buffer))
|
|
return;
|
|
|
|
l = (void*)x->buffer;
|
|
r = (void*)x->buffer + strlen(x->buffer);
|
|
|
|
if (direction <= 0) {
|
|
while (m[*l/(8*sizeof(size_t))]&((size_t)1<<(*l%(8*sizeof(size_t))))) {
|
|
++l;
|
|
}
|
|
}
|
|
|
|
if (direction >= 0) {
|
|
do {
|
|
--r;
|
|
} while (m[*r/(8*sizeof(size_t))]&((size_t)1<<(*r%(8*sizeof(size_t)))));
|
|
++r;
|
|
}
|
|
|
|
if (x->buffer != (char*)l) {
|
|
memmove(x->buffer, l, r-l);
|
|
r -= (char*)l - x->buffer;
|
|
}
|
|
|
|
*r = 0;
|
|
}
|
|
|
|
|
|
/*#####################################################################################################################*/
|
|
|
|
|
|
void libcdsb_string_trim_cstring(str_t* x, const char* s, int direction) {
|
|
|
|
u8_t* l;
|
|
u8_t* r;
|
|
size_t n;
|
|
_Bool f;
|
|
|
|
struct {
|
|
const char* p;
|
|
size_t n;
|
|
}* m;
|
|
|
|
if (is_null(s))
|
|
return libcdsb_string_trim_spaces(x, direction);
|
|
|
|
if (is_null(x->buffer) || !*s)
|
|
return;
|
|
|
|
if (x->buffer == s) {
|
|
*x->buffer = 0;
|
|
return;
|
|
}
|
|
|
|
n = 0;
|
|
m = 0;
|
|
|
|
while (*(l = (void*)next_char((void*)s))) {
|
|
m = realloc(m, ++n*sizeof(*m));
|
|
m[n-1].n = (char*)l - s;
|
|
m[n-1].p = s;
|
|
s = (void*)l;
|
|
}
|
|
|
|
m = realloc(m, ++n*sizeof(*m));
|
|
m[n-1].n = (char*)l - s;
|
|
m[n-1].p = s;
|
|
|
|
l = (void*)x->buffer;
|
|
r = (void*)x->buffer + strlen(x->buffer);
|
|
|
|
if (direction <= 0) {
|
|
f = false;
|
|
do for (size_t i = 0; i < n; ++i) {
|
|
if (memcmp(l, m[i].p, m[i].n) == 0) {
|
|
f = true;
|
|
l += m[i].n;
|
|
break;
|
|
}
|
|
} while(f && !(f = false));
|
|
}
|
|
|
|
if (direction >= 0) {
|
|
f = false;
|
|
do for (size_t i = 0; i < n; ++i) {
|
|
if (memcmp(r - m[i].n, m[i].p, m[i].n) == 0) {
|
|
f = true;
|
|
r -= m[i].n;
|
|
break;
|
|
}
|
|
} while(f && !(f = false));
|
|
}
|
|
|
|
if (x->buffer != (char*)l) {
|
|
memmove(x->buffer, l, r-l);
|
|
r -= (char*)l - x->buffer;
|
|
}
|
|
|
|
*r = 0;
|
|
}
|
|
|
|
|
|
void libcdsb_string_trim_char(str_t* x, int sc, int direction) {
|
|
|
|
u8_t* l;
|
|
u8_t* r;
|
|
char p[4];
|
|
size_t n;
|
|
|
|
if (!sc)
|
|
return libcdsb_string_trim_spaces(x, direction);
|
|
if (is_null(x->buffer) || is_null(l = (void*)tochar_unicode(p, sc)))
|
|
return;
|
|
|
|
n = (char*)l - p;
|
|
|
|
l = (void*)x->buffer;
|
|
r = (void*)x->buffer + strlen(x->buffer);
|
|
|
|
if (direction <= 0) {
|
|
while (memcmp(l, p, n) == 0) {
|
|
l += n;
|
|
}
|
|
}
|
|
|
|
if (direction >= 0) {
|
|
while (memcmp(r-n, p, n) == 0) {
|
|
r -= n;
|
|
}
|
|
}
|
|
|
|
if (x->buffer != (char*)l) {
|
|
memmove(x->buffer, l, r-l);
|
|
r -= (char*)l - x->buffer;
|
|
}
|
|
|
|
*r = 0;
|
|
}
|