summaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-05-24 10:54:49 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-05-24 10:54:49 -0300
commitef62b340e0a6b7b18931000dcbb19c4703bfe0e8 (patch)
treed9d995116a8a686b798d1b625b06ead26f28ba58 /lstrlib.c
parent5c2dd7a9e0a5b871a71ba66c4683cd88fe4f5aa4 (diff)
downloadlua-ef62b340e0a6b7b18931000dcbb19c4703bfe0e8.tar.gz
lua-ef62b340e0a6b7b18931000dcbb19c4703bfe0e8.tar.bz2
lua-ef62b340e0a6b7b18931000dcbb19c4703bfe0e8.zip
code cleaner for 16 bits.
Diffstat (limited to 'lstrlib.c')
-rw-r--r--lstrlib.c67
1 files changed, 35 insertions, 32 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 6a38fd01..da75bbd7 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,11 +1,12 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.41 2000/03/03 14:58:26 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.42 2000/05/02 18:32:22 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*/
6 6
7 7
8#include <ctype.h> 8#include <ctype.h>
9#include <stddef.h>
9#include <stdio.h> 10#include <stdio.h>
10#include <stdlib.h> 11#include <stdlib.h>
11#include <string.h> 12#include <string.h>
@@ -18,7 +19,7 @@
18 19
19 20
20 21
21static void addnchar (lua_State *L, const char *s, int n) { 22static void addnchar (lua_State *L, const char *s, size_t n) {
22 char *b = luaL_openspace(L, n); 23 char *b = luaL_openspace(L, n);
23 memcpy(b, s, n); 24 memcpy(b, s, n);
24 luaL_addsize(L, n); 25 luaL_addsize(L, n);
@@ -26,7 +27,7 @@ static void addnchar (lua_State *L, const char *s, int n) {
26 27
27 28
28static void str_len (lua_State *L) { 29static void str_len (lua_State *L) {
29 long l; 30 size_t l;
30 luaL_check_lstr(L, 1, &l); 31 luaL_check_lstr(L, 1, &l);
31 lua_pushnumber(L, l); 32 lua_pushnumber(L, l);
32} 33}
@@ -37,19 +38,19 @@ static void closeandpush (lua_State *L) {
37} 38}
38 39
39 40
40static long posrelat (long pos, long len) { 41static long posrelat (long pos, size_t len) {
41 /* relative string position: negative means back from end */ 42 /* relative string position: negative means back from end */
42 return (pos>=0) ? pos : len+pos+1; 43 return (pos>=0) ? pos : (long)len+pos+1;
43} 44}
44 45
45 46
46static void str_sub (lua_State *L) { 47static void str_sub (lua_State *L) {
47 long l; 48 size_t l;
48 const char *s = luaL_check_lstr(L, 1, &l); 49 const char *s = luaL_check_lstr(L, 1, &l);
49 long start = posrelat(luaL_check_long(L, 2), l); 50 long start = posrelat(luaL_check_long(L, 2), l);
50 long end = posrelat(luaL_opt_long(L, 3, -1), l); 51 long end = posrelat(luaL_opt_long(L, 3, -1), l);
51 if (start < 1) start = 1; 52 if (start < 1) start = 1;
52 if (end > l) end = l; 53 if (end > (long)l) end = l;
53 if (start <= end) 54 if (start <= end)
54 lua_pushlstring(L, s+start-1, end-start+1); 55 lua_pushlstring(L, s+start-1, end-start+1);
55 else lua_pushstring(L, ""); 56 else lua_pushstring(L, "");
@@ -57,8 +58,8 @@ static void str_sub (lua_State *L) {
57 58
58 59
59static void str_lower (lua_State *L) { 60static void str_lower (lua_State *L) {
60 long l; 61 size_t l;
61 int i; 62 size_t i;
62 const char *s = luaL_check_lstr(L, 1, &l); 63 const char *s = luaL_check_lstr(L, 1, &l);
63 luaL_resetbuffer(L); 64 luaL_resetbuffer(L);
64 for (i=0; i<l; i++) 65 for (i=0; i<l; i++)
@@ -68,8 +69,8 @@ static void str_lower (lua_State *L) {
68 69
69 70
70static void str_upper (lua_State *L) { 71static void str_upper (lua_State *L) {
71 long l; 72 size_t l;
72 int i; 73 size_t i;
73 const char *s = luaL_check_lstr(L, 1, &l); 74 const char *s = luaL_check_lstr(L, 1, &l);
74 luaL_resetbuffer(L); 75 luaL_resetbuffer(L);
75 for (i=0; i<l; i++) 76 for (i=0; i<l; i++)
@@ -78,7 +79,7 @@ static void str_upper (lua_State *L) {
78} 79}
79 80
80static void str_rep (lua_State *L) { 81static void str_rep (lua_State *L) {
81 long l; 82 size_t l;
82 const char *s = luaL_check_lstr(L, 1, &l); 83 const char *s = luaL_check_lstr(L, 1, &l);
83 int n = luaL_check_int(L, 2); 84 int n = luaL_check_int(L, 2);
84 luaL_resetbuffer(L); 85 luaL_resetbuffer(L);
@@ -89,10 +90,10 @@ static void str_rep (lua_State *L) {
89 90
90 91
91static void str_byte (lua_State *L) { 92static void str_byte (lua_State *L) {
92 long l; 93 size_t l;
93 const char *s = luaL_check_lstr(L, 1, &l); 94 const char *s = luaL_check_lstr(L, 1, &l);
94 long pos = posrelat(luaL_opt_long(L, 2, 1), l); 95 long pos = posrelat(luaL_opt_long(L, 2, 1), l);
95 luaL_arg_check(L, 0<pos && pos<=l, 2, "out of range"); 96 luaL_arg_check(L, 0<pos && (size_t)pos<=l, 2, "out of range");
96 lua_pushnumber(L, (unsigned char)s[pos-1]); 97 lua_pushnumber(L, (unsigned char)s[pos-1]);
97} 98}
98 99
@@ -126,7 +127,7 @@ struct Capture {
126 int level; /* total number of captures (finished or unfinished) */ 127 int level; /* total number of captures (finished or unfinished) */
127 struct { 128 struct {
128 const char *init; 129 const char *init;
129 int len; /* -1 signals unfinished capture */ 130 long len; /* -1 signals unfinished capture */
130 } capture[MAX_CAPTURES]; 131 } capture[MAX_CAPTURES];
131}; 132};
132 133
@@ -238,7 +239,8 @@ int luaI_singlematch (int c, const char *p, const char *ep) {
238} 239}
239 240
240 241
241static const char *match (lua_State *L, const char *s, const char *p, struct Capture *cap); 242static const char *match (lua_State *L, const char *s, const char *p,
243 struct Capture *cap);
242 244
243 245
244static const char *matchbalance (lua_State *L, const char *s, const char *p, 246static const char *matchbalance (lua_State *L, const char *s, const char *p,
@@ -261,9 +263,9 @@ static const char *matchbalance (lua_State *L, const char *s, const char *p,
261} 263}
262 264
263 265
264static const char *max_expand (lua_State *L, const char *s, const char *p, const char *ep, 266static const char *max_expand (lua_State *L, const char *s, const char *p,
265 struct Capture *cap) { 267 const char *ep, struct Capture *cap) {
266 int i = 0; /* counts maximum expand for item */ 268 long i = 0; /* counts maximum expand for item */
267 while ((s+i)<cap->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep)) 269 while ((s+i)<cap->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep))
268 i++; 270 i++;
269 /* keeps trying to match mith the maximum repetitions */ 271 /* keeps trying to match mith the maximum repetitions */
@@ -276,8 +278,8 @@ static const char *max_expand (lua_State *L, const char *s, const char *p, const
276} 278}
277 279
278 280
279static const char *min_expand (lua_State *L, const char *s, const char *p, const char *ep, 281static const char *min_expand (lua_State *L, const char *s, const char *p,
280 struct Capture *cap) { 282 const char *ep, struct Capture *cap) {
281 for (;;) { 283 for (;;) {
282 const char *res = match(L, s, ep+1, cap); 284 const char *res = match(L, s, ep+1, cap);
283 if (res != NULL) 285 if (res != NULL)
@@ -317,15 +319,16 @@ static const char *end_capture (lua_State *L, const char *s, const char *p,
317static const char *match_capture (lua_State *L, const char *s, int level, 319static const char *match_capture (lua_State *L, const char *s, int level,
318 struct Capture *cap) { 320 struct Capture *cap) {
319 int l = check_capture(L, level, cap); 321 int l = check_capture(L, level, cap);
320 int len = cap->capture[l].len; 322 size_t len = cap->capture[l].len;
321 if (cap->src_end-s >= len && 323 if ((size_t)(cap->src_end-s) >= len &&
322 memcmp(cap->capture[l].init, s, len) == 0) 324 memcmp(cap->capture[l].init, s, len) == 0)
323 return s+len; 325 return s+len;
324 else return NULL; 326 else return NULL;
325} 327}
326 328
327 329
328static const char *match (lua_State *L, const char *s, const char *p, struct Capture *cap) { 330static const char *match (lua_State *L, const char *s, const char *p,
331 struct Capture *cap) {
329 init: /* using goto's to optimize tail recursion */ 332 init: /* using goto's to optimize tail recursion */
330 switch (*p) { 333 switch (*p) {
331 case '(': /* start capture */ 334 case '(': /* start capture */
@@ -397,12 +400,12 @@ static const char *memfind (const char *s1, long l1, const char *s2, long l2) {
397 400
398 401
399static void str_find (lua_State *L) { 402static void str_find (lua_State *L) {
400 long l1, l2; 403 size_t l1, l2;
401 const char *s = luaL_check_lstr(L, 1, &l1); 404 const char *s = luaL_check_lstr(L, 1, &l1);
402 const char *p = luaL_check_lstr(L, 2, &l2); 405 const char *p = luaL_check_lstr(L, 2, &l2);
403 long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1; 406 long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
404 struct Capture cap; 407 struct Capture cap;
405 luaL_arg_check(L, 0 <= init && init <= l1, 3, "out of range"); 408 luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, "out of range");
406 if (lua_getparam(L, 4) != LUA_NOOBJECT || 409 if (lua_getparam(L, 4) != LUA_NOOBJECT ||
407 strpbrk(p, SPECIALS) == NULL) { /* no special characters? */ 410 strpbrk(p, SPECIALS) == NULL) { /* no special characters? */
408 const char *s2 = memfind(s+init, l1-init, p, l2); 411 const char *s2 = memfind(s+init, l1-init, p, l2);
@@ -434,8 +437,8 @@ static void str_find (lua_State *L) {
434static void add_s (lua_State *L, lua_Object newp, struct Capture *cap) { 437static void add_s (lua_State *L, lua_Object newp, struct Capture *cap) {
435 if (lua_isstring(L, newp)) { 438 if (lua_isstring(L, newp)) {
436 const char *news = lua_getstring(L, newp); 439 const char *news = lua_getstring(L, newp);
437 int l = lua_strlen(L, newp); 440 size_t l = lua_strlen(L, newp);
438 int i; 441 size_t i;
439 for (i=0; i<l; i++) { 442 for (i=0; i<l; i++) {
440 if (news[i] != ESC) 443 if (news[i] != ESC)
441 luaL_addchar(L, news[i]); 444 luaL_addchar(L, news[i]);
@@ -453,7 +456,7 @@ static void add_s (lua_State *L, lua_Object newp, struct Capture *cap) {
453 else { /* is a function */ 456 else { /* is a function */
454 lua_Object res; 457 lua_Object res;
455 int status; 458 int status;
456 int oldbuff; 459 size_t oldbuff;
457 lua_beginblock(L); 460 lua_beginblock(L);
458 push_captures(L, cap); 461 push_captures(L, cap);
459 /* function may use buffer, so save it and create a new one */ 462 /* function may use buffer, so save it and create a new one */
@@ -474,7 +477,7 @@ static void add_s (lua_State *L, lua_Object newp, struct Capture *cap) {
474 477
475 478
476static void str_gsub (lua_State *L) { 479static void str_gsub (lua_State *L) {
477 long srcl; 480 size_t srcl;
478 const char *src = luaL_check_lstr(L, 1, &srcl); 481 const char *src = luaL_check_lstr(L, 1, &srcl);
479 const char *p = luaL_check_string(L, 2); 482 const char *p = luaL_check_string(L, 2);
480 lua_Object newp = lua_getparam(L, 3); 483 lua_Object newp = lua_getparam(L, 3);
@@ -510,7 +513,7 @@ static void str_gsub (lua_State *L) {
510 513
511 514
512static void luaI_addquoted (lua_State *L, int arg) { 515static void luaI_addquoted (lua_State *L, int arg) {
513 long l; 516 size_t l;
514 const char *s = luaL_check_lstr(L, arg, &l); 517 const char *s = luaL_check_lstr(L, arg, &l);
515 luaL_addchar(L, '"'); 518 luaL_addchar(L, '"');
516 while (l--) { 519 while (l--) {
@@ -573,7 +576,7 @@ static void str_format (lua_State *L) {
573 luaI_addquoted(L, arg); 576 luaI_addquoted(L, arg);
574 continue; /* skip the "addsize" at the end */ 577 continue; /* skip the "addsize" at the end */
575 case 's': { 578 case 's': {
576 long l; 579 size_t l;
577 const char *s = luaL_check_lstr(L, arg, &l); 580 const char *s = luaL_check_lstr(L, arg, &l);
578 if (cap.capture[1].len == 0 && l >= 100) { 581 if (cap.capture[1].len == 0 && l >= 100) {
579 /* no precision and string is too long to be formatted; 582 /* no precision and string is too long to be formatted;