From: "Ara.T.Howard" Date: 2004-07-22T13:32:03+09:00 Subject: Re: [PATCH] was - Re: substring by range parameter (bug?) On Thu, 22 Jul 2004, Yukihiro Matsumoto wrote: > Hi, > > In message "[PATCH] was - Re: substring by range parameter (bug?)" > on 04/07/22, "Ara.T.Howard" writes: > > |if you look at the source, it is clear that rb_range_beg_len intends to fail > |with ranges like -1...-42. i think it is a bug that it does not. this patch > |should fix it: > > I think I understand you, but not sure. Can you show me an example of > wrong behavior? > > matz. note the second case: wrong: ~ > irb irb(main):001:0> 'foobar'[-1..-1] => "r" irb(main):002:0> 'foobar'[-1..-2] => "" irb(main):003:0> 'foobar'[-1..-3] => nil right (patch applied): ~ > irb irb(main):001:0> 'foobar'[-1..-1] => "r" irb(main):002:0> 'foobar'[-1..-2] => nil irb(main):003:0> 'foobar'[-1..-3] => nil it seems like a simple off by one error cause by mapping -n,...,-3,-2,-1 => 0,...,n-3,n-2,n-1 my interpretation of rb_range_beg_len was that the test if (len < 0) is to catch errors made by specifying ranges whose ends are before their beginings, like in 'foobar'[5..2] # => nil however this breaks down in the case of 'foobar'[-1..-2] # => '' because end++; occurs AFTER neg ranges are mapped to pos ranges but before if (!EXCL(range)... i think it should always be the case that beg <= end after beg and end have been mapped positive but BEFORE end is incremented to include end if it's exclusive. else we have and off by one problem... this is, i think, what testing len < 0 was supposed to catch - but this fails when beg = -n end = -n - 1 as is the case with -1..-2 maybe i've misinterpreted this? if not here is the patch again - please note my first one was wrong! sorry. diff -u range.c.org range.c --- range.c.org 2004-07-21 15:56:02.000000000 -0600 +++ range.c 2004-07-21 16:54:48.000000000 -0600 @@ -22,6 +22,7 @@ end = len; } if (end < 0) end += len; + if (end < beg) goto out_of_range; /* b4 including end point require end >= beg */ if (!EXCL(range)) end++; /* include end point */ if (end < 0) goto out_of_range; len = end - beg; kind regards. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | A flower falls, even though we love it; | and a weed grows, even though we do not love it. | --Dogen ===============================================================================