Subject: bug#6176: [PATCH] sort: adjust the leading blanks --debug warning

From: Pádraig Brady <[email-masked]>
Date: Sat, 22 May 2010 14:21:20 +0100

I was reviewing some tricky sorting I've done previously to see did
the new --debug option warn me about the issues I encountered.
One thing I missed was that if one specifies character offsets,
then we should warn, even if the type implicitly skips spaces when sorting.
For example, one has to specify 'b' to both start and end positions
in the second key in the example below (which sorts apache access.log by date).

printf "127.0.0.1 - - [01/Jan/2008:02:08:26 +0000] ...\n" |
sort --debug -b -k4.9,4.12 -k4.5b,4.7Mb -k4.2,4.3 -k4.14,4

One caveat with always warning about a missing 'b' when character
offsets are specified, is that this form is now warned about: -k1.x,1.y
That is commonly used to specify offsets into a fixed spaced file.
I.E. it can be seen as a line offset, rather than a field offset.
So I've excluded that form from any warnings.

Also previously we would warn about a missing 'b' when
it could have been legitimately unused to support sorting
right aligned indexes, aligned with spaces.

I'm wary about being "too clever" with these warnings,
but I think it worth suppressing this warning in the
above 2 fairly common cases, as the user can still see the
blanks being included when the key annotations are displayed.

cheers,
Pádraig.

diff --git a/src/sort.c b/src/sort.c
index 8a9309a..bebbd11 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -2261,8 +2261,13 @@ key_warnings (struct keyfield const *gkey, bool gkey_only)
         error (0, 0, _("key %lu has zero width and will be ignored"), keynum);

       /* Warn about significant leading blanks.  */
-      if (!gkey_only && tab == TAB_DEFAULT && !key->skipsblanks
-          && !key_numeric (key) && !key->month)
+      bool implicit_skip = key_numeric (key) || key->month;
+      bool support_space_aligned = !hard_LC_COLLATE && default_key_compare (key) && !key->schar;
+      bool line_offset = key->eword == 0 && key->echar != 0; /* -k1.x,1.y  */
+      if (!gkey_only && tab == TAB_DEFAULT && !line_offset
+          && ((!key->skipsblanks && !(implicit_skip || support_space_aligned))
+              || (!key->skipsblanks && key->schar)
+              || (!key->skipeblanks && key->echar)))
         error (0, 0, _("leading blanks are significant in key %lu; "
                        "consider also specifying `b'"), keynum);