Project

General

Profile

Feature #6317 » range_cover.diff

masaakiaoyagi (Masaaki Aoyagi), 04/18/2012 08:51 PM

View differences:

range.c
935 935
static VALUE
936 936
range_cover(VALUE range, VALUE val)
937 937
{
938
    VALUE beg, end;
938
    VALUE beg, end, val_beg, val_end;
939
    VALUE range_p = rb_obj_is_kind_of(val, rb_cRange);
940
    int c;
939 941

  
940 942
    beg = RANGE_BEG(range);
941 943
    end = RANGE_END(range);
942
    if (r_le(beg, val)) {
943
	if (EXCL(range)) {
944
	    if (r_lt(val, end))
945
		return Qtrue;
946
	}
947
	else {
948
	    if (r_le(val, end))
949
		return Qtrue;
950
	}
944
    if (range_p) {
945
        val_beg = RANGE_BEG(val);
946
        val_end = RANGE_END(val);
947
        c = r_le(val_beg, val_end);
948
        if (c == Qfalse)
949
            return Qfalse;
950
        if ((c == (int)INT2FIX(0)) && EXCL(val))
951
            return Qfalse;
952
    }
953
    else {
954
        val_beg = val;
955
        val_end = val;
956
    }
957

  
958
    if (r_le(beg, val_beg)) {
959
        c = r_le(val_end, end);
960
        if (c == Qtrue)
961
            return Qtrue;
962
        if (c == (int)INT2FIX(0)) {
963
            if (EXCL(range)) {
964
                if (range_p && EXCL(val))
965
                    return Qtrue;
966
            }
967
            else {
968
                return Qtrue;
969
            }
970
        }
951 971
    }
952 972
    return Qfalse;
953 973
}
test/ruby/test_range.rb
280 280
    assert(("a"..."z").cover?("y"))
281 281
    assert(!(("a"..."z").cover?("z")))
282 282
    assert(("a".."z").cover?("cc"))
283
    assert(!(("z".."a").cover?("c")))
284
    assert(("e".."r").cover?("e".."r"))
285
    assert(("e".."r").cover?("e"..."r"))
286
    assert(!(("e".."r").cover?("d".."r")))
287
    assert(!(("e".."r").cover?("e".."s")))
288
    assert(!(("e".."r").cover?("e"..."s")))
289
    assert(!(("e".."r").cover?("a".."d")))
290
    assert(!(("e".."r").cover?("s".."z")))
291
    assert(!(("e"..."r").cover?("e".."r")))
292
    assert(("e"..."r").cover?("e"..."r"))
293
    assert(!(("r".."e").cover?("e".."r")))
294
    assert(!(("e".."r").cover?("r".."e")))
295
    assert(("e".."r").cover?("g".."g"))
296
    assert(!(("e".."r").cover?("g"..."g")))
283 297
  end
284 298

  
285 299
  def test_beg_len