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

Re: Feature request: ${(l[-3][0])var} to do left padding *without truncation*



2024-08-03 14:28:03 -0700, Bart Schaefer:
> On Sat, Aug 3, 2024 at 1:42 PM Stephane Chazelas <stephane@xxxxxxxxxxxx> wrote:
> >
> > And in $var:F[3]s/X/Y/ to repeat the substitution 3 times. This time no check
> > for negative values.
> On Sat, Aug 3, 2024 at 1:53 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> >
> > A closer examination of get_intarg() indicates that a negative return
> > means a parse error

Ah yes, sorry, I missed the point about get_intarg returning -1
upon error.

> This may mean that the :F case is hiding a bug.

Yes, negative values are handled like "f" (repeat as long as it
changes something), but get_intarg() only returns negative upon
error.

So for instance a=a; echo $a:F[1-]s/a/aa/ outputs an error but
runs into an infinite loop.

Maybe best would be to have get_intarg() return true/false and
the value by reference, and handle negative value on a case by
case basis in a more useful way:

${(I[-3])var/x/y} substitute the 3rd last occurrence
${(l[-3][0])var}  pad to length 3 without truncating
${(r[-3][0])var}  pad to length 3 without truncating
$var:F[-3]s/x/y/  error or treat like "f"

-- 
Stephane




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