X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=src%2Fcstring.c;h=d321260465c9015c59d53ba87e60e4c43c3cdcca;hb=4499a0ea0cecd41b7053ce5768a85efdfc48848a;hp=d00421791e5aa1be7c7cdd8b2243ba6002a18b09;hpb=d35a52bc79a1f8b1b1cd48519b7a9aef86439633;p=smdp.git diff --git a/src/cstring.c b/src/cstring.c index d004217..d321260 100644 --- a/src/cstring.c +++ b/src/cstring.c @@ -19,7 +19,7 @@ * */ -#include // strlen +#include // strlen, memmove #include // fprintf #include // malloc, realloc @@ -32,6 +32,7 @@ cstring_t *cstring_init() { x->size = x->alloc = 0; x->expand = cstring_expand; x->expand_arr = cstring_expand_arr; + x->strip = cstring_strip; x->reset = cstring_reset; x->delete = cstring_delete; } else { @@ -67,6 +68,18 @@ void cstring_expand_arr(cstring_t *self, char *x) { self->text[self->size+1] = '\0'; } +void cstring_strip(cstring_t *self, int pos, int len) { + if(pos + len >= self->size) { + if(pos <= self->size) { + self->text[pos] = '\0'; + self->size = pos; + } + return; + } + memmove(&self->text[pos], &self->text[pos+len], self->size - pos); + self->size -= len; +} + void cstring_reset(cstring_t *self) { free(self->text); self->text = NULL;