cut: code shrink
This change eliminates one temporary: - if (dcount++ < cut_list[cl_pos].startpos) + dcount++; + if (dcount <= cut_list[cl_pos].startpos) function old new delta cut_main 1402 1373 -29 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
b03f5162ac
commit
1ea89fa98a
1 changed files with 13 additions and 9 deletions
|
@ -91,8 +91,9 @@ struct cut_range {
|
||||||
|
|
||||||
static int cmpfunc(const void *a, const void *b)
|
static int cmpfunc(const void *a, const void *b)
|
||||||
{
|
{
|
||||||
return (((struct cut_range *) a)->startpos -
|
const struct cut_range *aa = a;
|
||||||
((struct cut_range *) b)->startpos);
|
const struct cut_range *bb = b;
|
||||||
|
return aa->startpos - bb->startpos;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define END_OF_LIST(list_elem) ((list_elem).startpos == UINT_MAX)
|
#define END_OF_LIST(list_elem) ((list_elem).startpos == UINT_MAX)
|
||||||
|
@ -109,18 +110,18 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
|
||||||
while ((line = xmalloc_fgetline(file)) != NULL) {
|
while ((line = xmalloc_fgetline(file)) != NULL) {
|
||||||
|
|
||||||
/* set up a list so we can keep track of what's been printed */
|
/* set up a list so we can keep track of what's been printed */
|
||||||
int linelen = strlen(line);
|
unsigned linelen = strlen(line);
|
||||||
unsigned cl_pos = 0;
|
unsigned cl_pos = 0;
|
||||||
|
|
||||||
/* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
|
/* Cut based on chars/bytes XXX: only works when sizeof(char) == byte */
|
||||||
if (option_mask32 & (OPT_CHAR | OPT_BYTE)) {
|
if (option_mask32 & (OPT_CHAR | OPT_BYTE)) {
|
||||||
char *printed = xzalloc(linelen + 1);
|
char *printed = xzalloc(linelen + 1);
|
||||||
int need_odelim = 0;
|
int need_odelim = 0;
|
||||||
|
|
||||||
/* print the chars specified in each cut list */
|
/* print the chars specified in each cut list */
|
||||||
for (; NOT_END_OF_LIST(cut_list[cl_pos]); cl_pos++) {
|
for (; NOT_END_OF_LIST(cut_list[cl_pos]); cl_pos++) {
|
||||||
unsigned spos;
|
unsigned spos = cut_list[cl_pos].startpos;
|
||||||
for (spos = cut_list[cl_pos].startpos; spos < linelen;) {
|
while (spos < linelen) {
|
||||||
if (!printed[spos]) {
|
if (!printed[spos]) {
|
||||||
printed[spos] = 'X';
|
printed[spos] = 'X';
|
||||||
if (need_odelim && spos != 0 && !printed[spos-1]) {
|
if (need_odelim && spos != 0 && !printed[spos-1]) {
|
||||||
|
@ -129,8 +130,10 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
|
||||||
}
|
}
|
||||||
putchar(line[spos]);
|
putchar(line[spos]);
|
||||||
}
|
}
|
||||||
if (++spos > cut_list[cl_pos].endpos) {
|
spos++;
|
||||||
need_odelim = (odelim && odelim[0]); /* will print OSEP (if not empty) */
|
if (spos > cut_list[cl_pos].endpos) {
|
||||||
|
/* will print OSEP (if not empty) */
|
||||||
|
need_odelim = (odelim && odelim[0]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -242,7 +245,8 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
/* Got delimiter */
|
/* Got delimiter */
|
||||||
if (dcount++ < cut_list[cl_pos].startpos) {
|
dcount++;
|
||||||
|
if (dcount <= cut_list[cl_pos].startpos) {
|
||||||
/* Not yet within range - loop */
|
/* Not yet within range - loop */
|
||||||
start = next;
|
start = next;
|
||||||
continue;
|
continue;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue