Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: string range between 1 and 0.
- X-seq: zsh-workers 7189
- From: "Bart Schaefer" <schaefer@xxxxxxxxxxxxxxxxxxxxxxx>
- To: Tanaka Akira <akr@xxxxxxxxxxx>, zsh-workers@xxxxxxxxxxxxxx
- Subject: Re: string range between 1 and 0.
- Date: Mon, 19 Jul 1999 05:04:57 +0000
- In-reply-to: <rsqbtd9v4eu.fsf@xxxxxxxxxxxxxxxxx>
- Mailing-list: contact zsh-workers-help@xxxxxxxxxxxxxx; run by ezmlm
- References: <rsqvhbi7fww.fsf@xxxxxxxxxxxxxxxxx> <990719000259.ZM8741@xxxxxxxxxxxxxxxxxxxxxxx> <rsqbtd9v4eu.fsf@xxxxxxxxxxxxxxxxx>
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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author