Rename struct linebufline to struct line and add linecmp()

This simplifies the handling in sort(1) and comm(1) quite a bit.
This commit is contained in:
FRIGN 2016-03-07 02:04:29 +01:00 committed by sin
parent 54d3f3b3a5
commit 0fa5a3e5bb
4 changed files with 17 additions and 22 deletions

View File

@ -57,6 +57,7 @@ LIBUTILSRC =\
libutil/fshut.c\ libutil/fshut.c\
libutil/getlines.c\ libutil/getlines.c\
libutil/human.c\ libutil/human.c\
libutil/linecmp.c\
libutil/md5.c\ libutil/md5.c\
libutil/memmem.c\ libutil/memmem.c\
libutil/mkdirp.c\ libutil/mkdirp.c\

9
comm.c
View File

@ -9,7 +9,7 @@
static int show = 0x07; static int show = 0x07;
static void static void
printline(int pos, struct linebufline *line) printline(int pos, struct line *line)
{ {
int i; int i;
@ -33,7 +33,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
FILE *fp[2]; FILE *fp[2];
static struct linebufline line[2]; static struct line line[2];
size_t linecap[2] = { 0, 0 }; size_t linecap[2] = { 0, 0 };
ssize_t len; ssize_t len;
int ret = 0, i, diff = 0, seenline = 0; int ret = 0, i, diff = 0, seenline = 0;
@ -83,10 +83,7 @@ main(int argc, char *argv[])
eprintf("getline %s:", argv[!i]); eprintf("getline %s:", argv[!i]);
goto end; goto end;
} }
if (!(diff = memcmp(line[0].data, line[1].data, diff = linecmp(&line[0], &line[1]);
MIN(line[0].len, line[1].len)))) {
diff = (line[0].len > line[1].len);
}
LIMIT(diff, -1, 1); LIMIT(diff, -1, 1);
seenline = 0; seenline = 0;
printline((2 - diff) % 3, &line[MAX(0, diff)]); printline((2 - diff) % 3, &line[MAX(0, diff)]);

24
sort.c
View File

@ -33,10 +33,10 @@ static TAILQ_HEAD(kdhead, keydef) kdhead = TAILQ_HEAD_INITIALIZER(kdhead);
static int Cflag = 0, cflag = 0, uflag = 0; static int Cflag = 0, cflag = 0, uflag = 0;
static char *fieldsep = NULL; static char *fieldsep = NULL;
static size_t fieldseplen = 0; static size_t fieldseplen = 0;
static struct linebufline col1, col2; static struct line col1, col2;
static void static void
skipblank(struct linebufline *a) skipblank(struct line *a)
{ {
while (a->len && (*(a->data) == ' ' || *(a->data) == '\t')) { while (a->len && (*(a->data) == ' ' || *(a->data) == '\t')) {
a->data++; a->data++;
@ -45,7 +45,7 @@ skipblank(struct linebufline *a)
} }
static void static void
skipnonblank(struct linebufline *a) skipnonblank(struct line *a)
{ {
while (a->len && (*(a->data) != '\n' && *(a->data) != ' ' && while (a->len && (*(a->data) != '\n' && *(a->data) != ' ' &&
*(a->data) != '\t')) { *(a->data) != '\t')) {
@ -55,7 +55,7 @@ skipnonblank(struct linebufline *a)
} }
static void static void
skipcolumn(struct linebufline *a, int skip_to_next_col) skipcolumn(struct line *a, int skip_to_next_col)
{ {
char *s; char *s;
@ -77,10 +77,10 @@ skipcolumn(struct linebufline *a, int skip_to_next_col)
} }
static size_t static size_t
columns(struct linebufline *line, const struct keydef *kd, struct linebufline *col) columns(struct line *line, const struct keydef *kd, struct line *col)
{ {
Rune r; Rune r;
struct linebufline start, end; struct line start, end;
size_t len, utflen, rlen; size_t len, utflen, rlen;
int i; int i;
@ -130,7 +130,7 @@ columns(struct linebufline *line, const struct keydef *kd, struct linebufline *c
} }
static int static int
skipmodcmp(struct linebufline *a, struct linebufline *b, int flags) skipmodcmp(struct line *a, struct line *b, int flags)
{ {
Rune r1, r2; Rune r1, r2;
size_t offa = 0, offb = 0; size_t offa = 0, offb = 0;
@ -171,7 +171,7 @@ skipmodcmp(struct linebufline *a, struct linebufline *b, int flags)
} }
static int static int
slinecmp(struct linebufline *a, struct linebufline *b) slinecmp(struct line *a, struct line *b)
{ {
int res = 0; int res = 0;
long double x, y; long double x, y;
@ -193,11 +193,7 @@ slinecmp(struct linebufline *a, struct linebufline *b)
} else if (kd->flags & (MOD_D | MOD_F | MOD_I)) { } else if (kd->flags & (MOD_D | MOD_F | MOD_I)) {
res = skipmodcmp(&col1, &col2, kd->flags); res = skipmodcmp(&col1, &col2, kd->flags);
} else { } else {
if (!(res = memcmp(col1.data, col2.data, res = linecmp(&col1, &col2);
MIN(col1.len, col2.len)))) {
res = col1.data[MIN(col1.len, col2.len)] -
col2.data[MIN(col1.len, col2.len)];
}
} }
if (kd->flags & MOD_R) if (kd->flags & MOD_R)
@ -212,7 +208,7 @@ slinecmp(struct linebufline *a, struct linebufline *b)
static int static int
check(FILE *fp, const char *fname) check(FILE *fp, const char *fname)
{ {
static struct linebufline prev, cur, tmp; static struct line prev, cur, tmp;
static size_t prevsize, cursize, tmpsize; static size_t prevsize, cursize, tmpsize;
if (!prev.data && (prev.len = getline(&prev.data, &prevsize, fp)) < 0) if (!prev.data && (prev.len = getline(&prev.data, &prevsize, fp)) < 0)

5
text.h
View File

@ -1,12 +1,12 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
struct linebufline { struct line {
char *data; char *data;
size_t len; size_t len;
}; };
struct linebuf { struct linebuf {
struct linebufline *lines; struct line *lines;
size_t nlines; size_t nlines;
size_t capacity; size_t capacity;
}; };
@ -14,3 +14,4 @@ struct linebuf {
void getlines(FILE *, struct linebuf *); void getlines(FILE *, struct linebuf *);
void concat(FILE *, const char *, FILE *, const char *); void concat(FILE *, const char *, FILE *, const char *);
int linecmp(struct line *, struct line *);