aboutsummaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-06-20 14:46:06 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-06-20 14:46:06 -0300
commita08d82eb132bfd9db5b91e0d5ebcb81d7b26dcd0 (patch)
tree45618815246686a535a28fb4e6f9736a60be00d4 /lstrlib.c
parent55ac40f859ad8e28fe71a8801d49f4a4140e8aa3 (diff)
downloadlua-a08d82eb132bfd9db5b91e0d5ebcb81d7b26dcd0.tar.gz
lua-a08d82eb132bfd9db5b91e0d5ebcb81d7b26dcd0.tar.bz2
lua-a08d82eb132bfd9db5b91e0d5ebcb81d7b26dcd0.zip
llimits.h being used by all Lua code
The definitions in llimits.h are useful not only for the core. That header only defines types and '#define's, so libs and core still do not share any real code/data.
Diffstat (limited to 'lstrlib.c')
-rw-r--r--lstrlib.c49
1 files changed, 23 insertions, 26 deletions
diff --git a/lstrlib.c b/lstrlib.c
index a90c4fd1..97d974f8 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -24,6 +24,7 @@
24 24
25#include "lauxlib.h" 25#include "lauxlib.h"
26#include "lualib.h" 26#include "lualib.h"
27#include "llimits.h"
27 28
28 29
29/* 30/*
@@ -36,10 +37,6 @@
36#endif 37#endif
37 38
38 39
39/* macro to 'unsign' a character */
40#define uchar(c) ((unsigned char)(c))
41
42
43/* 40/*
44** Some sizes are better limited to fit in 'int', but must also fit in 41** Some sizes are better limited to fit in 'int', but must also fit in
45** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.) 42** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.)
@@ -128,7 +125,7 @@ static int str_lower (lua_State *L) {
128 const char *s = luaL_checklstring(L, 1, &l); 125 const char *s = luaL_checklstring(L, 1, &l);
129 char *p = luaL_buffinitsize(L, &b, l); 126 char *p = luaL_buffinitsize(L, &b, l);
130 for (i=0; i<l; i++) 127 for (i=0; i<l; i++)
131 p[i] = tolower(uchar(s[i])); 128 p[i] = tolower(cast_uchar(s[i]));
132 luaL_pushresultsize(&b, l); 129 luaL_pushresultsize(&b, l);
133 return 1; 130 return 1;
134} 131}
@@ -141,7 +138,7 @@ static int str_upper (lua_State *L) {
141 const char *s = luaL_checklstring(L, 1, &l); 138 const char *s = luaL_checklstring(L, 1, &l);
142 char *p = luaL_buffinitsize(L, &b, l); 139 char *p = luaL_buffinitsize(L, &b, l);
143 for (i=0; i<l; i++) 140 for (i=0; i<l; i++)
144 p[i] = toupper(uchar(s[i])); 141 p[i] = toupper(cast_uchar(s[i]));
145 luaL_pushresultsize(&b, l); 142 luaL_pushresultsize(&b, l);
146 return 1; 143 return 1;
147} 144}
@@ -187,7 +184,7 @@ static int str_byte (lua_State *L) {
187 n = (int)(pose - posi) + 1; 184 n = (int)(pose - posi) + 1;
188 luaL_checkstack(L, n, "string slice too long"); 185 luaL_checkstack(L, n, "string slice too long");
189 for (i=0; i<n; i++) 186 for (i=0; i<n; i++)
190 lua_pushinteger(L, uchar(s[posi+i-1])); 187 lua_pushinteger(L, cast_uchar(s[posi+i-1]));
191 return n; 188 return n;
192} 189}
193 190
@@ -200,7 +197,7 @@ static int str_char (lua_State *L) {
200 for (i=1; i<=n; i++) { 197 for (i=1; i<=n; i++) {
201 lua_Unsigned c = (lua_Unsigned)luaL_checkinteger(L, i); 198 lua_Unsigned c = (lua_Unsigned)luaL_checkinteger(L, i);
202 luaL_argcheck(L, c <= (lua_Unsigned)UCHAR_MAX, i, "value out of range"); 199 luaL_argcheck(L, c <= (lua_Unsigned)UCHAR_MAX, i, "value out of range");
203 p[i - 1] = uchar(c); 200 p[i - 1] = cast_uchar(c);
204 } 201 }
205 luaL_pushresultsize(&b, n); 202 luaL_pushresultsize(&b, n);
206 return 1; 203 return 1;
@@ -459,15 +456,15 @@ static int matchbracketclass (int c, const char *p, const char *ec) {
459 while (++p < ec) { 456 while (++p < ec) {
460 if (*p == L_ESC) { 457 if (*p == L_ESC) {
461 p++; 458 p++;
462 if (match_class(c, uchar(*p))) 459 if (match_class(c, cast_uchar(*p)))
463 return sig; 460 return sig;
464 } 461 }
465 else if ((*(p+1) == '-') && (p+2 < ec)) { 462 else if ((*(p+1) == '-') && (p+2 < ec)) {
466 p+=2; 463 p+=2;
467 if (uchar(*(p-2)) <= c && c <= uchar(*p)) 464 if (cast_uchar(*(p-2)) <= c && c <= cast_uchar(*p))
468 return sig; 465 return sig;
469 } 466 }
470 else if (uchar(*p) == c) return sig; 467 else if (cast_uchar(*p) == c) return sig;
471 } 468 }
472 return !sig; 469 return !sig;
473} 470}
@@ -478,12 +475,12 @@ static int singlematch (MatchState *ms, const char *s, const char *p,
478 if (s >= ms->src_end) 475 if (s >= ms->src_end)
479 return 0; 476 return 0;
480 else { 477 else {
481 int c = uchar(*s); 478 int c = cast_uchar(*s);
482 switch (*p) { 479 switch (*p) {
483 case '.': return 1; /* matches any char */ 480 case '.': return 1; /* matches any char */
484 case L_ESC: return match_class(c, uchar(*(p+1))); 481 case L_ESC: return match_class(c, cast_uchar(*(p+1)));
485 case '[': return matchbracketclass(c, p, ep-1); 482 case '[': return matchbracketclass(c, p, ep-1);
486 default: return (uchar(*p) == c); 483 default: return (cast_uchar(*p) == c);
487 } 484 }
488 } 485 }
489} 486}
@@ -612,8 +609,8 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
612 luaL_error(ms->L, "missing '[' after '%%f' in pattern"); 609 luaL_error(ms->L, "missing '[' after '%%f' in pattern");
613 ep = classend(ms, p); /* points to what is next */ 610 ep = classend(ms, p); /* points to what is next */
614 previous = (s == ms->src_init) ? '\0' : *(s - 1); 611 previous = (s == ms->src_init) ? '\0' : *(s - 1);
615 if (!matchbracketclass(uchar(previous), p, ep - 1) && 612 if (!matchbracketclass(cast_uchar(previous), p, ep - 1) &&
616 matchbracketclass(uchar(*s), p, ep - 1)) { 613 matchbracketclass(cast_uchar(*s), p, ep - 1)) {
617 p = ep; goto init; /* return match(ms, s, ep); */ 614 p = ep; goto init; /* return match(ms, s, ep); */
618 } 615 }
619 s = NULL; /* match failed */ 616 s = NULL; /* match failed */
@@ -622,7 +619,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
622 case '0': case '1': case '2': case '3': 619 case '0': case '1': case '2': case '3':
623 case '4': case '5': case '6': case '7': 620 case '4': case '5': case '6': case '7':
624 case '8': case '9': { /* capture results (%0-%9)? */ 621 case '8': case '9': { /* capture results (%0-%9)? */
625 s = match_capture(ms, s, uchar(*(p + 1))); 622 s = match_capture(ms, s, cast_uchar(*(p + 1)));
626 if (s != NULL) { 623 if (s != NULL) {
627 p += 2; goto init; /* return match(ms, s, p + 2) */ 624 p += 2; goto init; /* return match(ms, s, p + 2) */
628 } 625 }
@@ -887,7 +884,7 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
887 luaL_addchar(b, *p); 884 luaL_addchar(b, *p);
888 else if (*p == '0') /* '%0' */ 885 else if (*p == '0') /* '%0' */
889 luaL_addlstring(b, s, e - s); 886 luaL_addlstring(b, s, e - s);
890 else if (isdigit(uchar(*p))) { /* '%n' */ 887 else if (isdigit(cast_uchar(*p))) { /* '%n' */
891 const char *cap; 888 const char *cap;
892 ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap); 889 ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap);
893 if (resl == CAP_POSITION) 890 if (resl == CAP_POSITION)
@@ -1065,7 +1062,7 @@ static int lua_number2strx (lua_State *L, char *buff, int sz,
1065 if (fmt[SIZELENMOD] == 'A') { 1062 if (fmt[SIZELENMOD] == 'A') {
1066 int i; 1063 int i;
1067 for (i = 0; i < n; i++) 1064 for (i = 0; i < n; i++)
1068 buff[i] = toupper(uchar(buff[i])); 1065 buff[i] = toupper(cast_uchar(buff[i]));
1069 } 1066 }
1070 else if (l_unlikely(fmt[SIZELENMOD] != 'a')) 1067 else if (l_unlikely(fmt[SIZELENMOD] != 'a'))
1071 return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); 1068 return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented");
@@ -1132,12 +1129,12 @@ static void addquoted (luaL_Buffer *b, const char *s, size_t len) {
1132 luaL_addchar(b, '\\'); 1129 luaL_addchar(b, '\\');
1133 luaL_addchar(b, *s); 1130 luaL_addchar(b, *s);
1134 } 1131 }
1135 else if (iscntrl(uchar(*s))) { 1132 else if (iscntrl(cast_uchar(*s))) {
1136 char buff[10]; 1133 char buff[10];
1137 if (!isdigit(uchar(*(s+1)))) 1134 if (!isdigit(cast_uchar(*(s+1))))
1138 l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s)); 1135 l_sprintf(buff, sizeof(buff), "\\%d", (int)cast_uchar(*s));
1139 else 1136 else
1140 l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s)); 1137 l_sprintf(buff, sizeof(buff), "\\%03d", (int)cast_uchar(*s));
1141 luaL_addstring(b, buff); 1138 luaL_addstring(b, buff);
1142 } 1139 }
1143 else 1140 else
@@ -1214,9 +1211,9 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
1214 1211
1215 1212
1216static const char *get2digits (const char *s) { 1213static const char *get2digits (const char *s) {
1217 if (isdigit(uchar(*s))) { 1214 if (isdigit(cast_uchar(*s))) {
1218 s++; 1215 s++;
1219 if (isdigit(uchar(*s))) s++; /* (2 digits at most) */ 1216 if (isdigit(cast_uchar(*s))) s++; /* (2 digits at most) */
1220 } 1217 }
1221 return s; 1218 return s;
1222} 1219}
@@ -1239,7 +1236,7 @@ static void checkformat (lua_State *L, const char *form, const char *flags,
1239 spec = get2digits(spec); /* skip precision */ 1236 spec = get2digits(spec); /* skip precision */
1240 } 1237 }
1241 } 1238 }
1242 if (!isalpha(uchar(*spec))) /* did not go to the end? */ 1239 if (!isalpha(cast_uchar(*spec))) /* did not go to the end? */
1243 luaL_error(L, "invalid conversion specification: '%s'", form); 1240 luaL_error(L, "invalid conversion specification: '%s'", form);
1244} 1241}
1245 1242