[hackers] [sbase] sort: don't repeat skipping columns logic || Jakob Kramer

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

commit 56b9a26de9835bd1f778a142d3c8aa8710b81525
Author: Jakob Kramer <jakob.kramer_AT_gmx.de>
Date: Sat May 3 18:34:51 2014 +0200

    sort: don't repeat skipping columns logic

diff --git a/sort.c b/sort.c
index 4b98cd9..be1f699 100644
--- a/sort.c
+++ b/sort.c
_AT_@ -29,6 +29,7 @@ static int linecmp(const char **, const char **);
 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 *columns(char *, const struct keydef *);
 
 static bool rflag = false;
_AT_@ -194,6 +195,20 @@ next_blank(char *s)
 }
 
 static char *
+skip_columns(char *s, size_t n)
+{
+ size_t i;
+
+ for(i = 0; i < n; i++) {
+ if(i != 0)
+ s = next_blank(s);
+ s = next_nonblank(s);
+ }
+
+ return s;
+}
+
+static char *
 columns(char *line, const struct keydef *kd)
 {
         char *rest;
_AT_@ -201,31 +216,23 @@ columns(char *line, const struct keydef *kd)
         char *res;
         unsigned int i;
 
- rest = line;
- for(i = 0; i < kd->start_column; i++) {
- if(i != 0)
- rest = next_blank(rest);
- rest = next_nonblank(rest);
- }
+ rest = skip_columns(line, kd->start_column);
         for(i = 1; i < kd->start_char && *rest && !isblank(*rest); i++)
                 rest++;
         start = rest;
 
         if(kd->end_column) {
- rest = line;
- for(i = 0; i < kd->end_column; i++) {
- if(i != 0)
- rest = next_blank(rest);
- rest = next_nonblank(rest);
- }
+ rest = skip_columns(line, kd->end_column);
                 if(kd->end_char)
                         for(i = 1; i < kd->end_char && *rest && !isblank(*rest); i++)
                                 rest++;
                 else
                         rest = next_blank(rest);
                 end = rest - 1;
- } else
+ } else {
                 end = rest + strlen(rest);
+ }
+
         if((res = strndup(start, end - start)) == NULL)
                 enprintf(2, "strndup:");
         return res;
Received on Sun May 04 2014 - 01:15:06 CEST

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