Skip to content

Instantly share code, notes, and snippets.

@methodmissing
Created August 29, 2009 23:51
Show Gist options
  • Select an option

  • Save methodmissing/177771 to your computer and use it in GitHub Desktop.

Select an option

Save methodmissing/177771 to your computer and use it in GitHub Desktop.
/* hash.c */
static int
rb_strhash_hash_cmp(VALUE a, VALUE b)
{
#ifdef RUBY18
VALUE args[2];
#endif
if (a == b) return 0;
if (FIXNUM_P(a) && FIXNUM_P(b)) return a != b;
if (a == Qundef || b == Qundef) return -1;
if (SYMBOL_P(a) && SYMBOL_P(b)) return a != b;
if ((TYPE(a) == T_STRING && RBASIC(a)->klass == rb_cString && SYMBOL_P(b)) || (TYPE(b) == T_STRING && RBASIC(b)->klass == rb_cString && SYMBOL_P(a))) {
return rb_strhash_cmp(&a, &b);
}
if (TYPE(a) == T_STRING && RBASIC(a)->klass == rb_cString &&
TYPE(b) == T_STRING && RBASIC(b)->klass == rb_cString) {
return rb_str_cmp(a, b);
}
#ifdef RUBY18
args[0] = a;
args[1] = b;
return !rb_with_disable_interrupt(eql, (VALUE)args);
#else
return !rb_eql(a, b);
#endif
}
/* hash.c */
static int
rb_strhash_hash(VALUE a)
{
VALUE hval;
int hnum;
switch (TYPE(a)) {
case T_FIXNUM:
#ifdef RUBY18
return (int)a;
#else
hnum = (int)a;
break;
#endif
case T_SYMBOL:
hnum = rb_sym_strhash(&a);
break;
case T_STRING:
hnum = rb_str_strhash(&a);
break;
default:
hval = rb_hash(a);
#ifdef RUBY18
if (!FIXNUM_P(hval)) {
hval = rb_funcall(hval, '%', 1, hash_format);
}
#endif
hnum = (int)hval;
}
#ifdef RUBY18
return hnum;
#else
hnum <<= 1;
return RSHIFT(hnum, 1);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment