zsh-workers
 help / color / mirror / code / Atom feed
* string range between 1 and 0.
@ 1999-07-18  4:14 Tanaka Akira
  1999-07-19  0:02 ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Tanaka Akira @ 1999-07-18  4:14 UTC (permalink / raw)
  To: zsh-workers

Hi. I wondered that $var[1,0] is expanded to first character of v.

Z(2):akr@localhost% zsh -f
localhost% v=abcd
localhost% echo $v[1,0]
a
localhost% echo $v[2,1]

localhost% echo $v[2,0]

localhost% 

I think that $v[1,0] should be null string as $v[2,0].
Is this intentional behaviour?
-- 
Tanaka Akira


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

* Re: string range between 1 and 0.
  1999-07-18  4:14 string range between 1 and 0 Tanaka Akira
@ 1999-07-19  0:02 ` Bart Schaefer
  1999-07-19  1:01   ` Tanaka Akira
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 1999-07-19  0:02 UTC (permalink / raw)
  To: Tanaka Akira, zsh-workers

On Jul 18,  1:14pm, Tanaka Akira wrote:
} Subject: string range between 1 and 0.
}
} Hi. I wondered that $var[1,0] is expanded to first character of v.
[...]
} Is this intentional behaviour?

This is a side-effect of ksh array compatibility, believe it or not.

For quite some time now, it has been the case that $x[0] == $x[1] when
NOT emulating ksh.  This documented in section 2.1 of the FAQ, though it
does not seem to be mentioned in the actual manual.

The same code is used to interpret the second value in a subscript range,
so $x[0,0] == $x[1,1] == $x[0,1] == $x[1,0] unless you are using zero-
based arrays as in ksh.  This also means that $x[2,0] == $x[2,1] which is
the empty string either way; the only interesting thing happens when the
first of the range is 1.

} I think that $v[1,0] should be null string as $v[2,0].

It is, when ksharrays is set ...

If it weren't handled this way, then $x[anything,0] in the non-ksharrays
case would have to yield an "invalid subscript" error -- the only other
alternative is to treat $x[n,0] as $x[n,-1] which gives all the elements
from n to the end.

So, given the choices
    (1) leave it as is
    (2) treat [n,0] as [n,-1]
    (3) treat [n,0] as an error
my own preference is for (1).

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


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

* Re: string range between 1 and 0.
  1999-07-19  0:02 ` Bart Schaefer
@ 1999-07-19  1:01   ` Tanaka Akira
  1999-07-19  5:04     ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Tanaka Akira @ 1999-07-19  1:01 UTC (permalink / raw)
  To: zsh-workers

In article <990719000259.ZM8741@candle.brasslantern.com>,
  "Bart Schaefer" <schaefer@candle.brasslantern.com> writes:

> This is a side-effect of ksh array compatibility, believe it or not.

Hm.

> So, given the choices
>     (1) leave it as is
>     (2) treat [n,0] as [n,-1]
>     (3) treat [n,0] as an error
> my own preference is for (1).

I encount this problem with the code such as:

region="$buffer[pos1,pos2]"

I represent a region by pos1 and pos2.
To represent null region, I assign pos2 to pos1 - 1.
I think it's natural, but it's not works when pos1 is 1.

So, my preference is "treat [n,0] as null string if 0 < n".

However, it is no ploblem with the code such as:

local tmp=" $buffer"
region="$tmp[pos1 + 1, pos2 + 1]"

But, I think it's ugly.
Is there exists a more smart code?
# For example, is it representable with only variable expansion?
-- 
Tanaka Akira


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

* Re: string range between 1 and 0.
  1999-07-19  1:01   ` Tanaka Akira
@ 1999-07-19  5:04     ` Bart Schaefer
  1999-07-19 19:10       ` Wayne Davison
  1999-07-20  5:59       ` Tanaka Akira
  0 siblings, 2 replies; 6+ messages in thread
From: Bart Schaefer @ 1999-07-19  5:04 UTC (permalink / raw)
  To: Tanaka Akira, zsh-workers

On Jul 19, 10:01am, Tanaka Akira wrote:
} Subject: Re: string range between 1 and 0.
}
} region="$buffer[pos1,pos2]"
} 
} I represent a region by pos1 and pos2.
} To represent null region, I assign pos2 to pos1 - 1.
} I think it's natural, but it's not works when pos1 is 1.

That scheme wouldn't work in ksh mode in any case ... ${buffer[0,-1]} is
the whole buffer, not the empty string.

I presume you want pos1 to continue to refer to the start of the region,
so doing something like ((pos1=pos2+1)) is right out.

} So, my preference is "treat [n,0] as null string if 0 < n".

The difficulty with doing this is that C arrays are indexed from 0.  So
when zsh uses 1-based indices, it has to decrement them before indexing
into the internal representation of the value.  This decrement is done
at parse time (unless the value is already zero), so that thereafter the
same code can be used for ksh and zsh arrays.  Since negative numbers
are already used for indexing back from the end, there isn't any value
to represent the nonexistent position, which is in effect what you're
asking for (because the only position to the left of the leftmost is
the one that doesn't exist).

This shows up in other places:  $x[1,-$#x] == $x[1] but $[2,-$#x] == "".
Anything that might refer to a position "off the low end" of the array
is instead forced to refer to position 1.  Ksh emulation aside, this is
the result of a decision that only bad syntax (not bad numbers) would be
permitted to result in a subscripting error.

It might have been better if zsh subscripts had been $var[pos,len] form,
so that a length of 0 could represent an empty range, but it's a lot too
late for that now ...

} Is there exists a more smart code?
} # For example, is it representable with only variable expansion?

If you represent the null region by unsetting pos2 or setting it empty,
you can do this:

	${pos2:+$buffer[pos1,pos2]}


-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


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

* Re: string range between 1 and 0.
  1999-07-19  5:04     ` Bart Schaefer
@ 1999-07-19 19:10       ` Wayne Davison
  1999-07-20  5:59       ` Tanaka Akira
  1 sibling, 0 replies; 6+ messages in thread
From: Wayne Davison @ 1999-07-19 19:10 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Tanaka Akira, zsh-workers

On Mon, 19 Jul 1999, Bart Schaefer wrote:
> The difficulty with doing this is that C arrays are indexed from 0.  So
> when zsh uses 1-based indices, it has to decrement them before indexing
> into the internal representation of the value.  This decrement is done
> at parse time (unless the value is already zero), so that thereafter the
> same code can be used for ksh and zsh arrays.

Sounds to me like a good fix would be to change the parse code to
turn ksh arrays into zsh arrays (incrementing non-negative numbers)
OR to turn the values into an internal representation of offset and
length at parse time (though you may have to leave -1 unchanged if
you don't know the length).  This way, the C code would be able to
handle the more flexible indexing values properly (the zsh values)
without affecting ksh compatibility.

..wayne..


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

* Re: string range between 1 and 0.
  1999-07-19  5:04     ` Bart Schaefer
  1999-07-19 19:10       ` Wayne Davison
@ 1999-07-20  5:59       ` Tanaka Akira
  1 sibling, 0 replies; 6+ messages in thread
From: Tanaka Akira @ 1999-07-20  5:59 UTC (permalink / raw)
  To: zsh-workers

In article <990719050457.ZM8827@candle.brasslantern.com>,
  "Bart Schaefer" <schaefer@candle.brasslantern.com> writes:

> That scheme wouldn't work in ksh mode in any case ... ${buffer[0,-1]} is
> the whole buffer, not the empty string.

I agree. ksharrays makes the scheme damaged.

> The difficulty with doing this is that C arrays are indexed from 0.  So
> when zsh uses 1-based indices, it has to decrement them before indexing
> into the internal representation of the value.  This decrement is done
> at parse time (unless the value is already zero), so that thereafter the
> same code can be used for ksh and zsh arrays.  Since negative numbers
> are already used for indexing back from the end, there isn't any value
> to represent the nonexistent position, which is in effect what you're
> asking for (because the only position to the left of the leftmost is
> the one that doesn't exist).

I understand that my expectation is not so trivial.

> If you represent the null region by unsetting pos2 or setting it empty,
> you can do this:
> 
> 	${pos2:+$buffer[pos1,pos2]}

I found another tricky answer.

  $buffer[pos2 == 0 ? 2 : pos1, pos2]

Hm. Arithmetic evaluation has the much closer syntax to ordinary
language such as perl or C. It's good. I feel regret that it cannot
handle objects other than integers.
-- 
Tanaka Akira


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

end of thread, other threads:[~1999-07-20  6:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-07-18  4:14 string range between 1 and 0 Tanaka Akira
1999-07-19  0:02 ` Bart Schaefer
1999-07-19  1:01   ` Tanaka Akira
1999-07-19  5:04     ` Bart Schaefer
1999-07-19 19:10       ` Wayne Davison
1999-07-20  5:59       ` Tanaka Akira

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