** checks whether short string exists and reuses it or creates a new one */
| 165 | ** checks whether short string exists and reuses it or creates a new one |
| 166 | */ |
| 167 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { |
| 168 | TString *ts; |
| 169 | global_State *g = G(L); |
| 170 | unsigned int h = luaS_hash(str, l, g->seed); |
| 171 | TString **list = &g->strt.hash[lmod(h, g->strt.size)]; |
| 172 | lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ |
| 173 | for (ts = *list; ts != NULL; ts = ts->u.hnext) { |
| 174 | if (l == ts->shrlen && |
| 175 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { |
| 176 | /* found! */ |
| 177 | if (isdead(g, ts)) /* dead (but not collected yet)? */ |
| 178 | changewhite(ts); /* resurrect it */ |
| 179 | return ts; |
| 180 | } |
| 181 | } |
| 182 | if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) { |
| 183 | luaS_resize(L, g->strt.size * 2); |
| 184 | list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ |
| 185 | } |
| 186 | ts = createstrobj(L, l, LUA_TSHRSTR, h); |
| 187 | memcpy(getstr(ts), str, l * sizeof(char)); |
| 188 | ts->shrlen = cast_byte(l); |
| 189 | ts->u.hnext = *list; |
| 190 | *list = ts; |
| 191 | g->strt.nuse++; |
| 192 | return ts; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /* |
no test coverage detected