zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-users@zsh.org
Subject: Re: ${(s::)VAR} vs "${(s::)VAR}"
Date: Fri, 27 Apr 2012 21:38:37 +0100	[thread overview]
Message-ID: <20120427213837.1b861851@pws-pc.ntlworld.com> (raw)
In-Reply-To: <CAFvQaj4rz82ByYJGhWBG-pZzZuJk4myGBsuSvj_PF64_HCfu1w@mail.gmail.com>

On Thu, 26 Apr 2012 16:48:00 -0400
Kynn Jones <kynnjo@gmail.com> wrote:
> % (VAR=123; for c ( "${(s::)VAR}" ) echo ">$c<")
> >1<
> >2<
> >3<
> ><

I think I can say with some degree of certainty I haven't a clue what's
going on here, and it doesn't agree with the documentation "An empty
string may also be given in which case every character will be a
separate element" since you don't magically get an extra character
by sticking the expression in double quotes.

This comes from the function wordcount() in utils.c, which is called in
a couple of functions used for splitting words, none of which has any
comments apart from a few speculations from me dating from 2003.  The
function takes a mysterious argument mul which presumably has got
something to do with doing something multiple times.

It seems hard to believe it should ever be counting an extra character
when it's reached the null at the end of the string.  The following
seems to fix it without any obvious side effects.

Index: Src/utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/utils.c,v
retrieving revision 1.268
diff -p -u -r1.268 utils.c
--- Src/utils.c	16 Apr 2012 11:26:10 -0000	1.268
+++ Src/utils.c	27 Apr 2012 20:35:40 -0000
@@ -3114,7 +3114,7 @@ wordcount(char *s, char *sep, int mul)
 	r = 1;
 	sl = strlen(sep);
 	for (; (c = findsep(&s, sep, 0)) >= 0; s += sl)
-	    if ((c && *(s + sl)) || mul)
+	    if ((c || mul) && *(s + sl))
 		r++;
     } else {
 	char *t = s;
Index: Test/D04parameter.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/D04parameter.ztst,v
retrieving revision 1.67
diff -p -u -r1.67 D04parameter.ztst
--- Test/D04parameter.ztst	22 Apr 2012 18:39:53 -0000	1.67
+++ Test/D04parameter.ztst	27 Apr 2012 20:35:40 -0000
@@ -1304,6 +1304,19 @@
 >in
 >it
 
+  str=abcd
+  print -l ${(s..)str}
+  print -l "${(s..)str}"
+0:splitting of strings into characters
+>a
+>b
+>c
+>d
+>a
+>b
+>c
+>d
+
   array=('%' '$' 'j' '*' '$foo')
   print ${array[(i)*]} "${array[(i)*]}"
   print ${array[(ie)*]} "${array[(ie)*]}"

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


  reply	other threads:[~2012-04-27 20:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-26 20:48 Kynn Jones
2012-04-27 20:38 ` Peter Stephenson [this message]
2012-04-28  2:50   ` Bart Schaefer
2012-04-29 18:22     ` Peter Stephenson
2012-04-29 18:41     ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120427213837.1b861851@pws-pc.ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).