aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2007-10-25 17:30:36 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2007-10-25 17:30:36 -0200
commit7a78495f3136c958acd39006e6d2f00d974c4626 (patch)
tree46f1ddc72641a1dd6b9dbb4de0c487f48224842d
parent3138afbe2e16f5a9ac3c40757e30b9563b23256b (diff)
downloadlua-7a78495f3136c958acd39006e6d2f00d974c4626.tar.gz
lua-7a78495f3136c958acd39006e6d2f00d974c4626.tar.bz2
lua-7a78495f3136c958acd39006e6d2f00d974c4626.zip
avoid problems with 'ptrdiff_t'
-rw-r--r--lstrlib.c52
1 files changed, 27 insertions, 25 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 96160867..5f5269eb 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.135 2006/09/18 16:33:14 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.136 2007/02/07 17:53:08 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*/
@@ -28,26 +28,28 @@
28static int str_len (lua_State *L) { 28static int str_len (lua_State *L) {
29 size_t l; 29 size_t l;
30 luaL_checklstring(L, 1, &l); 30 luaL_checklstring(L, 1, &l);
31 lua_pushinteger(L, l); 31 lua_pushinteger(L, (lua_Integer)l);
32 return 1; 32 return 1;
33} 33}
34 34
35 35
36static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) { 36/* translate a relative string position: negative means back from end */
37 /* relative string position: negative means back from end */ 37static size_t posrelat (ptrdiff_t pos, size_t len) {
38 return (pos>=0) ? pos : (ptrdiff_t)len+pos+1; 38 if (pos >= 0) return (size_t)pos;
39 else if ((size_t)-pos > len) return 0;
40 else return len - ((size_t)-pos) + 1;
39} 41}
40 42
41 43
42static int str_sub (lua_State *L) { 44static int str_sub (lua_State *L) {
43 size_t l; 45 size_t l;
44 const char *s = luaL_checklstring(L, 1, &l); 46 const char *s = luaL_checklstring(L, 1, &l);
45 ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l); 47 size_t start = posrelat(luaL_checkinteger(L, 2), l);
46 ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l); 48 size_t end = posrelat(luaL_optinteger(L, 3, -1), l);
47 if (start < 1) start = 1; 49 if (start < 1) start = 1;
48 if (end > (ptrdiff_t)l) end = (ptrdiff_t)l; 50 if (end > l) end = l;
49 if (start <= end) 51 if (start <= end)
50 lua_pushlstring(L, s+start-1, end-start+1); 52 lua_pushlstring(L, s + start - 1, end - start + 1);
51 else lua_pushliteral(L, ""); 53 else lua_pushliteral(L, "");
52 return 1; 54 return 1;
53} 55}
@@ -105,11 +107,11 @@ static int str_rep (lua_State *L) {
105static int str_byte (lua_State *L) { 107static int str_byte (lua_State *L) {
106 size_t l; 108 size_t l;
107 const char *s = luaL_checklstring(L, 1, &l); 109 const char *s = luaL_checklstring(L, 1, &l);
108 ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l); 110 size_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
109 ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l); 111 size_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
110 int n, i; 112 int n, i;
111 if (posi <= 0) posi = 1; 113 if (posi < 1) posi = 1;
112 if ((size_t)pose > l) pose = l; 114 if (pose > l) pose = l;
113 if (posi > pose) return 0; /* empty interval; return no values */ 115 if (posi > pose) return 0; /* empty interval; return no values */
114 n = (int)(pose - posi + 1); 116 n = (int)(pose - posi + 1);
115 if (posi + n <= pose) /* overflow? */ 117 if (posi + n <= pose) /* overflow? */
@@ -495,33 +497,33 @@ static int str_find_aux (lua_State *L, int find) {
495 size_t l1, l2; 497 size_t l1, l2;
496 const char *s = luaL_checklstring(L, 1, &l1); 498 const char *s = luaL_checklstring(L, 1, &l1);
497 const char *p = luaL_checklstring(L, 2, &l2); 499 const char *p = luaL_checklstring(L, 2, &l2);
498 ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1; 500 size_t init = posrelat(luaL_optinteger(L, 3, 1), l1);
499 if (init < 0) init = 0; 501 if (init < 1) init = 1;
500 else if ((size_t)(init) > l1) init = (ptrdiff_t)l1; 502 else if (init > l1) init = l1 + 1;
501 if (find && (lua_toboolean(L, 4) || /* explicit request? */ 503 if (find && (lua_toboolean(L, 4) || /* explicit request? */
502 strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */ 504 strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */
503 /* do a plain search */ 505 /* do a plain search */
504 const char *s2 = lmemfind(s+init, l1-init, p, l2); 506 const char *s2 = lmemfind(s + init - 1, l1 - init + 1, p, l2);
505 if (s2) { 507 if (s2) {
506 lua_pushinteger(L, s2-s+1); 508 lua_pushinteger(L, s2 - s + 1);
507 lua_pushinteger(L, s2-s+l2); 509 lua_pushinteger(L, s2 - s + l2);
508 return 2; 510 return 2;
509 } 511 }
510 } 512 }
511 else { 513 else {
512 MatchState ms; 514 MatchState ms;
513 int anchor = (*p == '^') ? (p++, 1) : 0; 515 int anchor = (*p == '^') ? (p++, 1) : 0;
514 const char *s1=s+init; 516 const char *s1 = s + init - 1;
515 ms.L = L; 517 ms.L = L;
516 ms.src_init = s; 518 ms.src_init = s;
517 ms.src_end = s+l1; 519 ms.src_end = s + l1;
518 do { 520 do {
519 const char *res; 521 const char *res;
520 ms.level = 0; 522 ms.level = 0;
521 if ((res=match(&ms, s1, p)) != NULL) { 523 if ((res=match(&ms, s1, p)) != NULL) {
522 if (find) { 524 if (find) {
523 lua_pushinteger(L, s1-s+1); /* start */ 525 lua_pushinteger(L, s1 - s + 1); /* start */
524 lua_pushinteger(L, res-s); /* end */ 526 lua_pushinteger(L, res - s); /* end */
525 return push_captures(&ms, NULL, 0) + 2; 527 return push_captures(&ms, NULL, 0) + 2;
526 } 528 }
527 else 529 else
@@ -648,9 +650,9 @@ static int str_gsub (lua_State *L) {
648 size_t srcl; 650 size_t srcl;
649 const char *src = luaL_checklstring(L, 1, &srcl); 651 const char *src = luaL_checklstring(L, 1, &srcl);
650 const char *p = luaL_checkstring(L, 2); 652 const char *p = luaL_checkstring(L, 2);
651 int max_s = luaL_optint(L, 4, srcl+1); 653 size_t max_s = luaL_optinteger(L, 4, srcl+1);
652 int anchor = (*p == '^') ? (p++, 1) : 0; 654 int anchor = (*p == '^') ? (p++, 1) : 0;
653 int n = 0; 655 size_t n = 0;
654 MatchState ms; 656 MatchState ms;
655 luaL_Buffer b; 657 luaL_Buffer b;
656 luaL_buffinit(L, &b); 658 luaL_buffinit(L, &b);