** Create or reuse a zero-terminated string, first checking in the ** cache (using the string address as a key). The cache can contain ** only zero-terminated strings, so it is safe to use 'strcmp' to ** check hits. */
| 251 | ** check hits. |
| 252 | */ |
| 253 | TString *luaS_new (lua_State *L, const char *str) { |
| 254 | unsigned int i = point2uint(str) % STRCACHE_N; /* hash */ |
| 255 | int j; |
| 256 | TString **p = G(L)->strcache[i]; |
| 257 | for (j = 0; j < STRCACHE_M; j++) { |
| 258 | if (strcmp(str, getstr(p[j])) == 0) /* hit? */ |
| 259 | return p[j]; /* that is it */ |
| 260 | } |
| 261 | /* normal route */ |
| 262 | for (j = STRCACHE_M - 1; j > 0; j--) |
| 263 | p[j] = p[j - 1]; /* move out last element */ |
| 264 | /* new element is first in the list */ |
| 265 | p[0] = luaS_newlstr(L, str, strlen(str)); |
| 266 | return p[0]; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) { |
no test coverage detected