annotate lib/lua/lua-5.2.2/lstring.c @ 2410:2d3c3d1dff0c

added RenderStruct.h
author Ritor1
date Tue, 15 Jul 2014 15:44:45 +0600
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: lstring.c,v 2.26 2013/01/08 13:50:10 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** String table (keeps all strings handled by Lua)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 #include <string.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10 #define lstring_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11 #define LUA_CORE
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15 #include "lmem.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16 #include "lobject.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 #include "lstate.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18 #include "lstring.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22 ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 ** compute its hash
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25 #if !defined(LUAI_HASHLIMIT)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26 #define LUAI_HASHLIMIT 5
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31 ** equality for long strings
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33 int luaS_eqlngstr (TString *a, TString *b) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34 size_t len = a->tsv.len;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35 lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36 return (a == b) || /* same instance or... */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 ((len == b->tsv.len) && /* equal length and ... */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38 (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 ** equality for strings
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45 int luaS_eqstr (TString *a, TString *b) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 return (a->tsv.tt == b->tsv.tt) &&
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 (a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 unsigned int h = seed ^ cast(unsigned int, l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53 size_t l1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 size_t step = (l >> LUAI_HASHLIMIT) + 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55 for (l1 = l; l1 >= step; l1 -= step)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1]));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 return h;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 ** resizes the string table
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64 void luaS_resize (lua_State *L, int newsize) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66 stringtable *tb = &G(L)->strt;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67 /* cannot resize while GC is traversing strings */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 luaC_runtilstate(L, ~bitmask(GCSsweepstring));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69 if (newsize > tb->size) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70 luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71 for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73 /* rehash */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74 for (i=0; i<tb->size; i++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75 GCObject *p = tb->hash[i];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76 tb->hash[i] = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77 while (p) { /* for each node in the list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78 GCObject *next = gch(p)->next; /* save next */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79 unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80 gch(p)->next = tb->hash[h]; /* chain it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81 tb->hash[h] = p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82 resetoldbit(p); /* see MOVE OLD rule */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83 p = next;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86 if (newsize < tb->size) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 /* shrinking slice must be empty */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88 lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89 luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91 tb->size = newsize;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96 ** creates a new string object
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98 static TString *createstrobj (lua_State *L, const char *str, size_t l,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99 int tag, unsigned int h, GCObject **list) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100 TString *ts;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101 size_t totalsize; /* total size of TString object */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102 totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103 ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104 ts->tsv.len = l;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105 ts->tsv.hash = h;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 ts->tsv.extra = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 memcpy(ts+1, str, l*sizeof(char));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108 ((char *)(ts+1))[l] = '\0'; /* ending 0 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 return ts;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114 ** creates a new short string, inserting it into string table
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116 static TString *newshrstr (lua_State *L, const char *str, size_t l,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117 unsigned int h) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 GCObject **list; /* (pointer to) list where it will be inserted */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119 stringtable *tb = &G(L)->strt;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120 TString *s;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121 if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 luaS_resize(L, tb->size*2); /* too crowded */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123 list = &tb->hash[lmod(h, tb->size)];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124 s = createstrobj(L, str, l, LUA_TSHRSTR, h, list);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125 tb->nuse++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 return s;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131 ** checks whether short string exists and reuses it or creates a new one
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 static TString *internshrstr (lua_State *L, const char *str, size_t l) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 GCObject *o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 unsigned int h = luaS_hash(str, l, g->seed);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137 for (o = g->strt.hash[lmod(h, g->strt.size)];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138 o != NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139 o = gch(o)->next) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140 TString *ts = rawgco2ts(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141 if (h == ts->tsv.hash &&
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142 l == ts->tsv.len &&
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143 (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 changewhite(o); /* resurrect it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146 return ts;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149 return newshrstr(L, str, l, h); /* not found; create a new string */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154 ** new string (with explicit length)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156 TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157 if (l <= LUAI_MAXSHORTLEN) /* short string? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158 return internshrstr(L, str, l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160 if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161 luaM_toobig(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162 return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168 ** new zero-terminated string
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
169 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
170 TString *luaS_new (lua_State *L, const char *str) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
171 return luaS_newlstr(L, str, strlen(str));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
174
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
175 Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
176 Udata *u;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
177 if (s > MAX_SIZET - sizeof(Udata))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
178 luaM_toobig(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
179 u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
180 u->uv.len = s;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
181 u->uv.metatable = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
182 u->uv.env = e;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
183 return u;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
184 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
185