[PATCH 2/8] sort: don't repeat skipping columns logic

From: Jakob Kramer <jakob.kramer_AT_gmx.de>
Date: Sat, 3 May 2014 18:34:51 +0200

---
 sort.c | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)
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;
-- 
1.8.5.1
--/04w6evG8XlLl3ft
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment; filename="0003-sort-remove-rest-variable.patch"
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Sat May 03 2014 - 21:24:03 CEST