Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: PATCH: quote parameter expansion from GLOB_SUBST



Peter Stephenson wrote on Sun, May 10, 2015 at 00:44:44 +0100:
> +item(tt(b))(
> +Quote with backslashes only characters that are special to pattern
> +matching. This is useful when the contents of the variable are to be
> +tested using tt(GLOB_SUBST), including the tt(${~)var(...)tt(}) switch.
> +
> +Quoting using one of the tt(q) family of flags does not work
> +for this purpose since quotes are not stripped from non-pattern
> +characters by tt(GLOB_SUBST).  In other words,
> +
> +example(foo='a\ b'
> +[[ 'a b' = ${~foo} ]])
> +
> +fails, whereas
> +
> +example(foo='a\*b'
> +[[ 'a*b' = ${~foo} ]])
> +
> +succeeds.  The tt(b) flag ensures the correct quoting.
> +)

Maybe state here that '[[ $foo == ${~${(b)foo}} ]]' is always true (like
the code comments already state)?  That way the doc will have an example
of using the (b) modifier, not just an example of how using (q) doesn't
work :-)

Speaking of which, it's not immediately clear when reading the above
example that 'a\ b' and 'a\*b' are ${(q):-"a b"} and ${(q):-"a*b"}; if
that example remains, I would suggest stating that more explicitly.

So, overall:

  +Quoting using one of the tt(q) family of flags does not work
  +for this purpose since quotes are not stripped from non-pattern
  +characters by tt(GLOB_SUBST).  In other words,
  +
  +example(pattern=${(q)str}
  +[[ $str = ${~pattern} ]])
  +
  +works if tt($str) is tt('a*b') but not if it is tt('a b'), whereas
  +
  +example(pattern=${(b)str}
  +[[ $str = ${~pattern} ]])
  +
  +is always true for any possible value of tt($str).
  +)

Cheers,

Daniel

>      /*
> +     * Only quote pattern characters.
> +     * ${(b)foo} guarantees that ${~foo} matches the string
> +     * contained in foo.
> +     */



Messages sorted by: Reverse Date, Date, Thread, Author