@@ -53,15 +53,6 @@ int luaS_eqstr (TString *a, TString *b) {
5353}
5454
5555
56- // Phase 115.1: Pointer-based implementation for performance
57- unsigned luaS_hash (const char *str, size_t l, unsigned seed) {
58- unsigned int h = seed ^ cast_uint (l);
59- for (; l > 0 ; l--)
60- h ^= ((h<<5 ) + (h>>2 ) + cast_byte (str[l - 1 ]));
61- return h;
62- }
63-
64-
6556unsigned luaS_hashlongstr (TString *ts) {
6657 return ts->hashLongStr ();
6758}
@@ -137,15 +128,31 @@ void luaS_init (lua_State *L) {
137128 tablerehash (tb->getHash (), 0 , MINSTRTABSIZE); /* clear array */
138129 tb->setSize (MINSTRTABSIZE);
139130 /* pre-create memory-error message */
140- g->setMemErrMsg (luaS_newliteral (L, MEMERRMSG));
131+ g->setMemErrMsg (TString::create (L, MEMERRMSG, sizeof (MEMERRMSG) - 1 ));
141132 obj2gco (g->getMemErrMsg ())->fix (L); /* Phase 25c: it should never be collected */
142133 for (i = 0 ; i < STRCACHE_N; i++) /* fill cache with valid strings */
143134 for (j = 0 ; j < STRCACHE_M; j++)
144135 g->setStrCache (i, j, g->getMemErrMsg ());
145136}
146137
147138
148- size_t luaS_sizelngstr (size_t len, int kind) {
139+ // Phase 122: TString static method implementations
140+
141+ // Static helper: hash a string
142+ unsigned TString::hashString (const char * str, size_t l, unsigned seed) {
143+ unsigned int h = seed ^ cast_uint (l);
144+ for (; l > 0 ; l--)
145+ h ^= ((h<<5 ) + (h>>2 ) + cast_byte (str[l - 1 ]));
146+ return h;
147+ }
148+
149+ // Static helper: hash a string (std::span overload)
150+ unsigned TString::hashString (std::span<const char > str, unsigned seed) {
151+ return hashString (str.data (), str.size (), seed);
152+ }
153+
154+ // Static helper: calculate size of long string
155+ size_t TString::longStringSize (size_t len, int kind) {
149156 switch (kind) {
150157 case LSTRREG: /* regular long string */
151158 /* don't need 'falloc'/'ud', but need space for content */
@@ -222,8 +229,9 @@ static TString *createstrobj (lua_State *L, size_t totalsize, lu_byte tag,
222229}
223230
224231
225- TString *luaS_createlngstrobj (lua_State *L, size_t l) {
226- size_t totalsize = luaS_sizelngstr (l, LSTRREG);
232+ // Static factory: create long string object
233+ TString* TString::createLongString (lua_State* L, size_t l) {
234+ size_t totalsize = longStringSize (l, LSTRREG);
227235 TString *ts = createstrobj (L, totalsize, LUA_VLNGSTR, G (L)->getSeed ());
228236 ts->setLnglen (l);
229237 ts->setShrlen (LSTRREG); /* signals that it is a regular long string */
@@ -254,7 +262,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
254262 TString *ts;
255263 global_State *g = G (L);
256264 stringtable *tb = g->getStringTable ();
257- unsigned int h = luaS_hash (str, l, g->getSeed ());
265+ unsigned int h = TString::hashString (str, l, g->getSeed ());
258266 TString **list = &tb->getHash ()[lmod (h, tb->getSize ())];
259267 lua_assert (str != nullptr ); /* otherwise 'memcmp'/'memcpy' are undefined */
260268 for (ts = *list; ts != nullptr ; ts = ts->getNext ()) {
@@ -283,30 +291,28 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
283291}
284292
285293
286- /*
287- ** new string (with explicit length)
288- */
289- TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
294+ // Static factory: create string with explicit length
295+ TString* TString::create (lua_State* L, const char * str, size_t l) {
290296 if (l <= LUAI_MAXSHORTLEN) /* short string? */
291297 return internshrstr (L, str, l);
292298 else {
293299 TString *ts;
294300 if (l_unlikely (l * sizeof (char ) >= (MAX_SIZE - sizeof (TString))))
295301 luaM_toobig (L);
296- ts = luaS_createlngstrobj (L, l);
302+ ts = createLongString (L, l);
297303 std::copy_n (str, l, getlngstr (ts));
298304 return ts;
299305 }
300306}
301307
308+ // Static factory: create string from std::span
309+ TString* TString::create (lua_State* L, std::span<const char > str) {
310+ return create (L, str.data (), str.size ());
311+ }
302312
303- /*
304- ** Create or reuse a zero-terminated string, first checking in the
305- ** cache (using the string address as a key). The cache can contain
306- ** only zero-terminated strings, so it is safe to use 'strcmp' to
307- ** check hits.
308- */
309- TString *luaS_new (lua_State *L, const char *str) {
313+
314+ // Static factory: create string from null-terminated C string
315+ TString* TString::create (lua_State* L, const char * str) {
310316 unsigned int i = point2uint (str) % STRCACHE_N; /* hash */
311317 unsigned int j;
312318 global_State *g = G (L);
@@ -318,7 +324,7 @@ TString *luaS_new (lua_State *L, const char *str) {
318324 for (j = STRCACHE_M - 1 ; j > 0 ; j--)
319325 g->setStrCache (i, j, g->getStrCache (i, j - 1 )); /* move out last element */
320326 /* new element is first in the list */
321- TString *newstr = luaS_newlstr (L, str, strlen (str));
327+ TString *newstr = create (L, str, strlen (str));
322328 g->setStrCache (i, 0 , newstr);
323329 return newstr;
324330}
@@ -365,13 +371,14 @@ struct NewExt {
365371
366372static void f_newext (lua_State *L, void *ud) {
367373 NewExt *ne = static_cast <NewExt*>(ud);
368- size_t size = luaS_sizelngstr (0 , ne->kind );
374+ size_t size = TString::longStringSize (0 , ne->kind );
369375 ne->ts = createstrobj (L, size, LUA_VLNGSTR, G (L)->getSeed ());
370376}
371377
372378
373- TString *luaS_newextlstr (lua_State *L,
374- const char *s, size_t len, lua_Alloc falloc, void *ud) {
379+ // Static factory: create external long string
380+ TString* TString::createExternal (lua_State* L, const char * s, size_t len,
381+ lua_Alloc falloc, void * ud) {
375382 struct NewExt ne;
376383 if (!falloc) {
377384 ne.kind = LSTRFIX;
@@ -401,7 +408,7 @@ unsigned TString::hashLongStr() {
401408 lua_assert (getType () == LUA_VLNGSTR);
402409 if (getExtra () == 0 ) { /* no hash? */
403410 size_t len = getLnglen ();
404- setHash (luaS_hash (getlngstr (this ), len, getHash ()));
411+ setHash (hashString (getlngstr (this ), len, getHash ()));
405412 setExtra (1 ); /* now it has its hash */
406413 }
407414 return getHash ();
0 commit comments