aboutsummaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-02-22 14:15:18 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-02-22 14:15:18 -0300
commit52ee91dd73199e068d31d3ac138d933ddd4fb9b1 (patch)
tree9f9f1fdc16c8a3464fef14b9946482df688c2ea8 /lstrlib.c
parent191fd35f0a6cd47ea03417a663395acf5d0e9bf5 (diff)
downloadlua-52ee91dd73199e068d31d3ac138d933ddd4fb9b1.tar.gz
lua-52ee91dd73199e068d31d3ac138d933ddd4fb9b1.tar.bz2
lua-52ee91dd73199e068d31d3ac138d933ddd4fb9b1.zip
better encapsulation of some types
Diffstat (limited to 'lstrlib.c')
-rw-r--r--lstrlib.c72
1 files changed, 36 insertions, 36 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 42e4e9d3..07ec43ad 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.61 2001/01/10 16:58:11 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.62 2001/02/02 19:02:40 roberto Exp roberto $
3** Standard library for string operations and pattern-matching 3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -55,7 +55,7 @@ static int str_lower (lua_State *L) {
55 const char *s = luaL_check_lstr(L, 1, &l); 55 const char *s = luaL_check_lstr(L, 1, &l);
56 luaL_buffinit(L, &b); 56 luaL_buffinit(L, &b);
57 for (i=0; i<l; i++) 57 for (i=0; i<l; i++)
58 luaL_putchar(&b, tolower((unsigned char)(s[i]))); 58 luaL_putchar(&b, tolower(uchar(s[i])));
59 luaL_pushresult(&b); 59 luaL_pushresult(&b);
60 return 1; 60 return 1;
61} 61}
@@ -68,7 +68,7 @@ static int str_upper (lua_State *L) {
68 const char *s = luaL_check_lstr(L, 1, &l); 68 const char *s = luaL_check_lstr(L, 1, &l);
69 luaL_buffinit(L, &b); 69 luaL_buffinit(L, &b);
70 for (i=0; i<l; i++) 70 for (i=0; i<l; i++)
71 luaL_putchar(&b, toupper((unsigned char)(s[i]))); 71 luaL_putchar(&b, toupper(uchar(s[i])));
72 luaL_pushresult(&b); 72 luaL_pushresult(&b);
73 return 1; 73 return 1;
74} 74}
@@ -91,7 +91,7 @@ static int str_byte (lua_State *L) {
91 const char *s = luaL_check_lstr(L, 1, &l); 91 const char *s = luaL_check_lstr(L, 1, &l);
92 sint32 pos = posrelat(luaL_opt_long(L, 2, 1), l); 92 sint32 pos = posrelat(luaL_opt_long(L, 2, 1), l);
93 luaL_arg_check(L, 0<pos && (size_t)pos<=l, 2, "out of range"); 93 luaL_arg_check(L, 0<pos && (size_t)pos<=l, 2, "out of range");
94 lua_pushnumber(L, (unsigned char)s[pos-1]); 94 lua_pushnumber(L, uchar(s[pos-1]));
95 return 1; 95 return 1;
96} 96}
97 97
@@ -103,8 +103,8 @@ static int str_char (lua_State *L) {
103 luaL_buffinit(L, &b); 103 luaL_buffinit(L, &b);
104 for (i=1; i<=n; i++) { 104 for (i=1; i<=n; i++) {
105 int c = luaL_check_int(L, i); 105 int c = luaL_check_int(L, i);
106 luaL_arg_check(L, (unsigned char)c == c, i, "invalid value"); 106 luaL_arg_check(L, uchar(c) == c, i, "invalid value");
107 luaL_putchar(&b, (unsigned char)c); 107 luaL_putchar(&b, uchar(c));
108 } 108 }
109 luaL_pushresult(&b); 109 luaL_pushresult(&b);
110 return 1; 110 return 1;
@@ -177,26 +177,26 @@ static const char *luaI_classend (MatchState *ms, const char *p) {
177} 177}
178 178
179 179
180static int match_class (int c, int cl) { 180static int match_class (char c, char cl) {
181 int res; 181 int res;
182 switch (tolower(cl)) { 182 switch (tolower(uchar(cl))) {
183 case 'a' : res = isalpha(c); break; 183 case 'a' : res = isalpha(uchar(c)); break;
184 case 'c' : res = iscntrl(c); break; 184 case 'c' : res = iscntrl(uchar(c)); break;
185 case 'd' : res = isdigit(c); break; 185 case 'd' : res = isdigit(uchar(c)); break;
186 case 'l' : res = islower(c); break; 186 case 'l' : res = islower(uchar(c)); break;
187 case 'p' : res = ispunct(c); break; 187 case 'p' : res = ispunct(uchar(c)); break;
188 case 's' : res = isspace(c); break; 188 case 's' : res = isspace(uchar(c)); break;
189 case 'u' : res = isupper(c); break; 189 case 'u' : res = isupper(uchar(c)); break;
190 case 'w' : res = isalnum(c); break; 190 case 'w' : res = isalnum(uchar(c)); break;
191 case 'x' : res = isxdigit(c); break; 191 case 'x' : res = isxdigit(uchar(c)); break;
192 case 'z' : res = (c == '\0'); break; 192 case 'z' : res = (c == '\0'); break;
193 default: return (cl == c); 193 default: return (cl == c);
194 } 194 }
195 return (islower(cl) ? res : !res); 195 return (islower(uchar(cl)) ? res : !res);
196} 196}
197 197
198 198
199static int matchbracketclass (int c, const char *p, const char *endclass) { 199static int matchbracketclass (char c, const char *p, const char *endclass) {
200 int sig = 1; 200 int sig = 1;
201 if (*(p+1) == '^') { 201 if (*(p+1) == '^') {
202 sig = 0; 202 sig = 0;
@@ -205,30 +205,30 @@ static int matchbracketclass (int c, const char *p, const char *endclass) {
205 while (++p < endclass) { 205 while (++p < endclass) {
206 if (*p == ESC) { 206 if (*p == ESC) {
207 p++; 207 p++;
208 if (match_class(c, (unsigned char)*p)) 208 if (match_class(c, *p))
209 return sig; 209 return sig;
210 } 210 }
211 else if ((*(p+1) == '-') && (p+2 < endclass)) { 211 else if ((*(p+1) == '-') && (p+2 < endclass)) {
212 p+=2; 212 p+=2;
213 if ((int)(unsigned char)*(p-2) <= c && c <= (int)(unsigned char)*p) 213 if (uchar(*(p-2)) <= uchar(c) && uchar(c) <= uchar(*p))
214 return sig; 214 return sig;
215 } 215 }
216 else if ((int)(unsigned char)*p == c) return sig; 216 else if (*p == c) return sig;
217 } 217 }
218 return !sig; 218 return !sig;
219} 219}
220 220
221 221
222static int luaI_singlematch (int c, const char *p, const char *ep) { 222static int luaI_singlematch (char c, const char *p, const char *ep) {
223 switch (*p) { 223 switch (*p) {
224 case '.': /* matches any char */ 224 case '.': /* matches any char */
225 return 1; 225 return 1;
226 case ESC: 226 case ESC:
227 return match_class(c, (unsigned char)*(p+1)); 227 return match_class(c, *(p+1));
228 case '[': 228 case '[':
229 return matchbracketclass(c, p, ep-1); 229 return matchbracketclass(c, p, ep-1);
230 default: 230 default:
231 return ((unsigned char)*p == c); 231 return (*p == c);
232 } 232 }
233} 233}
234 234
@@ -258,7 +258,7 @@ static const char *matchbalance (MatchState *ms, const char *s, const char *p) {
258static const char *max_expand (MatchState *ms, const char *s, const char *p, 258static const char *max_expand (MatchState *ms, const char *s, const char *p,
259 const char *ep) { 259 const char *ep) {
260 sint32 i = 0; /* counts maximum expand for item */ 260 sint32 i = 0; /* counts maximum expand for item */
261 while ((s+i)<ms->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep)) 261 while ((s+i)<ms->src_end && luaI_singlematch(*(s+i), p, ep))
262 i++; 262 i++;
263 /* keeps trying to match with the maximum repetitions */ 263 /* keeps trying to match with the maximum repetitions */
264 while (i>=0) { 264 while (i>=0) {
@@ -276,7 +276,7 @@ static const char *min_expand (MatchState *ms, const char *s, const char *p,
276 const char *res = match(ms, s, ep+1); 276 const char *res = match(ms, s, ep+1);
277 if (res != NULL) 277 if (res != NULL)
278 return res; 278 return res;
279 else if (s<ms->src_end && luaI_singlematch((unsigned char)*s, p, ep)) 279 else if (s<ms->src_end && luaI_singlematch(*s, p, ep))
280 s++; /* try with one more repetition */ 280 s++; /* try with one more repetition */
281 else return NULL; 281 else return NULL;
282 } 282 }
@@ -328,7 +328,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
328 case ')': /* end capture */ 328 case ')': /* end capture */
329 return end_capture(ms, s, p+1); 329 return end_capture(ms, s, p+1);
330 case ESC: /* may be %[0-9] or %b */ 330 case ESC: /* may be %[0-9] or %b */
331 if (isdigit((unsigned char)(*(p+1)))) { /* capture? */ 331 if (isdigit(uchar(*(p+1)))) { /* capture? */
332 s = match_capture(ms, s, *(p+1)); 332 s = match_capture(ms, s, *(p+1));
333 if (s == NULL) return NULL; 333 if (s == NULL) return NULL;
334 p+=2; goto init; /* else return match(ms, s, p+2) */ 334 p+=2; goto init; /* else return match(ms, s, p+2) */
@@ -347,7 +347,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
347 else goto dflt; 347 else goto dflt;
348 default: dflt: { /* it is a pattern item */ 348 default: dflt: { /* it is a pattern item */
349 const char *ep = luaI_classend(ms, p); /* points to what is next */ 349 const char *ep = luaI_classend(ms, p); /* points to what is next */
350 int m = s<ms->src_end && luaI_singlematch((unsigned char)*s, p, ep); 350 int m = s<ms->src_end && luaI_singlematch(*s, p, ep);
351 switch (*ep) { 351 switch (*ep) {
352 case '?': { /* optional */ 352 case '?': { /* optional */
353 const char *res; 353 const char *res;
@@ -460,7 +460,7 @@ static void add_s (MatchState *ms, luaL_Buffer *b) {
460 luaL_putchar(b, news[i]); 460 luaL_putchar(b, news[i]);
461 else { 461 else {
462 i++; /* skip ESC */ 462 i++; /* skip ESC */
463 if (!isdigit((unsigned char)news[i])) 463 if (!isdigit(uchar(news[i])))
464 luaL_putchar(b, news[i]); 464 luaL_putchar(b, news[i]);
465 else { 465 else {
466 int level = check_capture(ms, news[i]); 466 int level = check_capture(ms, news[i]);
@@ -552,15 +552,15 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form,
552 int *hasprecision) { 552 int *hasprecision) {
553 const char *p = strfrmt; 553 const char *p = strfrmt;
554 while (strchr("-+ #0", *p)) p++; /* skip flags */ 554 while (strchr("-+ #0", *p)) p++; /* skip flags */
555 if (isdigit((unsigned char)*p)) p++; /* skip width */ 555 if (isdigit(uchar(*p))) p++; /* skip width */
556 if (isdigit((unsigned char)*p)) p++; /* (2 digits at most) */ 556 if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
557 if (*p == '.') { 557 if (*p == '.') {
558 p++; 558 p++;
559 *hasprecision = 1; 559 *hasprecision = 1;
560 if (isdigit((unsigned char)*p)) p++; /* skip precision */ 560 if (isdigit(uchar(*p))) p++; /* skip precision */
561 if (isdigit((unsigned char)*p)) p++; /* (2 digits at most) */ 561 if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
562 } 562 }
563 if (isdigit((unsigned char)*p)) 563 if (isdigit(uchar(*p)))
564 lua_error(L, "invalid format (width or precision too long)"); 564 lua_error(L, "invalid format (width or precision too long)");
565 if (p-strfrmt+2 > MAX_FORMAT) /* +2 to include `%' and the specifier */ 565 if (p-strfrmt+2 > MAX_FORMAT) /* +2 to include `%' and the specifier */
566 lua_error(L, "invalid format (too long)"); 566 lua_error(L, "invalid format (too long)");
@@ -585,7 +585,7 @@ static int str_format (lua_State *L) {
585 char form[MAX_FORMAT]; /* to store the format (`%...') */ 585 char form[MAX_FORMAT]; /* to store the format (`%...') */
586 char buff[MAX_ITEM]; /* to store the formatted item */ 586 char buff[MAX_ITEM]; /* to store the formatted item */
587 int hasprecision = 0; 587 int hasprecision = 0;
588 if (isdigit((unsigned char)*strfrmt) && *(strfrmt+1) == '$') 588 if (isdigit(uchar(*strfrmt)) && *(strfrmt+1) == '$')
589 lua_error(L, "obsolete `format' option (d$)"); 589 lua_error(L, "obsolete `format' option (d$)");
590 arg++; 590 arg++;
591 strfrmt = scanformat(L, strfrmt, form, &hasprecision); 591 strfrmt = scanformat(L, strfrmt, form, &hasprecision);