From 34cb3b49aed3f145ccf68f083041e493cc5fc924 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sat, 9 Jan 2021 18:01:42 -0800 Subject: [PATCH] pivot-table: Always put footnote markers in sorted order. This is compatible behavior. --- src/output/pivot-table.c | 23 +++++++++++++++++++++++ src/output/pivot-table.h | 1 + src/output/spv/spv-light-decoder.c | 1 + 3 files changed, 25 insertions(+) diff --git a/src/output/pivot-table.c b/src/output/pivot-table.c index 369ab63525..85ac16d09c 100644 --- a/src/output/pivot-table.c +++ b/src/output/pivot-table.c @@ -2859,6 +2859,29 @@ pivot_value_add_footnote (struct pivot_value *v, v->footnote_indexes = xrealloc ( v->footnote_indexes, (v->n_footnotes + 1) * sizeof *v->footnote_indexes); v->footnote_indexes[v->n_footnotes++] = footnote->idx; + pivot_value_sort_footnotes (v); +} + +static int +compare_footnote_indexes (const void *a_, const void *b_) +{ + const size_t *ap = a_; + const size_t *bp = b_; + size_t a = *ap; + size_t b = *bp; + return a < b ? -1 : a > b; +} + +/* Sorts the footnote references in V in the standard ascending order. + + This is only necessary if code adds (plural) footnotes to a pivot_value by + itself, because pivot_value_add_footnote() does it automatically. */ +void +pivot_value_sort_footnotes (struct pivot_value *v) +{ + if (v->n_footnotes > 1) + qsort (v->footnote_indexes, v->n_footnotes, sizeof *v->footnote_indexes, + compare_footnote_indexes); } /* If VALUE is a numeric value, and RC is a result class such as diff --git a/src/output/pivot-table.h b/src/output/pivot-table.h index 0c89725a24..2a5d8467f8 100644 --- a/src/output/pivot-table.h +++ b/src/output/pivot-table.h @@ -749,6 +749,7 @@ struct pivot_value *pivot_value_new_user_text_nocopy (char *); /* Footnotes. */ void pivot_value_add_footnote (struct pivot_value *, const struct pivot_footnote *); +void pivot_value_sort_footnotes (struct pivot_value *); /* Numeric formats. */ void pivot_value_set_rc (const struct pivot_table *, struct pivot_value *, diff --git a/src/output/spv/spv-light-decoder.c b/src/output/spv/spv-light-decoder.c index 20f7afa11b..ce5bd067c9 100644 --- a/src/output/spv/spv-light-decoder.c +++ b/src/output/spv/spv-light-decoder.c @@ -415,6 +415,7 @@ decode_spvlb_value (const struct pivot_table *table, out->footnote_indexes[out->n_footnotes++] = idx; } + pivot_value_sort_footnotes (out); } if (vm->style_pair) -- 2.30.2