Project

General

Profile

Feature #14473 ยป v4-0001-range.c-allow-cover-to-accept-Range-argument.patch

owst (Owen Stephens), 07/19/2018 12:01 AM

View differences:

range.c
1326 1326
    return Qundef;
1327 1327
}
1328 1328

  
1329
static VALUE r_cover_range_p(VALUE range, VALUE beg, VALUE end, VALUE val);
1329 1330

  
1330 1331
/*
1331 1332
 *  call-seq:
1332
 *     rng.cover?(obj)  ->  true or false
1333
 *     rng.cover?(obj)   ->  true or false
1334
 *     rng.cover?(range) ->  true or false
1333 1335
 *
1334 1336
 *  Returns <code>true</code> if +obj+ is between the begin and end of
1335 1337
 *  the range.
......
1337 1339
 *  This tests <code>begin <= obj <= end</code> when #exclude_end? is +false+
1338 1340
 *  and <code>begin <= obj < end</code> when #exclude_end? is +true+.
1339 1341
 *
1340
 *     ("a".."z").cover?("c")    #=> true
1341
 *     ("a".."z").cover?("5")    #=> false
1342
 *     ("a".."z").cover?("cc")   #=> true
1342
 *  Returns <code>true</code> for a Range when it is covered by the receiver,
1343
 *  by comparing the begin and end values directly. This requires the end
1344
 *  object of the receiver to implement <code>succ</code> if the end of +range+
1345
 *  is greater than the end of the receiver and +range+ excludes its end, but
1346
 *  the receiver does not.
1347
 *
1348
 *     ("a".."z").cover?("c")      #=> true
1349
 *     ("a".."z").cover?("5")      #=> false
1350
 *     ("a".."z").cover?("cc")     #=> true
1351
 *     ("a".."z").cover?("b".."c") #=> true
1352
 *     ("a".."z").cover?("0".."z") #=> false
1343 1353
 */
1344 1354

  
1355

  
1345 1356
static VALUE
1346 1357
range_cover(VALUE range, VALUE val)
1347 1358
{
......
1349 1360

  
1350 1361
    beg = RANGE_BEG(range);
1351 1362
    end = RANGE_END(range);
1363

  
1364
    if (rb_obj_is_kind_of(val, rb_cRange)) {
1365
    return RBOOL(r_cover_range_p(range, beg, end, val));
1366
    }
1352 1367
    return r_cover_p(range, beg, end, val);
1353 1368
}
1354 1369

  
1370
static VALUE
1371
r_cover_range_p(VALUE range, VALUE beg, VALUE end, VALUE val)
1372
{
1373
    VALUE val_beg, val_end;
1374
    int cmp_end;
1375

  
1376
    val_beg = RANGE_BEG(val);
1377
    val_end = RANGE_END(val);
1378

  
1379
    if (!r_cover_p(range, beg, end, val_beg)) return FALSE;
1380

  
1381
    cmp_end = r_less(end, val_end);
1382

  
1383
    if (EXCL(range) == EXCL(val)) {
1384
    return cmp_end >= 0;
1385
    } else if (EXCL(range)) {
1386
    return cmp_end > 0;
1387
    } else if (cmp_end >= 0) {
1388
    return TRUE;
1389
    } else if (!discrete_object_p(end)) {
1390
      rb_raise(rb_eTypeError, "can't iterate from %s",
1391
        rb_obj_classname(end));
1392
    }
1393

  
1394
    return r_less(rb_funcallv(end, id_succ, 0, 0), val_end) == 0;
1395
}
1396

  
1355 1397
static VALUE
1356 1398
r_cover_p(VALUE range, VALUE beg, VALUE end, VALUE val)
1357 1399
{
test/ruby/test_range.rb
521 521
    assert_not_operator(5..., :cover?, 0)
522 522
    assert_not_operator(5..., :cover?, "a")
523 523
    assert_operator(5.., :cover?, 10)
524

  
525
    assert_operator(2..5, :cover?, 2..5)
526
    assert_operator(2...6, :cover?, 2...6)
527
    assert_operator(2...6, :cover?, 2..5)
528
    assert_operator(2..5, :cover?, 2...6)
529
    assert_operator(2..5, :cover?, 2..4)
530
    assert_operator(2..5, :cover?, 2...4)
531
    assert_operator(2..5, :cover?, 2...5)
532
    assert_operator(2..5, :cover?, 3..5)
533
    assert_operator(2..5, :cover?, 3..4)
534
    assert_operator(2..5, :cover?, 3...6)
535
    assert_operator(2...6, :cover?, 2...5)
536
    assert_operator(2...6, :cover?, 2..5)
537
    assert_operator(2..6, :cover?, 2...6)
538
    assert_operator(2.., :cover?, 2..)
539
    assert_operator(2.., :cover?, 3..)
540
    assert_operator(1.0..4.0, :cover?, 2.0...3.0)
541

  
542
    assert_not_operator(2..5, :cover?, 1..5)
543
    assert_not_operator(2...6, :cover?, 1..5)
544
    assert_not_operator(2..5, :cover?, 1...6)
545
    assert_not_operator(1..3, :cover?, 1...6)
546
    assert_not_operator(2..5, :cover?, 2..6)
547
    assert_not_operator(2...6, :cover?, 2..6)
548
    assert_not_operator(2...6, :cover?, 2...7)
549
    assert_not_operator(2..3, :cover?, 1..4)
550
    assert_not_operator(1..2, :cover?, 4..3)
551
    assert_not_operator(2..1, :cover?, 1..2)
552
    assert_not_operator(1...2, :cover?, 1...3)
553
    assert_not_operator(2.., :cover?, 1..)
524 554
  end
525 555

  
526 556
  def test_beg_len
527
-