Skip to content

Commit 5f80f9d

Browse files
committed
Better naming in count_words()
1 parent 5359e3d commit 5f80f9d

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

src/file_operations.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,7 @@ is_valid_filename(const char *name)
679679
int only_dots = 1;
680680
const char *s = name;
681681
while (*s) {
682-
/* Contains control characters (being not UTF-8 leading nor
683-
* continuation bytes) */
682+
/* Contains control characters (being not UTF-8 bytes) */
684683
if (*s < ' ' && !IS_UTF8_CHAR(*s))
685684
return print_val_err(name, UNSAFE_CONTROL);
686685

src/strings.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -142,34 +142,34 @@ count_chars(const char *restrict s, const char c)
142142
size_t
143143
count_words(size_t *start_word, size_t *full_word)
144144
{
145-
size_t words = 0, w = 0, first_non_space = 0;
146-
char q = 0;
145+
size_t words = 0, i = 0, first_non_space = 0;
146+
char quote = 0;
147147
char *b = rl_line_buffer;
148148

149-
for (; b[w]; w++) {
149+
for (; b[i]; i++) {
150150
/* Keep track of open quotes. */
151-
if (b[w] == '\'' || b[w] == '"')
152-
q = q == b[w] ? 0 : b[w];
151+
if (b[i] == '\'' || b[i] == '"')
152+
quote = quote == b[i] ? 0 : b[i];
153153

154-
if (!first_non_space && b[w] != ' ') {
154+
if (first_non_space == 0 && b[i] != ' ') {
155155
words = 1;
156-
*start_word = w;
156+
*start_word = i;
157157
first_non_space = 1;
158158
continue;
159159
}
160160

161-
if (w > 0 && b[w] == ' ' && b[w - 1] != '\\') {
162-
if (!*full_word && b[w - 1] != '|'
163-
&& b[w - 1] != ';' && b[w - 1] != '&')
164-
*full_word = w; /* Index of the end of the first full word (cmd). */
165-
if (b[w + 1] && b[w + 1] != ' ')
161+
if (i > 0 && b[i] == ' ' && b[i - 1] != '\\') {
162+
if (!*full_word && b[i - 1] != '|'
163+
&& b[i - 1] != ';' && b[i - 1] != '&')
164+
*full_word = i; /* Index of the end of the first full word (cmd). */
165+
if (b[i + 1] && b[i + 1] != ' ')
166166
words++;
167167
}
168168

169169
/* If a process separator char is found, reset variables so that we
170170
* can start counting again for the new command. */
171-
if (!q && cur_color != hq_c && w > 0 && b[w - 1] != '\\'
172-
&& ((b[w] == '&' && b[w - 1] == '&') || b[w] == '|' || b[w] == ';'))
171+
if (quote == 0 && cur_color != hq_c && i > 0 && b[i - 1] != '\\'
172+
&& ((b[i] == '&' && b[i - 1] == '&') || b[i] == '|' || b[i] == ';'))
173173
words = first_non_space = *full_word = 0;
174174
}
175175

@@ -227,11 +227,9 @@ xstrcasechr(char *s, char c)
227227

228228
const char uc = (char)TOUPPER(c);
229229
while (*s) {
230-
if (TOUPPER(*s) != uc) {
231-
s++;
232-
continue;
233-
}
234-
return s;
230+
if (TOUPPER(*s) == uc)
231+
return s;
232+
s++;
235233
}
236234

237235
return (char *)NULL;

0 commit comments

Comments
 (0)