Browse Source

small changes in lstring

Roberto Ierusalimschy 24 years ago
parent
commit
08496eea8b
6 changed files with 30 additions and 44 deletions
  1. 2 2
      lapi.c
  2. 5 4
      llex.c
  3. 4 3
      lparser.c
  4. 14 30
      lstring.c
  5. 3 3
      lstring.h
  6. 2 2
      lvm.c

+ 2 - 2
lapi.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 1.113 2000/12/26 18:46:09 roberto Exp roberto $
+** $Id: lapi.c,v 1.114 2000/12/28 12:55:41 roberto Exp roberto $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -456,7 +456,7 @@ LUA_API int lua_next (lua_State *L, int index) {
 
 LUA_API int lua_getn (lua_State *L, int index) {
   Hash *h = hvalue(luaA_index(L, index));
-  const TObject *value = luaH_getstr(h, luaS_new(L, "n"));  /* value = h.n */
+  const TObject *value = luaH_getstr(h, luaS_newliteral(L, "n"));  /* = h.n */
   if (ttype(value) == LUA_TNUMBER)
     return (int)nvalue(value);
   else {

+ 5 - 4
llex.c

@@ -1,5 +1,5 @@
 /*
-** $Id: llex.c,v 1.72 2000/10/20 16:39:03 roberto Exp roberto $
+** $Id: llex.c,v 1.73 2001/01/10 16:40:56 roberto Exp roberto $
 ** Lexical Analyzer
 ** See Copyright Notice in lua.h
 */
@@ -133,7 +133,7 @@ void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
 #define save_and_next(L, LS, l)  (save(L, LS->current, l), next(LS))
 
 
-static const char *readname (LexState *LS) {
+static size_t readname (LexState *LS) {
   lua_State *L = LS->L;
   size_t l = 0;
   checkbuffer(L, 10, l);
@@ -142,7 +142,7 @@ static const char *readname (LexState *LS) {
     save_and_next(L, LS, l);
   } while (isalnum(LS->current) || LS->current == '_');
   save(L, '\0', l);
-  return L->Mbuffer;
+  return l-1;
 }
 
 
@@ -369,7 +369,8 @@ int luaX_lex (LexState *LS, SemInfo *seminfo) {
           return c;
         }
         tname: {  /* identifier or reserved word */
-          TString *ts = luaS_new(LS->L, readname(LS));
+          size_t l = readname(LS);
+          TString *ts = luaS_newlstr(LS->L, LS->L->Mbuffer, l);
           if (ts->marked >= RESERVEDMARK)  /* reserved word? */
             return ts->marked-RESERVEDMARK+FIRST_RESERVED;
           seminfo->ts = ts;

+ 4 - 3
lparser.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lparser.c,v 1.121 2000/12/28 12:55:41 roberto Exp roberto $
+** $Id: lparser.c,v 1.122 2001/01/10 16:40:56 roberto Exp roberto $
 ** LL(1) Parser and code generator for Lua
 ** See Copyright Notice in lua.h
 */
@@ -182,7 +182,7 @@ static void removelocalvars (LexState *ls, int nvars) {
 
 
 static void new_localvarstr (LexState *ls, const char *name, int n) {
-  new_localvar(ls, luaS_newfixed(ls->L, name), n);
+  new_localvar(ls, luaS_new(ls->L, name), n);
 }
 
 
@@ -884,7 +884,8 @@ static void forlist (LexState *ls, TString *indexname) {
   valname = str_checkname(ls);
   /* next test is dirty, but avoids `in' being a reserved word */
   check_condition(ls,
-       (ls->t.token == TK_NAME && ls->t.seminfo.ts == luaS_new(ls->L, "in")),
+       (ls->t.token == TK_NAME &&
+        ls->t.seminfo.ts == luaS_newliteral(ls->L, "in")),
        "`in' expected");
   next(ls);  /* skip `in' */
   exp1(ls);  /* table */

+ 14 - 30
lstring.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstring.c,v 1.47 2000/12/22 16:57:46 roberto Exp roberto $
+** $Id: lstring.c,v 1.48 2000/12/28 12:55:41 roberto Exp roberto $
 ** String table (keeps all strings handled by Lua)
 ** See Copyright Notice in lua.h
 */
@@ -15,6 +15,8 @@
 #include "lstring.h"
 
 
+#define lmod(s,size)	((int)((s) & ((size)-1)))
+
 
 void luaS_init (lua_State *L) {
   luaS_resize(L, &L->strt, MINPOWER2);
@@ -30,15 +32,6 @@ void luaS_freeall (lua_State *L) {
 }
 
 
-static luint32 hash_s (const char *s, size_t l) {
-  luint32 h = l;  /* seed */
-  size_t step = (l>>5)|1;  /* if string is too long, don't hash all its chars */
-  for (; l>=step; l-=step)
-    h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
-  return h;
-}
-
-
 void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
   TString **newhash = luaM_newvector(L, newsize, TString *);
   int i;
@@ -49,8 +42,8 @@ void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
     while (p) {  /* for each node in the list */
       TString *next = p->nexthash;  /* save next */
       luint32 h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value);
-      int h1 = h&(newsize-1);  /* new position */
-      LUA_ASSERT(h%newsize == (h&(newsize-1)),
+      int h1 = lmod(h, newsize);  /* new position */
+      LUA_ASSERT(h%newsize == lmod(h, newsize),
                     "a&(x-1) == a%x, for x power of 2");
       p->nexthash = newhash[h1];  /* chain it in new position */
       newhash[h1] = p;
@@ -74,10 +67,13 @@ static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) {
 
 
 TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
-  luint32 h = hash_s(str, l);
-  int h1 = h & (L->strt.size-1);
   TString *ts;
-  for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash) {
+  luint32 h = l;  /* seed */
+  size_t step = (l>>5)+1;  /* if string is too long, don't hash all its chars */
+  size_t l1;
+  for (l1=l; l1>=step; l1-=step)  /* compute hash */
+    h = h ^ ((h<<5)+(h>>2)+(unsigned char)str[l1-1]);
+  for (ts = L->strt.hash[lmod(h, L->strt.size)]; ts; ts = ts->nexthash) {
     if (ts->len == l && (memcmp(str, ts->str, l) == 0))
       return ts;
   }
@@ -90,7 +86,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
   ts->u.s.constindex = 0;
   memcpy(ts->str, str, l);
   ts->str[l] = 0;  /* ending 0 */
-  newentry(L, &L->strt, ts, h1);  /* insert it on table */
+  newentry(L, &L->strt, ts, lmod(h, L->strt.size));  /* insert it on table */
   return ts;
 }
 
@@ -104,13 +100,13 @@ TString *luaS_newudata (lua_State *L, size_t s, void *udata) {
   ts->u.d.tag = 0;
   ts->u.d.value = (udata == NULL) ? uts+1 : udata;
   /* insert it on table */
-  newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size-1));
+  newentry(L, &L->udt, ts, lmod(IntPoint(ts->u.d.value), L->udt.size));
   return ts;
 }
 
 
 TString *luaS_createudata (lua_State *L, void *udata, int tag) {
-  int h1 = IntPoint(udata) & (L->udt.size-1);
+  int h1 = lmod(IntPoint(udata), L->udt.size);
   TString *ts;
   for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) {
     if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
@@ -123,15 +119,3 @@ TString *luaS_createudata (lua_State *L, void *udata, int tag) {
   return ts;
 }
 
-
-TString *luaS_new (lua_State *L, const char *str) {
-  return luaS_newlstr(L, str, strlen(str));
-}
-
-
-TString *luaS_newfixed (lua_State *L, const char *str) {
-  TString *ts = luaS_new(L, str);
-  if (ts->marked == 0) ts->marked = FIXMARK;  /* avoid GC */
-  return ts;
-}
-

+ 3 - 3
lstring.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lstring.h,v 1.25 2000/11/24 17:39:56 roberto Exp roberto $
+** $Id: lstring.h,v 1.26 2000/12/28 12:55:41 roberto Exp roberto $
 ** String table (keep all strings handled by Lua)
 ** See Copyright Notice in lua.h
 */
@@ -36,6 +36,8 @@ union L_UTString {
 
 #define sizeudata(l)	((luint32)sizeof(union L_UTString)+(l))
 
+#define luaS_new(L, s)	(luaS_newlstr(L, s, strlen(s)))
+#define luaS_newliteral(L, s)	(luaS_newlstr(L, "" s, (sizeof(s))-1))
 
 void luaS_init (lua_State *L);
 void luaS_resize (lua_State *L, stringtable *tb, int newsize);
@@ -43,8 +45,6 @@ TString *luaS_newudata (lua_State *L, size_t s, void *udata);
 TString *luaS_createudata (lua_State *L, void *udata, int tag);
 void luaS_freeall (lua_State *L);
 TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
-TString *luaS_new (lua_State *L, const char *str);
-TString *luaS_newfixed (lua_State *L, const char *str);
 
 
 #endif

+ 2 - 2
lvm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.c,v 1.148 2000/12/04 18:33:40 roberto Exp roberto $
+** $Id: lvm.c,v 1.149 2000/12/28 12:55:41 roberto Exp roberto $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -325,7 +325,7 @@ static void luaV_pack (lua_State *L, StkId firstelem) {
   for (i=0; firstelem+i<L->top; i++)
     *luaH_setint(L, htab, i+1) = *(firstelem+i);
   /* store counter in field `n' */
-  luaH_setstrnum(L, htab, luaS_new(L, "n"), i);
+  luaH_setstrnum(L, htab, luaS_newliteral(L, "n"), i);
   L->top = firstelem;  /* remove elements from the stack */
   ttype(L->top) = LUA_TTABLE;
   hvalue(L->top) = htab;