MCPcopy Create free account
hub / github.com/ArduPilot/ardupilot / internshrstr

Function internshrstr

libraries/AP_Scripting/lua/src/lstring.c:172–198  ·  view source on GitHub ↗

** checks whether short string exists and reuses it or creates a new one */

Source from the content-addressed store, hash-verified

170** checks whether short string exists and reuses it or creates a new one
171*/
172static TString *internshrstr (lua_State *L, const char *str, size_t l) {
173 TString *ts;
174 global_State *g = G(L);
175 unsigned int h = luaS_hash(str, l, g->seed);
176 TString **list = &g->strt.hash[lmod(h, g->strt.size)];
177 lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
178 for (ts = *list; ts != NULL; ts = ts->u.hnext) {
179 if (l == ts->shrlen &&
180 (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
181 /* found! */
182 if (isdead(g, ts)) /* dead (but not collected yet)? */
183 changewhite(ts); /* resurrect it */
184 return ts;
185 }
186 }
187 if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
188 luaS_resize(L, g->strt.size * 2);
189 list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
190 }
191 ts = createstrobj(L, l, LUA_TSHRSTR, h);
192 memcpy(getstr(ts), str, l * sizeof(char));
193 ts->shrlen = cast_byte(l);
194 ts->u.hnext = *list;
195 *list = ts;
196 g->strt.nuse++;
197 return ts;
198}
199
200
201/*

Callers 1

luaS_newlstrFunction · 0.85

Calls 4

luaS_hashFunction · 0.85
luaS_resizeFunction · 0.85
createstrobjFunction · 0.85
memcpyFunction · 0.85

Tested by

no test coverage detected