From: Ben Pfaff Date: Wed, 3 Jun 2009 04:55:50 +0000 (-0700) Subject: crosstabs: Trim unsightly spaces from titles in output. X-Git-Tag: v0.7.3~59 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c9b92e317e7426db24fce2636134e1e46eb05d40;p=pspp-builds.git crosstabs: Trim unsightly spaces from titles in output. Unfortunately, none of the tests exercise this code, so it's hard to say whether it is correct. --- diff --git a/src/language/stats/crosstabs.q b/src/language/stats/crosstabs.q index e296da2d..907868c0 100644 --- a/src/language/stats/crosstabs.q +++ b/src/language/stats/crosstabs.q @@ -1218,10 +1218,17 @@ create_crosstab_table (struct crosstabs_proc *proc, struct pivot_table *pt) for (i = 0; i < pt->n_consts; i++) { const struct variable *var = pt->const_vars[i]; + size_t ofs; + ds_put_format (&title, ", %s=", var_get_name (var)); + + /* Insert the formatted value of the variable, then trim + leading spaces in what was just inserted. */ + ofs = ds_length (&title); data_out (&pt->const_values[i], var_get_print_format (var), ds_put_uninit (&title, var_get_width (var))); - /* XXX remove any leading space in what was just inserted. */ + ds_remove (&title, ofs, ss_cspan (ds_substr (&title, ofs, SIZE_MAX), + ss_cstr (" "))); } ds_put_cstr (&title, " ["); diff --git a/src/libpspp/str.c b/src/libpspp/str.c index 44d325e0..ccd7739c 100644 --- a/src/libpspp/str.c +++ b/src/libpspp/str.c @@ -1071,6 +1071,34 @@ ds_set_length (struct string *st, size_t new_length, char pad) st->ss.length = new_length; } +/* Removes N characters from ST starting at offset START. */ +void +ds_remove (struct string *st, size_t start, size_t n) +{ + if (n > 0 && start < st->ss.length) + { + if (st->ss.length - start <= n) + { + /* All characters at or beyond START are deleted. */ + st->ss.length = start; + } + else + { + /* Some characters remain and must be shifted into + position. */ + memmove (st->ss.string + st->ss.length, + st->ss.string + st->ss.length + n, + st->ss.length - start - n); + st->ss.length -= n; + } + } + else + { + /* There are no characters to delete or no characters at or + beyond START, hence deletion is a no-op. */ + } +} + /* Returns true if ST is empty, false otherwise. */ bool ds_is_empty (const struct string *st) diff --git a/src/libpspp/str.h b/src/libpspp/str.h index b057c2bd..b9be394c 100644 --- a/src/libpspp/str.h +++ b/src/libpspp/str.h @@ -177,6 +177,7 @@ bool ds_tokenize (const struct string *src, struct substring delimiters, size_t *save_idx, struct substring *token); void ds_rpad (struct string *, size_t length, char pad); void ds_set_length (struct string *, size_t new_length, char pad); +void ds_remove (struct string *, size_t start, size_t n); /* Extracting substrings. */ struct substring ds_ss (const struct string *);