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

Re: PATCH: bash-style substrings & subarrays

On Fri, 19 Nov 2010 10:01:45 -0800
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Nov 17,  4:54pm, Peter Stephenson wrote:
> }
> } This implements the ${NAME:OFFSET} and ${NAME:OFFSET:LENGTH} syntax.
> } This is basically for compatibility; we don't need the extra
> } functionality, but it's a syntax people are nowadays assuming they can
> } use.
> I'm wondering whether :OFFSET:LENGTH shouldn't always use KSH_ARRAYS
> semantics, or be a valid syntax only when KSH_ARRAYS is set?  If it's
> for compatibility with people who are assuming it works, those people
> are also going to assume it has zero-offset, aren't they?

I can see that if you interpret the word "OFFSET" literally it's
different from a subscript and you might interpret it as starting from 0
in any case.  I still think on balance consistency with normal
subscripting is preferable.  I don't think half-measures compatibility
with other shells is particularly useful, in the end is probably more
confusing when you find some things work the way you expect and some
things don't.  However, with a stress in the documentation on the fact
that it's an offset, not a subscript, I can see there's an argument for
the other way.

> There may be some error cases not yet caught:
> schaefer<508> foo=123456789
> schaefer<509> unset y x
> schaefer<510> echo ${foo:$y:$x} 
> zsh: bad math expression: illegal character: Ã
> That à looks like uninitialized memory garbage.

It's the fact that empty strings turn into Nularg. I should be tidying
up the string after the expansion.  After the patch empty expansions evaluate
to 0, consistent with the effect of 

% print $(( ))

Index: Src/subst.c
RCS file: /cvsroot/zsh/zsh/Src/subst.c,v
retrieving revision 1.110
diff -p -u -r1.110 subst.c
--- Src/subst.c	18 Nov 2010 13:57:19 -0000	1.110
+++ Src/subst.c	20 Nov 2010 20:58:22 -0000
@@ -1403,6 +1403,8 @@ check_colon_subscript(char *str, char **
     if (parsestr(str = dupstring(str)))
 	return NULL;
+    remnulargs(str);
+    untokenize(str);
     **endp = sav;
     return str;

Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/

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