aboutsummaryrefslogtreecommitdiff
path: root/lstring.c
diff options
context:
space:
mode:
Diffstat (limited to 'lstring.c')
-rw-r--r--lstring.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/lstring.c b/lstring.c
index a6ffbdd0..6f157473 100644
--- a/lstring.c
+++ b/lstring.c
@@ -23,7 +23,7 @@
23 23
24 24
25/* 25/*
26** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to 26** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a long string to
27** compute its hash 27** compute its hash
28*/ 28*/
29#if !defined(LUAI_HASHLIMIT) 29#if !defined(LUAI_HASHLIMIT)
@@ -50,9 +50,9 @@ int luaS_eqlngstr (TString *a, TString *b) {
50} 50}
51 51
52 52
53unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { 53unsigned int luaS_hash (const char *str, size_t l, unsigned int seed,
54 size_t step) {
54 unsigned int h = seed ^ cast_uint(l); 55 unsigned int h = seed ^ cast_uint(l);
55 size_t step = (l >> LUAI_HASHLIMIT) + 1;
56 for (; l >= step; l -= step) 56 for (; l >= step; l -= step)
57 h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); 57 h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
58 return h; 58 return h;
@@ -62,7 +62,9 @@ unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
62unsigned int luaS_hashlongstr (TString *ts) { 62unsigned int luaS_hashlongstr (TString *ts) {
63 lua_assert(ts->tt == LUA_VLNGSTR); 63 lua_assert(ts->tt == LUA_VLNGSTR);
64 if (ts->extra == 0) { /* no hash? */ 64 if (ts->extra == 0) { /* no hash? */
65 ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash); 65 size_t len = ts->u.lnglen;
66 size_t step = (len >> LUAI_HASHLIMIT) + 1;
67 ts->hash = luaS_hash(getstr(ts), len, ts->hash, step);
66 ts->extra = 1; /* now it has its hash */ 68 ts->extra = 1; /* now it has its hash */
67 } 69 }
68 return ts->hash; 70 return ts->hash;
@@ -199,7 +201,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
199 TString *ts; 201 TString *ts;
200 global_State *g = G(L); 202 global_State *g = G(L);
201 stringtable *tb = &g->strt; 203 stringtable *tb = &g->strt;
202 unsigned int h = luaS_hash(str, l, g->seed); 204 unsigned int h = luaS_hash(str, l, g->seed, 1);
203 TString **list = &tb->hash[lmod(h, tb->size)]; 205 TString **list = &tb->hash[lmod(h, tb->size)];
204 lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ 206 lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
205 for (ts = *list; ts != NULL; ts = ts->u.hnext) { 207 for (ts = *list; ts != NULL; ts = ts->u.hnext) {