[hackers] [sbase] sort: add -b flag; don't use it as default || Jakob Kramer

From: <git_AT_suckless.org>
Date: Sun, 04 May 2014 01:15:53 +0200

commit d965985a5219ed5dbe09fc83177beb5bb56f73fd
Author: Jakob Kramer <jakob.kramer_AT_gmx.de>
Date: Sat May 3 20:24:08 2014 +0200

    sort: add -b flag; don't use it as default

diff --git a/sort.c b/sort.c
index 63b9509..5af81d2 100644
--- a/sort.c
+++ b/sort.c
_AT_@ -30,11 +30,13 @@ static char *next_nonblank(char *);
 static char *next_blank(char *);
 static int parse_keydef(struct keydef *, char *);
 static char *skip_columns(char *, size_t);
+static char *end_column(char *);
 static char *columns(char *, const struct keydef *);
 
 static bool rflag = false;
 static bool uflag = false;
 static bool nflag = false;
+static bool bflag = false;
 
 static void
 usage(void)
_AT_@ -59,6 +61,9 @@ main(int argc, char *argv[])
         case 'u':
                 uflag = true;
                 break;
+ case 'b':
+ bflag = true;
+ break;
         case 'k':
                 addkeydef(EARGF(usage()));
                 break;
_AT_@ -169,7 +174,7 @@ parse_keydef(struct keydef *kd, char *s)
                 kd->start_char = strtoul(rest+1, &rest, 10);
         if(*rest == ',') {
                 kd->end_column = strtoul(rest+1, &rest, 10);
- if(kd->end_column < kd->start_column)
+ if(kd->end_column && kd->end_column < kd->start_column)
                         enprintf(2, ",%u is too small
", kd->end_column);
                 if(*rest == '.')
                         kd->end_char = strtoul(rest+1, &rest, 10);
_AT_@ -201,29 +206,45 @@ skip_columns(char *s, size_t n)
         size_t i;
 
         for(i = 0; i < n; i++) {
- if(i != 0)
+ if(bflag) {
+ if(i != 0)
+ s = next_blank(s);
+ s = next_nonblank(s);
+ } else {
+ if(i == 0)
+ continue;
+ s = next_nonblank(s);
                         s = next_blank(s);
- s = next_nonblank(s);
+ }
         }
 
         return s;
 }
 
 static char *
+end_column(char *s)
+{
+ if(bflag)
+ return next_blank(s);
+ else
+ return next_blank(next_nonblank(s));
+}
+
+static char *
 columns(char *line, const struct keydef *kd)
 {
         char *start, *end;
         char *res;
 
         start = skip_columns(line, kd->start_column);
- start += MIN(kd->start_char, next_blank(start) - start) - 1;
+ start += MIN(kd->start_char, end_column(start) - start) - 1;
 
         if(kd->end_column) {
                 end = skip_columns(line, kd->end_column);
                 if(kd->end_char)
- end += MIN(kd->end_char, next_blank(end) - end);
+ end += MIN(kd->end_char, end_column(end) - end);
                 else
- end = next_blank(end);
+ end = end_column(end);
         } else {
                 end = line + strlen(line);
         }
Received on Sun May 04 2014 - 01:15:53 CEST

This archive was generated by hypermail 2.3.0 : Sun May 04 2014 - 01:24:12 CEST