diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-01-10 15:41:50 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-01-10 15:41:50 -0200 |
commit | 08496eea8b277d37c4de9cf75a011715ad6a4100 (patch) | |
tree | 9c3eef538b85f79ece0a0993e4d99930be6f6039 | |
parent | 4ff55457095728b95cc5dcdbab0bca7255bd5387 (diff) | |
download | lua-08496eea8b277d37c4de9cf75a011715ad6a4100.tar.gz lua-08496eea8b277d37c4de9cf75a011715ad6a4100.tar.bz2 lua-08496eea8b277d37c4de9cf75a011715ad6a4100.zip |
small changes in lstring
-rw-r--r-- | lapi.c | 4 | ||||
-rw-r--r-- | llex.c | 9 | ||||
-rw-r--r-- | lparser.c | 7 | ||||
-rw-r--r-- | lstring.c | 44 | ||||
-rw-r--r-- | lstring.h | 6 | ||||
-rw-r--r-- | lvm.c | 4 |
6 files changed, 30 insertions, 44 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.113 2000/12/26 18:46:09 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.114 2000/12/28 12:55:41 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -456,7 +456,7 @@ LUA_API int lua_next (lua_State *L, int index) { | |||
456 | 456 | ||
457 | LUA_API int lua_getn (lua_State *L, int index) { | 457 | LUA_API int lua_getn (lua_State *L, int index) { |
458 | Hash *h = hvalue(luaA_index(L, index)); | 458 | Hash *h = hvalue(luaA_index(L, index)); |
459 | const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */ | 459 | const TObject *value = luaH_getstr(h, luaS_newliteral(L, "n")); /* = h.n */ |
460 | if (ttype(value) == LUA_TNUMBER) | 460 | if (ttype(value) == LUA_TNUMBER) |
461 | return (int)nvalue(value); | 461 | return (int)nvalue(value); |
462 | else { | 462 | else { |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: llex.c,v 1.72 2000/10/20 16:39:03 roberto Exp roberto $ | 2 | ** $Id: llex.c,v 1.73 2001/01/10 16:40:56 roberto Exp roberto $ |
3 | ** Lexical Analyzer | 3 | ** Lexical Analyzer |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -133,7 +133,7 @@ void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) { | |||
133 | #define save_and_next(L, LS, l) (save(L, LS->current, l), next(LS)) | 133 | #define save_and_next(L, LS, l) (save(L, LS->current, l), next(LS)) |
134 | 134 | ||
135 | 135 | ||
136 | static const char *readname (LexState *LS) { | 136 | static size_t readname (LexState *LS) { |
137 | lua_State *L = LS->L; | 137 | lua_State *L = LS->L; |
138 | size_t l = 0; | 138 | size_t l = 0; |
139 | checkbuffer(L, 10, l); | 139 | checkbuffer(L, 10, l); |
@@ -142,7 +142,7 @@ static const char *readname (LexState *LS) { | |||
142 | save_and_next(L, LS, l); | 142 | save_and_next(L, LS, l); |
143 | } while (isalnum(LS->current) || LS->current == '_'); | 143 | } while (isalnum(LS->current) || LS->current == '_'); |
144 | save(L, '\0', l); | 144 | save(L, '\0', l); |
145 | return L->Mbuffer; | 145 | return l-1; |
146 | } | 146 | } |
147 | 147 | ||
148 | 148 | ||
@@ -369,7 +369,8 @@ int luaX_lex (LexState *LS, SemInfo *seminfo) { | |||
369 | return c; | 369 | return c; |
370 | } | 370 | } |
371 | tname: { /* identifier or reserved word */ | 371 | tname: { /* identifier or reserved word */ |
372 | TString *ts = luaS_new(LS->L, readname(LS)); | 372 | size_t l = readname(LS); |
373 | TString *ts = luaS_newlstr(LS->L, LS->L->Mbuffer, l); | ||
373 | if (ts->marked >= RESERVEDMARK) /* reserved word? */ | 374 | if (ts->marked >= RESERVEDMARK) /* reserved word? */ |
374 | return ts->marked-RESERVEDMARK+FIRST_RESERVED; | 375 | return ts->marked-RESERVEDMARK+FIRST_RESERVED; |
375 | seminfo->ts = ts; | 376 | seminfo->ts = ts; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.c,v 1.121 2000/12/28 12:55:41 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 1.122 2001/01/10 16:40:56 roberto Exp roberto $ |
3 | ** LL(1) Parser and code generator for Lua | 3 | ** LL(1) Parser and code generator for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -182,7 +182,7 @@ static void removelocalvars (LexState *ls, int nvars) { | |||
182 | 182 | ||
183 | 183 | ||
184 | static void new_localvarstr (LexState *ls, const char *name, int n) { | 184 | static void new_localvarstr (LexState *ls, const char *name, int n) { |
185 | new_localvar(ls, luaS_newfixed(ls->L, name), n); | 185 | new_localvar(ls, luaS_new(ls->L, name), n); |
186 | } | 186 | } |
187 | 187 | ||
188 | 188 | ||
@@ -884,7 +884,8 @@ static void forlist (LexState *ls, TString *indexname) { | |||
884 | valname = str_checkname(ls); | 884 | valname = str_checkname(ls); |
885 | /* next test is dirty, but avoids `in' being a reserved word */ | 885 | /* next test is dirty, but avoids `in' being a reserved word */ |
886 | check_condition(ls, | 886 | check_condition(ls, |
887 | (ls->t.token == TK_NAME && ls->t.seminfo.ts == luaS_new(ls->L, "in")), | 887 | (ls->t.token == TK_NAME && |
888 | ls->t.seminfo.ts == luaS_newliteral(ls->L, "in")), | ||
888 | "`in' expected"); | 889 | "`in' expected"); |
889 | next(ls); /* skip `in' */ | 890 | next(ls); /* skip `in' */ |
890 | exp1(ls); /* table */ | 891 | exp1(ls); /* table */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 1.47 2000/12/22 16:57:46 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 1.48 2000/12/28 12:55:41 roberto Exp roberto $ |
3 | ** String table (keeps all strings handled by Lua) | 3 | ** String table (keeps all strings handled by Lua) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -15,6 +15,8 @@ | |||
15 | #include "lstring.h" | 15 | #include "lstring.h" |
16 | 16 | ||
17 | 17 | ||
18 | #define lmod(s,size) ((int)((s) & ((size)-1))) | ||
19 | |||
18 | 20 | ||
19 | void luaS_init (lua_State *L) { | 21 | void luaS_init (lua_State *L) { |
20 | luaS_resize(L, &L->strt, MINPOWER2); | 22 | luaS_resize(L, &L->strt, MINPOWER2); |
@@ -30,15 +32,6 @@ void luaS_freeall (lua_State *L) { | |||
30 | } | 32 | } |
31 | 33 | ||
32 | 34 | ||
33 | static luint32 hash_s (const char *s, size_t l) { | ||
34 | luint32 h = l; /* seed */ | ||
35 | size_t step = (l>>5)|1; /* if string is too long, don't hash all its chars */ | ||
36 | for (; l>=step; l-=step) | ||
37 | h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++)); | ||
38 | return h; | ||
39 | } | ||
40 | |||
41 | |||
42 | void luaS_resize (lua_State *L, stringtable *tb, int newsize) { | 35 | void luaS_resize (lua_State *L, stringtable *tb, int newsize) { |
43 | TString **newhash = luaM_newvector(L, newsize, TString *); | 36 | TString **newhash = luaM_newvector(L, newsize, TString *); |
44 | int i; | 37 | int i; |
@@ -49,8 +42,8 @@ void luaS_resize (lua_State *L, stringtable *tb, int newsize) { | |||
49 | while (p) { /* for each node in the list */ | 42 | while (p) { /* for each node in the list */ |
50 | TString *next = p->nexthash; /* save next */ | 43 | TString *next = p->nexthash; /* save next */ |
51 | luint32 h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value); | 44 | luint32 h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value); |
52 | int h1 = h&(newsize-1); /* new position */ | 45 | int h1 = lmod(h, newsize); /* new position */ |
53 | LUA_ASSERT(h%newsize == (h&(newsize-1)), | 46 | LUA_ASSERT(h%newsize == lmod(h, newsize), |
54 | "a&(x-1) == a%x, for x power of 2"); | 47 | "a&(x-1) == a%x, for x power of 2"); |
55 | p->nexthash = newhash[h1]; /* chain it in new position */ | 48 | p->nexthash = newhash[h1]; /* chain it in new position */ |
56 | newhash[h1] = p; | 49 | newhash[h1] = p; |
@@ -74,10 +67,13 @@ static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) { | |||
74 | 67 | ||
75 | 68 | ||
76 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | 69 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { |
77 | luint32 h = hash_s(str, l); | ||
78 | int h1 = h & (L->strt.size-1); | ||
79 | TString *ts; | 70 | TString *ts; |
80 | for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash) { | 71 | luint32 h = l; /* seed */ |
72 | size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ | ||
73 | size_t l1; | ||
74 | for (l1=l; l1>=step; l1-=step) /* compute hash */ | ||
75 | h = h ^ ((h<<5)+(h>>2)+(unsigned char)str[l1-1]); | ||
76 | for (ts = L->strt.hash[lmod(h, L->strt.size)]; ts; ts = ts->nexthash) { | ||
81 | if (ts->len == l && (memcmp(str, ts->str, l) == 0)) | 77 | if (ts->len == l && (memcmp(str, ts->str, l) == 0)) |
82 | return ts; | 78 | return ts; |
83 | } | 79 | } |
@@ -90,7 +86,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | |||
90 | ts->u.s.constindex = 0; | 86 | ts->u.s.constindex = 0; |
91 | memcpy(ts->str, str, l); | 87 | memcpy(ts->str, str, l); |
92 | ts->str[l] = 0; /* ending 0 */ | 88 | ts->str[l] = 0; /* ending 0 */ |
93 | newentry(L, &L->strt, ts, h1); /* insert it on table */ | 89 | newentry(L, &L->strt, ts, lmod(h, L->strt.size)); /* insert it on table */ |
94 | return ts; | 90 | return ts; |
95 | } | 91 | } |
96 | 92 | ||
@@ -104,13 +100,13 @@ TString *luaS_newudata (lua_State *L, size_t s, void *udata) { | |||
104 | ts->u.d.tag = 0; | 100 | ts->u.d.tag = 0; |
105 | ts->u.d.value = (udata == NULL) ? uts+1 : udata; | 101 | ts->u.d.value = (udata == NULL) ? uts+1 : udata; |
106 | /* insert it on table */ | 102 | /* insert it on table */ |
107 | newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size-1)); | 103 | newentry(L, &L->udt, ts, lmod(IntPoint(ts->u.d.value), L->udt.size)); |
108 | return ts; | 104 | return ts; |
109 | } | 105 | } |
110 | 106 | ||
111 | 107 | ||
112 | TString *luaS_createudata (lua_State *L, void *udata, int tag) { | 108 | TString *luaS_createudata (lua_State *L, void *udata, int tag) { |
113 | int h1 = IntPoint(udata) & (L->udt.size-1); | 109 | int h1 = lmod(IntPoint(udata), L->udt.size); |
114 | TString *ts; | 110 | TString *ts; |
115 | for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) { | 111 | for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) { |
116 | if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG)) | 112 | 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) { | |||
123 | return ts; | 119 | return ts; |
124 | } | 120 | } |
125 | 121 | ||
126 | |||
127 | TString *luaS_new (lua_State *L, const char *str) { | ||
128 | return luaS_newlstr(L, str, strlen(str)); | ||
129 | } | ||
130 | |||
131 | |||
132 | TString *luaS_newfixed (lua_State *L, const char *str) { | ||
133 | TString *ts = luaS_new(L, str); | ||
134 | if (ts->marked == 0) ts->marked = FIXMARK; /* avoid GC */ | ||
135 | return ts; | ||
136 | } | ||
137 | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.h,v 1.25 2000/11/24 17:39:56 roberto Exp roberto $ | 2 | ** $Id: lstring.h,v 1.26 2000/12/28 12:55:41 roberto Exp roberto $ |
3 | ** String table (keep all strings handled by Lua) | 3 | ** String table (keep all strings handled by Lua) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -36,6 +36,8 @@ union L_UTString { | |||
36 | 36 | ||
37 | #define sizeudata(l) ((luint32)sizeof(union L_UTString)+(l)) | 37 | #define sizeudata(l) ((luint32)sizeof(union L_UTString)+(l)) |
38 | 38 | ||
39 | #define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) | ||
40 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, (sizeof(s))-1)) | ||
39 | 41 | ||
40 | void luaS_init (lua_State *L); | 42 | void luaS_init (lua_State *L); |
41 | void luaS_resize (lua_State *L, stringtable *tb, int newsize); | 43 | 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); | |||
43 | TString *luaS_createudata (lua_State *L, void *udata, int tag); | 45 | TString *luaS_createudata (lua_State *L, void *udata, int tag); |
44 | void luaS_freeall (lua_State *L); | 46 | void luaS_freeall (lua_State *L); |
45 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l); | 47 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l); |
46 | TString *luaS_new (lua_State *L, const char *str); | ||
47 | TString *luaS_newfixed (lua_State *L, const char *str); | ||
48 | 48 | ||
49 | 49 | ||
50 | #endif | 50 | #endif |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 1.148 2000/12/04 18:33:40 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.149 2000/12/28 12:55:41 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -325,7 +325,7 @@ static void luaV_pack (lua_State *L, StkId firstelem) { | |||
325 | for (i=0; firstelem+i<L->top; i++) | 325 | for (i=0; firstelem+i<L->top; i++) |
326 | *luaH_setint(L, htab, i+1) = *(firstelem+i); | 326 | *luaH_setint(L, htab, i+1) = *(firstelem+i); |
327 | /* store counter in field `n' */ | 327 | /* store counter in field `n' */ |
328 | luaH_setstrnum(L, htab, luaS_new(L, "n"), i); | 328 | luaH_setstrnum(L, htab, luaS_newliteral(L, "n"), i); |
329 | L->top = firstelem; /* remove elements from the stack */ | 329 | L->top = firstelem; /* remove elements from the stack */ |
330 | ttype(L->top) = LUA_TTABLE; | 330 | ttype(L->top) = LUA_TTABLE; |
331 | hvalue(L->top) = htab; | 331 | hvalue(L->top) = htab; |