From: Ben Pfaff Date: Fri, 17 Feb 2023 16:39:35 +0000 (-0800) Subject: pivot-table: Honor blank variable labels. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=374ec8c7a2f7418f0e050d733f8a1c6a429d86a8;p=pspp pivot-table: Honor blank variable labels. pivot_value_new_variable() ignored empty-string variable labels. This honors them. Requested by Frans Houweling. --- diff --git a/doc/variables.texi b/doc/variables.texi index 6a033db42b..c1b18edfa6 100644 --- a/doc/variables.texi +++ b/doc/variables.texi @@ -293,30 +293,26 @@ dictionary; use @cmd{NEW FILE} to do that (@pxref{NEW FILE}). @section VARIABLE LABELS @vindex VARIABLE LABELS -In addition to a variable's name, each variable can have a @dfn{label}. -Whereas the name is limited to certain constraints (@pxref{Attributes}) a variable's -label has no such constraints. -Typically, the names are concise, easy to type mnemonics for the variable -and the labels are longer, more verbose descriptions. +In addition to a variable's name, each variable can have a +@dfn{label}. Whereas a variable name is a concise, easy-to-type +mnemonic for the variable, a label may be longer and more descriptive. @display VARIABLE LABELS - @var{var_list} '@var{var_label}' - [ /@var{var_list} '@var{var_label}'] - . - . - . - [ /@var{var_list} '@var{var_label}'] + @var{variable} '@var{label}' + [@var{variable} '@var{label}']@dots{} @end display @cmd{VARIABLE LABELS} associates explanatory names with variables. This name, called a @dfn{variable label}, is displayed by statistical procedures. -To assign a variable label to a group of variables, specify a -list of variable names and the variable label as a string. -To assign different labels to different variables in the same command, -precede the subsequent variable list with a slash (@samp{/}). +Specify each variable followed by its label as a quoted string. +Variable-label pairs may be separated by an optional slash @samp{/}. + +If a listed variable already has a label, the new one replaces it. +Specifying an empty string as the label, e.g.@:@samp{''}, removes a +label. @node PRINT FORMATS @section PRINT FORMATS diff --git a/src/data/variable.c b/src/data/variable.c index 87e7d07823..4fdfdcbb65 100644 --- a/src/data/variable.c +++ b/src/data/variable.c @@ -760,23 +760,20 @@ var_get_label (const struct variable *v) /* Sets V's variable label to UTF-8 encoded string LABEL, stripping off leading and trailing white space. If LABEL is a null pointer or if LABEL is an - empty string (after stripping white space), then V's variable label (if any) - is removed. */ + empty string, then V's variable label (if any) is removed. */ static void var_set_label_quiet (struct variable *v, const char *label) { free (v->label); v->label = NULL; - if (label != NULL && label[strspn (label, CC_SPACES)]) + if (label != NULL && label[0]) v->label = xstrdup (label); ds_destroy (&v->name_and_label); ds_init_empty (&v->name_and_label); } - - /* Sets V's variable label to UTF-8 encoded string LABEL, stripping off leading and trailing white space. If LABEL is a null pointer or if LABEL is an empty string (after stripping white space), then V's variable label (if any) @@ -789,7 +786,6 @@ var_set_label (struct variable *v, const char *label) dict_var_changed (v, VAR_TRAIT_LABEL, ov); } - /* Removes any variable label from V. */ void var_clear_label (struct variable *v) diff --git a/src/output/pivot-table.c b/src/output/pivot-table.c index 661813f28b..bec8638bd0 100644 --- a/src/output/pivot-table.c +++ b/src/output/pivot-table.c @@ -2915,7 +2915,7 @@ pivot_value_new_variable__ (const char *name, const char *label) .variable = { .type = PIVOT_VALUE_VARIABLE, .var_name = xstrdup (name), - .var_label = xstrdup_if_nonempty (label), + .var_label = xstrdup_if_nonnull (label), }, }; return value; diff --git a/tests/automake.mk b/tests/automake.mk index 32e083e752..41d8edba73 100644 --- a/tests/automake.mk +++ b/tests/automake.mk @@ -434,6 +434,7 @@ TESTSUITE_AT = \ tests/language/commands/title.at \ tests/language/commands/update.at \ tests/language/commands/value-labels.at \ + tests/language/commands/variable-labels.at \ tests/language/commands/variable-display.at \ tests/language/commands/vector.at \ tests/language/commands/weight.at \ diff --git a/tests/language/commands/variable-labels.at b/tests/language/commands/variable-labels.at new file mode 100644 index 0000000000..aefc002e64 --- /dev/null +++ b/tests/language/commands/variable-labels.at @@ -0,0 +1,25 @@ +AT_BANNER([VARIABLE LABELS]) + +AT_SETUP([VARIABLE LABELS]) +AT_DATA([var-labels.sps], [dnl +DATA LIST LIST NOTABLE/x y z. +VARIABLE LABELS x 'First variable' y ' '. +BEGIN DATA. +1 2 3 +4 5 6 +END DATA. +DESCRIPTIVES x y z. +]) +AT_CHECK([pspp -O box=unicode var-labels.sps], [0], [dnl + Descriptive Statistics +╭────────────────────┬─┬────┬───────┬───────┬───────╮ +│ │N│Mean│Std Dev│Minimum│Maximum│ +├────────────────────┼─┼────┼───────┼───────┼───────┤ +│First variable │2│2.50│ 2.12│ 1.00│ 4.00│ +│ │2│3.50│ 2.12│ 2.00│ 5.00│ +│z │2│4.50│ 2.12│ 3.00│ 6.00│ +│Valid N (listwise) │2│ │ │ │ │ +│Missing N (listwise)│0│ │ │ │ │ +╰────────────────────┴─┴────┴───────┴───────┴───────╯ +]) +AT_CLEANUP \ No newline at end of file