• R/O
  • SSH
  • HTTPS

yash: Commit


Commit MetaInfo

Revision3882 (tree)
Time2018-09-19 00:55:24
Authormagicant

Log Message

Optimize candidate name check in alias substitution

This refactoring commit moves the
is_token_delimiter_char(buf->contents[j])
and
is_redir_fd(&buf->contents[i])
calls out of the substitute_alias_range function. These calls are
omitted in the psubstitute_alias function since the token has already
been delimited and categorized.

The psubstitute_alias function no longer uses the count_name_length
function to find the end of the token. Now the token passed from
psubstitute_alias to substitute_alias_range can contain non-alias-name
characters, but such a token will not be substituted anyway since no
alias can be defined with such a name.

The is_alias_name_char function is now defined with an internal linkage.

Change Summary

Incremental Difference

--- yash/branches/token_based_parser/alias.c (revision 3881)
+++ yash/branches/token_based_parser/alias.c (revision 3882)
@@ -67,6 +67,8 @@
6767 * substitution after another substitution that ends with a blank.
6868 * Alias list items are sorted in the order of `limitindex'. */
6969
70+static bool is_alias_name_char(wchar_t c)
71+ __attribute__((pure));
7072 static void free_alias(alias_T *alias);
7173 static inline void vfreealias(kvpair_T kv);
7274 static void define_alias(
@@ -103,7 +105,7 @@
103105 }
104106
105107 /* Returns true iff `c' is a character that can be used in an alias name. */
106-inline bool is_alias_name_char(wchar_t c)
108+bool is_alias_name_char(wchar_t c)
107109 {
108110 return !wcschr(L" \t\n=$<>\\'\"`;&|()#", c) && !iswblank(c);
109111 }
@@ -282,9 +284,14 @@
282284 bool substitute_alias(xwcsbuf_T *restrict buf, size_t i,
283285 aliaslist_T **restrict list, substaliasflags_T flags)
284286 {
287+ if (is_redir_fd(&buf->contents[i]))
288+ return false;
289+
285290 size_t j = i;
286291 while (is_alias_name_char(buf->contents[j]))
287292 j++;
293+ if (!is_token_delimiter_char(buf->contents[j]))
294+ return false;
288295 return substitute_alias_range(buf, i, j, list, flags);
289296 }
290297
@@ -307,16 +314,11 @@
307314 if (!(flags & AF_NONGLOBAL) && posixly_correct)
308315 return false;
309316
310- /* check if there is an alias name */
311317 if (i >= j)
312318 return false;
313319 if (flags & AF_NOEOF)
314320 if (j == buf->length)
315321 return false;
316- if (!is_token_delimiter_char(buf->contents[j]))
317- return false;
318- if (is_redir_fd(buf->contents + i))
319- return false;
320322
321323 alias_T *alias;
322324
--- yash/branches/token_based_parser/alias.h (revision 3881)
+++ yash/branches/token_based_parser/alias.h (revision 3882)
@@ -1,6 +1,6 @@
11 /* Yash: yet another shell */
22 /* alias.h: alias substitution */
3-/* (C) 2007-2017 magicant */
3+/* (C) 2007-2018 magicant */
44
55 /* This program is free software: you can redistribute it and/or modify
66 * it under the terms of the GNU General Public License as published by
@@ -32,8 +32,6 @@
3232 } substaliasflags_T;
3333
3434 extern void init_alias(void);
35-extern _Bool is_alias_name_char(wchar_t c)
36- __attribute__((pure));
3735 extern const wchar_t *get_alias_value(const wchar_t *aliasname)
3836 __attribute__((nonnull,pure));
3937 extern void destroy_aliaslist(struct aliaslist_T *list);
--- yash/branches/token_based_parser/parser.c (revision 3881)
+++ yash/branches/token_based_parser/parser.c (revision 3882)
@@ -1182,15 +1182,19 @@
11821182 }
11831183
11841184 /* Performs alias substitution with the given parse state. Proceeds to the
1185- * next token if substitution occurred. */
1185+ * next token if substitution occurred. This function does not substitute an
1186+ * IO_NUMBER token, but do a keyword token. */
11861187 bool psubstitute_alias(parsestate_T *ps, substaliasflags_T flags)
11871188 {
11881189 if (!ps->enable_alias)
11891190 return false;
1191+ if (ps->tokentype == TT_IO_NUMBER)
1192+ return false;
1193+ if (!is_single_string_word(ps->token))
1194+ return false;
11901195
1191- size_t len = count_name_length(ps, is_alias_name_char);
11921196 bool substituted = substitute_alias_range(
1193- &ps->src, ps->index, ps->index + len, &ps->aliases, flags);
1197+ &ps->src, ps->index, ps->next_index, &ps->aliases, flags);
11941198 if (substituted) {
11951199 /* parse the result of the substitution. */
11961200 ps->next_index = ps->index;
Show on old repository browser