- Subject: Re: a[[i1:i2]], substring and min, max
- From: "Klaus Schmid" <klaus.schmid@xxxxxxx>
- Date: Thu, 7 Aug 2003 23:38:42 +0200
John E. Davis wrote:
> For performance reasons, indexing by arrays in slang uses modular
> arithmetic. The alternative would be to perform a bounds check on
> each index of the array resulting in a performance hit. I will look
> into why a[[0:-5]] returns the empty string.
Actually I had no real problem with this until now. I was just comparing
this kind of substrings to substr referring bounds and error conditions.
Anyway, I think the functionality of a[[i1:i2]] is very useful, but
unfortunatly not usable in some context like array_map.
Therefor my proposal about substring.
Here revised, sans typo and redundant if:
define substring( s, i1, i2)
{
if ( i1 < 0) i1+= strlen(s);
if ( i2 < 0) i2+= strlen(s);
i1= max( 0, i1);
i2= min( i2, strlen(s)-1);
if ( i1 > i2) return "";
return s[[i1:i2]];
}
Wouldn't it make sense to have both, the a[[*]] with the
faster modulo logic and a substring like above?
-- Klaus
[2003 date index]
[2003 thread index]
[Thread Prev] [Thread Next]
[Date Prev] [Date Next]