aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lstrlib.c26
1 files changed, 12 insertions, 14 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 9526cb2e..f66e56ae 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.107 2004/11/01 14:33:33 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.108 2004/11/19 16:58:43 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*/
@@ -24,8 +24,6 @@
24#define uchar(c) ((unsigned char)(c)) 24#define uchar(c) ((unsigned char)(c))
25 25
26 26
27typedef lua_Integer sint32; /* a signed version for size_t */
28
29 27
30static int str_len (lua_State *L) { 28static int str_len (lua_State *L) {
31 size_t l; 29 size_t l;
@@ -35,19 +33,19 @@ static int str_len (lua_State *L) {
35} 33}
36 34
37 35
38static sint32 posrelat (sint32 pos, size_t len) { 36static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) {
39 /* relative string position: negative means back from end */ 37 /* relative string position: negative means back from end */
40 return (pos>=0) ? pos : (sint32)len+pos+1; 38 return (pos>=0) ? pos : (ptrdiff_t)len+pos+1;
41} 39}
42 40
43 41
44static int str_sub (lua_State *L) { 42static int str_sub (lua_State *L) {
45 size_t l; 43 size_t l;
46 const char *s = luaL_checklstring(L, 1, &l); 44 const char *s = luaL_checklstring(L, 1, &l);
47 sint32 start = posrelat(luaL_checkinteger(L, 2), l); 45 ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
48 sint32 end = posrelat(luaL_optinteger(L, 3, -1), l); 46 ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
49 if (start < 1) start = 1; 47 if (start < 1) start = 1;
50 if (end > (sint32)l) end = (sint32)l; 48 if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
51 if (start <= end) 49 if (start <= end)
52 lua_pushlstring(L, s+start-1, end-start+1); 50 lua_pushlstring(L, s+start-1, end-start+1);
53 else lua_pushliteral(L, ""); 51 else lua_pushliteral(L, "");
@@ -107,8 +105,8 @@ static int str_rep (lua_State *L) {
107static int str_byte (lua_State *L) { 105static int str_byte (lua_State *L) {
108 size_t l; 106 size_t l;
109 const char *s = luaL_checklstring(L, 1, &l); 107 const char *s = luaL_checklstring(L, 1, &l);
110 sint32 posi = posrelat(luaL_optinteger(L, 2, 1), l); 108 ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
111 sint32 pose = posrelat(luaL_optinteger(L, 3, posi), l); 109 ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
112 int n, i; 110 int n, i;
113 if (posi <= 0) posi = 1; 111 if (posi <= 0) posi = 1;
114 if ((size_t)pose > l) pose = l; 112 if ((size_t)pose > l) pose = l;
@@ -173,7 +171,7 @@ typedef struct MatchState {
173 int level; /* total number of captures (finished or unfinished) */ 171 int level; /* total number of captures (finished or unfinished) */
174 struct { 172 struct {
175 const char *init; 173 const char *init;
176 sint32 len; 174 ptrdiff_t len;
177 } capture[MAX_CAPTURES]; 175 } capture[MAX_CAPTURES];
178} MatchState; 176} MatchState;
179 177
@@ -299,7 +297,7 @@ static const char *matchbalance (MatchState *ms, const char *s,
299 297
300static const char *max_expand (MatchState *ms, const char *s, 298static const char *max_expand (MatchState *ms, const char *s,
301 const char *p, const char *ep) { 299 const char *p, const char *ep) {
302 sint32 i = 0; /* counts maximum expand for item */ 300 ptrdiff_t i = 0; /* counts maximum expand for item */
303 while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep)) 301 while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))
304 i++; 302 i++;
305 /* keeps trying to match with the maximum repetitions */ 303 /* keeps trying to match with the maximum repetitions */
@@ -490,9 +488,9 @@ static int str_find (lua_State *L) {
490 size_t l1, l2; 488 size_t l1, l2;
491 const char *s = luaL_checklstring(L, 1, &l1); 489 const char *s = luaL_checklstring(L, 1, &l1);
492 const char *p = luaL_checklstring(L, 2, &l2); 490 const char *p = luaL_checklstring(L, 2, &l2);
493 sint32 init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1; 491 ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
494 if (init < 0) init = 0; 492 if (init < 0) init = 0;
495 else if ((size_t)(init) > l1) init = (sint32)l1; 493 else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
496 if (lua_toboolean(L, 4) || /* explicit request? */ 494 if (lua_toboolean(L, 4) || /* explicit request? */
497 strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */ 495 strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */
498 /* do a plain search */ 496 /* do a plain search */