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

Re: Avoiding empty element when splitting NUL-separated string from command substitution



On Tue, 16 Aug 2011 14:13:58 +0200
Nikolai Weibull <now@xxxxxxxx> wrote:
> % b=(${(0)"$(print -n)"})
> % echo $#b
> 1
> 
> How do I avoid b containing an empty element in this case?  That is,
> when the command substitution generated no output.
> 
> (The $(print -n) is, obviously, just an example.  It can be the
> equivalent of, for example, $(print -n $'a\0\b\0c) as well.)

I think that's a bug.  You can work around it by assigning the
double-quoted expression to an intermediate parameter.

The fix might be as simple as the following, but it probably needs a bit
more investigation as to where the Nularg came from.  If it was a
nulstring, used in subst.c for reasons I can never remember (and, of
course, aren't commented) this is fine; if it was something that should
already have had remnulargs() applied to it the fix is somewhere else.

Index: Src/utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/utils.c,v
retrieving revision 1.261
diff -p -u -r1.261 utils.c
--- Src/utils.c	4 Aug 2011 08:30:51 -0000	1.261
+++ Src/utils.c	16 Aug 2011 12:46:29 -0000
@@ -3176,6 +3176,10 @@ sepsplit(char *s, char *sep, int allownu
     int n, sl;
     char *t, *tt, **r, **p;
 
+    /* Null string?  Treat as empty string. */
+    if (s[0] == Nularg && !s[1])
+	s++;
+
     if (!sep)
 	return spacesplit(s, allownull, heap, 0);
 

-- 
Peter Stephenson <pws@xxxxxxx>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog



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