diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-10-07 17:13:41 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-10-07 17:13:41 -0300 |
| commit | f04fe526cd9de3e5460b614b2ff06268ad51872d (patch) | |
| tree | 9e9fd4fc0bae4858d74c04a22d8ce5748191d5a9 /lstrlib.c | |
| parent | 21947deddc5976536665cd2397d7d5c9e6bd7e48 (diff) | |
| download | lua-f04fe526cd9de3e5460b614b2ff06268ad51872d.tar.gz lua-f04fe526cd9de3e5460b614b2ff06268ad51872d.tar.bz2 lua-f04fe526cd9de3e5460b614b2ff06268ad51872d.zip | |
new functions `lua_tointeger' and lua_pushinteger'
Diffstat (limited to 'lstrlib.c')
| -rw-r--r-- | lstrlib.c | 34 |
1 files changed, 17 insertions, 17 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstrlib.c,v 1.98 2003/04/03 13:35:34 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.99 2003/05/14 14:35:54 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 | */ |
| @@ -25,13 +25,13 @@ | |||
| 25 | #endif | 25 | #endif |
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | typedef long sint32; /* a signed version for size_t */ | 28 | typedef lua_Integer sint32; /* a signed version for size_t */ |
| 29 | 29 | ||
| 30 | 30 | ||
| 31 | static int str_len (lua_State *L) { | 31 | static int str_len (lua_State *L) { |
| 32 | size_t l; | 32 | size_t l; |
| 33 | luaL_checklstring(L, 1, &l); | 33 | luaL_checklstring(L, 1, &l); |
| 34 | lua_pushnumber(L, (lua_Number)l); | 34 | lua_pushinteger(L, l); |
| 35 | return 1; | 35 | return 1; |
| 36 | } | 36 | } |
| 37 | 37 | ||
| @@ -45,8 +45,8 @@ static sint32 posrelat (sint32 pos, size_t len) { | |||
| 45 | static int str_sub (lua_State *L) { | 45 | static int str_sub (lua_State *L) { |
| 46 | size_t l; | 46 | size_t l; |
| 47 | const char *s = luaL_checklstring(L, 1, &l); | 47 | const char *s = luaL_checklstring(L, 1, &l); |
| 48 | sint32 start = posrelat(luaL_checklong(L, 2), l); | 48 | sint32 start = posrelat(luaL_checkinteger(L, 2), l); |
| 49 | sint32 end = posrelat(luaL_optlong(L, 3, -1), l); | 49 | sint32 end = posrelat(luaL_optinteger(L, 3, -1), l); |
| 50 | if (start < 1) start = 1; | 50 | if (start < 1) start = 1; |
| 51 | if (end > (sint32)l) end = (sint32)l; | 51 | if (end > (sint32)l) end = (sint32)l; |
| 52 | if (start <= end) | 52 | if (start <= end) |
| @@ -108,10 +108,10 @@ static int str_rep (lua_State *L) { | |||
| 108 | static int str_byte (lua_State *L) { | 108 | static int str_byte (lua_State *L) { |
| 109 | size_t l; | 109 | size_t l; |
| 110 | const char *s = luaL_checklstring(L, 1, &l); | 110 | const char *s = luaL_checklstring(L, 1, &l); |
| 111 | sint32 pos = posrelat(luaL_optlong(L, 2, 1), l); | 111 | sint32 pos = posrelat(luaL_optinteger(L, 2, 1), l); |
| 112 | if (pos <= 0 || (size_t)(pos) > l) /* index out of range? */ | 112 | if (pos <= 0 || (size_t)(pos) > l) /* index out of range? */ |
| 113 | return 0; /* no answer */ | 113 | return 0; /* no answer */ |
| 114 | lua_pushnumber(L, uchar(s[pos-1])); | 114 | lua_pushinteger(L, uchar(s[pos-1])); |
| 115 | return 1; | 115 | return 1; |
| 116 | } | 116 | } |
| 117 | 117 | ||
| @@ -463,7 +463,7 @@ static void push_onecapture (MatchState *ms, int i) { | |||
| 463 | int l = ms->capture[i].len; | 463 | int l = ms->capture[i].len; |
| 464 | if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); | 464 | if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); |
| 465 | if (l == CAP_POSITION) | 465 | if (l == CAP_POSITION) |
| 466 | lua_pushnumber(ms->L, (lua_Number)(ms->capture[i].init - ms->src_init + 1)); | 466 | lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); |
| 467 | else | 467 | else |
| 468 | lua_pushlstring(ms->L, ms->capture[i].init, l); | 468 | lua_pushlstring(ms->L, ms->capture[i].init, l); |
| 469 | } | 469 | } |
| @@ -488,7 +488,7 @@ static int str_find (lua_State *L) { | |||
| 488 | size_t l1, l2; | 488 | size_t l1, l2; |
| 489 | const char *s = luaL_checklstring(L, 1, &l1); | 489 | const char *s = luaL_checklstring(L, 1, &l1); |
| 490 | const char *p = luaL_checklstring(L, 2, &l2); | 490 | const char *p = luaL_checklstring(L, 2, &l2); |
| 491 | sint32 init = posrelat(luaL_optlong(L, 3, 1), l1) - 1; | 491 | sint32 init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1; |
| 492 | if (init < 0) init = 0; | 492 | if (init < 0) init = 0; |
| 493 | else if ((size_t)(init) > l1) init = (sint32)l1; | 493 | else if ((size_t)(init) > l1) init = (sint32)l1; |
| 494 | if (lua_toboolean(L, 4) || /* explicit request? */ | 494 | if (lua_toboolean(L, 4) || /* explicit request? */ |
| @@ -496,8 +496,8 @@ static int str_find (lua_State *L) { | |||
| 496 | /* do a plain search */ | 496 | /* do a plain search */ |
| 497 | const char *s2 = lmemfind(s+init, l1-init, p, l2); | 497 | const char *s2 = lmemfind(s+init, l1-init, p, l2); |
| 498 | if (s2) { | 498 | if (s2) { |
| 499 | lua_pushnumber(L, (lua_Number)(s2-s+1)); | 499 | lua_pushinteger(L, s2-s+1); |
| 500 | lua_pushnumber(L, (lua_Number)(s2-s+l2)); | 500 | lua_pushinteger(L, s2-s+l2); |
| 501 | return 2; | 501 | return 2; |
| 502 | } | 502 | } |
| 503 | } | 503 | } |
| @@ -512,8 +512,8 @@ static int str_find (lua_State *L) { | |||
| 512 | const char *res; | 512 | const char *res; |
| 513 | ms.level = 0; | 513 | ms.level = 0; |
| 514 | if ((res=match(&ms, s1, p)) != NULL) { | 514 | if ((res=match(&ms, s1, p)) != NULL) { |
| 515 | lua_pushnumber(L, (lua_Number)(s1-s+1)); /* start */ | 515 | lua_pushinteger(L, s1-s+1); /* start */ |
| 516 | lua_pushnumber(L, (lua_Number)(res-s)); /* end */ | 516 | lua_pushinteger(L, res-s); /* end */ |
| 517 | return push_captures(&ms, NULL, 0) + 2; | 517 | return push_captures(&ms, NULL, 0) + 2; |
| 518 | } | 518 | } |
| 519 | } while (s1++<ms.src_end && !anchor); | 519 | } while (s1++<ms.src_end && !anchor); |
| @@ -532,7 +532,7 @@ static int gfind_aux (lua_State *L) { | |||
| 532 | ms.L = L; | 532 | ms.L = L; |
| 533 | ms.src_init = s; | 533 | ms.src_init = s; |
| 534 | ms.src_end = s+ls; | 534 | ms.src_end = s+ls; |
| 535 | for (src = s + (size_t)lua_tonumber(L, lua_upvalueindex(3)); | 535 | for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3)); |
| 536 | src <= ms.src_end; | 536 | src <= ms.src_end; |
| 537 | src++) { | 537 | src++) { |
| 538 | const char *e; | 538 | const char *e; |
| @@ -540,7 +540,7 @@ static int gfind_aux (lua_State *L) { | |||
| 540 | if ((e = match(&ms, src, p)) != NULL) { | 540 | if ((e = match(&ms, src, p)) != NULL) { |
| 541 | int newstart = e-s; | 541 | int newstart = e-s; |
| 542 | if (e == src) newstart++; /* empty match? go at least one position */ | 542 | if (e == src) newstart++; /* empty match? go at least one position */ |
| 543 | lua_pushnumber(L, (lua_Number)newstart); | 543 | lua_pushinteger(L, newstart); |
| 544 | lua_replace(L, lua_upvalueindex(3)); | 544 | lua_replace(L, lua_upvalueindex(3)); |
| 545 | return push_captures(&ms, src, e); | 545 | return push_captures(&ms, src, e); |
| 546 | } | 546 | } |
| @@ -553,7 +553,7 @@ static int gfind (lua_State *L) { | |||
| 553 | luaL_checkstring(L, 1); | 553 | luaL_checkstring(L, 1); |
| 554 | luaL_checkstring(L, 2); | 554 | luaL_checkstring(L, 2); |
| 555 | lua_settop(L, 2); | 555 | lua_settop(L, 2); |
| 556 | lua_pushnumber(L, 0); | 556 | lua_pushinteger(L, 0); |
| 557 | lua_pushcclosure(L, gfind_aux, 3); | 557 | lua_pushcclosure(L, gfind_aux, 3); |
| 558 | return 1; | 558 | return 1; |
| 559 | } | 559 | } |
| @@ -627,7 +627,7 @@ static int str_gsub (lua_State *L) { | |||
| 627 | } | 627 | } |
| 628 | luaL_addlstring(&b, src, ms.src_end-src); | 628 | luaL_addlstring(&b, src, ms.src_end-src); |
| 629 | luaL_pushresult(&b); | 629 | luaL_pushresult(&b); |
| 630 | lua_pushnumber(L, (lua_Number)n); /* number of substitutions */ | 630 | lua_pushinteger(L, n); /* number of substitutions */ |
| 631 | return 2; | 631 | return 2; |
| 632 | } | 632 | } |
| 633 | 633 | ||
