sort: don't repeat skipping columns logic

This commit is contained in:
Jakob Kramer 2014-05-03 18:34:51 +02:00 committed by sin
parent c4e5354a32
commit 56b9a26de9

33
sort.c
View File

@ -29,6 +29,7 @@ static int linecmp(const char **, const char **);
static char *next_nonblank(char *); static char *next_nonblank(char *);
static char *next_blank(char *); static char *next_blank(char *);
static int parse_keydef(struct keydef *, char *); static int parse_keydef(struct keydef *, char *);
static char *skip_columns(char *, size_t);
static char *columns(char *, const struct keydef *); static char *columns(char *, const struct keydef *);
static bool rflag = false; static bool rflag = false;
@ -193,6 +194,20 @@ next_blank(char *s)
return s; return 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 * static char *
columns(char *line, const struct keydef *kd) columns(char *line, const struct keydef *kd)
{ {
@ -201,31 +216,23 @@ columns(char *line, const struct keydef *kd)
char *res; char *res;
unsigned int i; unsigned int i;
rest = line; rest = skip_columns(line, kd->start_column);
for(i = 0; i < kd->start_column; i++) {
if(i != 0)
rest = next_blank(rest);
rest = next_nonblank(rest);
}
for(i = 1; i < kd->start_char && *rest && !isblank(*rest); i++) for(i = 1; i < kd->start_char && *rest && !isblank(*rest); i++)
rest++; rest++;
start = rest; start = rest;
if(kd->end_column) { if(kd->end_column) {
rest = line; rest = skip_columns(line, kd->end_column);
for(i = 0; i < kd->end_column; i++) {
if(i != 0)
rest = next_blank(rest);
rest = next_nonblank(rest);
}
if(kd->end_char) if(kd->end_char)
for(i = 1; i < kd->end_char && *rest && !isblank(*rest); i++) for(i = 1; i < kd->end_char && *rest && !isblank(*rest); i++)
rest++; rest++;
else else
rest = next_blank(rest); rest = next_blank(rest);
end = rest - 1; end = rest - 1;
} else } else {
end = rest + strlen(rest); end = rest + strlen(rest);
}
if((res = strndup(start, end - start)) == NULL) if((res = strndup(start, end - start)) == NULL)
enprintf(2, "strndup:"); enprintf(2, "strndup:");
return res; return res;