test )LABEL[n]
authorBen Pfaff <blp@cs.stanford.edu>
Fri, 12 Aug 2022 17:46:00 +0000 (10:46 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Fri, 12 Aug 2022 17:46:00 +0000 (10:46 -0700)
src/language/stats/ctables.c
tests/language/stats/ctables.at

index 0d2399f31088c219709e231690549e4549ee91d1..eed3a8e3c8b57554403b20ea46d01cf1d61b3ca9 100644 (file)
@@ -3893,21 +3893,88 @@ merge_item_compare_3way (const struct merge_item *a, const struct merge_item *b)
   return strcmp (as_label, bs_label);
 }
 
-static struct pivot_value *
-ctables_category_create_label__ (const struct ctables_category *cat,
-                                 const struct variable *var,
-                                 const union value *value)
+static void
+ctables_category_format_number (double number, const struct variable *var,
+                                struct string *s)
 {
-  return (cat->type == CCT_TOTAL || cat->type == CCT_SUBTOTAL
-          ? pivot_value_new_user_text (cat->total_label, SIZE_MAX)
-          : pivot_value_new_var_value (var, value));
+  struct pivot_value *pv = pivot_value_new_var_value (
+    var, &(union value) { .f = number });
+  pivot_value_format (pv, NULL, s);
+  pivot_value_destroy (pv);
+}
+
+static void
+ctables_category_format_string (struct substring string,
+                                const struct variable *var, struct string *out)
+{
+  int width = var_get_width (var);
+  char *s = xmalloc (width);
+  buf_copy_rpad (s, width, string.string, string.length, ' ');
+  struct pivot_value *pv = pivot_value_new_var_value (
+    var, &(union value) { .s = CHAR_CAST (uint8_t *, s) });
+  pivot_value_format (pv, NULL, out);
+  pivot_value_destroy (pv);
+  free (s);
+}
+
+static bool
+ctables_category_format_label (const struct ctables_category *cat,
+                               const struct variable *var,
+                               struct string *s)
+{
+  switch (cat->type)
+    {
+    case CCT_NUMBER:
+      ctables_category_format_number (cat->number, var, s);
+      return true;
+
+    case CCT_STRING:
+      ctables_category_format_string (cat->string, var, s);
+      return true;
+
+    case CCT_NRANGE:
+      ctables_category_format_number (cat->nrange[0], var, s);
+      ds_put_format (s, " THRU ");
+      ctables_category_format_number (cat->nrange[1], var, s);
+      return true;
+
+    case CCT_SRANGE:
+      ctables_category_format_string (cat->srange[0], var, s);
+      ds_put_format (s, " THRU ");
+      ctables_category_format_string (cat->srange[1], var, s);
+      return true;
+
+    case CCT_MISSING:
+      ds_put_cstr (s, "MISSING");
+      return true;
+
+    case CCT_OTHERNM:
+      ds_put_cstr (s, "OTHERNM");
+      return true;
+
+    case CCT_POSTCOMPUTE:
+      ds_put_format (s, "&%s", cat->pc->name);
+      return true;
+
+    case CCT_TOTAL:
+    case CCT_SUBTOTAL:
+      ds_put_cstr (s, cat->total_label);
+      return true;
+
+    case CCT_VALUE:
+    case CCT_LABEL:
+    case CCT_FUNCTION:
+    case CCT_EXCLUDED_MISSING:
+      return false;
+    }
+
+  return false;
 }
 
 static struct pivot_value *
 ctables_postcompute_label (const struct ctables_categories *cats,
                            const struct ctables_category *cat,
-                           const struct variable *var,
-                           const union value *value)
+                           const struct variable *var)
 {
   struct substring in = ss_cstr (cat->pc->label);
   struct substring target = ss_cstr (")LABEL[");
@@ -3939,12 +4006,8 @@ ctables_postcompute_label (const struct ctables_categories *cats,
         goto error;
 
       struct ctables_category *cat2 = &cats->cats[idx - 1];
-      struct pivot_value *label2
-        = ctables_category_create_label__ (cat2, var, value);
-      char *label2_s = pivot_value_to_string_defaults (label2);
-      ds_put_cstr (&out, label2_s);
-      free (label2_s);
-      pivot_value_destroy (label2);
+      if (!ctables_category_format_label (cat2, var, &out))
+        goto error;
     }
 
 error:
@@ -3953,14 +4016,16 @@ error:
 }
 
 static struct pivot_value *
-ctables_category_create_label (const struct ctables_categories *cats,
-                               const struct ctables_category *cat,
-                               const struct variable *var,
-                               const union value *value)
+ctables_category_create_value_label (const struct ctables_categories *cats,
+                                     const struct ctables_category *cat,
+                                     const struct variable *var,
+                                     const union value *value)
 {
   return (cat->type == CCT_POSTCOMPUTE && cat->pc->label
-          ? ctables_postcompute_label (cats, cat, var, value)
-          : ctables_category_create_label__ (cat, var, value));
+          ? ctables_postcompute_label (cats, cat, var)
+          : cat->type == CCT_TOTAL || cat->type == CCT_SUBTOTAL
+          ? pivot_value_new_user_text (cat->total_label, SIZE_MAX)
+          : pivot_value_new_var_value (var, value));
 }
 
 static struct ctables_value *
@@ -4421,9 +4486,10 @@ ctables_table_output (struct ctables *ct, struct ctables_table *t)
           const struct ctables_value *value = t->clabels_values[i];
           const struct ctables_category *cat = ctables_categories_match (c, &value->value, var);
           assert (cat != NULL);
-          pivot_category_create_leaf (d->root, ctables_category_create_label (
-                                        c, cat, t->clabels_example,
-                                        &value->value));
+          pivot_category_create_leaf (
+            d->root, ctables_category_create_value_label (c, cat,
+                                                          t->clabels_example,
+                                                          &value->value));
         }
     }
 
@@ -4636,7 +4702,7 @@ ctables_table_output (struct ctables *ct, struct ctables_table *t)
                       else if (level->type == CTL_CATEGORY)
                         {
                           const struct ctables_cell_value *cv = &cell->axes[a].cvs[level->var_idx];
-                          label = ctables_category_create_label (
+                          label = ctables_category_create_value_label (
                             t->categories[var_get_dict_index (var)],
                             cv->category, var, &cv->value);
                         }
index 5f8120c69bf1d1c3588c27b9bb5a81e9ad1dc7bc..432da3deb4df29a3fbce98c8c85423edf35cfe63 100644 (file)
@@ -17,8 +17,6 @@ dnl   * PCOMPUTE for more than one kind of summary (e.g. [COUNT, ROWPCT]).
 dnl   * MISSING, OTHERNM
 dnl   * multi-dimensional (multiple CCT_POSTCOMPUTE in one cell)
 dnl   * dates
-dnl - PPROPERTIES:
-dnl   * )LABEL[N].
 dnl - Summary functions:
 dnl   * U-prefix for unweighted summaries.
 dnl   * areaPCT.SUM and UareaPCT.SUM functions.
@@ -1619,7 +1617,7 @@ CTABLES
     /PCOMPUTE &x=EXPR([3] + [4])
     /PCOMPUTE &y=EXPR([4] + [5])
     /PPROPERTIES &x LABEL='3+4' HIDESOURCECATS=YES FORMAT=COUNT F8.2
-    /PPROPERTIES &y LABEL='4+5'
+    /PPROPERTIES &y LABEL=')LABEL[5]+)LABEL[6]'
     /TABLE=qn105ba BY qns1
     /CATEGORIES VARIABLES=qns1 [1, 2, SUBTOTAL, 3, 4, 5, &x, &y, SUBTOTAL]
 ]])