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

[PATHC] Fix handling of reversed slices in assignstrvalue



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

The following patch fixes this.

Fix handling of reversed slices in assignstrvalue

Philippe

diff --git a/Src/params.c b/Src/params.c
index 461e02acf..4498a57c5 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -2747,8 +2747,6 @@ assignstrvalue(Value v, char *val, int flags)
 		v->end += zlen;
 		if (v->end < 0) {
 		    v->end = 0;
-		} else if (v->end >= zlen) {
-		    v->end = zlen;
 		} else {
 #ifdef MULTIBYTE_SUPPORT
 		    if (isset(MULTIBYTE)) {
@@ -2761,6 +2759,8 @@ assignstrvalue(Value v, char *val, int flags)
 #endif
 		}
 	    }
+	    if (v->end < v->start)
+		v->end = v->start;
 	    else if (v->end > zlen)
 		v->end = zlen;
 
diff --git a/Test/A06assign.ztst b/Test/A06assign.ztst
index 9f779b9a8..af6f889cb 100644
--- a/Test/A06assign.ztst
+++ b/Test/A06assign.ztst
@@ -133,6 +133,12 @@
 >1 2 42 43 44 5
 >1 2 42 100 99 5
 
+ array=(1 2 3 4 5)
+ array[4,2]=(42 43 44)
+ print $array
+0:Replacement of slice with end index preceding start index
+>1 2 3 42 43 44 4 5
+
 # (subsection: append to array)
 
  array=( )
@@ -708,6 +714,12 @@
 0:overwrite [-2,-1] characters in short string
 >ax
 
+ a="abcdefgh"
+ a[6,3]="xyz"
+ print $a
+0:overwrite [6,3] characters in short string
+>abcdexyzfgh
+
  a="a"
  a[-1]="xx"
  print $a


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