From 1e42e3f8b2a362acbf6f5b422c2b3121fb37b367 Mon Sep 17 00:00:00 2001 From: DTB Date: Thu, 31 Aug 2023 19:29:18 -0400 Subject: [PATCH] trimfront, trimend --- libfatptr/libfatptr.c | 57 +++++++++++++++++++++++++++---------------- libfatptr/libfatptr.h | 56 +++++++++++++++++++++++++++++++++++------- 2 files changed, 83 insertions(+), 30 deletions(-) diff --git a/libfatptr/libfatptr.c b/libfatptr/libfatptr.c index 753327f..92ae739 100644 --- a/libfatptr/libfatptr.c +++ b/libfatptr/libfatptr.c @@ -1,18 +1,12 @@ -#include /* free(3), malloc(3), realloc(3) */ -#include /* strncpy(3), size_t */ +#include /* free(3), malloc(3), realloc(3), size_t */ #include "libfatptr.h" -/* quick and dirty ASCII edition */ - -/* Size by which to grow memory allocations. */ -static size_t FatPtr_growinterval = 100; /* in sizeof chars */ - struct FatPtr * FatPtr_append(struct FatPtr *p, FatPtr_scalar_t c){ FatPtr_vector_t t; if(p == NULL || (p->s + (sizeof *(p->v)) > p->a - && FatPtr_grow(p, FatPtr_growinterval) == NULL)) + && FatPtr_grow(p, sizeof FatPtr_scalar_t) == NULL)) return NULL; (p->v)[(p->s)++] = c; return p; @@ -21,18 +15,7 @@ FatPtr_append(struct FatPtr *p, FatPtr_scalar_t c){ struct FatPtr * FatPtr_construct(struct FatPtr *p){ - return FatPtr_grow(FatPtr_initialize(p), FatPtr_growinterval); -} - -char * -FatPtr_convert(struct FatPtr *p){ - char *r; - - /* allocation might be wrong */ - return (p == NULL || (r = malloc(p->s * (sizeof *(p->v)))) == NULL) - ? NULL - : strncpy(r, p->v, p->s) - ; + return FatPtr_grow(FatPtr_initialize(p), sizeof FatPtr_scalar_t); } struct FatPtr * @@ -68,5 +51,37 @@ FatPtr_initialize(struct FatPtr *p){ } struct FatPtr * -FatPtr_trimfront(struct FatPtr *p, size_t units){ +FatPtr_shiftleft(struct FatPtr *p, size_t units){ + size_t i; + + if(p != NULL) + for(i = 0; i < p->s; ++i) + (p->v)[i] = (p->v)[i + units]; + return p; +} + +struct FatPtr * +FatPtr_shiftright(struct FatPtr *p, size_t units){ + size_t i; + + if(FatPtr_grow(p, units * (sizeof FatPtr_scalar_t)) == NULL) + return NULL; + for(i = 0; i < p->s; ++i) + (p->v)[i + units] = (p->v)[i]; + p->s += units; + return p; +} + +struct FatPtr * +FatPtr_trimend(struct FatPtr *p, size_t units){ + + if(p != NULL) + p->s -= units; + return p; +} + +struct FatPtr * +FatPtr_trimfront(struct FatPtr *p, size_t units){ + + return FatPtr_trimend(FatPtr_shiftleft(p, units), units); } diff --git a/libfatptr/libfatptr.h b/libfatptr/libfatptr.h index a2fcbdd..a685cc9 100644 --- a/libfatptr/libfatptr.h +++ b/libfatptr/libfatptr.h @@ -18,23 +18,61 @@ struct FatPtr{ * - FatPtr *p: object to which to append * - FatPtr_scalar_t c: scalar to be appended * Returns NULL if the operation failed (due to malloc(3)) or if p was NULL. - * Otherwise returns p. */ + * Otherwise returns p. + * Appends c to p. */ struct FatPtr *FatPtr_append(struct FatPtr *p, FatPtr_scalar_t c); +/* FatPtr_construct + * - FatPtr *p: object to be constructed + * Returns p. + * Initializes p and grows its maximum length to a reasonable amount of units. + */ struct FatPtr *FatPtr_construct(struct FatPtr *p); -/* FatPtr_convert - * - FatPtr *p: object to convert - * Returns NULL if the operation failed (due to malloc(3)) or if p was NULL. - * Otherwise returns a malloc(3)d char * with the current content of p, which - * should be free(3)d when no longer in use. */ -char *FatPtr_convert(struct FatPtr *p); - /* FatPtr_destruct * - FatPtr *p: object to destruct - * Returns NULL. */ + * Returns NULL. + * Destroys p without leaking memory. */ struct FatPtr *FatPtr_destruct(struct FatPtr *p); +/* FatPtr_grow + * - FatPtr *p: object to grow + * - size_t units: units (in sizeof char) by which to grow p + * Returns p. + * Grows p's maximum length by units (in sizeof char). */ struct FatPtr *FatPtr_grow(struct FatPtr *p, size_t units); + +/* FatPtr_initialize + * - FatPtr *p: object to initialize + * Returns p. + * Initializes p to zero values. */ struct FatPtr *FatPtr_initialize(struct FatPtr *p); + +/* FatPtr_shiftleft + * - FatPtr *p: object to shift + * - size_t units: units (in sizeof *v) by which to shift p + * Returns p. + * Shifts members of p left by units, leaving the last units members intact. */ +struct FatPtr *FatPtr_shiftleft(struct FatPtr *p, size_t units); + +/* FatPtr_shiftright + * - FatPtr *p: object to shift + * - size_t units: units (in sizeof *v) by which to shift p + * Returns p. + * Shifts members of p right by units, leaving the first units members intact. + */ +struct FatPtr *FatPtr_shiftright(struct FatPtr *p, size_t units); + +/* FatPtr_trimend + * - FatPtr *p: object to trim + * - size_t units: units (in sizeof *v) by which to shift p + * Returns p. + * Removes the last units members of p from p. */ +struct FatPtr *FatPtr_trimend(struct FatPtr *p, size_t units); + +/* FatPtr_trimfront + * - FatPtr *p: object to trim + * - size_t units: units (in sizeof *v) by which to shift p + * Returns p. + * Removes the first units members of p from p. */ struct FatPtr *FatPtr_trimfront(struct FatPtr *p, size_t units);