зеркало из https://github.com/github/ruby.git
* hash.c (rb_hash_assoc): revert r42224. table->type->compare is
called only if hashes are matched. * test/ruby/test_hash.rb: add a test to check using #== to compare. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42232 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
7440815128
Коммит
ef01a5a649
|
@ -1,3 +1,10 @@
|
||||||
|
Mon Jul 29 20:14:24 2013 Masaki Matsushita <glass.saga@gmail.com>
|
||||||
|
|
||||||
|
* hash.c (rb_hash_assoc): revert r42224. table->type->compare is
|
||||||
|
called only if hashes are matched.
|
||||||
|
|
||||||
|
* test/ruby/test_hash.rb: add a test to check using #== to compare.
|
||||||
|
|
||||||
Mon Jul 29 17:00:31 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Mon Jul 29 17:00:31 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* parse.y (yycompile): store file name as String to keep the encoding.
|
* parse.y (yycompile): store file name as String to keep the encoding.
|
||||||
|
|
56
hash.c
56
hash.c
|
@ -2123,34 +2123,15 @@ rb_hash_merge(VALUE hash1, VALUE hash2)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
assoc_cmp(VALUE a, VALUE b)
|
assoc_i(VALUE key, VALUE val, VALUE arg)
|
||||||
{
|
{
|
||||||
return !RTEST(rb_equal(a, b));
|
VALUE *args = (VALUE *)arg;
|
||||||
}
|
|
||||||
|
|
||||||
struct lookup2_arg {
|
if (RTEST(rb_equal(args[0], key))) {
|
||||||
VALUE hash;
|
args[1] = rb_assoc_new(key, val);
|
||||||
VALUE key;
|
return ST_STOP;
|
||||||
};
|
}
|
||||||
|
return ST_CONTINUE;
|
||||||
static VALUE
|
|
||||||
lookup2_call(VALUE arg)
|
|
||||||
{
|
|
||||||
struct lookup2_arg *p = (struct lookup2_arg *)arg;
|
|
||||||
return rb_hash_lookup2(p->hash, p->key, Qundef);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct reset_hash_type_arg {
|
|
||||||
VALUE hash;
|
|
||||||
const struct st_hash_type *orighash;
|
|
||||||
};
|
|
||||||
|
|
||||||
static VALUE
|
|
||||||
reset_hash_type(VALUE arg)
|
|
||||||
{
|
|
||||||
struct reset_hash_type_arg *p = (struct reset_hash_type_arg *)arg;
|
|
||||||
RHASH(p->hash)->ntbl->type = p->orighash;
|
|
||||||
return Qundef;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -2168,25 +2149,14 @@ reset_hash_type(VALUE arg)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
rb_hash_assoc(VALUE hash, VALUE key)
|
rb_hash_assoc(VALUE hash, VALUE obj)
|
||||||
{
|
{
|
||||||
VALUE value;
|
VALUE args[2];
|
||||||
st_table *table = hash_tbl(hash);
|
|
||||||
struct lookup2_arg arg;
|
|
||||||
struct reset_hash_type_arg ensure_arg;
|
|
||||||
const struct st_hash_type *orighash = table->type;
|
|
||||||
struct st_hash_type assochash;
|
|
||||||
|
|
||||||
assochash.compare = assoc_cmp;
|
args[0] = obj;
|
||||||
assochash.hash = orighash->hash;
|
args[1] = Qnil;
|
||||||
table->type = &assochash;
|
rb_hash_foreach(hash, assoc_i, (VALUE)args);
|
||||||
arg.hash = hash;
|
return args[1];
|
||||||
arg.key = key;
|
|
||||||
ensure_arg.hash = hash;
|
|
||||||
ensure_arg.orighash = orighash;
|
|
||||||
value = rb_ensure(lookup2_call, (VALUE)&arg, reset_hash_type, (VALUE)&ensure_arg);
|
|
||||||
if (value == Qundef) return Qnil;
|
|
||||||
return rb_assoc_new(key, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
|
|
@ -876,6 +876,7 @@ class TestHash < Test::Unit::TestCase
|
||||||
def test_assoc
|
def test_assoc
|
||||||
assert_equal([3,4], {1=>2, 3=>4, 5=>6}.assoc(3))
|
assert_equal([3,4], {1=>2, 3=>4, 5=>6}.assoc(3))
|
||||||
assert_nil({1=>2, 3=>4, 5=>6}.assoc(4))
|
assert_nil({1=>2, 3=>4, 5=>6}.assoc(4))
|
||||||
|
assert_equal([1.0,1], {1.0=>1}.assoc(1))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_rassoc
|
def test_rassoc
|
||||||
|
|
Загрузка…
Ссылка в новой задаче