1
0
Fork 0

trimfront, trimend

This commit is contained in:
dtb 2023-08-31 19:29:18 -04:00
parent ccdcd53889
commit 1e42e3f8b2
2 changed files with 83 additions and 30 deletions

View File

@ -1,18 +1,12 @@
#include <stdlib.h> /* free(3), malloc(3), realloc(3) */
#include <string.h> /* strncpy(3), size_t */
#include <stdlib.h> /* 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);
}

View File

@ -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);