zsh-users
 help / color / mirror / code / Atom feed
* Avoiding empty element when splitting NUL-separated string from command substitution
@ 2011-08-16 12:13 Nikolai Weibull
  2011-08-16 12:51 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Nikolai Weibull @ 2011-08-16 12:13 UTC (permalink / raw)
  To: Zsh Users

% 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.)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Avoiding empty element when splitting NUL-separated string from command substitution
  2011-08-16 12:13 Avoiding empty element when splitting NUL-separated string from command substitution Nikolai Weibull
@ 2011-08-16 12:51 ` Peter Stephenson
  2011-08-16 14:06   ` Nikolai Weibull
  2011-08-16 14:53   ` Peter Stephenson
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Stephenson @ 2011-08-16 12:51 UTC (permalink / raw)
  To: Zsh Users

On Tue, 16 Aug 2011 14:13:58 +0200
Nikolai Weibull <now@bitwi.se> 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@csr.com>            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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Avoiding empty element when splitting NUL-separated string from command substitution
  2011-08-16 12:51 ` Peter Stephenson
@ 2011-08-16 14:06   ` Nikolai Weibull
  2011-08-16 14:53   ` Peter Stephenson
  1 sibling, 0 replies; 4+ messages in thread
From: Nikolai Weibull @ 2011-08-16 14:06 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh Users

On Tue, Aug 16, 2011 at 14:51, Peter Stephenson
<Peter.Stephenson@csr.com> wrote:
> On Tue, 16 Aug 2011 14:13:58 +0200
> Nikolai Weibull <now@bitwi.se> 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.

OK, thanks.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Avoiding empty element when splitting NUL-separated string from command substitution
  2011-08-16 12:51 ` Peter Stephenson
  2011-08-16 14:06   ` Nikolai Weibull
@ 2011-08-16 14:53   ` Peter Stephenson
  1 sibling, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2011-08-16 14:53 UTC (permalink / raw)
  To: Zsh Users

On Tue, 16 Aug 2011 13:51:38 +0100
Peter Stephenson <Peter.Stephenson@csr.com> wrote:
> On Tue, 16 Aug 2011 14:13:58 +0200
> Nikolai Weibull <now@bitwi.se> wrote:
> > % b=(${(0)"$(print -n)"})
> > % echo $#b
> > 1
>
> I think that's a bug.
> 
> 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.

This is indeed a specially created null string propagating up from
readoutput() via multsub(), so I think the fix is legitimate.  It's not
clear if sepsplit() should be using null strings in its output, however,
since I have no idea what they're for.  That would be a different
pre-existing bug with different effects.

If you have any theories, you can post them to zsh-workers.

Index: Test/D04parameter.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/D04parameter.ztst,v
retrieving revision 1.59
diff -p -u -r1.59 D04parameter.ztst
--- Test/D04parameter.ztst	3 Jun 2011 22:03:44 -0000	1.59
+++ Test/D04parameter.ztst	16 Aug 2011 14:50:13 -0000
@@ -1453,3 +1453,8 @@
    print ${foo:5:-6}
 1:Regression test for total length < 0 in array
 ?(eval):2: substring expression: 3 < 5
+
+   foo=(${(0)"$(print -n)"})
+   print ${#foo}
+0:Nularg removed from split empty string
+>0


-- 
Peter Stephenson <pws@csr.com>            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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-08-16 15:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-16 12:13 Avoiding empty element when splitting NUL-separated string from command substitution Nikolai Weibull
2011-08-16 12:51 ` Peter Stephenson
2011-08-16 14:06   ` Nikolai Weibull
2011-08-16 14:53   ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).