From 2656b8c2936a4847eac96537f7e04b2d599cd880 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Wed, 14 Sep 2016 03:38:34 +0000 Subject: 39310/0001: internals: match_str: Document some local variables. See 39123. --- Src/Zle/compmatch.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) (limited to 'Src/Zle/compmatch.c') diff --git a/Src/Zle/compmatch.c b/Src/Zle/compmatch.c index 0e41ac3a5..183eb6803 100644 --- a/Src/Zle/compmatch.c +++ b/Src/Zle/compmatch.c @@ -593,9 +593,63 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, continue; if (mp->wlen < 0) { - int both, loff, aoff, llen, alen, zoff, moff, ct, ict, aol; + /* + * 1 iff the anchor and the word are on the same side of + * the line pattern; that is: if either + * - the anchor is on the left and we are matching + * a prefix; or + * - the anchor is on the right and we are matching + * a suffix. + */ + int both; + /* + * Offset from the line pattern pointer ('l') to the start + * of the line pattern. + */ + int loff; + /* + * Offset from the line pattern pointer ('l') to the start + * of the anchor. + */ + int aoff; + /* + * The length of the line pattern. + */ + int llen; + /* + * The length of the anchor. + * + * SEE: ap; aol, aop + */ + int alen; + /* + * ### These two are related: they're set symmetrically. + */ + int zoff, moff; + /* + * ### These two are related. + */ + int ct, ict; + /* + * The length of the OTHER anchor: the left anchor when + * we're anchored on the right, and of the right anchor + * when we're anchored on the left. + */ + int aol; + /* + * LOST: Documentation comment. Last seen 10 years ago in + * the temporal lobe. Reward promised for its safe return. + * Contact zsh-workers@zsh.org. + */ char *tp, savl = '\0', savw; - Cpattern ap, aop; + /* + * The anchor on this end. + */ + Cpattern ap; + /* + * The anchor on the other end. + */ + Cpattern aop; /* This is for `*' patterns, first initialise some * local variables. */ -- cgit v1.2.3 From 14989cea4ad5fcf17f4a967a804146acf67c4439 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Wed, 14 Sep 2016 03:38:34 +0000 Subject: 39310/0002: internals: match_str: Simplify by removing 'zoff'. 'zoff' was only used within 'if (sfx)' blocks, in which case it was initialized to 'alen', so simply s/zoff/alen/g. 'alen' is not const but it first changes on line 794, after the last use of 'zoff'. --- ChangeLog | 3 +++ Src/Zle/compmatch.c | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) (limited to 'Src/Zle/compmatch.c') diff --git a/ChangeLog b/ChangeLog index c62863036..4b5f2e639 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2016-09-16 Daniel Shahaf + * 39310/0002: Src/Zle/compmatch.c: internals: match_str: + Simplify by removing 'zoff'. + * 39310/0001: Src/Zle/compmatch.c: internals: match_str: Document some local variables. See 39123. diff --git a/Src/Zle/compmatch.c b/Src/Zle/compmatch.c index 183eb6803..82cda6128 100644 --- a/Src/Zle/compmatch.c +++ b/Src/Zle/compmatch.c @@ -623,9 +623,9 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, */ int alen; /* - * ### These two are related: they're set symmetrically. + * ### Related to 'zoff', which was removed in 2016. */ - int zoff, moff; + int moff; /* * ### These two are related. */ @@ -665,14 +665,14 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, continue; if (mp->flags & CMF_LEFT) { - ap = mp->left; zoff = 0; moff = alen; aop = mp->right; + ap = mp->left; moff = alen; aop = mp->right; if (sfx) { both = 0; loff = -llen; aoff = -(llen + alen); } else { both = 1; loff = alen; aoff = 0; } } else { - ap = mp->right; zoff = alen; moff = 0; aop = mp->left; + ap = mp->right; moff = 0; aop = mp->left; if (sfx) { both = 1; loff = -(llen + alen); aoff = -alen; } else { @@ -698,8 +698,8 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, /* Fine, now we call ourselves recursively to find the * string matched by the `*'. */ - if (sfx && (savl = l[-(llen + zoff)])) - l[-(llen + zoff)] = '\0'; + if (sfx && (savl = l[-(llen + alen)])) + l[-(llen + alen)] = '\0'; for (t = 0, tp = w, ct = 0, ict = lw - alen + 1; ict; tp += add, ct++, ict--) { @@ -721,12 +721,12 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, !match_parts(l + aoff , tp - moff, alen, part)) break; if (sfx) { - if ((savw = tp[-zoff])) - tp[-zoff] = '\0'; + if ((savw = tp[-alen])) + tp[-alen] = '\0'; t = match_str(l - ll, w - lw, NULL, 0, NULL, 1, 2, part); if (savw) - tp[-zoff] = savw; + tp[-alen] = savw; } else t = match_str(l + llen + moff, tp + moff, NULL, 0, NULL, 0, 1, part); @@ -736,7 +736,7 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, } ict = ct; if (sfx && savl) - l[-(llen + zoff)] = savl; + l[-(llen + alen)] = savl; /* Have we found a position in w where the rest of l * matches? */ -- cgit v1.2.3 From 59aabe483c321f9122ea9998b0f0797eabe2aa6c Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Wed, 14 Sep 2016 03:38:34 +0000 Subject: 39310/0003: internals: match_str: Document 'savw'. Avoid magic number. All callees checked to ensure that they only check that parameter for nonzeroness. --- ChangeLog | 3 +++ Src/Zle/compmatch.c | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) (limited to 'Src/Zle/compmatch.c') diff --git a/ChangeLog b/ChangeLog index 4b5f2e639..c3ed089ba 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2016-09-16 Daniel Shahaf + * 39310/0003: Src/Zle/compmatch.c: internals: match_str: Document + 'savw'. Avoid magic number. + * 39310/0002: Src/Zle/compmatch.c: internals: match_str: Simplify by removing 'zoff'. diff --git a/Src/Zle/compmatch.c b/Src/Zle/compmatch.c index 82cda6128..a3988d542 100644 --- a/Src/Zle/compmatch.c +++ b/Src/Zle/compmatch.c @@ -641,7 +641,7 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, * the temporal lobe. Reward promised for its safe return. * Contact zsh-workers@zsh.org. */ - char *tp, savl = '\0', savw; + char *tp, savl = '\0'; /* * The anchor on this end. */ @@ -721,15 +721,18 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, !match_parts(l + aoff , tp - moff, alen, part)) break; if (sfx) { + /* Call ourselves recursively with the + * anchor removed. */ + char savw; if ((savw = tp[-alen])) tp[-alen] = '\0'; t = match_str(l - ll, w - lw, - NULL, 0, NULL, 1, 2, part); + NULL, 0, NULL, sfx, 2, part); if (savw) tp[-alen] = savw; } else t = match_str(l + llen + moff, tp + moff, - NULL, 0, NULL, 0, 1, part); + NULL, 0, NULL, sfx, 1, part); if (t || (mp->wlen == -1 && !both)) break; } -- cgit v1.2.3 From a08f8a4c2925c421d9e74604fa720034c5440376 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Wed, 14 Sep 2016 03:38:34 +0000 Subject: 39310/0004: internals: match_str: Document 'savl'. --- ChangeLog | 3 +++ Src/Zle/compmatch.c | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 'Src/Zle/compmatch.c') diff --git a/ChangeLog b/ChangeLog index c3ed089ba..9fddd78d9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2016-09-16 Daniel Shahaf + * 39310/0004: Src/Zle/compmatch.c: internals: match_str: Document + 'savl'. + * 39310/0003: Src/Zle/compmatch.c: internals: match_str: Document 'savw'. Avoid magic number. diff --git a/Src/Zle/compmatch.c b/Src/Zle/compmatch.c index a3988d542..d32d9f1e1 100644 --- a/Src/Zle/compmatch.c +++ b/Src/Zle/compmatch.c @@ -641,7 +641,22 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, * the temporal lobe. Reward promised for its safe return. * Contact zsh-workers@zsh.org. */ - char *tp, savl = '\0'; + char *tp; + /* + * Temporary variable. Used as temporary storage for a + * + * { + * () { + * local foo="$foo" + * foo[1]=bar + * ... + * } + * (use original $foo here) + * } + * + * operation. Similar to savw. + */ + char savl; /* * The anchor on this end. */ -- cgit v1.2.3 From c5f165a525dfcc3521f5e11cfaf023c632a7b131 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Wed, 14 Sep 2016 03:38:34 +0000 Subject: 39310/0005: internals: match_str: Constify some local variables. --- ChangeLog | 3 +++ Src/Zle/compmatch.c | 12 ++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) (limited to 'Src/Zle/compmatch.c') diff --git a/ChangeLog b/ChangeLog index 9fddd78d9..617877541 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2016-09-16 Daniel Shahaf + * 39310/0005: Src/Zle/compmatch.c: internals: match_str: + Constify some local variables. + * 39310/0004: Src/Zle/compmatch.c: internals: match_str: Document 'savl'. diff --git a/Src/Zle/compmatch.c b/Src/Zle/compmatch.c index d32d9f1e1..5587bf37f 100644 --- a/Src/Zle/compmatch.c +++ b/Src/Zle/compmatch.c @@ -498,14 +498,17 @@ add_match_sub(Cmatcher m, char *l, int ll, char *w, int wl) /**/ int match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, - int sfx, int test, int part) + const int sfx, int test, int part) { int ll = strlen(l), lw = strlen(w), oll = ll, olw = lw, exact = 0, wexact = 0; - int il = 0, iw = 0, t, ind, add, he = 0, bpc, obc = bc, bslash; + int il = 0, iw = 0, t, he = 0, bpc, bslash; char *ow; - Cmlist ms; + Cmlist ms; /* loop variable */ Cmatcher mp, lm = NULL; Brinfo bp = NULL; + const int obc = bc; + const int ind = (sfx ? -1 : 0); + const int add = (sfx ? -1 : 1); if (!test) { start_match(); @@ -516,9 +519,6 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, if (sfx) { l += ll; w += lw; - ind = -1; add = -1; - } else { - ind = 0; add = 1; } /* ow will always point to the beginning (or end) of that sub-string * in w that wasn't put in the match-variables yet. */ -- cgit v1.2.3 From 09a6e96cc02348c13d2f17dae571b818cccbb631 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Wed, 14 Sep 2016 03:38:34 +0000 Subject: 39310/0006: internals: match_str: Downscope local variable 'bpc'. --- ChangeLog | 3 +++ Src/Zle/compmatch.c | 10 +++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) (limited to 'Src/Zle/compmatch.c') diff --git a/ChangeLog b/ChangeLog index 617877541..dad1f92b6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2016-09-16 Daniel Shahaf + * 39310/0006: Src/Zle/compmatch.c: internals: match_str: + Downscope local variable 'bpc'. + * 39310/0005: Src/Zle/compmatch.c: internals: match_str: Constify some local variables. diff --git a/Src/Zle/compmatch.c b/Src/Zle/compmatch.c index 5587bf37f..d007b14cc 100644 --- a/Src/Zle/compmatch.c +++ b/Src/Zle/compmatch.c @@ -501,7 +501,7 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, const int sfx, int test, int part) { int ll = strlen(l), lw = strlen(w), oll = ll, olw = lw, exact = 0, wexact = 0; - int il = 0, iw = 0, t, he = 0, bpc, bslash; + int il = 0, iw = 0, t, he = 0, bslash; char *ow; Cmlist ms; /* loop variable */ Cmatcher mp, lm = NULL; @@ -824,12 +824,14 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, bc += llen; exact = 0; - if (!test) + if (!test) { + int bpc; while (bp && bc >= (bpc = (useqbr ? bp->qpos : bp->pos))) { bp->curpos = matchbufadded + bpc - bc + obc; bp = bp->next; } + } ow = w; if (!llen && !alen) { @@ -947,12 +949,14 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, bc += mp->llen; exact = 0; - if (!test) + if (!test) { + int bpc; while (bp && bc >= (bpc = (useqbr ? bp->qpos : bp->pos))) { bp->curpos = matchbufadded + bpc - bc + obc; bp = bp->next; } + } ow = w; lm = NULL; he = 0; -- cgit v1.2.3 From 31665068c12a0df574d490596f166886d6172405 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Wed, 14 Sep 2016 03:38:35 +0000 Subject: 39310/0007: internals: match_str: Rename and constify local variables 'oll', 'olw'. --- ChangeLog | 3 +++ Src/Zle/compmatch.c | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'Src/Zle/compmatch.c') diff --git a/ChangeLog b/ChangeLog index dad1f92b6..d7ff75b86 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2016-09-16 Daniel Shahaf + * 39310/0007: Src/Zle/compmatch.c: internals: match_str: Rename + and constify local variables 'oll', 'olw'. + * 39310/0006: Src/Zle/compmatch.c: internals: match_str: Downscope local variable 'bpc'. diff --git a/Src/Zle/compmatch.c b/Src/Zle/compmatch.c index d007b14cc..f670de781 100644 --- a/Src/Zle/compmatch.c +++ b/Src/Zle/compmatch.c @@ -500,7 +500,7 @@ int match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, const int sfx, int test, int part) { - int ll = strlen(l), lw = strlen(w), oll = ll, olw = lw, exact = 0, wexact = 0; + int ll = strlen(l), lw = strlen(w), exact = 0, wexact = 0; int il = 0, iw = 0, t, he = 0, bslash; char *ow; Cmlist ms; /* loop variable */ @@ -509,6 +509,7 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, const int obc = bc; const int ind = (sfx ? -1 : 0); const int add = (sfx ? -1 : 1); + const int original_ll = ll, original_lw = lw; if (!test) { start_match(); @@ -585,7 +586,7 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, for (mp = ms->matcher; mp; mp = mp->next) { t = 1; if ((lm && lm == mp) || - ((oll == ll || olw == lw) && + ((original_ll == ll || original_lw == lw) && (test == 1 || (test && !mp->left && !mp->right)) && mp->wlen < 0)) /* If we were called recursively, don't use `*' patterns -- cgit v1.2.3 From f37fa9293fa128e657d4e124ac1aa25086f65847 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Wed, 14 Sep 2016 03:38:35 +0000 Subject: 39310/0008: internals: match_str: Document several local variables. --- ChangeLog | 3 +++ Src/Zle/compmatch.c | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'Src/Zle/compmatch.c') diff --git a/ChangeLog b/ChangeLog index d7ff75b86..f1d2c02d7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2016-09-16 Daniel Shahaf + * 39310/0008: Src/Zle/compmatch.c: internals: match_str: + Document several local variables. + * 39310/0007: Src/Zle/compmatch.c: internals: match_str: Rename and constify local variables 'oll', 'olw'. diff --git a/Src/Zle/compmatch.c b/Src/Zle/compmatch.c index f670de781..0c6270dee 100644 --- a/Src/Zle/compmatch.c +++ b/Src/Zle/compmatch.c @@ -500,8 +500,16 @@ int match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, const int sfx, int test, int part) { - int ll = strlen(l), lw = strlen(w), exact = 0, wexact = 0; - int il = 0, iw = 0, t, he = 0, bslash; + /* How many characters from the line string and from the word string are + * yet to be matched. */ + int ll = strlen(l), lw = strlen(w); + /* Number of characters from the line string and word string matched. */ + int il = 0, iw = 0; + /* How many characters were matched exactly in the line and in the word. */ + int exact = 0, wexact = 0; + int he = 0; + int bslash; + int t; char *ow; Cmlist ms; /* loop variable */ Cmatcher mp, lm = NULL; @@ -511,6 +519,8 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, const int add = (sfx ? -1 : 1); const int original_ll = ll, original_lw = lw; + /* INVARIANT: il+ll == original_ll; iw+lw == original_lw */ + if (!test) { start_match(); bp = *bpp; @@ -629,6 +639,10 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, int moff; /* * ### These two are related. + * + * ### They may have a relation similar to that of lw/iw + * ### (q.v.), at least during the 'for' loop. They may be + * ### overloaded/repurposed after it. */ int ct, ict; /* -- cgit v1.2.3 From a182afe2f2a8c76c533b199c7da0c8edc6ccc5a5 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Wed, 14 Sep 2016 03:38:35 +0000 Subject: 39310/0009: internals: match_str: Downscope local variable 't'. Remove needless initialization (it is written to again before it is ever read). Note there was another 't' variable at the end of the function that shadowed the int 't'. --- ChangeLog | 3 +++ Src/Zle/compmatch.c | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'Src/Zle/compmatch.c') diff --git a/ChangeLog b/ChangeLog index f1d2c02d7..b839a4468 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2016-09-16 Daniel Shahaf + * 39310/0009: Src/Zle/compmatch.c: internals: match_str: + Downscope local variable 't'. + * 39310/0008: Src/Zle/compmatch.c: internals: match_str: Document several local variables. diff --git a/Src/Zle/compmatch.c b/Src/Zle/compmatch.c index 0c6270dee..fde7010a9 100644 --- a/Src/Zle/compmatch.c +++ b/Src/Zle/compmatch.c @@ -509,7 +509,6 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, int exact = 0, wexact = 0; int he = 0; int bslash; - int t; char *ow; Cmlist ms; /* loop variable */ Cmatcher mp, lm = NULL; @@ -594,7 +593,6 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, /* First try the matchers. Err... see above. */ for (mp = NULL, ms = mstack; !mp && ms; ms = ms->next) { for (mp = ms->matcher; mp; mp = mp->next) { - t = 1; if ((lm && lm == mp) || ((original_ll == ll || original_lw == lw) && (test == 1 || (test && !mp->left && !mp->right)) && @@ -604,6 +602,12 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, continue; if (mp->wlen < 0) { + /* `*'-pattern. */ + /* + * Similar to the identically-named variable in the 'else' + * block. + */ + int t; /* * 1 iff the anchor and the word are on the same side of * the line pattern; that is: if either @@ -861,6 +865,11 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, break; } else if (ll >= mp->llen && lw >= mp->wlen) { /* Non-`*'-pattern. */ + /* + * Similar to the identically-named variable in the 'if' + * block. + */ + int t = 1; char *tl, *tw; int tll, tlw, til, tiw; -- cgit v1.2.3 From 2df02212b0528e7636ae9032536bdee4557fb690 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Wed, 14 Sep 2016 03:38:35 +0000 Subject: 39310/0010: internals: match_str: Simplify expression. In the first hunk we actually know that ind==0 since sfx==0, but keep it identical to the last hunk. Also add a comment (unrelated). --- ChangeLog | 3 +++ Src/Zle/compmatch.c | 10 +++++----- 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'Src/Zle/compmatch.c') diff --git a/ChangeLog b/ChangeLog index b839a4468..87b766b10 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2016-09-16 Daniel Shahaf + * 39310/0010: Src/Zle/compmatch.c: internals: match_str: + Simplify expression. + * 39310/0009: Src/Zle/compmatch.c: internals: match_str: Downscope local variable 't'. diff --git a/Src/Zle/compmatch.c b/Src/Zle/compmatch.c index fde7010a9..f82f00e1d 100644 --- a/Src/Zle/compmatch.c +++ b/Src/Zle/compmatch.c @@ -569,8 +569,7 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, bslash = 0; if (!sfx && lw && (!part || test) && (l[ind] == w[ind] || - (bslash = (lw > 1 && w[ind] == '\\' && - (ind ? (w[0] == l[0]) : (w[1] == l[0])))))) { + (bslash = (lw > 1 && w[ind] == '\\' && w[ind+1] == l[0])))) { /* No matcher could be used, but the strings have the same * character here, skip over it. */ l += add; w += (bslash ? (add + add) : add); @@ -855,8 +854,10 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, if (!llen && !alen) { lm = mp; - if (he) + if (he) { + /* Signal the outer for loop to continue. */ mp = NULL; + } else he = 1; } else { @@ -996,8 +997,7 @@ match_str(char *l, char *w, Brinfo *bpp, int bc, int *rwlp, bslash = 0; if ((!test || sfx) && lw && (l[ind] == w[ind] || - (bslash = (lw > 1 && w[ind] == '\\' && - (ind ? (w[0] == l[0]) : (w[1] == l[0])))))) { + (bslash = (lw > 1 && w[ind] == '\\' && w[ind+1] == l[0])))) { /* No matcher could be used, but the strings have the same * character here, skip over it. */ l += add; w += (bslash ? (add + add ) : add); -- cgit v1.2.3