* Re: Tip of the day: (@)...[(R)...] is equal to (@M)...:#... and faster by 18-21%
2016-10-21 8:59 ` Tip of the day: (@)...[(R)...] is equal to (@M)...:#... and faster by 18-21% psprint
@ 2016-10-21 16:20 ` Bart Schaefer
0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2016-10-21 16:20 UTC (permalink / raw)
To: zsh-users
On Oct 21, 8:59am, <psprint@yahoo.com> wrote:
}
} found=( "${(@M)history:#(#i)*$~search_pattern*}" )
}
} can be replaced by:
}
} found=( "${(@)history[(R)(#i)*$~search_pattern*]}" )
}
} I wonder if there are there any pitfalls in such replacement?
There are some differences in the way the pattern may be interpreted
when it appears inside a subscript expression. There is an entire
section of the documentation (15.2.4 in "info" or HTML docs) devoted
to explaining this. One paragraph in particular applies here:
A last detail must be considered when reverse subscripting is
performed. Parameters appearing in the subscript expression are
first expanded and then the complete expression is interpreted
as a pattern. This has two effects: first, parameters behave as
if GLOB_SUBST were on (and it cannot be turned off); second,
backslashes are interpreted twice, once when parsing the array
subscript and again when parsing the pattern. In a reverse
subscript, it's necessary to use _four_ backslashes to cause a
single backslash to match literally in the pattern. For complex
patterns, it is often easiest to assign the desired pattern to a
parameter and then refer to that parameter in the subscript, because
then the backslashes, brackets, parentheses, etc., are seen only
when the complete expression is converted to a pattern. To match
the value of a parameter literally in a reverse subscript, rather
than as a pattern, use `${(q)NAME}' to quote the expanded value.
So in your example you don't need "$~search_pattern", the expansion is
always interpreted as a pattern. I think the literal match advice in
the final sentence could preferentially be ${(b)...} nowadays; there
are a couple of things about the array subscripting doc that might
need update.
^ permalink raw reply [flat|nested] 2+ messages in thread