|
|
 |
|
|
|
 |
Jeremy Cowgar wrote:
>
> CChris wrote:
> >
> > Why is -1 a special case? This is not documented. I'd expect a sequence of length
> > -1 to be as empty as if it was of length 0 or -2.
> >
>
> It should be documented, it's a useful feature. -1 means 1 from the end, -2 means
> 2 from the end, -3 means 3 from the end. 0 means the end (since in mid we cannot
> have $). So, if it is <= 0, it goes into a different index calculation.
>
> --
> Jeremy Cowgar
> http://jeremy.cowgar.com
This reply seems to imply that the third parameter to mid() is the end of the
slice. But it is not - or the docs are wrong. Letting slice(s,start,-1) return
s(start..$-1] is useful, granted.
I'd suggest changing mid() and its current docs so that
mid(s,start,-2) means the same as mid(s,start,length(s)-2). Here would be the
code:
global function mid(sequence st, atom start, atom len)
if len<0 then
len += length(st)
if len<0 then
crash("mid(): len was %d and should be greater than %d.",{len-length(st),-length(st)})
end if
end if
if start > length(st) or len=0 then
return ""
end if
if start<1 then
start=1
end if
if start+len-1 > length(st) then
return st[start..$]
elsif len+start-1 < 0 then
return ""
else
return st[start..len+start-1]
end if
end function
Moved testing for start<0 upstream, as return st[start..whatever] would usually
fail.
Thoughts?
CChris
CChris