aboutsummaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-11-24 15:39:56 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-11-24 15:39:56 -0200
commit96253ed8ceb38afa50887ccb5500442b5b220f08 (patch)
tree4b42b80f781b0cbe95782f8e8ebec06d85e80ae4 /lstrlib.c
parent35d6b1505702b0f4a2eee0e6d2f8dfc50943a1a7 (diff)
downloadlua-96253ed8ceb38afa50887ccb5500442b5b220f08.tar.gz
lua-96253ed8ceb38afa50887ccb5500442b5b220f08.tar.bz2
lua-96253ed8ceb38afa50887ccb5500442b5b220f08.zip
better support for 64-bit machines (avoid excessive use of longs)
Diffstat (limited to 'lstrlib.c')
-rw-r--r--lstrlib.c22
1 files changed, 12 insertions, 10 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 9b960190..5cc0e23b 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.56 2000/10/27 16:15:53 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.57 2000/11/23 13:49:35 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*/
@@ -17,6 +17,8 @@
17#include "lualib.h" 17#include "lualib.h"
18 18
19 19
20typedef long sint32; /* a "signed" version for size_t */
21
20 22
21static int str_len (lua_State *L) { 23static int str_len (lua_State *L) {
22 size_t l; 24 size_t l;
@@ -26,19 +28,19 @@ static int str_len (lua_State *L) {
26} 28}
27 29
28 30
29static long posrelat (long pos, size_t len) { 31static sint32 posrelat (sint32 pos, size_t len) {
30 /* relative string position: negative means back from end */ 32 /* relative string position: negative means back from end */
31 return (pos>=0) ? pos : (long)len+pos+1; 33 return (pos>=0) ? pos : (sint32)len+pos+1;
32} 34}
33 35
34 36
35static int str_sub (lua_State *L) { 37static int str_sub (lua_State *L) {
36 size_t l; 38 size_t l;
37 const char *s = luaL_check_lstr(L, 1, &l); 39 const char *s = luaL_check_lstr(L, 1, &l);
38 long start = posrelat(luaL_check_long(L, 2), l); 40 sint32 start = posrelat(luaL_check_long(L, 2), l);
39 long end = posrelat(luaL_opt_long(L, 3, -1), l); 41 sint32 end = posrelat(luaL_opt_long(L, 3, -1), l);
40 if (start < 1) start = 1; 42 if (start < 1) start = 1;
41 if (end > (long)l) end = l; 43 if (end > (sint32)l) end = l;
42 if (start <= end) 44 if (start <= end)
43 lua_pushlstring(L, s+start-1, end-start+1); 45 lua_pushlstring(L, s+start-1, end-start+1);
44 else lua_pushstring(L, ""); 46 else lua_pushstring(L, "");
@@ -87,7 +89,7 @@ static int str_rep (lua_State *L) {
87static int str_byte (lua_State *L) { 89static int str_byte (lua_State *L) {
88 size_t l; 90 size_t l;
89 const char *s = luaL_check_lstr(L, 1, &l); 91 const char *s = luaL_check_lstr(L, 1, &l);
90 long pos = posrelat(luaL_opt_long(L, 2, 1), l); 92 sint32 pos = posrelat(luaL_opt_long(L, 2, 1), l);
91 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");
92 lua_pushnumber(L, (unsigned char)s[pos-1]); 94 lua_pushnumber(L, (unsigned char)s[pos-1]);
93 return 1; 95 return 1;
@@ -126,7 +128,7 @@ typedef struct MatchState {
126 int level; /* total number of captures (finished or unfinished) */ 128 int level; /* total number of captures (finished or unfinished) */
127 struct { 129 struct {
128 const char *init; 130 const char *init;
129 long len; /* -1 signals unfinished capture */ 131 sint32 len; /* -1 signals unfinished capture */
130 } capture[MAX_CAPTURES]; 132 } capture[MAX_CAPTURES];
131 lua_State *L; 133 lua_State *L;
132} MatchState; 134} MatchState;
@@ -251,7 +253,7 @@ static const char *matchbalance (MatchState *ms, const char *s, const char *p) {
251 253
252static const char *max_expand (MatchState *ms, const char *s, const char *p, 254static const char *max_expand (MatchState *ms, const char *s, const char *p,
253 const char *ep) { 255 const char *ep) {
254 long i = 0; /* counts maximum expand for item */ 256 sint32 i = 0; /* counts maximum expand for item */
255 while ((s+i)<ms->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep)) 257 while ((s+i)<ms->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep))
256 i++; 258 i++;
257 /* keeps trying to match with the maximum repetitions */ 259 /* keeps trying to match with the maximum repetitions */
@@ -399,7 +401,7 @@ static int str_find (lua_State *L) {
399 size_t l1, l2; 401 size_t l1, l2;
400 const char *s = luaL_check_lstr(L, 1, &l1); 402 const char *s = luaL_check_lstr(L, 1, &l1);
401 const char *p = luaL_check_lstr(L, 2, &l2); 403 const char *p = luaL_check_lstr(L, 2, &l2);
402 long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1; 404 sint32 init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
403 luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, "out of range"); 405 luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, "out of range");
404 if (lua_gettop(L) > 3 || /* extra argument? */ 406 if (lua_gettop(L) > 3 || /* extra argument? */
405 strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */ 407 strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */