diff --git a/ChangeLog b/ChangeLog index 9f1b24f7bc..a9da009790 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Thu Dec 12 17:27:19 2002 Yukihiro Matsumoto + + * re.c (rb_reg_hash): define Regexp#hash to make regexps to be + hash keys. + + * re.c (Init_Regexp): define Regexp#eql? (alias to Regexp#==). + Thu Dec 12 16:26:31 2002 Nobuyoshi Nakada * marshal.c (r_object0): singleton class instance can't be loaded. diff --git a/re.c b/re.c index 06f6502530..8d482736d4 100644 --- a/re.c +++ b/re.c @@ -1056,6 +1056,25 @@ rb_reg_cur_kcode(re) return 0; } +static VALUE +rb_reg_hash(re) + VALUE re; +{ + int hashval, len; + char *p; + + rb_reg_check(re); + hashval = RREGEXP(re)->ptr->options; + len = RREGEXP(re)->len; + p = RREGEXP(re)->str; + while (len--) { + hashval = hashval * 33 + *p++; + } + hashval = hashval + (hashval>>5); + + return INT2FIX(hashval); +} + static VALUE rb_reg_equal(re1, re2) VALUE re1, re2; @@ -1560,6 +1579,8 @@ Init_Regexp() rb_define_method(rb_cRegexp, "initialize", rb_reg_initialize_m, -1); rb_define_method(rb_cRegexp, "copy_object", rb_reg_copy_object, 1); + rb_define_method(rb_cRegexp, "hash", rb_reg_hash, 0); + rb_define_method(rb_cRegexp, "eql?", rb_reg_equal, 1); rb_define_method(rb_cRegexp, "==", rb_reg_equal, 1); rb_define_method(rb_cRegexp, "=~", rb_reg_match, 1); rb_define_method(rb_cRegexp, "===", rb_reg_match, 1);