aboutsummaryrefslogtreecommitdiff
path: root/lstring.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lstring.c46
1 files changed, 20 insertions, 26 deletions
diff --git a/lstring.c b/lstring.c
index b5c8f89f..17c6fd8f 100644
--- a/lstring.c
+++ b/lstring.c
@@ -39,14 +39,14 @@
39 39
40 40
41/* 41/*
42** equality for long strings 42** generic equality for strings
43*/ 43*/
44int luaS_eqlngstr (TString *a, TString *b) { 44int luaS_eqstr (TString *a, TString *b) {
45 size_t len = a->u.lnglen; 45 size_t len1, len2;
46 lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR); 46 const char *s1 = getlstr(a, len1);
47 return (a == b) || /* same instance or... */ 47 const char *s2 = getlstr(b, len2);
48 ((len == b->u.lnglen) && /* equal length and ... */ 48 return ((len1 == len2) && /* equal length and ... */
49 (memcmp(getlngstr(a), getlngstr(b), len) == 0)); /* equal contents */ 49 (memcmp(s1, s2, len1) == 0)); /* equal contents */
50} 50}
51 51
52 52
@@ -315,28 +315,9 @@ static void f_newext (lua_State *L, void *ud) {
315} 315}
316 316
317 317
318static void f_pintern (lua_State *L, void *ud) {
319 struct NewExt *ne = cast(struct NewExt *, ud);
320 ne->ts = internshrstr(L, ne->s, ne->len);
321}
322
323
324TString *luaS_newextlstr (lua_State *L, 318TString *luaS_newextlstr (lua_State *L,
325 const char *s, size_t len, lua_Alloc falloc, void *ud) { 319 const char *s, size_t len, lua_Alloc falloc, void *ud) {
326 struct NewExt ne; 320 struct NewExt ne;
327 if (len <= LUAI_MAXSHORTLEN) { /* short string? */
328 ne.s = s; ne.len = len;
329 if (!falloc)
330 f_pintern(L, &ne); /* just internalize string */
331 else {
332 TStatus status = luaD_rawrunprotected(L, f_pintern, &ne);
333 (*falloc)(ud, cast_voidp(s), len + 1, 0); /* free external string */
334 if (status != LUA_OK) /* memory error? */
335 luaM_error(L); /* re-raise memory error */
336 }
337 return ne.ts;
338 }
339 /* "normal" case: long strings */
340 if (!falloc) { 321 if (!falloc) {
341 ne.kind = LSTRFIX; 322 ne.kind = LSTRFIX;
342 f_newext(L, &ne); /* just create header */ 323 f_newext(L, &ne); /* just create header */
@@ -357,3 +338,16 @@ TString *luaS_newextlstr (lua_State *L,
357} 338}
358 339
359 340
341/*
342** Normalize an external string: If it is short, internalize it.
343*/
344TString *luaS_normstr (lua_State *L, TString *ts) {
345 size_t len = ts->u.lnglen;
346 if (len > LUAI_MAXSHORTLEN)
347 return ts; /* long string; keep the original */
348 else {
349 const char *str = getlngstr(ts);
350 return internshrstr(L, str, len);
351 }
352}
353