zsh-users
 help / color / mirror / code / Atom feed
* End Index number not what I expected
@ 2016-12-06 17:09 Jim
  2016-12-06 20:16 ` Peter Stephenson
       [not found] ` <20161206201629.2970ef18__5617.78782071105$1481055469$gmane$org@ntlworld.com>
  0 siblings, 2 replies; 3+ messages in thread
From: Jim @ 2016-12-06 17:09 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1458 bytes --]

Hello List,

zsh version: 5.2

So far, searches on the web have not returned a satisfactory
answer.  If I missed the answer, shame on me and sorry for
wasting you time.

The script I'm working on globs a bunch of file names then
uses portions of the file names.  As with most things in
UNIX/Linux there are multiple ways of doing things, zsh is no
exception. While trying different approaches to the task I ran
into an index issue I don't understand. Using the parameter
expansion flags (SB) and (SE) to find the beginning and end
of a match I was surprised when the ending index returned the
end of the match + 1.

The man page states:

 B   Include the index of the beginning of the match in the result.
 E   Include the index of the end of the match in the result.

Logic to me would be, if the B flag returns the index of the first
character of the match, then the E flag should return the index
of the last character of the match, not +1.

Am I missing something here?

Alternately using the conditional expression
"string =~ regexp", the integer parameters MBEGIN and MEND
work as expected.

Also in researching this I found an example of using the E flag.
The example uses a variable with a string of 10 characters,
yet using the (SE) flags the match returned an index of 11.

Should a returned index be larger the the length of the variable?

Again, just trying to understand what I'm missing here.

Thanks in advance for your help.

Sincerely,

Jim

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

* Re: End Index number not what I expected
  2016-12-06 17:09 End Index number not what I expected Jim
@ 2016-12-06 20:16 ` Peter Stephenson
       [not found] ` <20161206201629.2970ef18__5617.78782071105$1481055469$gmane$org@ntlworld.com>
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2016-12-06 20:16 UTC (permalink / raw)
  To: zsh-users

On Tue, 6 Dec 2016 11:09:39 -0600
Jim <linux.tech.guy@gmail.com> wrote:
> The man page states:
> 
>  B   Include the index of the beginning of the match in the result.
>  E   Include the index of the end of the match in the result.
> 
> Logic to me would be, if the B flag returns the index of the first
> character of the match, then the E flag should return the index
> of the last character of the match, not +1.
> 
> Am I missing something here?

No.

There's a test in the test suite...

  # This (1) doesn't work with // or /
  #      (2) perhaps ought to be 18, to be consistent with normal zsh
  #          substring indexing and with backreferences.
  print ${(BES)string##white}
0:${(BE...)...}
>14 19

I think this is just an underused feature that's been allowed to get
away with it.  It should at least be properly documented, though it
would be preferable if it was more consistent.

pws

diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index 87ca790..cf4f69e 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -1394,7 +1394,8 @@ item(tt(B))(
 Include the index of the beginning of the match in the result.
 )
 item(tt(E))(
-Include the index of the end of the match in the result.
+Include the index one character past the end of the match in the result
+(note this is inconsistent with other uses of parameter index).
 )
 item(tt(M))(
 Include the matched portion in the result.


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

* Re: End Index number not what I expected
       [not found] ` <20161206201629.2970ef18__5617.78782071105$1481055469$gmane$org@ntlworld.com>
@ 2016-12-10 14:56   ` Daniel Shahaf
  0 siblings, 0 replies; 3+ messages in thread
From: Daniel Shahaf @ 2016-12-10 14:56 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

Peter Stephenson wrote on Tue, Dec 06, 2016 at 20:16:29 +0000:
> +++ b/Doc/Zsh/expn.yo
> @@ -1394,7 +1394,8 @@ item(tt(B))(
>  item(tt(E))(
> -Include the index of the end of the match in the result.
> +Include the index one character past the end of the match in the result
> +(note this is inconsistent with other uses of parameter index).

Also:

diff --git a/Completion/Zsh/Context/_brace_parameter b/Completion/Zsh/Context/_brace_parameter
index e77d4c5..ae69f77 100644
--- a/Completion/Zsh/Context/_brace_parameter
+++ b/Completion/Zsh/Context/_brace_parameter
@@ -179,7 +179,7 @@ if [[ $PREFIX = *'${('[^\)]# ]]; then
     "S:search substrings in #, %, / expressions"
     "I:search <argument>th match in #, %, / expressions"
     "B:include index of beginning of match in #, %, / expressions"
-    "E:include index of end of match in #, %, / expressions"
+    "E:include index of one past end of match in #, %, / expressions"
     "M:include matched portion in #, %, / expressions"
     "N:include length of match in #, % expressions"
     "R:include rest (unmatched portion) in #, %, / expressions"

I'll commit this now.


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

end of thread, other threads:[~2016-12-10 14:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-06 17:09 End Index number not what I expected Jim
2016-12-06 20:16 ` Peter Stephenson
     [not found] ` <20161206201629.2970ef18__5617.78782071105$1481055469$gmane$org@ntlworld.com>
2016-12-10 14:56   ` Daniel Shahaf

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