diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-01-02 16:47:59 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-01-02 20:28:29 +0900 |
| commit | 392aff70992260267590f431fb50c87b9d445ce2 (patch) | |
| tree | 8168dd14fa19cba3433e9a8bba549667d4d82335 /src/string.c | |
| parent | d55367582c6fa1c63a32530a9b55f37e0086f354 (diff) | |
| download | mruby-392aff70992260267590f431fb50c87b9d445ce2.tar.gz mruby-392aff70992260267590f431fb50c87b9d445ce2.zip | |
Use Jenkins One At A Time Hash for `mrb_str_hash()`.
Diffstat (limited to 'src/string.c')
| -rw-r--r-- | src/string.c | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/src/string.c b/src/string.c index 749d32698..946dc8be1 100644 --- a/src/string.c +++ b/src/string.c @@ -1752,13 +1752,17 @@ mrb_str_hash(mrb_state *mrb, mrb_value str) struct RString *s = mrb_str_ptr(str); mrb_int len = RSTR_LEN(s); char *p = RSTR_PTR(s); - uint32_t key = 0; + uint32_t hash = 0; - while (len--) { - key = (key << 6) + (key << 16) - key + *p; - p++; + for(int i = 0; i < len; ++i) { + hash += p[i]; + hash += (hash << 10); + hash ^= (hash >> 6); } - return key + (key>>5); + hash += (hash << 3); + hash ^= (hash >> 11); + hash += (hash << 15); + return hash; } /* 15.2.10.5.20 */ |
