diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2024-12-21 00:24:30 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2024-12-21 00:24:30 +0100 |
commit | 1ea89fa98a7c4b1b6924f136963c91caf5a5dccb (patch) | |
tree | ae5c07149d45c06bd57dcbe5949e384f95f27d4d | |
parent | b03f5162ac239c3743cfac246b3760b0020f4d23 (diff) | |
download | busybox-w32-1ea89fa98a7c4b1b6924f136963c91caf5a5dccb.tar.gz busybox-w32-1ea89fa98a7c4b1b6924f136963c91caf5a5dccb.tar.bz2 busybox-w32-1ea89fa98a7c4b1b6924f136963c91caf5a5dccb.zip |
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>
-rw-r--r-- | coreutils/cut.c | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/coreutils/cut.c b/coreutils/cut.c index 65e0e5c30..93b58b493 100644 --- a/coreutils/cut.c +++ b/coreutils/cut.c | |||
@@ -91,8 +91,9 @@ struct cut_range { | |||
91 | 91 | ||
92 | static int cmpfunc(const void *a, const void *b) | 92 | static int cmpfunc(const void *a, const void *b) |
93 | { | 93 | { |
94 | return (((struct cut_range *) a)->startpos - | 94 | const struct cut_range *aa = a; |
95 | ((struct cut_range *) b)->startpos); | 95 | const struct cut_range *bb = b; |
96 | return aa->startpos - bb->startpos; | ||
96 | } | 97 | } |
97 | 98 | ||
98 | #define END_OF_LIST(list_elem) ((list_elem).startpos == UINT_MAX) | 99 | #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, | |||
109 | while ((line = xmalloc_fgetline(file)) != NULL) { | 110 | while ((line = xmalloc_fgetline(file)) != NULL) { |
110 | 111 | ||
111 | /* set up a list so we can keep track of what's been printed */ | 112 | /* set up a list so we can keep track of what's been printed */ |
112 | int linelen = strlen(line); | 113 | unsigned linelen = strlen(line); |
113 | unsigned cl_pos = 0; | 114 | unsigned cl_pos = 0; |
114 | 115 | ||
115 | /* cut based on chars/bytes XXX: only works when sizeof(char) == byte */ | 116 | /* Cut based on chars/bytes XXX: only works when sizeof(char) == byte */ |
116 | if (option_mask32 & (OPT_CHAR | OPT_BYTE)) { | 117 | if (option_mask32 & (OPT_CHAR | OPT_BYTE)) { |
117 | char *printed = xzalloc(linelen + 1); | 118 | char *printed = xzalloc(linelen + 1); |
118 | int need_odelim = 0; | 119 | int need_odelim = 0; |
119 | 120 | ||
120 | /* print the chars specified in each cut list */ | 121 | /* print the chars specified in each cut list */ |
121 | for (; NOT_END_OF_LIST(cut_list[cl_pos]); cl_pos++) { | 122 | for (; NOT_END_OF_LIST(cut_list[cl_pos]); cl_pos++) { |
122 | unsigned spos; | 123 | unsigned spos = cut_list[cl_pos].startpos; |
123 | for (spos = cut_list[cl_pos].startpos; spos < linelen;) { | 124 | while (spos < linelen) { |
124 | if (!printed[spos]) { | 125 | if (!printed[spos]) { |
125 | printed[spos] = 'X'; | 126 | printed[spos] = 'X'; |
126 | if (need_odelim && spos != 0 && !printed[spos-1]) { | 127 | if (need_odelim && spos != 0 && !printed[spos-1]) { |
@@ -129,8 +130,10 @@ static void cut_file(FILE *file, const char *delim, const char *odelim, | |||
129 | } | 130 | } |
130 | putchar(line[spos]); | 131 | putchar(line[spos]); |
131 | } | 132 | } |
132 | if (++spos > cut_list[cl_pos].endpos) { | 133 | spos++; |
133 | need_odelim = (odelim && odelim[0]); /* will print OSEP (if not empty) */ | 134 | if (spos > cut_list[cl_pos].endpos) { |
135 | /* will print OSEP (if not empty) */ | ||
136 | need_odelim = (odelim && odelim[0]); | ||
134 | break; | 137 | break; |
135 | } | 138 | } |
136 | } | 139 | } |
@@ -242,7 +245,8 @@ static void cut_file(FILE *file, const char *delim, const char *odelim, | |||
242 | continue; | 245 | continue; |
243 | } | 246 | } |
244 | /* Got delimiter */ | 247 | /* Got delimiter */ |
245 | if (dcount++ < cut_list[cl_pos].startpos) { | 248 | dcount++; |
249 | if (dcount <= cut_list[cl_pos].startpos) { | ||
246 | /* Not yet within range - loop */ | 250 | /* Not yet within range - loop */ |
247 | start = next; | 251 | start = next; |
248 | continue; | 252 | continue; |