On Thu, 22 Jul 2004, D T wrote:


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:

--- range.c.org 2004-07-21 15:56:02.000000000 -0600
+++ range.c     2004-07-21 16:41:42.000000000 -0600
@@ -22,6 +22,7 @@
             end = len;
      }
      if (end < 0) end += len;
+    if (beg < end) 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;


beg and end are mapped to positive values and then the endpoint is included
iff range is not exclusive.  however, even before this step it is clearly a
pre-condition that end >= beg.

-a

> Correct to my previous conclusion.
> The empty string, itself, does have one and only one empty substring too.
> And the empty string case is even more strange...
>
> List all what I found here:
>
> s="ab"; s[-1..-2]="xxx"; p s   #=> s = "axxxb"
> s="ab"; s[-2..-3]="xxx"; p s   #=> s = "xxxab"
> s="ab"; s[1..0]="xxx"; p s     #=> s = "axxxb", same case as s[-1..-2]
> s="ab"; s[2..1]="xxx"; p s     #=> s = "abxxx"
>
> "ab"[3..2]   #=> nil
> "ab"[-3..-4] #=> nil
>
> empty string case: (only 1 empty substring)
> ""[-1..-2]  #=> nil
> ""[-1..0]   #=> nil
> ""[1..2]    #=> nil
> ""[0..-1]   #=> ""
> ""[0..1]    #=> ""
> ""[0..2]    #=> ""
> ""[0..3]    #=> ""
> ""[0..4]    #=> ""
> ""[0..5]    #=> ""
> .. etc
>
> (All empty substring of empty string is the same, you could re-assing to see
> it)
>
>
>> From: Hal Fulton <hal9000 / hypermetrics.com>
>> Reply-To: ruby-talk / ruby-lang.org
>> To: ruby-talk / ruby-lang.org (ruby-talk ML)
>> Subject: Re: substring by range parameter (bug?)
>> Date: Thu, 22 Jul 2004 07:19:40 +0900
>>
>> D T wrote:
>>> OK. You got my point.
>>> And your explanation seems logic to me.
>>> Thanks.
>>>
>>> Anyway, I still feel this is very strange...
>>> "a"[-1..-2] #=> ""
>>> ""[-1..-2] #=> nil
>>>
>>> My Conclusion is : For any no empty string, it exists exactly string's
>>> length + 1 of empty substring!
>>> (location does matter)
>>>
>>> Example: for "ab", there are exactly 3 empty substrings locate at "^a^b^"
>>> (^ shows empty string position)
>>>
>>> s="ab"; s[-1..-2]="xxx"; p s   #==> s = "axxxb"
>>> s="ab"; s[-2..-3]="xxx"; p s   #==> s = "xxxab"
>>> s="ab"; s[1..0]="xxx"; p s     #==> s = "axxxb", it is the same as
>>> s[-1..-2]
>>> s="ab"; s[2..1]="xxx"; p s     #==> s = "abxxx"
>>>
>>> As you can see, there are exactly 3 empty substrings on "ab". (and you can
>>> re-assing)  ;-)
>>
>>
>> This is the most logical analysis of this issue that I remember
>> seeing. Thank you for that.
>>
>> And also IMO it's the best justification for this behavior -- after
>> all, "before the beginning" and "after the end" are valid locations
>> as far as insertion goes.
>>
>> I now understand better what matz said long ago, about an imaginary
>> pointer in between the elements. I understand this better WRT insertion
>> than WRT accessing data.
>>
>> Array subranges work much the same way, I believe, correct?
>>
>>
>> Hal
>>
>>
>
> _________________________________________________________________
> Planning a family vacation? Check out the MSN Family Travel guide!
> http://dollar.msn.com
>
>
>
>

-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
===============================================================================