Skip to content

Commit 6d68378

Browse files
committed
etc.c: use mrb_byte_hash instead of simple hash function.
1 parent 61f447e commit 6d68378

2 files changed

Lines changed: 5 additions & 16 deletions

File tree

include/mruby/internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ mrb_value mrb_str_inspect(mrb_state *mrb, mrb_value str);
119119
mrb_bool mrb_str_beg_len(mrb_int str_len, mrb_int *begp, mrb_int *lenp);
120120
mrb_value mrb_str_byte_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len);
121121
mrb_value mrb_str_aref(mrb_state *mrb, mrb_value str, mrb_value idx, mrb_value len);
122+
uint32_t mrb_byte_hash(const uint8_t*, mrb_int);
123+
uint32_t mrb_byte_hash_step(const uint8_t*, mrb_int, uint32_t);
122124

123125
#ifdef MRB_UTF8_STRING
124126
mrb_int mrb_utf8len(const char *str, const char *end);

src/etc.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <mruby/data.h>
1010
#include <mruby/class.h>
1111
#include <mruby/numeric.h>
12+
#include <mruby/internal.h>
1213

1314
MRB_API struct RData*
1415
mrb_data_object_alloc(mrb_state *mrb, struct RClass *klass, void *ptr, const mrb_data_type *type)
@@ -70,24 +71,10 @@ mrb_obj_to_sym(mrb_state *mrb, mrb_value name)
7071
return 0; /* not reached */
7172
}
7273

73-
static mrb_int
74-
make_num_id(const char *p, size_t len)
75-
{
76-
uint32_t id = 0;
77-
78-
while (len--) {
79-
id = id*65599 + *p;
80-
p++;
81-
}
82-
id = id + (id>>5);
83-
84-
return (mrb_int)id;
85-
}
86-
8774
MRB_API mrb_int
8875
mrb_int_id(mrb_int n)
8976
{
90-
return make_num_id((const char*)&n, sizeof(n));
77+
return mrb_byte_hash((uint8_t*)&n, sizeof(n));
9178
}
9279

9380
#ifndef MRB_NO_FLOAT
@@ -96,7 +83,7 @@ mrb_float_id(mrb_float f)
9683
{
9784
/* normalize -0.0 to 0.0 */
9885
if (f == 0) f = 0.0;
99-
return make_num_id((const char*)&f, sizeof(f));
86+
return (mrb_int)mrb_byte_hash((uint8_t*)&f, sizeof(f));
10087
}
10188
#endif
10289

0 commit comments

Comments
 (0)