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

Re: [PATHC] Fix handling of reversed slices in assignstrvalue



On Sun, Apr 12, 2026 at 4:33 PM Philippe Altherr
<philippe.altherr@xxxxxxxxx> wrote:
>
> Reversed slices (with end index < start index) are handled correctly in array assignments but not in string assignments:
>
> a=(1 2 3 4 5 6 7 8); a[7,2]=(X Y Z); echo a=$a
> s=12345678         ; s[7,2]=XYZ    ; echo s=$s
>
> Actual output:
> a=1 2 3 4 5 6 X Y Z 7 8
> s=123456XYZ345678
>
> Expected output:
> a=1 2 3 4 5 6 X Y Z 7 8
> s=123456XYZ78

As far as I know, this syntax isn't documented, and from testing, it
seems to work fine if you use [7,6] instead of [7,2]. That said, the
patch is pretty small. I did suggest at one point that we document the
'trick' of [1,0] to prepend, but it was shot down.

One thing that stands out when reviewing it is that the second hunk is
adding an "if" between an existing "if" and "else if", which feels a
little weird.

Also, now that I look at it more, I'm confused what the purpose of the
first hunk is? How does allowing the second index to run off the end
of the string help us here?

-- 
Mikael Magnusson




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