11d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III/*
21d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III** $Id: lstring.c,v 2.26 2013/01/08 13:50:10 roberto Exp $
31d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III** String table (keeps all strings handled by Lua)
41d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III** See Copyright Notice in lua.h
51d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III*/
61d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
71d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
81d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III#include <string.h>
91d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
101d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III#define lstring_c
111d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III#define LUA_CORE
121d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
131d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III#include "lua.h"
141d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
151d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III#include "lmem.h"
161d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III#include "lobject.h"
171d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III#include "lstate.h"
181d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III#include "lstring.h"
191d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
201d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
211d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III/*
221d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
231d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III** compute its hash
241d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III*/
251d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III#if !defined(LUAI_HASHLIMIT)
261d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III#define LUAI_HASHLIMIT		5
271d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III#endif
281d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
291d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
301d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III/*
311d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III** equality for long strings
321d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III*/
331d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins IIIint luaS_eqlngstr (TString *a, TString *b) {
341d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  size_t len = a->tsv.len;
351d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR);
361d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  return (a == b) ||  /* same instance or... */
371d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    ((len == b->tsv.len) &&  /* equal length and ... */
381d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III     (memcmp(getstr(a), getstr(b), len) == 0));  /* equal contents */
391d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III}
401d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
411d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
421d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III/*
431d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III** equality for strings
441d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III*/
451d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins IIIint luaS_eqstr (TString *a, TString *b) {
461d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  return (a->tsv.tt == b->tsv.tt) &&
471d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III         (a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b));
481d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III}
491d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
501d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
511d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins IIIunsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
521d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  unsigned int h = seed ^ cast(unsigned int, l);
531d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  size_t l1;
541d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  size_t step = (l >> LUAI_HASHLIMIT) + 1;
551d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  for (l1 = l; l1 >= step; l1 -= step)
561d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1]));
571d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  return h;
581d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III}
591d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
601d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
611d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III/*
621d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III** resizes the string table
631d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III*/
641d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins IIIvoid luaS_resize (lua_State *L, int newsize) {
651d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  int i;
661d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  stringtable *tb = &G(L)->strt;
671d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  /* cannot resize while GC is traversing strings */
681d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  luaC_runtilstate(L, ~bitmask(GCSsweepstring));
691d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  if (newsize > tb->size) {
701d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
711d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL;
721d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  }
731d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  /* rehash */
741d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  for (i=0; i<tb->size; i++) {
751d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    GCObject *p = tb->hash[i];
761d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    tb->hash[i] = NULL;
771d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    while (p) {  /* for each node in the list */
781d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III      GCObject *next = gch(p)->next;  /* save next */
791d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III      unsigned int h = lmod(gco2ts(p)->hash, newsize);  /* new position */
801d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III      gch(p)->next = tb->hash[h];  /* chain it */
811d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III      tb->hash[h] = p;
821d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III      resetoldbit(p);  /* see MOVE OLD rule */
831d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III      p = next;
841d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    }
851d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  }
861d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  if (newsize < tb->size) {
871d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    /* shrinking slice must be empty */
881d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
891d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
901d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  }
911d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  tb->size = newsize;
921d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III}
931d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
941d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
951d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III/*
961d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III** creates a new string object
971d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III*/
981d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins IIIstatic TString *createstrobj (lua_State *L, const char *str, size_t l,
991d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III                              int tag, unsigned int h, GCObject **list) {
1001d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  TString *ts;
1011d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  size_t totalsize;  /* total size of TString object */
1021d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
1031d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts;
1041d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  ts->tsv.len = l;
1051d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  ts->tsv.hash = h;
1061d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  ts->tsv.extra = 0;
1071d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  memcpy(ts+1, str, l*sizeof(char));
1081d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  ((char *)(ts+1))[l] = '\0';  /* ending 0 */
1091d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  return ts;
1101d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III}
1111d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
1121d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
1131d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III/*
1141d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III** creates a new short string, inserting it into string table
1151d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III*/
1161d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins IIIstatic TString *newshrstr (lua_State *L, const char *str, size_t l,
1171d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III                                       unsigned int h) {
1181d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  GCObject **list;  /* (pointer to) list where it will be inserted */
1191d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  stringtable *tb = &G(L)->strt;
1201d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  TString *s;
1211d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
1221d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    luaS_resize(L, tb->size*2);  /* too crowded */
1231d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  list = &tb->hash[lmod(h, tb->size)];
1241d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  s = createstrobj(L, str, l, LUA_TSHRSTR, h, list);
1251d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  tb->nuse++;
1261d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  return s;
1271d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III}
1281d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
1291d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
1301d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III/*
1311d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III** checks whether short string exists and reuses it or creates a new one
1321d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III*/
1331d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins IIIstatic TString *internshrstr (lua_State *L, const char *str, size_t l) {
1341d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  GCObject *o;
1351d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  global_State *g = G(L);
1361d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  unsigned int h = luaS_hash(str, l, g->seed);
1371d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  for (o = g->strt.hash[lmod(h, g->strt.size)];
1381d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III       o != NULL;
1391d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III       o = gch(o)->next) {
1401d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    TString *ts = rawgco2ts(o);
1411d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    if (h == ts->tsv.hash &&
1421d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III        l == ts->tsv.len &&
1431d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III        (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
1441d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III      if (isdead(G(L), o))  /* string is dead (but was not collected yet)? */
1451d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III        changewhite(o);  /* resurrect it */
1461d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III      return ts;
1471d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    }
1481d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  }
1491d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  return newshrstr(L, str, l, h);  /* not found; create a new string */
1501d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III}
1511d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
1521d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
1531d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III/*
1541d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III** new string (with explicit length)
1551d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III*/
1561d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins IIITString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
1571d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  if (l <= LUAI_MAXSHORTLEN)  /* short string? */
1581d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    return internshrstr(L, str, l);
1591d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  else {
1601d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
1611d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III      luaM_toobig(L);
1621d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL);
1631d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  }
1641d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III}
1651d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
1661d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
1671d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III/*
1681d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III** new zero-terminated string
1691d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III*/
1701d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins IIITString *luaS_new (lua_State *L, const char *str) {
1711d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  return luaS_newlstr(L, str, strlen(str));
1721d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III}
1731d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
1741d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
1751d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins IIIUdata *luaS_newudata (lua_State *L, size_t s, Table *e) {
1761d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  Udata *u;
1771d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  if (s > MAX_SIZET - sizeof(Udata))
1781d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III    luaM_toobig(L);
1791d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u;
1801d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  u->uv.len = s;
1811d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  u->uv.metatable = NULL;
1821d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  u->uv.env = e;
1831d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III  return u;
1841d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III}
1851d9a26b35628ec99e6b03380bdedec2a0135d5f9Leon Scroggins III
186