Project

General

Profile

Feature #8229 ยป 0001-hash.c-Hash-include-improve.patch

nobu (Nobuyoshi Nakada), 04/09/2013 04:37 PM

View differences:

hash.c
1599 1599
/*
1600 1600
 *  call-seq:
1601 1601
 *     hsh.has_key?(key)    -> true or false
1602
 *     hsh.include?(key)    -> true or false
1603 1602
 *     hsh.key?(key)        -> true or false
1604 1603
 *     hsh.member?(key)     -> true or false
1605 1604
 *
......
1622 1621
    return Qfalse;
1623 1622
}
1624 1623

  
1624
/*
1625
 *  call-seq:
1626
 *     hsh.include?(key)        -> true or false
1627
 *     hsh.include?(key, value) -> true or false
1628
 *
1629
 *  Returns +true+ if the given _key_ is present in _hsh_,
1630
 *  and the value equals to the given _value_.
1631
 *
1632
 *     h = { "a" => 100, "b" => 200 }
1633
 *     h.include?("a")       #=> true
1634
 *     h.include?("z")       #=> false
1635
 *     h.include?("a", 100)  #=> true
1636
 *     h.include?("a", 101)  #=> false
1637
 *
1638
 */
1639

  
1640
static VALUE
1641
rb_hash_include_p(int argc, VALUE *argv, VALUE hash)
1642
{
1643
    st_data_t data;
1644

  
1645
    rb_check_arity(argc, 1, 2);
1646
    if (!RHASH(hash)->ntbl)
1647
        return Qfalse;
1648
    if (st_lookup(RHASH(hash)->ntbl, (st_data_t)argv[0], &data)) {
1649
	if (argc < 2) return Qtrue;
1650
	return rb_equal((VALUE)data, argv[1]);
1651
    }
1652
    return Qfalse;
1653
}
1654

  
1625 1655
static int
1626 1656
rb_hash_search_value(VALUE key, VALUE value, VALUE arg)
1627 1657
{
......
3008 3038
/*
3009 3039
 * call-seq:
3010 3040
 *   ENV.key?(name)     -> true or false
3011
 *   ENV.include?(name) -> true or false
3012 3041
 *   ENV.has_key?(name) -> true or false
3013 3042
 *   ENV.member?(name)  -> true or false
3014 3043
 *
......
3029 3058

  
3030 3059
/*
3031 3060
 * call-seq:
3061
 *   ENV.include?(name)        -> true or false
3062
 *   ENV.include?(name, value) -> true or false
3063
 *
3064
 * Returns +true+ if there is an environment variable with the given +name+,
3065
 * and the value equals to the given _value_.
3066
 */
3067
static VALUE
3068
env_include_p(int argc, VALUE *argv, VALUE env)
3069
{
3070
    const char *s, *v, *e;
3071
    VALUE key, value;
3072

  
3073
    rb_check_arity(argc, 1, 2);
3074
    rb_secure(4);
3075
    key = argv[0];
3076
    s = StringValuePtr(key);
3077
    if (memchr(s, '\0', RSTRING_LEN(key)))
3078
	rb_raise(rb_eArgError, "bad environment variable name");
3079
    e = getenv(s);
3080
    if (argc > 1) {
3081
	value = argv[1];
3082
	if (NIL_P(value)) return e ? Qfalse : Qtrue;
3083
	v = StringValueCStr(value);
3084
	return (!e || strcmp(v, e)) ? Qfalse : Qtrue;
3085
    }
3086
    return e ? Qtrue : Qfalse;
3087
}
3088

  
3089
/*
3090
 * call-seq:
3032 3091
 *   ENV.assoc(name) -> Array or nil
3033 3092
 *
3034 3093
 * Returns an Array of the name and value of the environment variable with
......
3487 3546
    rb_define_method(rb_cHash, "rassoc", rb_hash_rassoc, 1);
3488 3547
    rb_define_method(rb_cHash, "flatten", rb_hash_flatten, -1);
3489 3548

  
3490
    rb_define_method(rb_cHash,"include?", rb_hash_has_key, 1);
3549
    rb_define_method(rb_cHash,"include?", rb_hash_include_p, -1);
3491 3550
    rb_define_method(rb_cHash,"member?", rb_hash_has_key, 1);
3492 3551
    rb_define_method(rb_cHash,"has_key?", rb_hash_has_key, 1);
3493 3552
    rb_define_method(rb_cHash,"has_value?", rb_hash_has_value, 1);
......
3542 3601
    rb_define_singleton_method(envtbl,"keys", env_keys, 0);
3543 3602
    rb_define_singleton_method(envtbl,"values", env_values, 0);
3544 3603
    rb_define_singleton_method(envtbl,"values_at", env_values_at, -1);
3545
    rb_define_singleton_method(envtbl,"include?", env_has_key, 1);
3604
    rb_define_singleton_method(envtbl,"include?", env_include_p, -1);
3546 3605
    rb_define_singleton_method(envtbl,"member?", env_has_key, 1);
3547 3606
    rb_define_singleton_method(envtbl,"has_key?", env_has_key, 1);
3548 3607
    rb_define_singleton_method(envtbl,"has_value?", env_has_value, 1);
test/ruby/test_env.rb
394 394
    end
395 395
  end
396 396

  
397
  def test_include_p
398
    feature8229 = '[ruby-core:54131] [Feature #8229]'
399
    assert_not_send([ENV, :include?, "test"])
400
    ENV["test"] = "foo"
401
    assert_send([ENV, :include?, "test"])
402
    assert_send([ENV, :include?, "test", "foo"], feature8229)
403
    assert_not_send([ENV, :include?, "test", "bar"], feature8229)
404
  end
405

  
397 406
  if /mswin|mingw/ =~ RUBY_PLATFORM
398 407
    def test_win32_blocksize
399 408
      len = 32767 - ENV.to_a.flatten.inject(0) {|r,e| r + e.size + 2 }
test/ruby/test_hash.rb
435 435
  end
436 436

  
437 437
  def test_include?
438
    feature8229 = '[ruby-core:54131] [Feature #8229]'
438 439
    assert_not_send([@cls[], :include?, 1])
439 440
    assert_not_send([@cls[], :include?, nil])
440 441
    assert_send([@h, :include?, nil])
441 442
    assert_send([@h, :include?, 1])
442 443
    assert_not_send([@h, :include?, 'gumby'])
444
    assert_send([@h, :include?, 1, 'one'], feature8229)
445
    assert_not_send([@h, :include?, 1, 'uno'], feature8229)
443 446
  end
444 447

  
445 448
  def test_key
446
-