Skip to content

Commit

Permalink
delay commas until necessary
Browse files Browse the repository at this point in the history
we have a host of stopping conditions, which can be nested.
As a result, a simple `next != NULL` check isn't enough to
determine if we need to write out a comma.  Instead, we delay
the comma until necessary.
  • Loading branch information
alandekok committed Aug 11, 2023
1 parent dee9457 commit d8d8293
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/lib/util/pair_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ ssize_t fr_pair_print_secure(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_p

static ssize_t fr_pair_list_print_unflatten(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_pair_list_t const *list, fr_pair_t **vp_p)
{
bool comma = false;
fr_pair_t *vp = *vp_p;
fr_pair_t *next = fr_pair_list_next(list, vp);
fr_da_stack_t da_stack;
Expand All @@ -212,6 +213,8 @@ static ssize_t fr_pair_list_print_unflatten(fr_sbuff_t *out, fr_dict_attr_t cons
* Not yet at the correct parent. Print out the wrapper, and keep looping while the parent is the same.
*/
if (fr_type_is_leaf(vp->vp_type) && (da_stack.da[parent->depth - 1] == parent) && (vp->da->parent != parent)) {
if (comma) FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, ", ");

fr_assert(da_stack.da[parent->depth] != NULL);

FR_SBUFF_IN_STRCPY_RETURN(&our_out, da_stack.da[parent->depth]->name);
Expand Down Expand Up @@ -252,14 +255,17 @@ static ssize_t fr_pair_list_print_unflatten(fr_sbuff_t *out, fr_dict_attr_t cons

if (vp) {
next = fr_pair_list_next(list, vp);
if (next) FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, ", ");
if (next) comma = true;
} else {
next = NULL;
}

*vp_p = next;

if (!next) FR_SBUFF_SET_RETURN(out, &our_out);
if (!next) {
if (comma) FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, ", ");
FR_SBUFF_SET_RETURN(out, &our_out);
}

vp = next;
}
Expand All @@ -268,23 +274,27 @@ static ssize_t fr_pair_list_print_unflatten(fr_sbuff_t *out, fr_dict_attr_t cons
* Print out things which are at the root.
*/
while (vp->da->parent->flags.is_root) {
if (comma) FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, ", ");

FR_SBUFF_RETURN(fr_pair_print, &our_out, vp->da->parent, vp);
next = fr_pair_list_next(list, vp);
if (!next) goto done;

FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, ", ");
comma = true;
vp = next;
}

/*
* Allow nested attributes to be mixed with flat attributes.
*/
while (fr_type_is_structural(vp->vp_type) && (vp->da == parent)) {
if (comma) FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, ", ");

FR_SBUFF_RETURN(fr_pair_print, &our_out, vp->da->parent, vp);
next = fr_pair_list_next(list, vp);
if (!next) goto done;

FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, ", ");
comma = true;
vp = next;
}

Expand All @@ -294,11 +304,13 @@ static ssize_t fr_pair_list_print_unflatten(fr_sbuff_t *out, fr_dict_attr_t cons
* Finally loop over the correct children.
*/
while (vp->da->parent == parent) {
if (comma) FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, ", ");

FR_SBUFF_RETURN(fr_pair_print, &our_out, vp->da->parent, vp);
next = fr_pair_list_next(list, vp);
if (!next) goto done;

FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, ", ");
comma = true;
vp = next;
}

Expand Down

0 comments on commit d8d8293

Please sign in to comment.