1. mid(s,x,-1)
- Posted by CChris <christian.cuvier at agricu??ure.gouv.fr> May 08, 2008
- 583 views
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. CChris
2. Re: mid(s,x,-1)
- Posted by Jeremy Cowgar <jeremy at c?wg?r.com> May 08, 2008
- 548 views
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
3. Re: mid(s,x,-1)
- Posted by CChris <christian.cuvier at agriculture?gouv?fr> May 08, 2008
- 575 views
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 > <a href="http://jeremy.cowgar.com">http://jeremy.cowgar.com</a> 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
4. Re: mid(s,x,-1)
- Posted by Jeremy Cowgar <jeremy at cowgar?co?> May 08, 2008
- 550 views
CChris wrote: > > > 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. Oh! My mind must be going. Indeed, I was speaking of slice > 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). Yes, that's much better. The code you showed would be fine. -- Jeremy Cowgar http://jeremy.cowgar.com