pivot-table: Always put footnote markers in sorted order.
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 10 Jan 2021 02:01:42 +0000 (18:01 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 10 Jan 2021 02:03:09 +0000 (18:03 -0800)
This is compatible behavior.

src/output/pivot-table.c
src/output/pivot-table.h
src/output/spv/spv-light-decoder.c

index 369ab63525455ac48090a8ef7932e344f1b49009..85ac16d09c1bf2f311bdc7a02dd3cd8c8b8f0c14 100644 (file)
@@ -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
index 0c89725a248e85249577223bffe30bea4c718ae2..2a5d8467f81037e152505df4a8e7ae1049e24faa 100644 (file)
@@ -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 *,
index 20f7afa11b338819c902e3565bdddcde00463b8f..ce5bd067c9e930637dababca3fba654c474ff606 100644 (file)
@@ -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)