diff options
author | Peter Stephenson <pws@users.sourceforge.net> | 2000-04-14 11:49:31 +0000 |
---|---|---|
committer | Peter Stephenson <pws@users.sourceforge.net> | 2000-04-14 11:49:31 +0000 |
commit | 2295c59fcb4f1e31ef05555318bf3b3b4a2df8f9 (patch) | |
tree | 564d732962ee8faa96689a21f6c85d5856239986 | |
parent | 3f114b3237c9d46f27cb13bec1e2d7a84e5bfead (diff) | |
download | zsh-2295c59fcb4f1e31ef05555318bf3b3b4a2df8f9.tar.gz zsh-2295c59fcb4f1e31ef05555318bf3b3b4a2df8f9.tar.xz zsh-2295c59fcb4f1e31ef05555318bf3b3b4a2df8f9.zip |
10756: fix (#s) and (#e) for parameter substitution forms
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | Doc/Zsh/expn.yo | 10 | ||||
-rw-r--r-- | Src/glob.c | 47 | ||||
-rw-r--r-- | Src/pattern.c | 15 | ||||
-rw-r--r-- | Src/zsh.h | 2 |
5 files changed, 79 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog index ed8e86a5e..78fbba1ed 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2000-04-14 Peter Stephenson <pws@cambridgesiliconradio.com> + + * 10756: Doc/Zsh/expn.yo, Src/glob.c, Src/pattern.c, Src/zsh.h: + fix (#s) and (#e) to work in allerlei parameter substitutions; + fix potential problem with (#e) in exclusions; note things in + manual which should work. + 2000-04-13 Clint Adams <schizo@debian.org> * 10748: configure.in: set MAXJOB to whatever you want. diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo index 172949176..bac3c2094 100644 --- a/Doc/Zsh/expn.yo +++ b/Doc/Zsh/expn.yo @@ -1309,6 +1309,16 @@ are useful for matching path segments in patterns. For example, `tt(*((#s)|/)test((#e)|/)*)' matches a path segment `tt(test)' in any of the following strings: tt(test), tt(test/at/start), tt(at/end/test), tt(in/test/middle). + +Another use is in parameter substitution; for example +`tt(${array/(#s)A*Z(#e)})' will remove only elements of an array which +match the complete pattern `tt(A*Z)'. There are other ways of performing +many operations of this type, however the combination of the substitution +operations `tt(/)' and `tt(//)' with the `tt((#s))' and `tt((#e))' flags +provides a single simple and memorable method. + +Note that assertions of the form `tt((^(#s)))' also work, i.e. match +anywhere except at the start of the string. ) enditem() diff --git a/Src/glob.c b/Src/glob.c index 623a50706..828f703d5 100644 --- a/Src/glob.c +++ b/Src/glob.c @@ -2056,6 +2056,39 @@ getmatcharr(char ***ap, char *pat, int fl, int n, char *replstr) } /**/ +static void +set_pat_start(Patprog p, int offs) +{ + /* + * If we are messing around with the test string by advancing up + * it from the start, we need to tell the pattern matcher that + * a start-of-string assertion, i.e. (#s), should fail. Hence + * we test whether the offset of the real start of string from + * the actual start, passed as offs, is zero. + */ + if (offs) + p->flags |= PAT_NOTSTART; + else + p->flags &= ~PAT_NOTSTART; +} + +/**/ +static void +set_pat_end(Patprog p, char null_me) +{ + /* + * If we are messing around with the string by shortening it at the + * tail, we need to tell the pattern matcher that an end-of-string + * assertion, i.e. (#e), should fail. Hence we test whether + * the character null_me about to be zapped is or is not already a null. + */ + if (null_me) + p->flags |= PAT_NOTEND; + else + p->flags &= ~PAT_NOTEND; +} + +/**/ static int igetmatch(char **sp, Patprog p, int fl, int n, char *replstr) { @@ -2068,6 +2101,9 @@ igetmatch(char **sp, Patprog p, int fl, int n, char *replstr) if (p->mustoff && !strstr((char *)s, (char *)p + p->mustoff)) matched = 0; + /* in case we used the prog before... */ + p->flags &= ~(PAT_NOTSTART|PAT_NOTEND); + if (fl & SUB_ALL) { i = matched && pattry(p, s); *sp = get_match_ret(*sp, 0, i ? l : 0, fl, i ? replstr : 0); @@ -2092,6 +2128,7 @@ igetmatch(char **sp, Patprog p, int fl, int n, char *replstr) */ for (t = s; t < mpos; METAINC(t)) { sav = *t; + set_pat_end(p, sav); *t = '\0'; if (pattry(p, s)) { mpos = patinput; @@ -2112,6 +2149,7 @@ igetmatch(char **sp, Patprog p, int fl, int n, char *replstr) * There's no optimization here. */ patoffset = ml; for (t = s + l; t >= s; t--, patoffset--) { + set_pat_start(p, t-s); if (pattry(p, t)) { *sp = get_match_ret(*sp, t - s, l, fl, replstr); patoffset = 0; @@ -2128,6 +2166,7 @@ igetmatch(char **sp, Patprog p, int fl, int n, char *replstr) * move forward along string until we get a match. * * Again there's no optimisation. */ for (i = 0, t = s; i < l; i++, t++, patoffset++) { + set_pat_start(p, t-s); if (pattry(p, t)) { *sp = get_match_ret(*sp, i, l, fl, replstr); patoffset = 0; @@ -2141,6 +2180,7 @@ igetmatch(char **sp, Patprog p, int fl, int n, char *replstr) case SUB_SUBSTR: /* Smallest at start, but matching substrings. */ + set_pat_start(p, l); if (!(fl & SUB_GLOBAL) && pattry(p, s + l) && !--n) { *sp = get_match_ret(*sp, 0, 0, fl, replstr); return 1; @@ -2155,12 +2195,14 @@ igetmatch(char **sp, Patprog p, int fl, int n, char *replstr) matched = 0; for (; t < s + l; t++, patoffset++) { /* Find the longest match from this position. */ + set_pat_start(p, t-s); if (pattry(p, t) && patinput > t) { char *mpos = patinput; if (!(fl & SUB_LONG) && !(p->flags & PAT_PURES)) { char *ptr; for (ptr = t; ptr < mpos; METAINC(ptr)) { sav = *ptr; + set_pat_end(p, sav); *ptr = '\0'; if (pattry(p, t)) { mpos = patinput; @@ -2209,6 +2251,7 @@ igetmatch(char **sp, Patprog p, int fl, int n, char *replstr) * at the start. Goodness knows if this is a good idea * with global substitution, so it doesn't happen. */ + set_pat_start(p, l); if ((fl & (SUB_LONG|SUB_GLOBAL)) == SUB_LONG && pattry(p, s + l) && !--n) { *sp = get_match_ret(*sp, 0, 0, fl, replstr); @@ -2219,6 +2262,7 @@ igetmatch(char **sp, Patprog p, int fl, int n, char *replstr) case (SUB_END|SUB_SUBSTR): /* Shortest at end with substrings */ patoffset = ml; + set_pat_start(p, l); if (pattry(p, s + l) && !--n) { *sp = get_match_ret(*sp, l, l, fl, replstr); patoffset = 0; @@ -2230,6 +2274,7 @@ igetmatch(char **sp, Patprog p, int fl, int n, char *replstr) for (t = s + l - 1; t >= s; t--, patoffset--) { if (t > s && t[-1] == Meta) t--; + set_pat_start(p, t-s); if (pattry(p, t) && patinput > t && !--n) { /* Found the longest match */ char *mpos = patinput; @@ -2237,6 +2282,7 @@ igetmatch(char **sp, Patprog p, int fl, int n, char *replstr) char *ptr; for (ptr = t; ptr < mpos; METAINC(ptr)) { sav = *ptr; + set_pat_end(p, sav); *ptr = '\0'; if (pattry(p, t)) { mpos = patinput; @@ -2252,6 +2298,7 @@ igetmatch(char **sp, Patprog p, int fl, int n, char *replstr) } } patoffset = ml; + set_pat_start(p, l); if ((fl & SUB_LONG) && pattry(p, s + l) && !--n) { *sp = get_match_ret(*sp, l, l, fl, replstr); patoffset = 0; diff --git a/Src/pattern.c b/Src/pattern.c index 1c90f72a1..eef88ac6e 100644 --- a/Src/pattern.c +++ b/Src/pattern.c @@ -1757,6 +1757,9 @@ patmatch(Upat prog) * over, that doesn't matter: we should fail anyway. * The pointer also tells us where the asserted * pattern matched for use by the exclusion. + * + * P.S. in case you were wondering, this code + * is horrible. */ Upat syncstrp; unsigned char *oldsyncstr; @@ -1782,6 +1785,7 @@ patmatch(Upat prog) char savchar, *testptr; char *savpatinstart = patinstart; int savforce = forceerrs, savpatinlen = patinlen; + int savpatflags = patflags; forceerrs = -1; savglobdots = globdots; matchederrs = errsfound; @@ -1800,6 +1804,12 @@ patmatch(Upat prog) testptr = patinstart + (syncpt - syncstrp->p); DPUTS(testptr > matchpt, "BUG: EXCSYNC failed"); savchar = *testptr; + /* + * If this isn't really the end of the string, + * remember this for the (#e) assertion. + */ + if (savchar) + patflags |= PAT_NOTEND; *testptr = '\0'; next = PATNEXT(scan); while (next && P_ISEXCLUDE(next)) { @@ -1848,6 +1858,7 @@ patmatch(Upat prog) next = PATNEXT(next); } *testptr = savchar; + patflags = savpatflags; globdots = savglobdots; forceerrs = savforce; if (ret) @@ -2015,11 +2026,11 @@ patmatch(Upat prog) */ return 0; case P_ISSTART: - if (patinput != patinstart) + if (patinput != patinstart || (patflags & PAT_NOTSTART)) fail = 1; break; case P_ISEND: - if (*patinput) + if (*patinput || (patflags & PAT_NOTEND)) fail = 1; break; case P_END: diff --git a/Src/zsh.h b/Src/zsh.h index 306cc82e5..5c7dc27ad 100644 --- a/Src/zsh.h +++ b/Src/zsh.h @@ -1024,6 +1024,8 @@ struct patprog { #define PAT_STATIC 0x0040 /* Don't copy pattern to heap as per default */ #define PAT_SCAN 0x0080 /* Scanning, so don't try must-match test */ #define PAT_ZDUP 0x0100 /* Copy pattern in real memory */ +#define PAT_NOTSTART 0x0200 /* Start of string is not real start */ +#define PAT_NOTEND 0x0400 /* End of string is not real end */ /* Globbing flags: lower 8 bits gives approx count */ #define GF_LCMATCHUC 0x0100 |