Some minor cleanup.

This commit is contained in:
Pietro Gagliardi 2016-12-19 11:02:33 -05:00
parent 96e15116ba
commit 7ebfe73bce
1 changed files with 28 additions and 37 deletions

View File

@ -86,7 +86,8 @@ static int attrRangeIntersect(struct attr *a, size_t *start, size_t *end)
return 1; return 1;
} }
static void attrUnlink(struct attrlist *alist, struct attr *a, struct attr **next) // returns the old a->next, for forward iteration
static struct attr *attrUnlink(struct attrlist *alist, struct attr *a)
{ {
struct attr *p, *n; struct attr *p, *n;
@ -99,80 +100,73 @@ static void attrUnlink(struct attrlist *alist, struct attr *a, struct attr **nex
if (p == NULL && n == NULL) { if (p == NULL && n == NULL) {
alist->first = NULL; alist->first = NULL;
alist->last = NULL; alist->last = NULL;
*next = NULL; return NULL;
return;
} }
// start of list? // start of list?
if (p == NULL) { if (p == NULL) {
n->prev = NULL; n->prev = NULL;
alist->first = n; alist->first = n;
*next = n; return n;
return;
} }
// end of list? // end of list?
if (n == NULL) { if (n == NULL) {
p->next = NULL; p->next = NULL;
alist->last = p; alist->last = p;
*next = NULL; return NULL;
return;
} }
// middle of list // middle of list
p->next = n; p->next = n;
n->prev = p; n->prev = p;
*next = n; return n;
} }
static void attrDelete(struct attrlist *alist, struct attr *a, struct attr **next) // returns the old a->next, for forward iteration
static struct attr *attrDelete(struct attrlist *alist, struct attr *a)
{ {
attrUnlink(alist, a, next); struct attr *next;
next = attrUnlink(alist, a);
uiFree(a); uiFree(a);
return next;
} }
// attrDropRange() removes attributes without deleting characters. // attrDropRange() removes attributes without deleting characters.
// //
// If the attribute needs no change, 1 is returned. // If the attribute needs no change, then nothing is done.
// //
// If the attribute needs to be deleted, it is deleted and 0 is returned. // If the attribute needs to be deleted, it is deleted.
// //
// If the attribute only needs to be resized at the end, it is adjusted and 1 is returned. // If the attribute only needs to be resized at the end, it is adjusted.
// //
// If the attribute only needs to be resized at the start, it is adjusted, unlinked, and returned in tail, and 1 is returned. // If the attribute only needs to be resized at the start, it is adjusted, unlinked, and returned in tail.
// //
// Otherwise, the attribute needs to be split. The existing attribute is adjusted to make the left half and a new attribute with the right half. This attribute is kept unlinked and returned in tail. Then, 2 is returned. // Otherwise, the attribute needs to be split. The existing attribute is adjusted to make the left half and a new attribute with the right half. This attribute is kept unlinked and returned in tail.
// //
// In all cases, next is set to the next attribute to look at in a forward sequential loop. // In all cases, the return value is the next attribute to look at in a forward sequential loop.
// static struct attr *attrDropRange(struct attrlist *alist, struct attr *a, size_t start, size_t end, struct attr **tail)
// TODO is the return value relevant?
static int attrDropRange(struct attrlist *alist, struct attr *a, size_t start, size_t end, struct attr **tail, struct attr **next)
{ {
struct attr *b; struct attr *b;
// always pre-initialize tail to NULL // always pre-initialize tail to NULL
*tail = NULL; *tail = NULL;
if (!attrRangeIntersect(a, &start, &end)) { if (!attrRangeIntersect(a, &start, &end))
// out of range; nothing to do // out of range; nothing to do
*next = a->next; return a->next;
return 1;
}
// just outright delete the attribute? // just outright delete the attribute?
if (a->start == start && a->end == end) { if (a->start == start && a->end == end)
attrDelete(alist, a, next); return attrDelete(alist, a);
return 0;
}
// only chop off the start or end? // only chop off the start or end?
if (a->start == start) { // chop off the end if (a->start == start) { // chop off the end
a->end = end; a->end = end;
*next = a->next; return a->next;
return 1;
} }
if (a->end == end) { // chop off the start if (a->end == end) { // chop off the start
a->start = start; a->start = start;
attrUnlink(attr, a, next);
*tail = a; *tail = a;
return 1; return attrUnlink(attr, a);
} }
// we'll need to split the attribute into two // we'll need to split the attribute into two
@ -184,14 +178,12 @@ static int attrDropRange(struct attrlist *alist, struct attr *a, size_t start, s
*tail = b; *tail = b;
a->end = start; a->end = start;
*next = a->next; return a->next;
return 2;
} }
static void attrGrow(struct attrlist *alist, struct attr *a, size_t start, size_t end) static void attrGrow(struct attrlist *alist, struct attr *a, size_t start, size_t end)
{ {
struct attr *before; struct attr *before;
struct attr *unused;
// adjusting the end is simple: if it ends before our new end, just set the new end // adjusting the end is simple: if it ends before our new end, just set the new end
if (a->end < end) if (a->end < end)
@ -203,8 +195,7 @@ static void attrGrow(struct attrlist *alist, struct attr *a, size_t start, size_
if (a->start <= start) if (a->start <= start)
return; return;
a->start = start; a->start = start;
// TODO could we just return next? attrUnlink(alist, a);
attrUnlink(alist, a, &unused);
for (before = alist->first; before != NULL; before = before->next) for (before = alist->first; before != NULL; before = before->next)
if (before->start > a->start) if (before->start > a->start)
break; break;
@ -249,7 +240,7 @@ void attrlistInsertAt(struct attrlist *alist, uiAttribute type, uintptr_t val, s
return; return;
} }
// okay the values are different; we need to split apart // okay the values are different; we need to split apart
attrDropRange(alist, a, start, end, &tail, &before); before = attrDropRange(alist, a, start, end, &tail);
split = 1; split = 1;
continue; continue;